Parcourir la source

fixed broken serverExample

Leon Urbas il y a 11 ans
Parent
commit
0ae5917cd4

+ 4 - 4
examples/src/Makefile.am

@@ -1,5 +1,5 @@
 
-bin_PROGRAMS= $(top_builddir)/bin/exampleServer $(top_builddir)/bin/exampleServerMT $(top_builddir)/bin/simpleTest
+bin_PROGRAMS= $(top_builddir)/bin/exampleServer $(top_builddir)/bin/exampleServerMT $(top_builddir)/bin/exampleServerACPLT
 #__top_builddir__bin_exampleServer_LDFLAGS = -all-static
 __top_builddir__bin_exampleServer_CFLAGS = -I$(top_builddir)/src -I$(top_builddir)/include
 __top_builddir__bin_exampleServer_SOURCES = opcuaServer.c
@@ -9,8 +9,8 @@ __top_builddir__bin_exampleServerMT_CFLAGS = -I$(top_builddir)/src -I$(top_build
 __top_builddir__bin_exampleServerMT_SOURCES = opcuaServerMT.c
 __top_builddir__bin_exampleServerMT_LDADD= $(top_builddir)/lib/libopen62541.a -lpthread
 
-__top_builddir__bin_simpleTest_CFLAGS = -I$(top_builddir)/src -I$(top_builddir)/include
-__top_builddir__bin_simpleTest_SOURCES = simpleTest.c
-__top_builddir__bin_simpleTest_LDADD= $(top_builddir)/lib/libopen62541.a
+__top_builddir__bin_exampleServerACPLT_CFLAGS = -I$(top_builddir)/src -I$(top_builddir)/include
+__top_builddir__bin_exampleServerACPLT_SOURCES = opcuaServerMT.c
+__top_builddir__bin_exampleServerACPLT_LDADD= $(top_builddir)/lib/libopen62541.a -lpthread
 
 AM_CFLAGS = $(GLOBAL_AM_CFLAGS)

+ 151 - 0
examples/src/opcuaServerACPLT.c

@@ -0,0 +1,151 @@
+/*
+ ============================================================================
+ Name : opcuaServer.c
+ Author :
+ Version :
+ Copyright : Your copyright notice
+ Description :
+ ============================================================================
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <memory.h> // bzero
+
+#include "opcua.h"
+
+#ifdef LINUX
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <sys/socketvar.h>
+
+void server_init();
+void server_run();
+
+#endif
+
+#define PORT 16664
+#define MAXMSG 512
+#define BUFFER_SIZE 8192
+
+int main(void) {
+
+#ifdef LINUX
+	server_init();
+	server_run();
+#endif
+
+	return EXIT_SUCCESS;
+
+}
+
+#ifdef LINUX
+
+void server_init() {
+	printf("Starting open62541 demo server on port %d\n", PORT);
+}
+
+typedef struct T_Server {
+	int newDataToWrite;
+	UA_ByteString writeData;
+} Server;
+
+Server server;
+void server_writer(UA_TL_connection connection, UA_ByteString* msg) {
+	UA_ByteString_copy(msg,&server.writeData);
+	server.newDataToWrite = 1;
+}
+void server_run() {
+
+	UA_TL_connection connection;
+	connection.connectionState = connectionState_CLOSE;
+	connection.writerCallback = server_writer;
+	connection.localConf.maxChunkCount = 1;
+	connection.localConf.maxMessageSize = BUFFER_SIZE;
+	connection.localConf.protocolVersion = 0;
+	connection.localConf.recvBufferSize = BUFFER_SIZE;
+	connection.localConf.recvBufferSize = BUFFER_SIZE;
+
+	UA_ByteString slMessage = { -1, UA_NULL };
+
+	int optval = 1;
+	int sockfd, newsockfd, portno, clilen;
+	char buffer[BUFFER_SIZE];
+	struct sockaddr_in serv_addr, cli_addr;
+	int n;
+
+	/* First call to socket() function */
+	sockfd = socket(PF_INET, SOCK_STREAM, 0);
+	if (sockfd < 0) {
+		perror("ERROR opening socket");
+		exit(1);
+	}
+
+	/* Initialize socket structure */
+	bzero((void *) &serv_addr, sizeof(serv_addr));
+	portno = PORT;
+	serv_addr.sin_family = AF_INET;
+	serv_addr.sin_addr.s_addr = INADDR_ANY;
+	serv_addr.sin_port = htons(portno);
+
+	if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval)
+			== -1) {
+		perror("setsockopt");
+		exit(1);
+	}
+	/* Now bind the host address using bind() call.*/
+	if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
+		perror("ERROR on binding");
+		exit(1);
+	}
+
+	/* Now start listening for the clients, here process will
+	 * go in sleep mode and will wait for the incoming connection
+	 */
+	while (listen(sockfd, 5) != -1) {
+		clilen = sizeof(cli_addr);
+		/* Accept actual connection from the client */
+		newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
+		if (newsockfd < 0) {
+			perror("ERROR on accept");
+			exit(1);
+		}
+
+		printf("connection accepted\n");
+		/* communication loop */
+		while (connection.connectionState != connectionState_CLOSE) {
+			/* If connection is established then start communicating */
+			bzero(buffer, BUFFER_SIZE);
+
+			n = read(newsockfd, buffer, BUFFER_SIZE);
+
+			if (n > 0) {
+				slMessage.data = buffer;
+				slMessage.length = n;
+				UA_ByteString_printx("server_run - received=",slMessage);
+				TL_process(&connection, &slMessage);
+			} else if (n < 0) {
+				perror("ERROR reading from socket1");
+				exit(1);
+			}
+
+			if (server.newDataToWrite) {
+				UA_ByteString_printx("Send data:", &server.writeData);
+				n = write(newsockfd, server.writeData.data,
+						server.writeData.length);
+				printf("written %d bytes \n", n);
+				server.newDataToWrite = 0;
+				UA_ByteString_deleteMembers(&server.writeData);
+
+				server.writeData.data = UA_NULL;
+				server.writeData.length = 0;
+			}
+		}
+		close(newsockfd);
+		connection.connectionState = connectionState_CLOSED;
+	}
+}
+
+#endif

+ 0 - 43
examples/src/simpleTest.c

@@ -1,43 +0,0 @@
-/*
- * main.c
- *
- *  Created on: 07.03.2014
- *      Author: mrt
- */
-#include <stdio.h>
-#include "opcua.h"
-void testString() {
-	UA_Int32 pos = 0;
-	UA_Int32 retval = UA_SUCCESS;
-	UA_String string;
-
-	UA_Byte binString[12] = {0x08,0x00,0x00,0x00,'A','C','P','L','T',' ','U','A'};
-	UA_ByteString src = { 12, binString };
-
-	retval = UA_String_decodeBinary(&src, &pos, &string);
-	UA_String_deleteMembers(&string);
-
-}
-void testVariant() {
-	// given
-	UA_Int32 pos = 0;
-	UA_Byte data[] = { UA_INT32_NS0 | UA_VARIANT_ENCODINGMASKTYPE_ARRAY, 0x02, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF};
-	UA_ByteString src = { sizeof(data), data };
-	UA_Variant dst;
-	// when
-	UA_Int32 retval = UA_Variant_decodeBinary(&src, &pos, &dst);
-	UA_Variant_deleteMembers(&dst);
-}
-void testByte() {
-	// given
-	UA_Byte dst;
-	UA_Byte data[] = { 0x08 };
-	UA_ByteString src = { 1, data };
-	UA_Int32 pos = 0;
-	// when
-	UA_Int32 retval = UA_Byte_decodeBinary(&src, &pos, &dst);
-}
-int main() {
-	testByte();
-	return 0;
-}

+ 6 - 1
src/ua_connection.h

@@ -41,13 +41,18 @@ typedef struct
 }TL_buffer;
 
 /* Transport Layer Connection */
+struct T_TL_connection;		// forward declaration
+typedef void* (*UA_TL_reader)(struct T_TL_connection* c);
+typedef UA_Int32 (*UA_TL_writer)(struct T_TL_connection* c, UA_ByteString* msg);
+
 typedef struct T_TL_connection
 {
 	UA_Int32 connectionHandle;
 	UA_UInt32 connectionState;
 	pthread_t readerThread;
+	UA_TL_reader readerCallback;
 	TL_buffer localConf;
-	UA_Int32 (*UA_TL_writer)(struct T_TL_connection* c, UA_ByteString* msg);
+	UA_TL_writer writerCallback;
 	TL_buffer remoteConf;
 	UA_String localEndpointUrl;
 	UA_String remoteEndpointUrl;

+ 19 - 8
src/ua_stack.c

@@ -45,8 +45,7 @@ int UA_TL_TCP_SetNonBlocking(int sock) {
 
 /** the tcp reader thread - single shot if single-threaded, looping until CLOSE if multi-threaded
  */
-void* UA_TL_TCP_reader(void *p) {
-	UA_TL_connection* c = (UA_TL_connection*) p;
+void* UA_TL_TCP_reader(UA_TL_connection *c) {
 
 	UA_ByteString readBuffer;
 	UA_alloc((void**)&(readBuffer.data),c->localConf.recvBufferSize);
@@ -94,7 +93,7 @@ void* UA_TL_TCP_reader(void *p) {
 }
 
 /** write to a tcp transport layer connection */
-UA_Int32 UA_TL_TCP_write(struct T_TL_connection* c, UA_ByteString* msg) {
+UA_Int32 UA_TL_TCP_writer(struct T_TL_connection* c, UA_ByteString* msg) {
 	UA_ByteString_printx("write data:", msg);
 	int nWritten = 0;
 	while (nWritten < msg->length) {
@@ -145,12 +144,13 @@ void* UA_TL_TCP_listen(void *p) {
 				retval |= UA_alloc((void**)&c,sizeof(UA_TL_connection));
 				TL_Connection_init(c, tld);
 				c->connectionHandle = newsockfd;
-				c->UA_TL_writer = UA_TL_TCP_write;
+				c->writerCallback = UA_TL_TCP_writer;
+				c->readerCallback = UA_TL_TCP_reader;
 				// add to list
 				UA_list_addPayloadToBack(&(tld->connections),c);
 				if (tld->threaded == UA_STACK_MULTITHREADED) {
 					// TODO: handle retval of pthread_create
-					pthread_create( &(c->readerThread), NULL, UA_TL_TCP_reader, (void*) c);
+					pthread_create( &(c->readerThread), NULL, (void*(*)(void*)) UA_TL_TCP_reader, (void*) c);
 				} else {
 					UA_TL_TCP_SetNonBlocking(c->connectionHandle);
 				}
@@ -165,7 +165,7 @@ void* UA_TL_TCP_listen(void *p) {
 void checkFdSet(void* payload) {
   UA_TL_connection* c = (UA_TL_connection*) payload;
   if (FD_ISSET(c->connectionHandle, &(theTL.readerHandles))) {
-	  UA_TL_TCP_reader((void*)c);
+	  c->readerCallback((void*)c);
   }
 }
 
@@ -179,6 +179,17 @@ void setFdSet(void* payload) {
   UA_TL_addHandleToSet(c->connectionHandle);
 }
 
+
+UA_Int32 UA_Stack_addReaderHandle(UA_Int32 handle, UA_TL_reader reader) {
+	UA_Int32 retval = UA_SUCCESS;
+	UA_TL_connection* c;
+	retval = UA_alloc((void**)&c,sizeof(UA_TL_connection));
+	c->connectionHandle = handle;
+	c->connectionState = connectionState_ESTABLISHED;
+	c->readerCallback = reader;
+	return retval;
+}
+
 UA_Int32 UA_Stack_msgLoop(struct timeval *tv, UA_Int32(*worker)(void*), void *arg)  {
 	UA_Int32 result;
 	while (UA_TRUE) {
@@ -209,9 +220,9 @@ UA_Int32 UA_Stack_msgLoop(struct timeval *tv, UA_Int32(*worker)(void*), void *ar
 				printf("UA_Stack_msgLoop - result=bad arguments\n"); //FIXME: handle
 				break;
 			case EAGAIN:
-				printf("UA_Stack_msgLoop - result=do it again\n"); //FIXME: handle
+				printf("UA_Stack_msgLoop - result=do it again\n");
 			default:
-				printf("UA_Stack_msgLoop - result=%d\n",err); //FIXME: handle
+				printf("UA_Stack_msgLoop - result=%d\n",err);
 				worker(arg);
 			}
 		} else if (FD_ISSET(theTL.listenerHandle,&theTL.readerHandles)) { // activity on listener port

+ 3 - 2
src/ua_transportLayer.c

@@ -10,7 +10,8 @@ UA_Int32 TL_Connection_init(UA_TL_connection* c, UA_TL_data* tld)
 	c->connectionHandle = -1;
 	c->connectionState = connectionState_CLOSED;
 	c->readerThread = -1;
-	c->UA_TL_writer = UA_NULL;
+	c->writerCallback = UA_NULL;
+	c->readerCallback = UA_NULL;
 	memcpy(&(c->localConf),&(tld->tld->localConf),sizeof(TL_buffer));
 	memset(&(c->remoteConf),0,sizeof(TL_buffer));
 	UA_String_copy(&(tld->endpointUrl), &(c->localEndpointUrl));
@@ -173,7 +174,7 @@ UA_Int32 TL_send(UA_TL_connection* connection, UA_ByteString* msg)
 	DBG_VERBOSE(printf("TL_send - entered \n"));
 
 	if (TL_check(connection,msg,UA_TL_CHECK_REMOTE) == UA_SUCCESS) {
-		connection->UA_TL_writer(connection,msg);
+		connection->writerCallback(connection,msg);
 	}
 	else
 	{