Files
pip/libs/main/io_utils/pipackedtcp.h

105 lines
3.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*! \file pipackedtcp.h
* \ingroup IO-Utils
* \~\brief
* \~english Ethernet device
* \~russian Устройство Ethernet
*/
/*
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;
//! \~english Ethernet device
//! \~russian Устройство Ethernet
class PIP_IO_UTILS_EXPORT PIPackedTCP: public PIIODevice {
PIIODEVICE(PIPackedTCP, "ptcp");
public:
//! \~english Role of %PIPackedTCP
//! \~russian Роль %PIPackedTCP
enum Role {
Client /** TCP client */,
Server /** TCP server for one client */
};
//! \~english Constructs %PIPackedTCP with "role" and "addr" address
//! \~russian Создает %PIPackedTCP с ролью "role" и адресом "addr"
explicit PIPackedTCP(Role role = Client, const PINetworkAddress & addr = {});
//! \~english Destructor
//! \~russian Деструктор
virtual ~PIPackedTCP();
//! \~english Set server address for Server role or connect address for Client
//! \~russian Установить адрес сервера для роли Server или адрес подключения для Client
void setAddress(const PINetworkAddress & addr);
//! \~english Check if connected
//! \~russian Проверить подключение
bool isConnected() const;
//! \~english Check if connecting
//! \~russian Проверить подключение
bool isConnecting() const;
//! \~english Returns read address in format "i.i.i.i:p"
//! \~russian Возвращает адрес чтения в формате "i.i.i.i:p"
PINetworkAddress address() const { return m_addr; }
//! \~english Returns %PIEthernet type
//! \~russian Возвращает тип %PIEthernet
Role role() const { return m_role; }
EVENT0(connected);
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<PIByteArray> rec_queue;
private:
};
REGISTER_DEVICE(PIPackedTCP)
#endif