Browse Source

use generic writer in acplt server

Julius Pfrommer 11 years ago
parent
commit
3eac766bed

+ 2 - 2
examples/src/Makefile.am

@@ -29,7 +29,7 @@ __top_builddir__bin_exampleServer_LDADD= $(top_builddir)/lib/libopen62541.a $(MT
 __top_builddir__bin_exampleServer_LDFLAGS= $(COV_LDFLAGS)
 
 __top_builddir__bin_exampleServerACPLT_CFLAGS = -I$(top_builddir)/src -I$(top_builddir)/include $(GLOBAL_AM_CFLAGS)
-__top_builddir__bin_exampleServerACPLT_SOURCES = opcuaServerACPLT.c 
+__top_builddir__bin_exampleServerACPLT_SOURCES = opcuaServerACPLT.c networklayer.c
 __top_builddir__bin_exampleServerACPLT_LDADD= $(top_builddir)/lib/libopen62541.a $(MT_LDADD)
 __top_builddir__bin_exampleServerACPLT_LDFLAGS= $(COV_LDFLAGS)
-endif
+endif

+ 2 - 1
examples/src/networklayer.c

@@ -163,7 +163,7 @@ void* NL_TCP_readerThread(NL_Connection *c) {
 #endif
 
 /** write to a tcp transport layer connection */
-UA_Int32 NL_TCP_writer(TL_Connection* c, UA_ByteString** gather_buf, UA_UInt32 gather_len) {
+UA_Int32 NL_TCP_writer(struct TL_Connection_T* c, const UA_ByteString** gather_buf, UA_UInt32 gather_len) {
 
 	struct iovec iov[gather_len];
 	UA_UInt32 total_len = 0;
@@ -196,6 +196,7 @@ UA_Int32 NL_TCP_writer(TL_Connection* c, UA_ByteString** gather_buf, UA_UInt32 g
 			break;
 			// TODO: handle incompletely send messages
 		} else {
+			break;
 			// TODO: error handling
 		}
 	}

+ 1 - 0
examples/src/networklayer.h

@@ -55,5 +55,6 @@ typedef struct NL_Connection_T {
 
 NL_data* NL_init(NL_Description* tlDesc, UA_Int32 port);
 UA_Int32 NL_msgLoop(NL_data* nl, struct timeval* tv,UA_Int32 (*timeoutCallBack)(void*),void *arg);
+UA_Int32 NL_TCP_writer(struct TL_Connection_T* c, const UA_ByteString** gather_buf, UA_UInt32 gather_len);
 
 #endif /* NETWORKLAYER_H_ */

+ 8 - 49
examples/src/opcuaServerACPLT.c

@@ -5,6 +5,7 @@
 #include "opcua.h"
 #include "ua_transport.h"
 #include "ua_transport_binary.h"
+#include "networklayer.h"
 
 #ifdef LINUX
 
@@ -14,7 +15,6 @@
 #include <sys/socketvar.h>
 #include <unistd.h>
 
-void server_init();
 void server_run();
 
 #endif
@@ -26,49 +26,19 @@ void server_run();
 int main(void) {
 
 #ifdef LINUX
-    server_init();
-    server_run();
+	printf("Starting open62541 demo server on port %d\n", PORT);
+	server_run();
 #endif
 
-return EXIT_SUCCESS;
-
+	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;
-
-UA_Int32 server_writer(TL_Connection* connection, UA_ByteString** gather_buf, UA_UInt32 gather_len) {
-	UA_UInt32 total_len = 0;
-	for(UA_UInt32 i=0;i<gather_len;i++) {
-		total_len += gather_buf[i]->length;
-	}
-    UA_ByteString msg;
-	UA_ByteString_newMembers(&msg, total_len);
-
-	UA_UInt32 pos = 0;
-	for(UA_UInt32 i=0;i<gather_len;i++) {
-		memcpy(msg.data+pos, gather_buf[i]->data, gather_buf[i]->length);
-		pos += gather_buf[i]->length;
-	}
-	server.writeData.data = msg.data;
-	server.writeData.length = msg.length;
-	server.newDataToWrite = 1;
-	return UA_SUCCESS;
-}
-
 void server_run() {
 	TL_Connection connection;
 	connection.connectionState = CONNECTIONSTATE_CLOSED;
-	connection.writerCallback = (TL_Writer)server_writer;
+	connection.writerCallback = (TL_Writer) NL_TCP_writer;
 	connection.localConf.maxChunkCount = 1;
 	connection.localConf.maxMessageSize = BUFFER_SIZE;
 	connection.localConf.protocolVersion = 0;
@@ -97,11 +67,11 @@ void server_run() {
 	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) {
+	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");
@@ -121,12 +91,9 @@ void server_run() {
 		}
 
 		printf("server_run - connection accepted: %i, state: %i\n", newsockfd, connection.connectionState);
-		/* communication loop */
-		int i = 0;
+		connection.connectionHandle = newsockfd;
 		do {
-			/* If connection is established then start communicating */
             memset(buffer, 0, BUFFER_SIZE);
-
 			n = read(newsockfd, buffer, BUFFER_SIZE);
 			if (n > 0) {
                 slMessage.data = (UA_Byte*) buffer;
@@ -137,14 +104,6 @@ void server_run() {
 				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("server_run - written %d bytes \n\n", n);
-				server.newDataToWrite = 0;
-				UA_ByteString_deleteMembers(&server.writeData);
-			}
-			i++;
 		} while(connection.connectionState != CONNECTIONSTATE_CLOSE);
 		shutdown(newsockfd,2);
 		close(newsockfd);