173 lines
5.4 KiB
C++
173 lines
5.4 KiB
C++
//! \file picloudtcp.h
|
||
//! \ingroup Cloud
|
||
//! \brief
|
||
//! \~english PICloud TCP transport
|
||
//! \~russian TCP слой PICloud
|
||
//! \details
|
||
//! \~english Low-level TCP protocol implementation for PICloud communication.
|
||
//! \~russian Реализация низкоуровневого TCP-протокола для коммуникации PICloud.
|
||
/*
|
||
PIP - Platform Independent Primitives
|
||
PICloud TCP transport
|
||
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
|
||
|
||
This program is free software: you can redistribute it and/or modify
|
||
it under the terms of the GNU Lesser General Public License as published by
|
||
the Free Software Foundation, either version 3 of the License, or
|
||
(at your option) any later version.
|
||
|
||
This program is distributed in the hope that it will be useful,
|
||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
GNU Lesser General Public License for more details.
|
||
|
||
You should have received a copy of the GNU Lesser General Public License
|
||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
*/
|
||
|
||
#ifndef PICLOUDTCP_H
|
||
#define PICLOUDTCP_H
|
||
|
||
#include "pimutex.h"
|
||
#include "pip_cloud_export.h"
|
||
#include "pistring.h"
|
||
|
||
|
||
class PIEthernet;
|
||
class PIStreamPacker;
|
||
|
||
namespace PICloud {
|
||
|
||
//! \brief TCP transport protocol
|
||
//! \~english TCP transport protocol for cloud communication
|
||
//! \~russian TCP транспортный протокол для облачной коммуникации
|
||
|
||
class PIP_CLOUD_EXPORT TCP {
|
||
public:
|
||
//! \brief Protocol version
|
||
enum Version {
|
||
Version_1 = 1, //!< Version 1 / Версия 1
|
||
Version_2 = 2, //!< Version 2 / Версия 2
|
||
};
|
||
|
||
//! \brief Connection role
|
||
enum Role {
|
||
InvalidRole = 0, //!< Invalid role / Неверная роль
|
||
Server = 1, //!< Server role / Роль сервера
|
||
Client = 2, //!< Client role / Роль клиента
|
||
};
|
||
|
||
//! \brief Message type
|
||
enum Type {
|
||
InvalidType = 0, //!< Invalid type / Неверный тип
|
||
Connect = 1, //!< Connect message / Сообщение о подключении
|
||
Disconnect = 2, //!< Disconnect message / Сообщение об отключении
|
||
Data = 3, //!< Data message / Сообщение с данными
|
||
Ping = 4, //!< Ping message / Сообщение ping
|
||
};
|
||
|
||
//! \brief
|
||
//! \~english Constructs TCP transport
|
||
//! \~russian Создает TCP транспорт
|
||
//! \param s Stream packer instance / Экземпляр стримового упаковщика
|
||
TCP(PIStreamPacker * s);
|
||
|
||
//! \brief
|
||
//! \~english Sets connection role
|
||
//! \~russian Устанавливает роль соединения
|
||
void setRole(Role r);
|
||
|
||
//! \brief
|
||
//! \~english Returns connection role
|
||
//! \~russian Возвращает роль соединения
|
||
Role role() const { return (Role)header.role; }
|
||
|
||
//! \brief
|
||
//! \~english Sets server name
|
||
//! \~russian Устанавливает имя сервера
|
||
void setServerName(const PIString & server_name_);
|
||
|
||
//! \brief
|
||
//! \~english Returns server name
|
||
//! \~russian Возвращает имя сервера
|
||
PIString serverName() const;
|
||
|
||
//! \brief
|
||
//! \~english Sends start message
|
||
//! \~russian Отправляет сообщение о старте
|
||
void sendStart();
|
||
|
||
//! \brief
|
||
//! \~english Sends connected message
|
||
//! \~russian Отправляет сообщение о подключении
|
||
void sendConnected(uint client_id);
|
||
|
||
//! \brief
|
||
//! \~english Sends disconnected message
|
||
//! \~russian Отправляет сообщение об отключении
|
||
void sendDisconnected(uint client_id);
|
||
|
||
//! \brief
|
||
//! \~english Sends data to all clients
|
||
//! \~russian Отправляет данные всем клиентам
|
||
int sendData(const PIByteArray & data);
|
||
|
||
//! \brief
|
||
//! \~english Sends data to specific client
|
||
//! \~russian Отправляет данные конкретному клиенту
|
||
int sendData(const PIByteArray & data, uint client_id);
|
||
|
||
//! \brief
|
||
//! \~english Sends ping message
|
||
//! \~russian Отправляет сообщение ping
|
||
void sendPing();
|
||
|
||
//! \brief
|
||
//! \~english Parses header from buffer
|
||
//! \~russian Парсит заголовок из буфера
|
||
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> parseHeader(PIByteArray & ba);
|
||
|
||
//! \brief
|
||
//! \~english Checks if data can be parsed
|
||
//! \~russian Проверяет возможность парсинга данных
|
||
bool canParseData(PIByteArray & ba);
|
||
|
||
//! \brief
|
||
//! \~english Parses data for server
|
||
//! \~russian Парсит данные для сервера
|
||
PIPair<uint, PIByteArray> parseDataServer(PIByteArray & ba);
|
||
|
||
//! \brief
|
||
//! \~english Parses connect data
|
||
//! \~russian Парсит данные подключения
|
||
PIByteArray parseConnect_d(PIByteArray & ba);
|
||
|
||
//! \brief
|
||
//! \~english Parses connect message
|
||
//! \~russian Парсит сообщение подключения
|
||
uint parseConnect(PIByteArray & ba);
|
||
|
||
//! \brief
|
||
//! \~english Parses disconnect message
|
||
//! \~russian Парсит сообщение отключения
|
||
uint parseDisconnect(PIByteArray & ba);
|
||
|
||
private:
|
||
struct Header {
|
||
Header();
|
||
uchar version; // PICloud::Version
|
||
uchar type; // PICloud::Type
|
||
uchar role; // PICloud::Role
|
||
};
|
||
|
||
Header header;
|
||
PIByteArray suuid;
|
||
PIString server_name;
|
||
PIStreamPacker * streampacker;
|
||
PIMutex mutex_send;
|
||
};
|
||
|
||
} // namespace PICloud
|
||
|
||
#endif // PICLOUDTCP_H
|