Browse Source

modified listen/accept-logic and renamed NL_tcp_listen to NL_tcp_accept

uleon 9 years ago
parent
commit
f09ff6bc16
4 changed files with 38 additions and 31 deletions
  1. 1 1
      README.md
  2. 23 26
      examples/networklayer.c
  3. 6 1
      examples/networklayer.h
  4. 8 3
      examples/opcuaServer.c

+ 1 - 1
README.md

@@ -38,7 +38,7 @@ make doc # generate documentation
 ```
 
 ### Windows (Visual Studio)
-* Get and install Python and CMake: https://python.org/downloads, http://www.cmake.org/cmake/resources/software.html
+* Get and install Python 2.7.x and CMake: https://python.org/downloads, http://www.cmake.org/cmake/resources/software.html
 * Get and install Visual Studio 2013 (Express): http://www.microsoft.com/en-US/download/details.aspx?id=40787
 * Download the open62541 sources (using git or as a zipfile from github)
 * Add the folder with the python executable to the Windows PATH (System Settings)

+ 23 - 26
examples/networklayer.c

@@ -1,14 +1,14 @@
+#include "networklayer.h"
+#include "ua_transport_connection.h"
 
 #ifdef WIN32
 #pragma comment (lib,"ws2_32.lib")
-#include <sys/types.h>
-	#include <winsock2.h>
-	#include <Windows.h>
-	#include <ws2tcpip.h>
-
-    #define CLOSESOCKET(S) closesocket(S) \
+#define CLOSESOCKET(S) closesocket(S); \
 	WSACleanup();
 	#define IOCTLSOCKET ioctlsocket
+	#include <sys/types.h>
+	#include <Windows.h>
+	#include <ws2tcpip.h>
 #else
 #define CLOSESOCKET(S) close(S)
 	#define IOCTLSOCKET ioctl
@@ -24,8 +24,6 @@
 #include <memory.h> // memset
 #include <fcntl.h> // fcntl
 
-#include "networklayer.h"
-#include "ua_transport_connection.h"
 NL_Description NL_Description_TcpBinary  = {
 	NL_UA_ENCODING_BINARY,
 	NL_CONNECTIONTYPE_TCPV4,
@@ -324,21 +322,11 @@ void* NL_Connection_init(NL_Connection* c, NL_data* tld, UA_Int32 connectionHand
 	return UA_NULL;
 }
 
-/** the tcp listener routine */
-void* NL_TCP_listen(NL_Connection* c) {
+/** the tcp accept routine */
+void* NL_TCP_accept(NL_Connection* c) {
 	NL_data* tld = c->networkLayer;
 
-	DBG_VERBOSE(printf("NL_TCP_listen - enter listen\n"));
-
-
-	int retval = listen(c->connectionHandle, tld->tld->maxConnections);
-	DBG_VERBOSE(printf("NL_TCP_listen - leave listen, retval=%d\n",retval));
-
-	if (retval < 0) {
-		// TODO: Error handling
-		perror("NL_TCP_listen");
-		DBG_ERR(printf("NL_TCP_listen retval=%d, errno={%d,%s}\n",retval,errno,strerror(errno)));
-	} else if (tld->tld->maxConnections == -1 || tld->connections.size < tld->tld->maxConnections) {
+	if (tld->tld->maxConnections == -1 || tld->connections.size < tld->tld->maxConnections) {
 		// accept only if not max number of connections exceeded
 		struct sockaddr_in cli_addr;
 		socklen_t cli_len = sizeof(cli_addr);
@@ -369,8 +357,19 @@ void* NL_TCP_listen(NL_Connection* c) {
 
 #ifdef MULTITHREADING
 void* NL_TCP_listenThread(NL_Connection* c) {
+	NL_data* tld = c->networkLayer;
 	do {
-		NL_TCP_listen(c);
+		DBG_VERBOSE(printf("NL_TCP_listenThread - enter listen\n"));
+		int retval = listen(c->connectionHandle, tld->tld->maxConnections);
+		DBG_VERBOSE(printf("NL_TCP_listenThread - leave listen, retval=%d\n", retval));
+
+		if (retval < 0) {
+			// TODO: Error handling
+			perror("NL_TCP_listen");
+			DBG_ERR(printf("NL_TCP_listen retval=%d, errno={%d,%s}\n", retval, errno, strerror(errno)));
+		} else {
+			NL_TCP_accept(c);
+		}
 	} while (UA_TRUE);
 	UA_free(c);
 	pthread_exit(UA_NULL);
@@ -438,15 +437,13 @@ UA_Int32 NL_TCP_init(NL_data* tld, UA_Int32 port) {
 		NL_Connection* c;
 		UA_Int32 retval = UA_SUCCESS;
 		retval |= UA_alloc((void**)&c,sizeof(NL_Connection));
-		NL_Connection_init(c, tld, newsockfd, NL_TCP_listen, (TL_Writer) NL_TCP_writer);
+		NL_Connection_init(c, tld, newsockfd, NL_TCP_accept, (TL_Writer) NL_TCP_writer);
 #ifdef MULTITHREADING
 		pthread_create( &(c->readerThreadHandle), NULL, (void*(*)(void*)) NL_TCP_listenThread, (void*) c);
 #else
 		UA_list_addPayloadToBack(&(tld->connections),c);
 		NL_TCP_SetNonBlocking(c->connectionHandle);
-#ifdef WIN32
-		listen(c->connectionHandle, 0);
-#endif /*WIN32*/
+		listen(c->connectionHandle, tld->tld->maxConnections);
 #endif
 	}
 	return retval;

+ 6 - 1
examples/networklayer.h

@@ -10,7 +10,12 @@
 #include <pthread.h> // pthreadcreate, pthread_t
 #endif
 
-#include <sys/select.h> // FD_ZERO, FD_SET
+// fd_set, FD_ZERO, FD_SET
+#ifdef WIN32
+#include <winsock2.h>
+#else
+#include <sys/select.h> 
+#endif
 
 #define NL_MAXCONNECTIONS_DEFAULT 10
 

+ 8 - 3
examples/opcuaServer.c

@@ -4,10 +4,13 @@
 #include "networklayer.h"
 #include "ua_application.h"
 
+#ifdef WIN32
+#else
 #include <sys/mman.h>
-#include <sys/types.h>
 #include <sys/wait.h>
 #include <unistd.h>
+#endif
+#include <sys/types.h>
 #include <time.h>
 #include <fcntl.h>
 
@@ -40,7 +43,7 @@ UA_Int32 serverCallback(void * arg) {
 
 
 int main(int argc, char** argv) {
-
+	UA_Int32 retval;
 	/* gets called at ctrl-c */
 	signal(SIGINT, stopHandler);
 	
@@ -51,6 +54,8 @@ int main(int argc, char** argv) {
 	SL_ChannelManager_init(10,36000,244,2,&endpointUrl);
 	UA_SessionManager_init(10,3600000,25);
 	struct timeval tv = {1, 0}; // 1 second
-	NL_msgLoop(nl, &tv, serverCallback, argv[0], &running);
 
+	retval = NL_msgLoop(nl, &tv, serverCallback, argv[0], &running);
+
+	return retval == UA_SUCCESS ? 0 : retval;
 }