@@ -21,7 +21,6 @@ void arduino::MbedClient::readSocket() {
2121 do {
2222 if (rxBuffer.availableForStore () == 0 ) {
2323 yield ();
24- delay (100 );
2524 continue ;
2625 }
2726 mutex->lock ();
@@ -34,7 +33,6 @@ void arduino::MbedClient::readSocket() {
3433 }
3534 if (ret == NSAPI_ERROR_WOULD_BLOCK || ret == 0 ) {
3635 yield ();
37- delay (100 );
3836 mutex->unlock ();
3937 continue ;
4038 }
@@ -71,7 +69,7 @@ void arduino::MbedClient::configureSocket(Socket *_s) {
7169 }
7270 mutex->lock ();
7371 if (reader_th == nullptr ) {
74- reader_th = new rtos::Thread;
72+ reader_th = new rtos::Thread (osPriorityNormal - 2 ) ;
7573 reader_th->start (mbed::callback (this , &MbedClient::readSocket));
7674 }
7775 mutex->unlock ();
@@ -80,6 +78,15 @@ void arduino::MbedClient::configureSocket(Socket *_s) {
8078}
8179
8280int 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+
8390 if (sock == nullptr ) {
8491 sock = new TCPSocket ();
8592 _own_socket = true ;
@@ -206,7 +213,7 @@ size_t arduino::MbedClient::write(const uint8_t *buf, size_t size) {
206213 int ret = NSAPI_ERROR_WOULD_BLOCK;
207214 do {
208215 ret = sock->send (buf, size);
209- } while (ret != size && connected ());
216+ } while (( ret != size && ret == NSAPI_ERROR_WOULD_BLOCK) && connected ());
210217 configureSocket (sock);
211218 return size;
212219}
0 commit comments