Files
pip/utils/cloud_dispatcher/dispatcherclient.cpp
2020-08-27 19:40:11 +03:00

52 lines
1.1 KiB
C++

#include "dispatcherclient.h"
#include "picloudtcp.h"
DispatcherClient::DispatcherClient(PIEthernet * eth_) : eth(eth_), authorised(false) {
CONNECTU(&disconnect_tm, tickEvent, eth, close);
eth->startThreadedRead();
CONNECTU(eth, threadedReadEvent, this, readed);
CONNECTU(eth, disconnected, this, disconnected);
piCoutObj << "client connected" << eth->sendAddress();
disconnect_tm.start(10000);
}
DispatcherClient::~DispatcherClient() {
// delete eth;
}
PIString DispatcherClient::address() {
return eth->path();
}
void DispatcherClient::close() {
eth->close();
}
void DispatcherClient::disconnected(bool withError) {
piCoutObj << "client disconnected" << eth->sendAddress();
disconnectEvent(this);
}
void DispatcherClient::readed(uchar *data, int size) {
PIByteArray ba(data, size);
if (authorised) {
dataReaded(ba);
} else {
if (ba.size() < 4) return;
PICloud::Header hdr;
ba >> hdr;
if ((PICloud::HeaderType)hdr.type == PICloud::Server) {
registerServer(hdr.sname, this);
}
if ((PICloud::HeaderType)hdr.type == PICloud::Client) {
registerClient(hdr.sname, this);
}
}
}