Browse Source

remove crash in the client when the server does not respond

Julius Pfrommer 9 years ago
parent
commit
4f30c9aaf2
3 changed files with 21 additions and 11 deletions
  1. 8 7
      examples/networklayer_tcp.c
  2. 3 2
      examples/server.c
  3. 10 2
      src/client/ua_client.c

+ 8 - 7
examples/networklayer_tcp.c

@@ -483,6 +483,14 @@ ClientNetworkLayerTCP_connect(UA_ConnectionConfig localConf, char *endpointUrl,
     UA_Connection_init(&connection);
     connection.localConf = localConf;
 
+    //socket_set_nonblocking(connection.sockfd);
+    connection.send = socket_write;
+    connection.recv = socket_recv;
+    connection.close = ClientNetworkLayerClose;
+    connection.getSendBuffer = ClientNetworkLayerGetBuffer;
+    connection.releaseSendBuffer = ClientNetworkLayerReleaseBuffer;
+    connection.releaseRecvBuffer = ClientNetworkLayerReleaseBuffer;
+
     size_t urlLength = strlen(endpointUrl);
     if(urlLength < 11 || urlLength >= 512) {
         UA_LOG_WARNING((*logger), UA_LOGCATEGORY_NETWORK, "Server url size invalid");
@@ -554,12 +562,5 @@ ClientNetworkLayerTCP_connect(UA_ConnectionConfig localConf, char *endpointUrl,
     }
 #endif
 
-    //socket_set_nonblocking(connection.sockfd);
-    connection.send = socket_write;
-    connection.recv = socket_recv;
-    connection.close = ClientNetworkLayerClose;
-    connection.getSendBuffer = ClientNetworkLayerGetBuffer;
-    connection.releaseSendBuffer = ClientNetworkLayerReleaseBuffer;
-    connection.releaseRecvBuffer = ClientNetworkLayerReleaseBuffer;
     return connection;
 }

+ 3 - 2
examples/server.c

@@ -156,8 +156,9 @@ writeLedStatus(void *handle, const UA_NodeId nodeid,
 
 #ifdef ENABLE_METHODCALLS
 static UA_StatusCode
-getMonitoredItems(const UA_NodeId objectId, const UA_Variant *input,
-                  UA_Variant *output, void *handle) {
+getMonitoredItems(void *methodHandle, const UA_NodeId objectId,
+                  size_t inputSize, const UA_Variant *input,
+                  size_t outputSize, UA_Variant *output) {
     UA_String tmp = UA_STRING("Hello World");
     UA_Variant_setScalarCopy(output, &tmp, &UA_TYPES[UA_TYPES_STRING]);
     printf("getMonitoredItems was called\n");

+ 10 - 2
src/client/ua_client.c

@@ -148,6 +148,10 @@ static UA_StatusCode SecureChannelHandshake(UA_Client *client, UA_Boolean renew)
     if(renew && client->scExpiresAt - UA_DateTime_now() > client->config.timeToRenewSecureChannel * 10000)
         return UA_STATUSCODE_GOOD;
 
+    UA_Connection *c = &client->connection;
+    if(c->state != UA_CONNECTION_ESTABLISHED)
+        return UA_STATUSCODE_BADSERVERNOTCONNECTED;
+
     UA_SecureConversationMessageHeader messageHeader;
     messageHeader.messageHeader.messageTypeAndFinal = UA_MESSAGETYPEANDFINAL_OPNF;
     messageHeader.secureChannelId = 0;
@@ -180,7 +184,6 @@ static UA_StatusCode SecureChannelHandshake(UA_Client *client, UA_Boolean renew)
     }
 
     UA_ByteString message;
-    UA_Connection *c = &client->connection;
     UA_StatusCode retval = c->getSendBuffer(c, c->remoteConf.recvBufferSize, &message);
     if(retval != UA_STATUSCODE_GOOD) {
         UA_AsymmetricAlgorithmSecurityHeader_deleteMembers(&asymHeader);
@@ -280,7 +283,7 @@ static UA_StatusCode ActivateSession(UA_Client *client) {
 
     UA_AnonymousIdentityToken identityToken;
     UA_AnonymousIdentityToken_init(&identityToken);
-    identityToken.policyId = client->token.policyId,
+    UA_String_copy(&client->token.policyId, &identityToken.policyId);
 
     //manual ExtensionObject encoding of the identityToken
     request.userIdentityToken.encoding = UA_EXTENSIONOBJECT_DECODED_NODELETE;
@@ -313,6 +316,11 @@ static UA_StatusCode EndpointsHandshake(UA_Client *client) {
     __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_GETENDPOINTSREQUEST],
                         &response, &UA_TYPES[UA_TYPES_GETENDPOINTSRESPONSE]);
 
+    if(response.responseHeader.serviceResult != UA_STATUSCODE_GOOD) {
+        UA_LOG_ERROR(client->logger, UA_LOGCATEGORY_CLIENT, "GetEndpointRequest failed");
+        return response.responseHeader.serviceResult;
+    }
+
     UA_Boolean endpointFound = UA_FALSE;
     UA_Boolean tokenFound = UA_FALSE;
     UA_String securityNone = UA_STRING("http://opcfoundation.org/UA/SecurityPolicy#None");