Skip to content

Commit

Permalink
refactor(el): Remove the old server networking code (replaced with Ev…
Browse files Browse the repository at this point in the history
…entLoop)
  • Loading branch information
jpfr committed Apr 20, 2022
1 parent d0b541b commit de5d26a
Show file tree
Hide file tree
Showing 15 changed files with 35 additions and 255 deletions.
129 changes: 0 additions & 129 deletions arch/network_tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,142 +25,13 @@
#define MSG_NOSIGNAL 0
#endif

typedef struct ConnectionEntry {
UA_Connection connection;
LIST_ENTRY(ConnectionEntry) pointers;
} ConnectionEntry;

typedef struct {
const UA_Logger *logger;
UA_UInt16 port;
UA_UInt16 maxConnections;
UA_SOCKET serverSockets[FD_SETSIZE];
UA_UInt16 serverSocketsSize;
LIST_HEAD(, ConnectionEntry) connections;
UA_UInt16 connectionsSize;
} ServerNetworkLayerTCP;

/***************************/
/* Server NetworkLayer TCP */
/***************************/

#define MAXBACKLOG 100
#define NOHELLOTIMEOUT 120000 /* timeout in ms before close the connection
* if server does not receive Hello Message */

static UA_StatusCode
ServerNetworkLayerTCP_start(UA_ServerNetworkLayer *nl, const UA_Logger *logger,
const UA_String *customHostname) {
UA_initialize_architecture_network();

ServerNetworkLayerTCP *layer = (ServerNetworkLayerTCP *)nl->handle;
layer->logger = logger;

/* Get addrinfo of the server and create server sockets */
char hostname[512];
if(customHostname->length) {
if(customHostname->length >= sizeof(hostname))
return UA_STATUSCODE_BADOUTOFMEMORY;
memcpy(hostname, customHostname->data, customHostname->length);
hostname[customHostname->length] = '\0';
}
char portno[6];
UA_snprintf(portno, 6, "%d", layer->port);
struct addrinfo hints;
memset(&hints, 0, sizeof hints);

/* Get the discovery url from the hostname */
UA_String du = UA_STRING_NULL;
char discoveryUrlBuffer[256];
if(customHostname->length) {
du.length = (size_t)UA_snprintf(discoveryUrlBuffer, 255, "opc.tcp://%.*s:%d/",
(int)customHostname->length, customHostname->data,
layer->port);
du.data = (UA_Byte*)discoveryUrlBuffer;
} else {
char hostnameBuffer[256];
if(UA_gethostname(hostnameBuffer, 255) == 0) {
du.length = (size_t)UA_snprintf(discoveryUrlBuffer, 255, "opc.tcp://%s:%d/",
hostnameBuffer, layer->port);
du.data = (UA_Byte*)discoveryUrlBuffer;
} else {
UA_LOG_ERROR(layer->logger, UA_LOGCATEGORY_NETWORK, "Could not get the hostname");
return UA_STATUSCODE_BADINTERNALERROR;
}
}
UA_String_copy(&du, &nl->discoveryUrl);

UA_LOG_INFO(layer->logger, UA_LOGCATEGORY_NETWORK,
"TCP network layer listening on %.*s",
(int)nl->discoveryUrl.length, nl->discoveryUrl.data);
return UA_STATUSCODE_GOOD;
}

static UA_StatusCode
ServerNetworkLayerTCP_listen(UA_ServerNetworkLayer *nl, UA_Server *server,
UA_UInt16 timeout) {
return UA_STATUSCODE_GOOD;
}

static void
ServerNetworkLayerTCP_stop(UA_ServerNetworkLayer *nl, UA_Server *server) {
UA_deinitialize_architecture_network();
}

/* run only when the server is stopped */
static void
ServerNetworkLayerTCP_clear(UA_ServerNetworkLayer *nl) {
ServerNetworkLayerTCP *layer = (ServerNetworkLayerTCP *)nl->handle;
UA_String_clear(&nl->discoveryUrl);

/* Hard-close and remove remaining connections. The server is no longer
* running. So this is safe. */
ConnectionEntry *e, *e_tmp;
LIST_FOREACH_SAFE(e, &layer->connections, pointers, e_tmp) {
LIST_REMOVE(e, pointers);
layer->connectionsSize--;
UA_free(e);
if(nl->statistics) {
nl->statistics->currentConnectionCount--;
}
}

/* Free the layer */
UA_free(layer);
}

UA_ServerNetworkLayer
UA_ServerNetworkLayerTCP(UA_ConnectionConfig config, UA_UInt16 port,
UA_UInt16 maxConnections) {
UA_ServerNetworkLayer nl;
memset(&nl, 0, sizeof(UA_ServerNetworkLayer));
nl.clear = ServerNetworkLayerTCP_clear;
nl.localConnectionConfig = config;
nl.start = ServerNetworkLayerTCP_start;
nl.listen = ServerNetworkLayerTCP_listen;
nl.stop = ServerNetworkLayerTCP_stop;
nl.handle = NULL;

ServerNetworkLayerTCP *layer = (ServerNetworkLayerTCP*)
UA_calloc(1,sizeof(ServerNetworkLayerTCP));
if(!layer)
return nl;
nl.handle = layer;

layer->port = port;
layer->maxConnections = maxConnections;

return nl;
}

typedef struct TCPClientConnection {
struct addrinfo hints, *server;
UA_DateTime connStart;
UA_String endpointUrl;
UA_UInt32 timeout;
} TCPClientConnection;


/****************************/
/* Generic Socket Functions */
/****************************/
Expand Down
13 changes: 0 additions & 13 deletions include/open62541/network_tcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,6 @@

_UA_BEGIN_DECLS

/* Initializes a TCP network layer.
*
* @param config The connection config.
* @param port The TCP port to listen to.
* @param maxConnections Maximum number of TCP connections this network layer
* instance is allowed to allocate. Set to 0 for unlimited
* number of connections.
* @param logger Pointer to a logger
* @return Returns the network layer instance */
UA_ServerNetworkLayer UA_EXPORT
UA_ServerNetworkLayerTCP(UA_ConnectionConfig config, UA_UInt16 port,
UA_UInt16 maxConnections);

/* Open a non-blocking client TCP socket. The connection might not be fully
* opened yet. Drop into the _poll function withe a timeout to complete the
* connection. */
Expand Down
65 changes: 0 additions & 65 deletions include/open62541/plugin/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ typedef struct UA_Connection UA_Connection;
struct UA_SecureChannel;
typedef struct UA_SecureChannel UA_SecureChannel;

struct UA_ServerNetworkLayer;
typedef struct UA_ServerNetworkLayer UA_ServerNetworkLayer;

/**
* .. _networking:
*
Expand Down Expand Up @@ -114,68 +111,6 @@ struct UA_Connection {
void (*free)(UA_Connection *connection);
};

/**
* Server Network Layer
* --------------------
* The server exposes two functions to interact with remote clients:
* `processBinaryMessage` and `removeConnection`. These functions are called by
* the server network layer.
*
* It is the job of the server network layer to listen on a TCP socket, to
* accept new connections, to call the server with received messages and to
* signal closed connections to the server.
*
* The network layer is part of the server config. So users can provide a custom
* implementation if the provided example does not fit their architecture. The
* network layer is invoked only from the server's main loop. So the network
* layer does not need to be thread-safe. If the network layer receives a
* positive duration for blocking listening, the server's main loop will block
* until a message is received or the duration times out. */

struct UA_ServerNetworkLayer {
void *handle; /* Internal data */

/* Points to external memory, i.e. handled by server or client */
UA_NetworkStatistics *statistics;

UA_String discoveryUrl;

UA_ConnectionConfig localConnectionConfig;

/* Start listening on the network layer.
*
* @param nl The network layer
* @return Returns UA_STATUSCODE_GOOD or an error code. */
UA_StatusCode (*start)(UA_ServerNetworkLayer *nl, const UA_Logger *logger,
const UA_String *customHostname);

/* Listen for new and closed connections and arriving packets. Calls
* UA_Server_processBinaryMessage for the arriving packets. Closed
* connections are picked up here and forwarded to
* UA_Server_removeConnection where they are cleaned up and freed.
*
* @param nl The network layer
* @param server The server for processing the incoming packets and for
* closing connections.
* @param timeout The timeout during which an event must arrive in
* milliseconds
* @return A statuscode for the status of the network layer. */
UA_StatusCode (*listen)(UA_ServerNetworkLayer *nl, UA_Server *server,
UA_UInt16 timeout);

/* Close the network socket and all open connections. Afterwards, the
* network layer can be safely deleted.
*
* @param nl The network layer
* @param server The server that processes the incoming packets and for
* closing connections before deleting them.
* @return A statuscode for the status of the closing operation. */
void (*stop)(UA_ServerNetworkLayer *nl, UA_Server *server);

/* Deletes the network layer context. Call only after stopping. */
void (*clear)(UA_ServerNetworkLayer *nl);
};

/**
* Client Network Layer
* --------------------
Expand Down
1 change: 0 additions & 1 deletion tests/client/check_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

UA_Server *server;
UA_Boolean running;
UA_ServerNetworkLayer nl;
THREAD_HANDLE server_thread;

static void
Expand Down
2 changes: 0 additions & 2 deletions tests/client/check_client_async_connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
#include "thread_wrapper.h"

UA_Server *server;
UA_ServerNetworkLayer nl;

UA_Boolean connected = false;

static void
Expand Down
4 changes: 1 addition & 3 deletions tests/client/check_client_highlevel.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@

#include "thread_wrapper.h"

UA_Client *client;
UA_Server *server;
UA_Boolean running;
UA_ServerNetworkLayer nl;
THREAD_HANDLE server_thread;

UA_Client *client;

#define CUSTOM_NS "http://open62541.org/ns/test"
#define CUSTOM_NS_UPPER "http://open62541.org/ns/Test"

Expand Down
1 change: 0 additions & 1 deletion tests/client/check_client_subscriptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

UA_Server *server;
UA_Boolean running;
UA_ServerNetworkLayer nl;
THREAD_HANDLE server_thread;
static UA_Boolean noNewSubscription; /* Don't create a subscription when the
session activates */
Expand Down
1 change: 0 additions & 1 deletion tests/encryption/check_encryption_aes128sha256rsaoaep.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

UA_Server *server;
UA_Boolean running;
UA_ServerNetworkLayer nl;
THREAD_HANDLE server_thread;

THREAD_CALLBACK(serverloop) {
Expand Down
1 change: 0 additions & 1 deletion tests/encryption/check_encryption_basic128rsa15.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

UA_Server *server;
UA_Boolean running;
UA_ServerNetworkLayer nl;
THREAD_HANDLE server_thread;

THREAD_CALLBACK(serverloop) {
Expand Down
1 change: 0 additions & 1 deletion tests/encryption/check_encryption_basic256.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

UA_Server *server;
UA_Boolean running;
UA_ServerNetworkLayer nl;
THREAD_HANDLE server_thread;

THREAD_CALLBACK(serverloop) {
Expand Down
1 change: 0 additions & 1 deletion tests/encryption/check_encryption_basic256sha256.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

UA_Server *server;
UA_Boolean running;
UA_ServerNetworkLayer nl;
THREAD_HANDLE server_thread;

THREAD_CALLBACK(serverloop) {
Expand Down
1 change: 0 additions & 1 deletion tests/server/check_accesscontrol.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

UA_Server *server;
UA_Boolean running;
UA_ServerNetworkLayer nl;
THREAD_HANDLE server_thread;

THREAD_CALLBACK(serverloop) {
Expand Down
1 change: 0 additions & 1 deletion tests/server/check_server_callbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
int counter = 0;
UA_Server *server;
UA_Boolean running;
UA_ServerNetworkLayer nl;
UA_NodeId temperatureNodeId = {1, UA_NODEIDTYPE_NUMERIC, {1001}};
UA_Int32 temperature;
UA_NodeId pressureNodeId = {1, UA_NODEIDTYPE_NUMERIC, {1002}};
Expand Down
Loading

0 comments on commit de5d26a

Please sign in to comment.