Browse Source

adding timeout to client's config refers to #219
@jpfr any objections on such a way of setting config values? A dedicated function for the timeout would be more future-proof, but i would not like to export 1000000's of functions

Stasik0 10 years ago
parent
commit
355898e313
3 changed files with 23 additions and 3 deletions
  1. 1 0
      examples/client.c
  2. 6 0
      include/ua_client.h
  3. 16 3
      src/client/ua_client.c

+ 1 - 0
examples/client.c

@@ -11,6 +11,7 @@
 
 int main(int argc, char *argv[]) {
 	UA_Client *client = UA_Client_new();
+	UA_Client_setConfig(client, (UA_ClientConfig){.timeout=500});
 	UA_ClientNetworkLayer nl = ClientNetworkLayerTCP_new(UA_ConnectionConfig_standard);
 	UA_StatusCode retval = UA_Client_connect(client, UA_ConnectionConfig_standard, nl,
 			"opc.tcp://localhost:16664");

+ 6 - 0
include/ua_client.h

@@ -28,9 +28,15 @@ typedef struct {
 struct UA_Client;
 typedef struct UA_Client UA_Client;
 
+typedef struct UA_ClientConfig {
+	UA_Int32 timeout; //sync resonse timeout
+} UA_ClientConfig;
+
 UA_Client UA_EXPORT * UA_Client_new(void);
 void UA_Client_delete(UA_Client* client);
 
+UA_StatusCode UA_EXPORT UA_Client_setConfig(UA_Client* client, UA_ClientConfig config);
+
 UA_StatusCode UA_EXPORT UA_Client_connect(UA_Client *client, UA_ConnectionConfig conf,
                                           UA_ClientNetworkLayer networkLayer, char *endpointUrl);
 

+ 16 - 3
src/client/ua_client.c

@@ -23,6 +23,9 @@ struct UA_Client {
     /* Session */
     UA_NodeId sessionId;
     UA_NodeId authenticationToken;
+
+    /* Config */
+    UA_ClientConfig config;
 };
 
 UA_Client * UA_Client_new(void) {
@@ -42,9 +45,19 @@ UA_Client * UA_Client_new(void) {
     UA_ByteString_init(&client->serverNonce);
     
     UA_NodeId_init(&client->authenticationToken);
+
+    /* default clientConfig */
+    client->config.timeout = 500;
+
     return client;
 }
 
+UA_StatusCode UA_Client_setConfig(UA_Client* client, UA_ClientConfig config) {
+	if(client)
+		client->config = config;
+	return UA_STATUSCODE_GOOD;
+}
+
 void UA_Client_delete(UA_Client* client){
     client->networkLayer.destroy(client->networkLayer.nlHandle);
     UA_String_deleteMembers(&client->endpointUrl);
@@ -90,7 +103,7 @@ static UA_StatusCode HelAckHandshake(UA_Client *c) {
 
     UA_Byte replybuf[1024];
     UA_ByteString reply = {.data = replybuf, .length = 1024};
-    retval = c->networkLayer.awaitResponse(c->networkLayer.nlHandle, &reply, 0);
+    retval = c->networkLayer.awaitResponse(c->networkLayer.nlHandle, &reply, c->config.timeout * 1000);
 	if (retval)
 		return retval;
 
@@ -174,7 +187,7 @@ static UA_StatusCode SecureChannelHandshake(UA_Client *client) {
     UA_ByteString reply;
     UA_ByteString_newMembers(&reply, client->connection.localConf.recvBufferSize);
     do {
-        retval = client->networkLayer.awaitResponse(client->networkLayer.nlHandle, &reply, 500 * 1000);
+        retval = client->networkLayer.awaitResponse(client->networkLayer.nlHandle, &reply, client->config.timeout * 1000);
         if(retval != UA_STATUSCODE_GOOD) {
             UA_ByteString_deleteMembers(&reply);
             return retval;
@@ -279,7 +292,7 @@ static void sendReceiveRequest(UA_RequestHeader *request, const UA_DataType *req
     UA_ByteString reply;
     do {
         UA_ByteString_newMembers(&reply, client->connection.localConf.recvBufferSize);
-        retval = client->networkLayer.awaitResponse(client->networkLayer.nlHandle, &reply, 500 * 1000);
+        retval = client->networkLayer.awaitResponse(client->networkLayer.nlHandle, &reply, client->config.timeout * 1000);
         if(retval != UA_STATUSCODE_GOOD) {
             UA_ByteString_deleteMembers(&reply);
             respHeader->serviceResult = retval;