@@ -21,7 +21,6 @@ void arduino::MbedClient::readSocket() {
21
21
do {
22
22
if (rxBuffer.availableForStore () == 0 ) {
23
23
yield ();
24
- delay (100 );
25
24
continue ;
26
25
}
27
26
mutex->lock ();
@@ -34,7 +33,6 @@ void arduino::MbedClient::readSocket() {
34
33
}
35
34
if (ret == NSAPI_ERROR_WOULD_BLOCK || ret == 0 ) {
36
35
yield ();
37
- delay (100 );
38
36
mutex->unlock ();
39
37
continue ;
40
38
}
@@ -71,7 +69,7 @@ void arduino::MbedClient::configureSocket(Socket *_s) {
71
69
}
72
70
mutex->lock ();
73
71
if (reader_th == nullptr ) {
74
- reader_th = new rtos::Thread;
72
+ reader_th = new rtos::Thread (osPriorityNormal - 2 ) ;
75
73
reader_th->start (mbed::callback (this , &MbedClient::readSocket));
76
74
}
77
75
mutex->unlock ();
@@ -80,6 +78,15 @@ void arduino::MbedClient::configureSocket(Socket *_s) {
80
78
}
81
79
82
80
int arduino::MbedClient::connect (SocketAddress socketAddress) {
81
+
82
+ if (sock && reader_th) {
83
+ // trying to reuse a connection, let's call stop() to cleanup the state
84
+ char c;
85
+ if (sock->recv (&c, 1 ) < 0 ) {
86
+ stop ();
87
+ }
88
+ }
89
+
83
90
if (sock == nullptr ) {
84
91
sock = new TCPSocket ();
85
92
_own_socket = true ;
@@ -206,7 +213,7 @@ size_t arduino::MbedClient::write(const uint8_t *buf, size_t size) {
206
213
int ret = NSAPI_ERROR_WOULD_BLOCK;
207
214
do {
208
215
ret = sock->send (buf, size);
209
- } while (ret != size && connected ());
216
+ } while (( ret != size && ret == NSAPI_ERROR_WOULD_BLOCK) && connected ());
210
217
configureSocket (sock);
211
218
return size;
212
219
}
0 commit comments