@@ -82,6 +82,7 @@ static void doClose(int socketId);
82
82
static void releaseSocket (int socketId );
83
83
static void resetSocketByData (struct socketData * pSocketData );
84
84
static void resetSocketById (int sckt );
85
+ static void esp8266_dumpSocketData (struct socketData * pSocketData );
85
86
86
87
static void esp8266_callback_connectCB_inbound (void * arg );
87
88
static void esp8266_callback_connectCB_outbound (void * arg );
@@ -219,6 +220,18 @@ static struct socketData socketArray[MAX_SOCKETS];
219
220
*/
220
221
static bool g_socketsInitialized = false;
221
222
223
+ /**
224
+ * Dump all the socket structures.
225
+ * This is used exclusively for debugging. It walks through each of the
226
+ * socket structures and dumps their state to the debug log.
227
+ */
228
+ void esp8266_dumpAllSocketData () {
229
+ int i ;
230
+ for (i = 0 ; i < MAX_SOCKETS ; i ++ ) {
231
+ esp8266_dumpSocketData (& socketArray [i ]);
232
+ }
233
+ }
234
+
222
235
223
236
/**
224
237
* Write the details of a socket to the debug log.
@@ -228,7 +241,18 @@ void esp8266_dumpSocket(
228
241
int socketId //!< The ID of the socket data structure to be logged.
229
242
) {
230
243
struct socketData * pSocketData = getSocketData (socketId );
231
- LOG ("Dump of socket=%d\n" , socketId );
244
+ esp8266_dumpSocketData (pSocketData );
245
+ }
246
+
247
+
248
+ /**
249
+ * Write the details of a socketData to the debug log.
250
+ * The data associated with the socketData is dumped to the debug log.
251
+ */
252
+ static void esp8266_dumpSocketData (
253
+ struct socketData * pSocketData //!< The socket data structure to be logged
254
+ ) {
255
+ LOG ("Dump of socket=%d\n" , pSocketData -> socketId );
232
256
LOG (" - isConnected=%d" , pSocketData -> isConnected );
233
257
char * creationTypeMsg ;
234
258
switch (pSocketData -> creationType ) {
@@ -284,6 +308,7 @@ void esp8266_dumpSocket(
284
308
break ;
285
309
}
286
310
LOG (", state=%s" , stateMsg );
311
+ LOG (", espconn=0x%x" , (unsigned int )pSocketData -> pEspconn );
287
312
LOG (", errorCode=%d" , pSocketData -> errorCode );
288
313
289
314
// Print the errorMsg if it has anything to say
@@ -481,7 +506,6 @@ static void resetSocketByData(
481
506
482
507
pSocketData -> acceptedSocketsHead = 0 ; // Set the head to 0
483
508
pSocketData -> acceptedSocketsTail = 0 ; // Set the tail to 9.
484
- pSocketData -> pEspconn = NULL ;
485
509
}
486
510
487
511
@@ -511,9 +535,10 @@ static void releaseSocket(
511
535
512
536
// If this socket is not an incoming socket that means that the espconn structure was created
513
537
// by us and we should release the storage we allocated.
514
- if (pSocketData -> creationType != SOCKET_CREATED_INBOUND ) {
538
+ if (pSocketData -> creationType != SOCKET_CREATED_INBOUND && pSocketData -> creationType != SOCKET_CREATED_SERVER ) {
515
539
os_free (pSocketData -> pEspconn -> proto .tcp );
516
540
pSocketData -> pEspconn -> proto .tcp = NULL ;
541
+ os_printf (" - freeing espconn: 0x%x\n" , (unsigned int )pSocketData -> pEspconn );
517
542
os_free (pSocketData -> pEspconn );
518
543
pSocketData -> pEspconn = NULL ;
519
544
}
@@ -556,14 +581,23 @@ static void doClose(
556
581
557
582
struct socketData * pSocketData = getSocketData (socketId );
558
583
559
- if (pSocketData -> state != SOCKET_STATE_CLOSING ) {
560
- int rc = espconn_disconnect (pSocketData -> pEspconn );
584
+
585
+ if (pSocketData -> creationType == SOCKET_CREATED_SERVER ) {
586
+ int rc = espconn_delete (pSocketData -> pEspconn );
587
+ if (rc != 0 ) {
588
+ os_printf ("espconn_delete: rc=%s (%d)\n" ,esp8266_errorToString (rc ), rc );
589
+ setSocketInError (socketId , "espconn_disconnect" , rc );
590
+ }
561
591
pSocketData -> state = SOCKET_STATE_CLOSING ;
592
+ }
562
593
594
+ if (pSocketData -> state != SOCKET_STATE_CLOSING ) {
595
+ int rc = espconn_disconnect (pSocketData -> pEspconn );
563
596
if (rc != 0 ) {
564
- os_printf ("espconn_disconnect: rc=%d \n" , rc );
597
+ os_printf ("espconn_disconnect: rc=%s (%d) \n" ,esp8266_errorToString ( rc ), rc );
565
598
setSocketInError (socketId , "espconn_disconnect" , rc );
566
599
}
600
+ pSocketData -> state = SOCKET_STATE_CLOSING ;
567
601
}
568
602
// Our existing state on entry was SOCKET_STATE_CLOSING which means that we got here
569
603
// because we were previously flagged as closing.
@@ -695,6 +729,7 @@ static void esp8266_callback_disconnectCB(
695
729
// If the socket state is SOCKET_STATE_CLOSING then that means we can release the socket. The reason
696
730
// for this is that the last thing the user did was request an explicit socket close.
697
731
if (pSocketData -> state == SOCKET_STATE_CLOSING ) {
732
+
698
733
releaseSocket (pSocketData -> socketId );
699
734
} else {
700
735
pSocketData -> state = SOCKET_STATE_CLOSING ;
@@ -787,7 +822,7 @@ static void esp8266_callback_recvCB(
787
822
if (pSocketData -> rxBufLen == 0 ) {
788
823
pSocketData -> rxBuf = (void * )os_malloc (len );
789
824
if (pSocketData -> rxBuf == NULL ) {
790
- os_printf (" - Out of memory\n" );
825
+ os_printf (" - #1 Out of memory allocating %d \n" , len );
791
826
return ;
792
827
}
793
828
os_memcpy (pSocketData -> rxBuf , pData , len );
@@ -801,7 +836,7 @@ static void esp8266_callback_recvCB(
801
836
// Update the socket data.
802
837
uint8 * pNewBuf = (uint8 * )os_malloc (len + pSocketData -> rxBufLen );
803
838
if (pNewBuf == NULL ) {
804
- os_printf (" - Out of memory\n" );
839
+ os_printf (" - #2 Out of memory allocating %d \n" , len + pSocketData -> rxBufLen );
805
840
return ;
806
841
}
807
842
os_memcpy (pNewBuf , pSocketData -> rxBuf , pSocketData -> rxBufLen );
@@ -1039,6 +1074,7 @@ int net_ESP8266_BOARD_createSocket(
1039
1074
1040
1075
pSocketData -> pEspconn = (struct espconn * )os_malloc (sizeof (struct espconn ));
1041
1076
assert (pSocketData -> pEspconn );
1077
+ os_printf (" - espconn: 0x%x\n" , (unsigned int )pSocketData -> pEspconn );
1042
1078
1043
1079
struct espconn * pEspconn = pSocketData -> pEspconn ;
1044
1080
@@ -1124,7 +1160,7 @@ void net_ESP8266_BOARD_closeSocket(
1124
1160
//dumpEspConn(pSocketData->pEspconn);
1125
1161
1126
1162
// How we close the socket is a function of what kind of socket it is.
1127
-
1163
+ /*
1128
1164
// Close a server socket
1129
1165
if (pSocketData->creationType == SOCKET_CREATED_SERVER) {
1130
1166
int rc = espconn_delete(pSocketData->pEspconn);
@@ -1146,6 +1182,20 @@ void net_ESP8266_BOARD_closeSocket(
1146
1182
pSocketData->shouldClose = true;
1147
1183
}
1148
1184
}
1185
+ */
1186
+
1187
+ // If the state of the socket is idle, closing or error then we can actually close the socket.
1188
+ // If it is not one of these states, then we are in the middle of something so let that
1189
+ // something completed and then we can finish.
1190
+ if (pSocketData -> state == SOCKET_STATE_IDLE ||
1191
+ pSocketData -> state == SOCKET_STATE_CLOSING ||
1192
+ pSocketData -> state == SOCKET_STATE_ERROR ) {
1193
+ doClose (socketId );
1194
+ } else {
1195
+ pSocketData -> shouldClose = true;
1196
+ }
1197
+
1198
+ os_printf ("< net_ESP8266_BOARD_closeSocket\n" );
1149
1199
}
1150
1200
1151
1201
0 commit comments