picloud add server logics
This commit is contained in:
@@ -24,6 +24,7 @@
|
||||
PICloudClient::PICloudClient(const PIString & path, PIIODevice::DeviceMode mode) : PIIODevice(path, mode) {
|
||||
tcp.setRole(PICloud::TCP::Client);
|
||||
setName("cloud_client");
|
||||
is_connected = false;
|
||||
}
|
||||
|
||||
|
||||
@@ -67,6 +68,7 @@ bool PICloudClient::openDevice() {
|
||||
|
||||
|
||||
bool PICloudClient::closeDevice() {
|
||||
is_connected = false;
|
||||
eth.stop();
|
||||
eth.close();
|
||||
return true;
|
||||
@@ -95,11 +97,25 @@ void PICloudClient::readed(uchar *data, int size) {
|
||||
mutex_buff.lock();
|
||||
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> hdr = tcp.parseHeader(ba);
|
||||
if (hdr.second == tcp.role()) {
|
||||
piCoutObj << "readed" << ba.toHex();
|
||||
buff.append(data, size);
|
||||
cond_buff.notifyOne();
|
||||
switch (hdr.first) {
|
||||
case PICloud::TCP::Connect:
|
||||
if (tcp.parseConnect(ba) == 0) is_connected = true;
|
||||
break;
|
||||
case PICloud::TCP::Disconnect:
|
||||
is_connected = false;
|
||||
eth.stop();
|
||||
eth.close();
|
||||
break;
|
||||
case PICloud::TCP::Data:
|
||||
buff.append(data, size);
|
||||
cond_buff.notifyOne();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
//piCoutObj << "readed" << ba.toHex();
|
||||
}
|
||||
mutex_buff.unlock();
|
||||
while (buff.size() > threadedReadBufferSize()) piMSleep(100);
|
||||
while (buff.size_s() > threadedReadBufferSize()) piMSleep(100);
|
||||
}
|
||||
|
||||
|
||||
@@ -29,11 +29,12 @@ PICloudServer::PICloudServer(const PIString & path, PIIODevice::DeviceMode mode)
|
||||
|
||||
|
||||
PICloudServer::~PICloudServer() {
|
||||
for (auto & c : clients) {
|
||||
c.stop();
|
||||
c.close();
|
||||
}
|
||||
stop();
|
||||
eth.stop();
|
||||
for (auto & c : clients) {
|
||||
c->stop();
|
||||
c->close();
|
||||
}
|
||||
close();
|
||||
}
|
||||
|
||||
@@ -66,9 +67,9 @@ bool PICloudServer::openDevice() {
|
||||
|
||||
bool PICloudServer::closeDevice() {
|
||||
eth.stop();
|
||||
for (auto & c : clients) {
|
||||
c.stop();
|
||||
c.close();
|
||||
for (auto c : clients) {
|
||||
c->stop();
|
||||
c->close();
|
||||
}
|
||||
eth.close();
|
||||
return true;
|
||||
@@ -85,7 +86,17 @@ int PICloudServer::writeDevice(const void * data, int max_size) {
|
||||
}
|
||||
|
||||
|
||||
PICloudServer::Client::Client(PICloudServer * srv) : server(srv) {
|
||||
void PICloudServer::clientDisconnect(uint client_id) {
|
||||
tcp.sendDisconnected(ð, client_id);
|
||||
}
|
||||
|
||||
|
||||
int PICloudServer::sendData(const PIByteArray & data, uint client_id) {
|
||||
return tcp.sendData(ð, data, client_id);
|
||||
}
|
||||
|
||||
|
||||
PICloudServer::Client::Client(PICloudServer * srv, uint id) : server(srv), client_id(id) {
|
||||
|
||||
}
|
||||
|
||||
@@ -95,10 +106,67 @@ bool PICloudServer::Client::openDevice() {
|
||||
}
|
||||
|
||||
|
||||
bool PICloudServer::Client::closeDevice() {
|
||||
server->clientDisconnect(client_id);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
int PICloudServer::Client::readDevice(void * read_to, int max_size) {
|
||||
//piCoutObj << "readDevice";
|
||||
mutex_buff.lock();
|
||||
cond_buff.wait(mutex_buff, [this](){return !buff.isEmpty();});
|
||||
int sz = piMini(max_size, buff.size());
|
||||
memcpy(read_to, buff.data(), sz);
|
||||
buff.remove(0, sz);
|
||||
mutex_buff.unlock();
|
||||
return sz;
|
||||
}
|
||||
|
||||
|
||||
int PICloudServer::Client::writeDevice(const void * data, int max_size) {
|
||||
return server->sendData(PIByteArray(data, max_size), client_id);
|
||||
}
|
||||
|
||||
|
||||
void PICloudServer::Client::pushBuffer(const PIByteArray & ba) {
|
||||
mutex_buff.lock();
|
||||
buff.append(ba);
|
||||
cond_buff.notifyOne();
|
||||
mutex_buff.unlock();
|
||||
while (buff.size_s() > threadedReadBufferSize()) piMSleep(100);
|
||||
}
|
||||
|
||||
|
||||
void PICloudServer::readed(uchar *data, int size) {
|
||||
PIByteArray ba(data, size);
|
||||
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> hdr = tcp.parseHeader(ba);
|
||||
if (hdr.second == tcp.role()) {
|
||||
piCoutObj << "readed" << ba.toHex();
|
||||
switch (hdr.first) {
|
||||
case PICloud::TCP::Connect: {
|
||||
uint id = tcp.parseConnect(ba);
|
||||
Client * oc = index_clients.value(id, nullptr);
|
||||
if (oc) {
|
||||
tcp.sendDisconnected(ð, id);
|
||||
} else {
|
||||
Client * c = new Client(this, id);
|
||||
clients << c;
|
||||
index_clients.insert(id, c);
|
||||
newConnection(c);
|
||||
}
|
||||
} break;
|
||||
case PICloud::TCP::Disconnect: {
|
||||
uint id = tcp.parseDisconnect(ba);
|
||||
Client * oc = index_clients.value(id, nullptr);
|
||||
if (oc) oc->close();
|
||||
} break;
|
||||
case PICloud::TCP::Data: {
|
||||
PIPair<uint, PIByteArray> d = tcp.parseDataServer(ba);
|
||||
Client * oc = index_clients.value(d.first, nullptr);
|
||||
if (oc && !d.second.isEmpty()) oc->pushBuffer(d.second);
|
||||
} break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,6 +61,14 @@ void PICloud::TCP::sendConnected(PIEthernet * eth, uint client_id) {
|
||||
}
|
||||
|
||||
|
||||
void PICloud::TCP::sendDisconnected(PIEthernet * eth, uint client_id) {
|
||||
header.type = PICloud::TCP::Disconnect;
|
||||
PIByteArray ba;
|
||||
ba << header << client_id;
|
||||
eth->send(ba);
|
||||
}
|
||||
|
||||
|
||||
void PICloud::TCP::sendData(PIEthernet * eth, const PIByteArray & data) {
|
||||
header.type = PICloud::TCP::Data;
|
||||
PIByteArray ba;
|
||||
@@ -71,6 +79,16 @@ void PICloud::TCP::sendData(PIEthernet * eth, const PIByteArray & data) {
|
||||
}
|
||||
|
||||
|
||||
int PICloud::TCP::sendData(PIEthernet * eth, const PIByteArray & data, uint client_id) {
|
||||
header.type = PICloud::TCP::Data;
|
||||
PIByteArray ba;
|
||||
ba << header << client_id;
|
||||
ba.append(data);
|
||||
if (eth->send(ba)) return data.size_s();
|
||||
else return -1;
|
||||
}
|
||||
|
||||
|
||||
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> PICloud::TCP::parseHeader(PIByteArray & ba) {
|
||||
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> ret;
|
||||
ret.first = Invalid;
|
||||
@@ -88,11 +106,6 @@ PIPair<PICloud::TCP::Type, PICloud::TCP::Role> PICloud::TCP::parseHeader(PIByteA
|
||||
|
||||
|
||||
PIByteArray PICloud::TCP::parseData(PIByteArray & ba) {
|
||||
if (header.role == Server) {
|
||||
PIString client;
|
||||
ba >> client;
|
||||
return ba;
|
||||
}
|
||||
if (header.role == Client) {
|
||||
return ba;
|
||||
}
|
||||
@@ -100,8 +113,33 @@ PIByteArray PICloud::TCP::parseData(PIByteArray & ba) {
|
||||
}
|
||||
|
||||
|
||||
PIString PICloud::TCP::parseConnect(PIByteArray & ba) {
|
||||
PIPair<uint, PIByteArray> PICloud::TCP::parseDataServer(PIByteArray & ba) {
|
||||
PIPair<uint, PIByteArray> ret;
|
||||
ret.first = 0;
|
||||
if (header.role == Server) {
|
||||
ba >> ret.first;
|
||||
ret.second = ba;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
PIString PICloud::TCP::parseConnect_d(PIByteArray & ba) {
|
||||
PIString ret;
|
||||
ba >> ret;
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
uint PICloud::TCP::parseConnect(PIByteArray & ba) {
|
||||
uint ret;
|
||||
ba >> ret;
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
uint PICloud::TCP::parseDisconnect(PIByteArray & ba) {
|
||||
uint ret;
|
||||
ba >> ret;
|
||||
return ret;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user