Browse Source

fixed bug with reconnect in exampleServerACPLT

open62541 10 years ago
parent
commit
57d8fc7a3d

+ 4 - 4
examples/src/networklayer.c

@@ -47,7 +47,7 @@ int NL_TCP_SetNonBlocking(int sock) {
 void NL_Connection_printf(void* payload) {
   UA_UInt32 id;
   NL_Connection* c = (NL_Connection*) payload;
-  UA_TL_Connection_getId(c->connection,&id);
+  UA_TL_Connection_getHandle(c->connection,&id);
   printf("ListElement connectionHandle = %d\n",id);
 }
 void NL_addHandleToSet(UA_Int32 handle, NL_data* nl) {
@@ -57,13 +57,13 @@ void NL_addHandleToSet(UA_Int32 handle, NL_data* nl) {
 void NL_setFdSet(void* payload) {
   UA_UInt32 id;
   NL_Connection* c = (NL_Connection*) payload;
-  UA_TL_Connection_getId(c->connection,&id);
+  UA_TL_Connection_getHandle(c->connection,&id);
   NL_addHandleToSet(id, c->networkLayer);
 }
 void NL_checkFdSet(void* payload) {
 	  UA_UInt32 id;
   NL_Connection* c = (NL_Connection*) payload;
-  UA_TL_Connection_getId(c->connection,&id);
+  UA_TL_Connection_getHandle(c->connection,&id);
   if (FD_ISSET(id, &(c->networkLayer->readerHandles))) {
 	  c->reader((void*)c);
   }
@@ -119,7 +119,7 @@ void* NL_TCP_reader(NL_Connection *c) {
 
 	UA_UInt32 connectionId;
 	UA_TL_Connection_getLocalConfiguration(c->connection, &localBuffers);
-	UA_TL_Connection_getId(c->connection, &connectionId);
+	UA_TL_Connection_getHandle(c->connection, &connectionId);
 	UA_alloc((void**)&(readBuffer.data),localBuffers.recvBufferSize);
 
 

+ 19 - 3
examples/src/opcuaServerACPLT.c

@@ -8,6 +8,7 @@
 #include "networklayer.h"
 #include "ua_stack_channel_manager.h"
 #include "ua_transport_connection.h"
+#include "ua_stack_session_manager.h"
 
 #ifdef LINUX
 
@@ -59,7 +60,10 @@ void server_run() {
 	UA_String endpointUrl;
 	UA_String_copycstring("open62541.org",&endpointUrl);
 	SL_ChannelManager_init(2,3600000, 873, 23, &endpointUrl);
-	UA_TL_Connection_new(&connection, localBuffers, (TL_Writer)NL_TCP_writer);
+
+	UA_SessionManager_init(2,300000,5);
+
+
 
 
 
@@ -96,9 +100,13 @@ void server_run() {
 		exit(1);
 	}
 
+	//UA_TL_Connection_new(&connection, localBuffers, (TL_Writer)NL_TCP_writer);
+
 	/* Now start listening for the clients, here process will
 	 * go in sleep mode and will wait for the incoming connection
 	 */
+	UA_TL_Connection1 tmpConnection;
+
 	while (listen(sockfd, 5) != -1) {
 		clilen = sizeof(cli_addr);
 		/* Accept actual connection from the client */
@@ -107,8 +115,16 @@ void server_run() {
 			perror("ERROR on accept");
 			exit(1);
 		}
+
+		UA_TL_ConnectionManager_getConnectionById(newsockfd, &tmpConnection);
+		if(tmpConnection == UA_NULL)
+		{
+			UA_TL_Connection_new(&connection, localBuffers, (TL_Writer)NL_TCP_writer);
+		}
 		UA_TL_Connection_getState(connection, &connectionState);
+
 		printf("server_run - connection accepted: %i, state: %i\n", newsockfd, connectionState);
+
 		UA_TL_Connection_bind(connection, newsockfd);
 		//connection.connectionHandle = newsockfd;
 		do {
@@ -127,8 +143,8 @@ void server_run() {
 		} while(connectionState != CONNECTIONSTATE_CLOSE);
 		shutdown(newsockfd,2);
 		close(newsockfd);
-		UA_TL_Connection_close(connection);
-
+		UA_TL_ConnectionManager_getConnectionById(newsockfd, &tmpConnection);
+		UA_TL_ConnectionManager_removeConnection(tmpConnection);
 	}
 	shutdown(sockfd,2);
 	close(sockfd);

+ 8 - 4
src/ua_services_session.c

@@ -1,4 +1,5 @@
 #include "ua_services.h"
+#include "ua_stack_session_manager.h"
 #include "ua_application.h"
 
 Session sessionMockup = {
@@ -8,15 +9,18 @@ Session sessionMockup = {
 
 UA_Int32 Service_CreateSession(UA_Session session, const UA_CreateSessionRequest *request, UA_CreateSessionResponse *response) {
 	UA_String_printf("CreateSession Service - endpointUrl=", &(request->endpointUrl));
-	// FIXME: create session
+
 	UA_Session *newSession;
 
-	UA_Session_new(newSession);
-	UA_Session_init(*newSession, &request->sessionName,request->requestedSessionTimeout);
+	UA_Session_new(&newSession);
+	//TODO get maxResponseMessageSize
+	UA_Session_init(*newSession, (UA_String*)&request->sessionName, request->requestedSessionTimeout,request->maxResponseMessageSize,500);
 
-	UA_SessionManager_addSession(session);
+	UA_SessionManager_addSession(newSession);
 	UA_Session_getId(*newSession, &response->sessionId);
 
+
+
 	return UA_SUCCESS;
 }
 

+ 0 - 9
src/ua_stack_channel.c

@@ -214,8 +214,6 @@ UA_Int32 SL_Channel_new(SL_secureChannel **channel,
 	retval |= UA_ByteString_copy(senderCertificate,
 			&thisChannel->localAsymAlgSettings.senderCertificate);
 
-
-
 	thisChannel->state = UA_SL_CHANNEL_CLOSED;
 
 	**channel = (SL_secureChannel)thisChannel;
@@ -238,15 +236,8 @@ UA_Int32 SL_Channel_initByRequest(SL_secureChannel channel,
 
 	((SL_Channel1*)channel)->connection = connection;
 
-//	channel->createdAt = UA_DateTime_now();
-
-//	channel->localAsymAlgSettings.receiverCertificateThumbprint
-
-
 	SL_Channel_generateNonce(&channel->localNonce);
 
-
-
 	*pos += 4; // skip the securechannelid
 	UA_AsymmetricAlgorithmSecurityHeader_decodeBinary(msg, pos,
 			&channel->remoteAsymAlgSettings);

+ 16 - 3
src/ua_stack_session.c

@@ -16,23 +16,36 @@ typedef struct UA_SessionType
 	Application *application;
 	UA_list_List pendingRequests;
 	SL_secureChannel channel;
+	UA_UInt32 maxRequestMessageSize;
+	UA_UInt32 maxResponseMessageSize;
+
 }UA_SessionType;
 
-UA_Int32 UA_Session_new(UA_Session *newSession)
+UA_Int32 UA_Session_new(UA_Session **newSession)
 {
 	UA_Int32 retval = UA_SUCCESS;
-	retval |= UA_alloc((void**)&newSession,sizeof(UA_SessionType));
+	UA_Session *session;
+
+	retval |= UA_alloc((void**)&session,sizeof(UA_Session));
+
+	retval |= UA_alloc((void**)session,sizeof(UA_SessionType));
+	*newSession = session;
+	**newSession = *session;
 	//get memory for request list
 	return retval;
 }
 
-UA_Int32 UA_Session_init(UA_Session session, UA_String *sessionName, UA_Double requestedSessionTimeout)
+UA_Int32 UA_Session_init(UA_Session session, UA_String *sessionName, UA_Double requestedSessionTimeout, UA_UInt32 maxRequestMessageSize, UA_UInt32 maxResponseMessageSize)
 {
 	UA_Int32 retval = UA_SUCCESS;
 	retval |= UA_String_copy(sessionName, &((UA_SessionType*)session)->name);
+	((UA_SessionType*)session)->maxRequestMessageSize = maxRequestMessageSize;
+	((UA_SessionType*)session)->maxResponseMessageSize = maxResponseMessageSize;
+
 	//TODO handle requestedSessionTimeout
 	return retval;
 }
+
 UA_Boolean UA_Session_compare(UA_Session session1, UA_Session session2)
 {
 	if(session1 && session2){

+ 3 - 2
src/ua_stack_session.h

@@ -16,8 +16,9 @@
 
 typedef struct UA_SessionType *UA_Session;
 
-UA_Int32 UA_Session_new(UA_Session *newSession);
-UA_Int32 UA_Session_init(UA_Session session, UA_String* sessionName);
+UA_Int32 UA_Session_new(UA_Session **newSession);
+UA_Int32 UA_Session_init(UA_Session session, UA_String *sessionName, UA_Double requestedSessionTimeout, UA_UInt32 maxRequestMessageSize, UA_UInt32 maxResponseMessageSize);
+
 
 UA_Boolean UA_Session_compare(UA_Session session1, UA_Session session2);
 UA_Boolean UA_Session_compareByToken(UA_Session session, UA_NodeId *token);

+ 15 - 9
src/ua_stack_session_manager.c

@@ -26,6 +26,9 @@ UA_Int32 UA_SessionManager_init(UA_UInt32 maxSessionCount,UA_UInt32 sessionLifet
 {
 	UA_Int32 retval = UA_SUCCESS;
 	retval |= UA_alloc((void**)&sessionManager,sizeof(UA_SessionManagerType));
+
+	retval |= UA_list_init(sessionManager->sessions);
+
 	sessionManager->maxSessionCount = maxSessionCount;
 	sessionManager->lastSessionId = startSessionId;
 	sessionManager->sessionLifetime = sessionLifetime;
@@ -62,17 +65,20 @@ UA_Int32 UA_SessionManager_getSessionById(UA_NodeId *sessionId, UA_Session *sess
 
 UA_Int32 UA_SessionManager_getSessionByToken(UA_NodeId *token, UA_Session *session)
 {
- 	UA_list_Element* current = sessionManager->sessions->first;
-	while (current)
+	if(sessionManager->sessions)
 	{
-		if (current->payload)
+		UA_list_Element* current = sessionManager->sessions->first;
+		while (current)
 		{
-			UA_list_Element* elem = (UA_list_Element*) current;
-			*session = *((UA_Session*) (elem->payload));
-		 	if(UA_Session_compareByToken(*session,token))
-		 	{
-		 		return UA_SUCCESS;
-		 	}
+			if (current->payload)
+			{
+				UA_list_Element* elem = (UA_list_Element*) current;
+				*session = *((UA_Session*) (elem->payload));
+				if(UA_Session_compareByToken(*session,token))
+				{
+					return UA_SUCCESS;
+				}
+			}
 		}
 	}
 	*session = UA_NULL;

+ 1 - 0
src/ua_transport_binary.c

@@ -185,6 +185,7 @@ static UA_Int32 TL_handleClo(UA_TL_Connection1 connection, const UA_ByteString*
 
 	retval |= UA_SecureConversationMessageHeader_delete(header);
 
+
 //TODO remove that
 	UA_TL_Connection_close(connection);
 	return retval;

+ 3 - 3
src/ua_transport_binary_secure.c

@@ -137,13 +137,13 @@ static void init_response_header(UA_RequestHeader const * p,
 #define INVOKE_SERVICE(TYPE) \
 	UA_##TYPE##Request p; \
 	UA_##TYPE##Response r; \
-	UA_Session *session = UA_NULL; \
+	UA_Session session = UA_NULL; \
 	UA_##TYPE##Request_decodeBinary(msg, pos, &p); \
 	UA_##TYPE##Response_init(&r); \
 	init_response_header(&p.requestHeader, &r.responseHeader); \
-	UA_SessionManager_getSessionByToken(&p.requestHeader.authenticationToken, session); \
+	UA_SessionManager_getSessionByToken(&p.requestHeader.authenticationToken, &session); \
 	DBG_VERBOSE(printf("Invoke Service: %s\n", #TYPE)); \
-	Service_##TYPE(*session, &p, &r); \
+	Service_##TYPE(session, &p, &r); \
 	DBG_VERBOSE(printf("Finished Service: %s\n", #TYPE)); \
     *pos = 0; \
 	UA_ByteString_newMembers(&response_msg, UA_##TYPE##Response_calcSize(&r)); \

+ 4 - 3
src/ua_transport_connection.c

@@ -26,6 +26,7 @@ UA_Int32 UA_TL_Connection_new(UA_TL_Connection1 *connection, TL_Buffer localBuff
 	{
 		(*((TL_Connection**)connection))->localConf = localBuffers;
 		(*((TL_Connection**)connection))->writer = writer;
+		(*((TL_Connection**)connection))->state = CONNECTIONSTATE_CLOSED;
 	}
 	return retval;
 }
@@ -102,14 +103,14 @@ UA_Int32 UA_TL_Connection_getLocalConfiguration(UA_TL_Connection1 connection, TL
 		return UA_ERROR;
 	}
 }
-UA_Int32 UA_TL_Connection_getId(UA_TL_Connection1 connection, UA_UInt32 *connectionId)
+UA_Int32 UA_TL_Connection_getHandle(UA_TL_Connection1 connection, UA_UInt32 *connectionHandle)
 {
 	if(connection)
 	{
-		*connectionId = ((TL_Connection*)connection)->connectionHandle;
+		*connectionHandle = ((TL_Connection*)connection)->connectionHandle;
 		return UA_SUCCESS;
 	}else{
-			connectionId = 0;
+			connectionHandle = 0;
 			return UA_ERROR;
 		}
 }

+ 2 - 3
src/ua_transport_connection.h

@@ -31,13 +31,12 @@ UA_Int32 UA_TL_Connection_delete(UA_TL_Connection1 connection);
 UA_Int32 UA_TL_Connection_callWriter(UA_TL_Connection1 connection, const UA_ByteString** gather_bufs, UA_Int32 gather_len);
 
 UA_Int32 UA_TL_Connection_close(UA_TL_Connection1 connection);
-UA_Int32 UA_TL_Connection_new(
-		UA_TL_Connection1 *connection,
+UA_Int32 UA_TL_Connection_new(UA_TL_Connection1 *connection,
 		TL_Buffer localBuffers,TL_Writer writer);
 UA_Int32 UA_TL_Connection_bind(UA_TL_Connection1 connection, UA_Int32 handle);
 
 //getter
-UA_Int32 UA_TL_Connection_getId(UA_TL_Connection1 connection, UA_UInt32 *connectionId);
+UA_Int32 UA_TL_Connection_getHandle(UA_TL_Connection1 connection, UA_UInt32 *connectionId);
 UA_Int32 UA_TL_Connection_getProtocolVersion(UA_TL_Connection1 connection, UA_UInt32 *protocolVersion);
 
 UA_Int32 UA_TL_Connection_getState(UA_TL_Connection1 connection, UA_Int32 *connectionState);

+ 3 - 3
src/ua_transport_connection_manager.c

@@ -37,7 +37,7 @@ UA_Int32 UA_TL_ConnectionManager_init(UA_UInt32 maxConnectionCount)
 UA_Int32 UA_TL_ConnectionManager_addConnection(UA_TL_Connection1 *connection)
 {
 	UA_UInt32 connectionId;
-	UA_TL_Connection_getId(*connection, &connectionId);
+	UA_TL_Connection_getHandle(*connection, &connectionId);
 	return UA_indexedList_addValue(&(connectionManager->connections), connectionId, connection);
 }
 
@@ -45,11 +45,11 @@ UA_Int32 UA_TL_ConnectionManager_addConnection(UA_TL_Connection1 *connection)
 UA_Int32 UA_TL_ConnectionManager_removeConnection(UA_TL_Connection1 connection)
 {
 	UA_UInt32 connectionId;
-	UA_TL_Connection_getId(connection, &connectionId);
+	UA_TL_Connection_getHandle(connection, &connectionId);
 	UA_TL_Connection1 foundValue = UA_indexedList_findValue(&connectionManager->connections,connectionId);
 	if(foundValue)
 	{
-		//remove connection
+		UA_TL_Connection_delete(connection);//remove connection
 	}
 	return UA_SUCCESS;
 }