PICloud with streampacker

This commit is contained in:
2021-04-07 14:38:32 +03:00
parent fcf9f0f80e
commit 1fd9851068
13 changed files with 90 additions and 67 deletions

View File

@@ -21,6 +21,7 @@ PIString CloudServer::serverName() const {
void CloudServer::addClient(DispatcherClient * c) {
clients << c;
index_clients.insert(c->clientId(), c);
c->sendConnected();
}

View File

@@ -2,9 +2,9 @@
#include "picloudtcp.h"
DispatcherClient::DispatcherClient(PIEthernet * eth_, int id) : eth(eth_), authorised(false), client_id(id) {
DispatcherClient::DispatcherClient(PIEthernet * eth_, int id) : authorised(false), eth(eth_), streampacker(eth_), tcp(&streampacker), client_id(id) {
CONNECTU(&disconnect_tm, tickEvent, eth, close);
CONNECTU(eth, threadedReadEvent, this, readed);
CONNECTU(&streampacker, packetReceiveEvent, this, readed);
CONNECTU(eth, disconnected, this, disconnected);
piCoutObj << "client connected" << eth->sendAddress();
}
@@ -31,18 +31,19 @@ void DispatcherClient::close() {
void DispatcherClient::sendConnected() {
tcp.sendConnected(eth, client_id);
piCoutObj << "sendConnected";
tcp.sendConnected(1);
}
void DispatcherClient::sendData(const PIByteArray & data) {
if (tcp.role() == PICloud::TCP::Client) tcp.sendData(eth, data);
if (tcp.role() == PICloud::TCP::Client) tcp.sendData(data);
else piCoutObj << "error sendData, invalid role";
}
void DispatcherClient::sendDataToClient(const PIByteArray & data, uint client_id) {
if (tcp.role() == PICloud::TCP::Server) tcp.sendData(eth, data, client_id);
if (tcp.role() == PICloud::TCP::Server) tcp.sendData(data, client_id);
else piCoutObj << "error sendDataToClient, invalid role";
}
@@ -53,11 +54,10 @@ void DispatcherClient::disconnected(bool withError) {
}
void DispatcherClient::readed(uchar *data, int size) {
void DispatcherClient::readed(PIByteArray & ba) {
// piCout << size;
PIByteArray ba(data, size);
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> hdr = tcp.parseHeader(ba);
if (hdr.first == PICloud::TCP::Invalid) {
if (hdr.first == PICloud::TCP::InvalidType) {
disconnected(true);
return;
}

View File

@@ -3,6 +3,7 @@
#include "piethernet.h"
#include "picloudtcp.h"
#include "pistreampacker.h"
class DispatcherClient: public PIObject {
@@ -23,12 +24,13 @@ public:
EVENT1(dataReaded, PIByteArray, ba)
private:
EVENT_HANDLER2(void, readed, uchar * , data, int, size);
EVENT_HANDLER1(void, readed, PIByteArray &, data);
EVENT_HANDLER1(void, disconnected, bool, withError);
PIEthernet * eth;
PITimer disconnect_tm;
bool authorised;
PIEthernet * eth;
PIStreamPacker streampacker;
PICloud::TCP tcp;
uint client_id;
};