picloud data send and receive, not tested
This commit is contained in:
@@ -63,7 +63,7 @@ void PICloudClient::setKeepConnection(bool on) {
|
|||||||
|
|
||||||
bool PICloudClient::openDevice() {
|
bool PICloudClient::openDevice() {
|
||||||
PIString p = path();
|
PIString p = path();
|
||||||
piCout << "PICloudClient open device" << p;
|
// piCout << "PICloudClient open device" << p;
|
||||||
bool op = eth.connect(p, false);
|
bool op = eth.connect(p, false);
|
||||||
if (op) {
|
if (op) {
|
||||||
mutex_buff.lock();
|
mutex_buff.lock();
|
||||||
@@ -97,7 +97,7 @@ bool PICloudClient::closeDevice() {
|
|||||||
|
|
||||||
|
|
||||||
int PICloudClient::readDevice(void * read_to, int max_size) {
|
int PICloudClient::readDevice(void * read_to, int max_size) {
|
||||||
piCoutObj << "readDevice";
|
// piCoutObj << "readDevice";
|
||||||
if (!is_connected) return -1;
|
if (!is_connected) return -1;
|
||||||
mutex_buff.lock();
|
mutex_buff.lock();
|
||||||
cond_buff.wait(mutex_buff, [this](){return !buff.isEmpty();});
|
cond_buff.wait(mutex_buff, [this](){return !buff.isEmpty();});
|
||||||
@@ -110,7 +110,7 @@ int PICloudClient::readDevice(void * read_to, int max_size) {
|
|||||||
|
|
||||||
|
|
||||||
int PICloudClient::writeDevice(const void * data, int size) {
|
int PICloudClient::writeDevice(const void * data, int size) {
|
||||||
piCoutObj << "writeDevice";
|
// piCoutObj << "writeDevice";
|
||||||
return tcp.sendData(PIByteArray(data, size));
|
return tcp.sendData(PIByteArray(data, size));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -71,14 +71,14 @@ bool PICloudServer::closeDevice() {
|
|||||||
|
|
||||||
|
|
||||||
int PICloudServer::readDevice(void * read_to, int max_size) {
|
int PICloudServer::readDevice(void * read_to, int max_size) {
|
||||||
piCoutObj << "readDevice";
|
//piCoutObj << "readDevice";
|
||||||
piMSleep(eth.readTimeout());
|
piMSleep(eth.readTimeout());
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int PICloudServer::writeDevice(const void * data, int max_size) {
|
int PICloudServer::writeDevice(const void * data, int max_size) {
|
||||||
piCoutObj << "writeDevice";
|
//piCoutObj << "writeDevice";
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,23 +94,37 @@ int PICloudServer::sendData(const PIByteArray & data, uint client_id) {
|
|||||||
|
|
||||||
|
|
||||||
PICloudServer::Client::Client(PICloudServer * srv, uint id) : server(srv), client_id(id) {
|
PICloudServer::Client::Client(PICloudServer * srv, uint id) : server(srv), client_id(id) {
|
||||||
|
is_connected = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
PICloudServer::Client::~Client() {
|
||||||
|
if (is_connected) {
|
||||||
|
is_connected = false;
|
||||||
|
cond_buff.notifyOne();
|
||||||
|
}
|
||||||
|
close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool PICloudServer::Client::openDevice() {
|
bool PICloudServer::Client::openDevice() {
|
||||||
return true;
|
return is_connected;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool PICloudServer::Client::closeDevice() {
|
bool PICloudServer::Client::closeDevice() {
|
||||||
server->clientDisconnect(client_id);
|
server->clientDisconnect(client_id);
|
||||||
|
if (is_connected) {
|
||||||
|
is_connected = false;
|
||||||
|
cond_buff.notifyOne();
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int PICloudServer::Client::readDevice(void * read_to, int max_size) {
|
int PICloudServer::Client::readDevice(void * read_to, int max_size) {
|
||||||
//piCoutObj << "readDevice";
|
//piCoutObj << "readDevice";
|
||||||
|
if (!is_connected) return -1;
|
||||||
mutex_buff.lock();
|
mutex_buff.lock();
|
||||||
cond_buff.wait(mutex_buff, [this](){return !buff.isEmpty();});
|
cond_buff.wait(mutex_buff, [this](){return !buff.isEmpty();});
|
||||||
int sz = piMini(max_size, buff.size());
|
int sz = piMini(max_size, buff.size());
|
||||||
@@ -127,6 +141,7 @@ int PICloudServer::Client::writeDevice(const void * data, int size) {
|
|||||||
|
|
||||||
|
|
||||||
void PICloudServer::Client::pushBuffer(const PIByteArray & ba) {
|
void PICloudServer::Client::pushBuffer(const PIByteArray & ba) {
|
||||||
|
if (!is_connected) return;
|
||||||
mutex_buff.lock();
|
mutex_buff.lock();
|
||||||
buff.append(ba);
|
buff.append(ba);
|
||||||
cond_buff.notifyOne();
|
cond_buff.notifyOne();
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ void PICloud::TCP::setServerName(const PIString & server_name) {
|
|||||||
|
|
||||||
|
|
||||||
void PICloud::TCP::sendStart() {
|
void PICloud::TCP::sendStart() {
|
||||||
piCout << "sendStart";
|
//piCout << "sendStart";
|
||||||
header.type = PICloud::TCP::Connect;
|
header.type = PICloud::TCP::Connect;
|
||||||
PIByteArray ba;
|
PIByteArray ba;
|
||||||
ba << header << sname;
|
ba << header << sname;
|
||||||
|
|||||||
@@ -37,11 +37,11 @@ public:
|
|||||||
|
|
||||||
void setServerName(const PIString & server_name);
|
void setServerName(const PIString & server_name);
|
||||||
void setKeepConnection(bool on);
|
void setKeepConnection(bool on);
|
||||||
|
bool isConnected() const {return is_connected;}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool openDevice();
|
bool openDevice();
|
||||||
bool closeDevice();
|
bool closeDevice();
|
||||||
bool isConnected() const {return is_connected;}
|
|
||||||
int readDevice(void * read_to, int max_size);
|
int readDevice(void * read_to, int max_size);
|
||||||
int writeDevice(const void * data, int size);
|
int writeDevice(const void * data, int size);
|
||||||
|
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ public:
|
|||||||
friend class PICloudServer;
|
friend class PICloudServer;
|
||||||
public:
|
public:
|
||||||
Client(PICloudServer * srv = nullptr, uint id = 0);
|
Client(PICloudServer * srv = nullptr, uint id = 0);
|
||||||
|
virtual ~Client();
|
||||||
protected:
|
protected:
|
||||||
bool openDevice();
|
bool openDevice();
|
||||||
bool closeDevice();
|
bool closeDevice();
|
||||||
@@ -53,6 +54,7 @@ public:
|
|||||||
PIByteArray buff;
|
PIByteArray buff;
|
||||||
PIMutex mutex_buff;
|
PIMutex mutex_buff;
|
||||||
PIConditionVariable cond_buff;
|
PIConditionVariable cond_buff;
|
||||||
|
std::atomic_bool is_connected;
|
||||||
};
|
};
|
||||||
|
|
||||||
void setServerName(const PIString & server_name);
|
void setServerName(const PIString & server_name);
|
||||||
|
|||||||
Reference in New Issue
Block a user