120 lines
4.7 KiB
C++
120 lines
4.7 KiB
C++
/*! \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 <http://www.gnu.org/licenses/>.
|
||
*/
|
||
|
||
#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.
|
||
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 whether the underlying TCP side is connected.
|
||
//! \~russian Возвращает, подключена ли нижележащая TCP-сторона.
|
||
bool isConnected() const;
|
||
|
||
//! \~english Returns whether the underlying TCP side is establishing connection.
|
||
//! \~russian Возвращает, находится ли нижележащая 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; }
|
||
|
||
EVENT0(connected);
|
||
EVENT0(disconnected);
|
||
|
||
//! \events
|
||
//! \{
|
||
|
||
//! \fn void connected()
|
||
//! \~english Emitted when client connection becomes ready or a server accepts a client.
|
||
//! \~russian Генерируется, когда клиентское подключение готово или сервер принимает клиента.
|
||
|
||
//! \fn void disconnected()
|
||
//! \~english Emitted when active TCP connection is lost or closed.
|
||
//! \~russian Генерируется при потере или закрытии активного TCP-соединения.
|
||
|
||
//! \}
|
||
|
||
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<PIByteArray> rec_queue;
|
||
|
||
private:
|
||
};
|
||
|
||
REGISTER_DEVICE(PIPackedTCP)
|
||
|
||
#endif
|