Browse Source

TortoiseGit undid some changes during a pull.. :-/

Julius Pfrommer 9 years ago
parent
commit
d0a8b81f0d

+ 3 - 2
.travis.yml

@@ -24,9 +24,10 @@ before_install:
    - wget http://security.ubuntu.com/ubuntu/pool/main/c/check/check_0.9.10-6ubuntu3_amd64.deb
    - sudo dpkg -i check_0.9.10-6ubuntu3_amd64.deb
    - sudo pip install cpp-coveralls
-   - export CC=/usr/bin/gcc-4.8
+   - sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 20
+   - sudo update-alternatives --config gcc
 script: 
-   - mkdir build
+   - mkdir -p build
    - cd build
    - cmake -DGENERATE_DOCUMENTATION=ON .. 
    - echo "Production build"

+ 57 - 28
examples/networklayer.c

@@ -6,8 +6,7 @@
 #include <sys/types.h>
 #include <Windows.h>
 #include <ws2tcpip.h>
-#define CLOSESOCKET(S) closesocket(S); \
-	WSACleanup();
+#define CLOSESOCKET(S) closesocket(S)
 #define IOCTLSOCKET ioctlsocket
 #else
 #include <sys/socket.h>
@@ -75,7 +74,7 @@ void NL_Connection_printf(void* payload) {
 void NL_addHandleToSet(UA_Int32 handle, NL_data* nl) {
 	FD_SET(handle, &(nl->readerHandles));
 #ifdef WIN32
-	int err = WSAGetLastError();
+	// int err = WSAGetLastError();
 #endif
 	nl->maxReaderHandle = (handle > nl->maxReaderHandle) ? handle : nl->maxReaderHandle;
 }
@@ -89,8 +88,21 @@ void NL_checkFdSet(void* payload) {
 	  c->reader((void*)c);
   }
 }
+
+#if 0 
+char _str_error[256];
+char* strerror(int errno) {
+	FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
+		NULL, errno,
+		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
+		&_str_error[0], 256, NULL);
+	return &_str_error[0];
+}
+#endif
+
 UA_Int32 NL_msgLoop(NL_data* nl, struct timeval *tv, UA_Int32(*worker)(void*), void *arg, UA_Boolean *running)  {
 	UA_Int32 result;
+	UA_Int32 err;
 	while (*running) {
 		// determine the largest handle
 		nl->maxReaderHandle = 0;
@@ -98,51 +110,66 @@ UA_Int32 NL_msgLoop(NL_data* nl, struct timeval *tv, UA_Int32(*worker)(void*), v
 		DBG_VERBOSE(printf("\n------------\nUA_Stack_msgLoop - maxHandle=%d\n", nl->maxReaderHandle));
 
 		// copy tv, some unixes do overwrite and return the remaining time
+		// FIXME: actually we might want to do this ourselves to call the
+		// worker on a more regular cyclic basis
 		struct timeval tmptv;
 		memcpy(&tmptv,tv,sizeof(struct timeval));
 
 		// and wait
 		DBG_VERBOSE(printf("UA_Stack_msgLoop - enter select sec=%d,usec=%d\n",(UA_Int32) tmptv.tv_sec, (UA_Int32) tmptv.tv_usec));
 		result = select(nl->maxReaderHandle + 1, &(nl->readerHandles), UA_NULL, UA_NULL, &tmptv);
-
 		DBG_VERBOSE(printf("UA_Stack_msgLoop - leave select result=%d,sec=%d,usec=%d\n",result, (UA_Int32) tmptv.tv_sec, (UA_Int32) tmptv.tv_usec));
+
+		// handle timeout (winsock: result=0, unix: result=0,errno=0||EAGAIN) 
+		// and errors (winsock: result=SOCKET_ERROR (-1), unix: result = 0)
+		if (result <= 0) {
 #ifdef WIN32
-		if (result == -1) {
-			DBG_ERR(printf("UA_Stack_msgLoop - errno = { %d, %s }\n", WSAGetLastError()));
-		}
-		else if (result >= 0){ // activity on listener or client ports
+			err = (result == SOCKET_ERROR) ? WSAGetLastError() : 0;
 #else
-		if (result == 0) {
-			UA_Int32 err = errno;
-
+			err = errno;
+#endif
 			switch (err) {
+				// handle known errors
+#ifdef WIN32
+			case WSANOTINITIALISED:
+			case WSAEFAULT:
+			case WSAENETDOWN:
+			case WSAEINVAL:
+			case WSAEINTR:
+			case WSAEINPROGRESS:
+			case WSAENOTSOCK:
+#else
 			case EBADF:
 			case EINTR:
 			case EINVAL:
-				//FIXME: handle errors
-				DBG_ERR(printf("UA_Stack_msgLoop - errno={%d,%s}\n", errno, strerror(errno)));
+#endif
+				// FIXME: handle errors
+				printf("UA_Stack_msgLoop - result=%d, errno={%d,%s}\n", result, errno, strerror(errno));
 				break;
-			case EAGAIN: // timer due, call worker
-			default: //
-				DBG_VERBOSE(printf("UA_Stack_msgLoop - errno={%d,%s}\n", errno, strerror(errno)));
-				DBG_VERBOSE(printf("UA_Stack_msgLoop - call worker\n"));
-				worker(arg);
-				DBG_VERBOSE(printf("UA_Stack_msgLoop - return from worker\n"));
-			}
-		}
-
-		else if (result > 0){ // activity on listener or client ports
+			// otherwise we've got a timeout and call the worker
+#ifndef WIN32
+	        case EAGAIN:
 #endif
+			default:
+					DBG_VERBOSE(printf("UA_Stack_msgLoop - result=%d, errno={%d,%s}\n", result, errno, strerror(errno)));
+					worker(arg);
+			}
+		} else { // activity on listener or client ports
 			DBG_VERBOSE(printf("UA_Stack_msgLoop - activities on %d handles\n",result));
 			UA_list_iteratePayload(&(nl->connections),NL_checkFdSet);
+			// FIXME: Thought it would be a conceptional flaw to call the worker
+			// here. However, there is no guarantee that the timeout would be
+			// triggered, so we call it in this branch as well.
+			worker(arg);
 		}
-		// Calling worker here would execute worker any times we had received something or were interrupted
-		// worker(arg);
 	}
-
+#ifdef WIN32
+	// finally we should clean up the winsock.dll
+	WSACleanup();
+#endif
 	return UA_SUCCESS;
 }
-#endif
+#endif /* MULTITASKING */
 
 
 /** the tcp reader function */
@@ -188,13 +215,15 @@ void* NL_TCP_reader(NL_Connection *c) {
 			}
 #endif
 			TL_Process((c->connection),&readBuffer);
+		} else {
+			perror("NL_TCP_reader - ERROR reading from socket");
+			UA_TL_Connection_setState(c->connection, CONNECTIONSTATE_CLOSE);
 		}
 	}
 
 	UA_TL_Connection_getState(c->connection, &connectionState);
 	DBG_VERBOSE(printf("NL_TCP_reader - connectionState=%d\n",connectionState));
 	if (connectionState == CONNECTIONSTATE_CLOSE) {
-		DBG_VERBOSE(printf("NL_TCP_reader - closing connection"));
 		// set connection's state to CONNECTIONSTATE_CLOSED and call callback to actually close
 		UA_TL_Connection_close(c->connection);
 #ifndef MULTITHREADING

+ 1 - 2
src/ua_transport_binary.c

@@ -101,9 +101,8 @@ static UA_Int32 TL_handleMsg(UA_TL_Connection *connection, const UA_ByteString*
 static UA_Int32 TL_handleClo(UA_TL_Connection *connection, const UA_ByteString* msg, UA_UInt32* pos) {
 	UA_Int32 retval = UA_SUCCESS;
 	SL_Process(msg,pos);
-	// actually closing the connection should be handled elsewhere
+	// just prepare closing, closing and freeing structures is done elsewhere
 	// UA_TL_Connection_close(connection);
-	// here we only need to tell that we want to close
 	UA_TL_Connection_setState(connection, CONNECTIONSTATE_CLOSE);
 	return retval;
 }

+ 2 - 4
src/ua_transport_binary_secure.c

@@ -115,15 +115,13 @@ static UA_Int32 SL_Send(SL_Channel *channel,
 	return UA_SUCCESS;
 }
 
-static void init_response_header(UA_RequestHeader const * p,
-		UA_ResponseHeader * r)
-{
-	memset((void*) r, 0, sizeof(UA_ResponseHeader));
+static void init_response_header(UA_RequestHeader const * p, UA_ResponseHeader * r) {
 	r->requestHandle = p->requestHandle;
 	r->serviceResult = UA_STATUSCODE_GOOD;
 	r->stringTableSize = 0;
 	r->timestamp = UA_DateTime_now();
 }
+
 #define RESPONSE_PREPARE(TYPE) \
 	UA_##TYPE##Request p; \
 	UA_##TYPE##Response r; \

+ 4 - 4
src/ua_types.h

@@ -171,11 +171,11 @@ typedef struct UA_LocalizedText {
 typedef struct UA_ExtensionObject {
 	UA_NodeId typeId;
 	enum {
-		UA_EXTENSIONOBJECT_ENCODINGMASK_NOBODYISENCODED  = 0x00,
-		UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISBYTESTRING = 0x01,
-		UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISXML        = 0x02
+		UA_EXTENSIONOBJECT_ENCODINGMASK_NOBODYISENCODED  = 0,
+		UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISBYTESTRING = 1,
+		UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISXML        = 2
 	} encoding;
-	UA_ByteString body;         // contains either the bytestring or a pointer to the memory-object
+	UA_ByteString body; // contains either the bytestring or a pointer to the memory-object
 } UA_ExtensionObject;
 
 struct UA_VTable_Entry; // forwards declaration

+ 6 - 3
src/ua_types_encoding_binary.c

@@ -410,7 +410,7 @@ UA_Int32 UA_NodeId_calcSizeBinary(UA_NodeId const *p) {
 			break;
 
 		default:
-			break; // calcSize does not return errors. But the encoding function will.
+			UA_assert(UA_FALSE); // this must never happen
 		}
 	}
 	return length;
@@ -466,7 +466,7 @@ UA_TYPE_ENCODEBINARY(UA_NodeId,
 						 break;
 
 					 default:
-						 retval = UA_ERROR;
+						 UA_assert(UA_FALSE); // must never happen
 					 }
                      )
 
@@ -521,7 +521,7 @@ UA_Int32 UA_NodeId_decodeBinary(UA_ByteString const *src, UA_UInt32 *offset, UA_
 		break;
 
 	default:
-		retval = UA_ERROR;
+		retval = UA_ERROR; // the client sends an encodingByte we do not recognize
 	}
 	return retval;
 }
@@ -684,6 +684,9 @@ UA_TYPE_ENCODEBINARY(UA_ExtensionObject,
 					 case UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISXML:
 						 retval |= UA_ByteString_encodeBinary(&src->body, dst, offset);
 						 break;
+
+					 default:
+						 UA_assert(UA_FALSE);
 					 }
                      )
 

+ 3 - 0
src/util/ua_util.h

@@ -4,6 +4,7 @@
 #include <stdio.h>  // printf
 #include <stdlib.h> // malloc, free
 #include <string.h> // memcpy
+#include <assert.h> // assert
 #include "ua_types.h"
 
 /* Debug macros */
@@ -35,4 +36,6 @@ UA_Int32 UA_VTable_isValidType(UA_Int32 type);
 #define UA_free(ptr) _UA_free(ptr, # ptr, __FILE__, __LINE__)
 #define UA_alloc(ptr, size) _UA_alloc(ptr, size, # ptr, # size, __FILE__, __LINE__)
 
+#define UA_assert(ignore) assert(ignore)
+
 #endif /* UA_UTILITY_H_ */