Browse Source

implemented handling sockets that were closed by client, tnx to @FlorianPalm for some hints
*not tested for windows* :(
closes #145

Stasik0 10 years ago
parent
commit
0ccd13bb77
1 changed files with 8 additions and 1 deletions
  1. 8 1
      examples/networklayer_tcp.c

+ 8 - 1
examples/networklayer_tcp.c

@@ -16,6 +16,7 @@
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <sys/socketvar.h>
+#include <sys/ioctl.h>
 #include <unistd.h> // read, write, close
 #define CLOSESOCKET(S) close(S)
 #define IOCTLSOCKET ioctl
@@ -242,7 +243,13 @@ void worksocks(NetworklayerTCP *layer, UA_Server *server, UA_UInt32 workamount)
 	// read from established sockets
 	for(UA_UInt32 i=0;i<layer->connectionsSize && workamount > 0;i++) {
 		if(FD_ISSET(layer->connections[i].sockfd, &layer->fdset)) {
-			readConnection(layer, server, &layer->connections[i]);
+			int n = 0;
+			IOCTLSOCKET(layer->connections[i].sockfd, FIONREAD, &n);
+			if(n==0){ /* the socket has been closed by the client - remove the socket from the socket list */
+				layer->connections[i].connection.close(layer->connections[i].connection.callbackHandle);
+			}else{
+				readConnection(layer, server, &layer->connections[i]);
+			}
 			workamount--;
 		}
 	}