//! \~\file pipackedtcp.h
//! \~\ingroup IO-Utils
//! \~\brief
//! \~english Packet-oriented TCP device built on top of %PIStreamPacker
//! \~russian Пакетно-ориентированное TCP-устройство на основе %PIStreamPacker
/*
PIP - Platform Independent Primitives
Ethernet, UDP/TCP Broadcast/Multicast
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 .
*/
#ifndef pipackedtcp_H
#define pipackedtcp_H
#include "piiodevice.h"
#include "pinetworkaddress.h"
#include "pip_io_utils_export.h"
#include "pistreampacker.h"
class PIEthernet;
//! \~\ingroup IO-Utils
//! \~\brief
//! \~english TCP device wrapper that exposes framed packets through the %PIIODevice API.
//! \~russian Обертка над TCP-устройством, предоставляющая кадрированные пакеты через API %PIIODevice.
//! \details
//! \~english
//! The %PIPackedTCP class provides a TCP client/server device with automatic data packing/unpacking using PIStreamPacker. It
//! supports both client and server roles with reliable byte-stream transmission over TCP connections.
//! \~russian
//! Класс %PIPackedTCP предоставляет устройство TCP клиент/сервер с автоматической упаковкой/распаковкой данных с использованием
//! PIStreamPacker. Поддерживает обе роли: клиент и сервер с надежной передачей байтовых потоков по TCP соединениям.
class PIP_IO_UTILS_EXPORT PIPackedTCP: public PIIODevice {
PIIODEVICE(PIPackedTCP, "ptcp");
public:
//! \~english Operating role of %PIPackedTCP.
//! \~russian Роль работы %PIPackedTCP.
enum Role {
Client /** \~english TCP client side \~russian Сторона TCP-клиента */,
Server /** \~english TCP server for one client \~russian TCP-сервер для одного клиента */
};
//! \~english Constructs %PIPackedTCP with role "role" and endpoint "addr".
//! \~russian Создает %PIPackedTCP с ролью "role" и конечной точкой "addr".
explicit PIPackedTCP(Role role = Client, const PINetworkAddress & addr = {});
//! \~english Destroys the packed TCP device.
//! \~russian Уничтожает устройство пакетированного TCP.
virtual ~PIPackedTCP();
//! \~english Sets listen address for server mode or remote address for client mode.
//! \~russian Устанавливает адрес прослушивания для режима сервера или удаленный адрес для режима клиента.
void setAddress(const PINetworkAddress & addr);
//! \~english Returns true if the TCP connection is established and active
//! \~russian Возвращает true если TCP соединение установлено и активно
bool isConnected() const;
//! \~english Returns true if the device is currently attempting to establish a TCP connection
//! \~russian Возвращает true если устройство в данный момент пытается установить TCP соединение
bool isConnecting() const;
//! \~english Returns configured endpoint address.
//! \~russian Возвращает настроенный адрес конечной точки.
PINetworkAddress address() const { return m_addr; }
//! \~english Returns current operating role.
//! \~russian Возвращает текущую рабочую роль.
Role role() const { return m_role; }
//! \events
//! \{
//! \fn void connected()
//! \~english Emitted when client connection becomes ready or a server accepts a client.
//! \~russian Генерируется, когда клиентское подключение готово или сервер принимает клиента.
EVENT0(connected);
//! \fn void disconnected()
//! \~english Emitted when active TCP connection is lost or closed.
//! \~russian Генерируется при потере или закрытии активного TCP-соединения.
EVENT0(disconnected);
//! \}
protected:
void init();
DeviceInfoFlags deviceInfoFlags() const override;
PIString constructFullPathDevice() const override;
void configureFromFullPathDevice(const PIString & full_path) override;
ssize_t readDevice(void * read_to, ssize_t max_size) override;
ssize_t writeDevice(const void * data, ssize_t max_size) override;
bool openDevice() override;
bool closeDevice() override;
mutable PINetworkAddress m_addr;
Role m_role = Client;
PIEthernet *eth = nullptr, *client = nullptr;
PIStreamPacker packer;
PIMutex rec_mutex;
PIQueue rec_queue;
private:
};
REGISTER_DEVICE(PIPackedTCP)
#endif