some fixes for picloud, but still not working correctly
This commit is contained in:
@@ -33,22 +33,22 @@ PICloudClient::PICloudClient(const PIString & path, PIIODevice::DeviceMode mode)
|
||||
if (is_deleted) return;
|
||||
bool need_disconn = is_connected;
|
||||
//piCoutObj << "eth disconnected";
|
||||
eth.softStopThreadedRead();
|
||||
eth.stop();
|
||||
opened_ = false;
|
||||
internalDisconnect();
|
||||
if (need_disconn)
|
||||
disconnected();
|
||||
if (need_disconn) disconnected();
|
||||
//piCoutObj << "eth disconnected done";
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
PICloudClient::~PICloudClient() {
|
||||
//piCoutObj << "~PICloudClient()";
|
||||
piCoutObj << "~PICloudClient() ..." << this;
|
||||
is_deleted = true;
|
||||
stopAndWait();
|
||||
close();
|
||||
is_deleted = true;
|
||||
internalDisconnect();
|
||||
piCoutObj << "~PICloudClient() done" << this;
|
||||
}
|
||||
|
||||
|
||||
@@ -70,14 +70,14 @@ void PICloudClient::interrupt() {
|
||||
|
||||
|
||||
bool PICloudClient::openDevice() {
|
||||
//piCoutObj << "open";// << path();
|
||||
piCoutObj << "open";// << path();
|
||||
bool op = eth.connect(PIEthernet::Address::resolve(path()), false);
|
||||
if (op) {
|
||||
mutex_connect.lock();
|
||||
eth.startThreadedRead();
|
||||
//piCoutObj << "connecting...";
|
||||
piCoutObj << "connecting...";
|
||||
bool conn_ok = cond_connect.waitFor(mutex_connect, (int)eth.readTimeout());
|
||||
//piCoutObj << "conn_ok" << conn_ok << is_connected;
|
||||
piCoutObj << "conn_ok" << conn_ok << is_connected;
|
||||
mutex_connect.unlock();
|
||||
if (!conn_ok) {
|
||||
mutex_connect.lock();
|
||||
@@ -105,36 +105,37 @@ bool PICloudClient::closeDevice() {
|
||||
|
||||
|
||||
ssize_t PICloudClient::readDevice(void * read_to, ssize_t max_size) {
|
||||
if (is_deleted) return -1;
|
||||
//piCoutObj << "readDevice";
|
||||
if (is_deleted || max_size <= 0) return -1;
|
||||
piCoutObj << "readDevice ...";
|
||||
if (!is_connected && eth.isClosed()) openDevice();
|
||||
ssize_t sz = -1;
|
||||
mutex_buff.lock();
|
||||
cond_buff.wait(mutex_buff);
|
||||
if (is_connected) {
|
||||
if (buff.isEmpty()) {
|
||||
sz = 0;
|
||||
} else {
|
||||
sz = piMini(max_size, buff.size());
|
||||
sz = piMin<ssize_t>(max_size, buff.size_s());
|
||||
memcpy(read_to, buff.data(), sz);
|
||||
buff.remove(0, sz);
|
||||
}
|
||||
if (sz == 0) cond_buff.wait(mutex_buff);
|
||||
}
|
||||
mutex_buff.unlock();
|
||||
if (!is_connected) opened_ = false;
|
||||
//piCoutObj << "readDevice done" << sz;
|
||||
piCoutObj << "readDevice done" << sz;
|
||||
return sz;
|
||||
}
|
||||
|
||||
|
||||
ssize_t PICloudClient::writeDevice(const void * data, ssize_t size) {
|
||||
if (is_deleted) return -1;
|
||||
// piCoutObj << "writeDevice";
|
||||
piCoutObj << "writeDevice" << size;
|
||||
return tcp.sendData(PIByteArray(data, size));
|
||||
}
|
||||
|
||||
|
||||
void PICloudClient::internalDisconnect() {
|
||||
piCoutObj << "internalDisconnect";
|
||||
is_connected = false;
|
||||
cond_buff.notifyOne();
|
||||
cond_connect.notifyOne();
|
||||
@@ -146,7 +147,7 @@ void PICloudClient::internalDisconnect() {
|
||||
void PICloudClient::_readed(PIByteArray & ba) {
|
||||
if (is_deleted) return;
|
||||
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> hdr = tcp.parseHeader(ba);
|
||||
//piCoutObj << "_readed" << ba.size() << hdr.first << hdr.second;
|
||||
piCoutObj << "_readed" << ba.size() << hdr.first << hdr.second;
|
||||
if (hdr.second == tcp.role()) {
|
||||
switch (hdr.first) {
|
||||
case PICloud::TCP::Connect:
|
||||
@@ -159,7 +160,7 @@ void PICloudClient::_readed(PIByteArray & ba) {
|
||||
}
|
||||
break;
|
||||
case PICloud::TCP::Disconnect:
|
||||
eth.softStopThreadedRead();
|
||||
eth.stop();
|
||||
opened_ = false;
|
||||
eth.close();
|
||||
break;
|
||||
@@ -177,5 +178,5 @@ void PICloudClient::_readed(PIByteArray & ba) {
|
||||
//piCoutObj << "readed" << ba.toHex();
|
||||
}
|
||||
while (buff.size_s() > threadedReadBufferSize()) piMSleep(100); // FIXME: sleep here is bad
|
||||
//piCoutObj << "_readed done";
|
||||
piCoutObj << "_readed done";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user