add PIPackedTCP
This commit is contained in:
199
libs/io_utils/pipackedtcp.cpp
Normal file
199
libs/io_utils/pipackedtcp.cpp
Normal file
@@ -0,0 +1,199 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
PIPackedTCP
|
||||
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/>.
|
||||
*/
|
||||
|
||||
#include "pipackedtcp.h"
|
||||
|
||||
#include "piethernet.h"
|
||||
#include "piliterals.h"
|
||||
|
||||
|
||||
/** \class PIPackedTCP pipackedtcp.h
|
||||
* \brief
|
||||
* TCP packed channel
|
||||
*
|
||||
* \details
|
||||
* \section PITCP_sec0 Synopsis
|
||||
* %PIEthernet designed to work with IPv4 network via two protocols:
|
||||
* UDP and TCP. This class allow you send and receive packets to/from
|
||||
* another computer through network. Also it supports broadcast and
|
||||
* multicast extensions.
|
||||
*
|
||||
* */
|
||||
|
||||
|
||||
PIPackedTCP::PIPackedTCP(Role role, const PINetworkAddress & addr): m_role(role) {
|
||||
setMode(PIIODevice::ReadWrite);
|
||||
packer.setCryptEnabled(false);
|
||||
CONNECTL(&packer, packetReceiveEvent, [this](PIByteArray & data) {
|
||||
PIMutexLocker ml(rec_mutex);
|
||||
rec_queue.enqueue(data);
|
||||
});
|
||||
init();
|
||||
setAddress(addr);
|
||||
}
|
||||
|
||||
|
||||
PIPackedTCP::~PIPackedTCP() {
|
||||
stopAndWait();
|
||||
if (client) client->stopAndWait();
|
||||
if (eth) eth->stopAndWait();
|
||||
piDeleteSafety(eth);
|
||||
}
|
||||
|
||||
|
||||
void PIPackedTCP::setAddress(const PINetworkAddress & addr) {
|
||||
m_addr = addr;
|
||||
setPath(m_addr.toString());
|
||||
}
|
||||
|
||||
|
||||
bool PIPackedTCP::isConnected() const {
|
||||
if (m_role == Client) {
|
||||
return eth->isConnected();
|
||||
} else {
|
||||
if (client) return client->isConnected();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
bool PIPackedTCP::isConnecting() const {
|
||||
if (m_role == Client) {
|
||||
return eth->isConnecting();
|
||||
} else {
|
||||
if (client) return client->isConnecting();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void PIPackedTCP::init() {
|
||||
if (client) client->stopAndWait();
|
||||
if (eth) eth->stopAndWait();
|
||||
piDeleteSafety(eth);
|
||||
eth = new PIEthernet(m_role == Client ? PIEthernet::TCP_Client : PIEthernet::TCP_Server);
|
||||
if (m_role == Client) {
|
||||
eth->setReopenTimeout(100_ms);
|
||||
packer.assignDevice(eth);
|
||||
CONNECTL(eth, connected, [this]() {
|
||||
packer.clear();
|
||||
connected();
|
||||
});
|
||||
CONNECTL(eth, disconnected, [this](bool) {
|
||||
packer.clear();
|
||||
eth->connect(path(), true);
|
||||
disconnected();
|
||||
});
|
||||
} else {
|
||||
CONNECTL(eth, newConnection, [this](PIEthernet * c) {
|
||||
if (client) client->stopAndWait();
|
||||
piDeleteSafety(client);
|
||||
client = c;
|
||||
// piCout << "Server connected" << client;
|
||||
packer.assignDevice(client);
|
||||
CONNECTL(client, disconnected, [this](bool) {
|
||||
packer.assignDevice(nullptr);
|
||||
packer.clear();
|
||||
disconnected();
|
||||
});
|
||||
client->startThreadedRead();
|
||||
connected();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
PIIODevice::DeviceInfoFlags PIPackedTCP::deviceInfoFlags() const {
|
||||
return PIIODevice::Reliable;
|
||||
}
|
||||
|
||||
|
||||
PIString PIPackedTCP::constructFullPathDevice() const {
|
||||
return PIString(m_role == Client ? "client" : "server") + ":" + path();
|
||||
}
|
||||
|
||||
|
||||
void PIPackedTCP::configureFromFullPathDevice(const PIString & full_path) {
|
||||
PIStringList pl = full_path.split(":");
|
||||
if (pl.size() >= 1) {
|
||||
PIString p = pl[0].toLowerCase().left(1);
|
||||
if (p == "c") m_role = Client;
|
||||
if (p == "s") m_role = Server;
|
||||
init();
|
||||
}
|
||||
PINetworkAddress addr("0.0.0.0", 13362);
|
||||
if (pl.size() >= 2) {
|
||||
if (pl[1].isNotEmpty()) addr.setIP(pl[1]);
|
||||
}
|
||||
if (pl.size() >= 3) {
|
||||
if (pl[2].isNotEmpty()) addr.setPort(pl[2].toInt());
|
||||
}
|
||||
setAddress(addr);
|
||||
}
|
||||
|
||||
|
||||
ssize_t PIPackedTCP::readDevice(void * read_to, ssize_t max_size) {
|
||||
PIMutexLocker ml(rec_mutex);
|
||||
if (rec_queue.isNotEmpty()) {
|
||||
auto d = rec_queue.dequeue();
|
||||
auto sz = piMin(max_size, d.size_s());
|
||||
if (read_to) memcpy(read_to, d.data(), sz);
|
||||
return sz;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
ssize_t PIPackedTCP::writeDevice(const void * data, ssize_t max_size) {
|
||||
if (!isConnected()) return 0;
|
||||
packer.send(PIByteArray(data, max_size));
|
||||
// piCout << m_role << "write" << eth;
|
||||
return max_size;
|
||||
/*if (m_role == Client) {
|
||||
return eth->write(data, max_size);
|
||||
} else {
|
||||
if (client) return client->write(data, max_size);
|
||||
}*/
|
||||
}
|
||||
|
||||
|
||||
bool PIPackedTCP::openDevice() {
|
||||
if (m_role == Client) {
|
||||
if (eth->isConnected()) return true;
|
||||
if (eth->isConnecting()) return false;
|
||||
packer.clear();
|
||||
bool ret = eth->connect(path(), false);
|
||||
eth->startThreadedRead();
|
||||
return ret;
|
||||
} else {
|
||||
return eth->listen(path(), false);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
bool PIPackedTCP::closeDevice() {
|
||||
if (client) {
|
||||
client->close();
|
||||
client->stopAndWait();
|
||||
piDeleteSafety(client);
|
||||
packer.assignDevice(nullptr);
|
||||
}
|
||||
return eth->close();
|
||||
}
|
||||
@@ -569,15 +569,14 @@ PIStringList PIIODevice::availableClasses() {
|
||||
|
||||
|
||||
void PIIODevice::registerDevice(PIConstChars prefix, PIConstChars classname, PIIODevice * (*fabric)()) {
|
||||
// printf("registerDevice %s %s %d\n", prefix.data(), classname.data(), fabrics().size());
|
||||
if (prefix.isEmpty()) return;
|
||||
// printf("registerDevice %s %d %d\n", prefix, p.isEmpty(), fabrics().size());
|
||||
if (!fabrics().contains(prefix)) {
|
||||
FabricInfo fi;
|
||||
fi.prefix = prefix;
|
||||
fi.classname = classname;
|
||||
fi.fabricator = fabric;
|
||||
fabrics()[prefix] = fi;
|
||||
}
|
||||
if (fabrics().contains(prefix)) return;
|
||||
FabricInfo fi;
|
||||
fi.prefix = prefix;
|
||||
fi.classname = classname;
|
||||
fi.fabricator = fabric;
|
||||
fabrics()[prefix] = fi;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -57,6 +57,7 @@
|
||||
#include "pidatatransfer.h"
|
||||
#include "pidiagnostics.h"
|
||||
#include "pifiletransfer.h"
|
||||
#include "pipackedtcp.h"
|
||||
#include "pipacketextractor.h"
|
||||
#include "piparsehelper.h"
|
||||
|
||||
|
||||
90
libs/main/io_utils/pipackedtcp.h
Normal file
90
libs/main/io_utils/pipackedtcp.h
Normal file
@@ -0,0 +1,90 @@
|
||||
/*! \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;
|
||||
|
||||
|
||||
class PIP_IO_UTILS_EXPORT PIPackedTCP: public PIIODevice {
|
||||
PIIODEVICE(PIPackedTCP, "ptcp");
|
||||
|
||||
public:
|
||||
//! \brief Role of %PIPackedTCP
|
||||
enum Role {
|
||||
Client /** TCP client */,
|
||||
Server /** TCP server for one client */
|
||||
};
|
||||
|
||||
//! Contructs %PIPackedTCP with "role" and "addr" address
|
||||
explicit PIPackedTCP(Role role = Client, const PINetworkAddress & addr = {});
|
||||
|
||||
virtual ~PIPackedTCP();
|
||||
|
||||
//! Set server address for Server role or connect address for Client
|
||||
void setAddress(const PINetworkAddress & addr);
|
||||
|
||||
bool isConnected() const;
|
||||
bool isConnecting() const;
|
||||
|
||||
//! Returns read address in format "i.i.i.i:p"
|
||||
PINetworkAddress address() const { return m_addr; }
|
||||
|
||||
//! Returns %PIEthernet type
|
||||
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
|
||||
Reference in New Issue
Block a user