/** * @brief Initialize a allocated connection manager. */ OpcUa_StatusCode OpcUa_TcpListener_ConnectionManager_Initialize( OpcUa_TcpListener_ConnectionManager* a_pConnectionManager) { OpcUa_StatusCode uStatus = OpcUa_Good; OpcUa_DeclareErrorTraceModule(OpcUa_Module_TcpListener); if (a_pConnectionManager == OpcUa_Null) { return OpcUa_BadInvalidArgument; } OpcUa_MemSet(a_pConnectionManager, 0, sizeof(OpcUa_TcpListener_ConnectionManager)); uStatus = OpcUa_List_Create( &(a_pConnectionManager->Connections)); OpcUa_ReturnErrorIfBad(uStatus); OpcUa_ReturnErrorIfBad(uStatus); return uStatus; }
/** * Initialize all ressources needed for tracing. */ OpcUa_StatusCode OPCUA_DLLCALL OpcUa_Trace_Initialize(OpcUa_Void) { OpcUa_StatusCode uStatus = OpcUa_Good; #if OPCUA_USE_SYNCHRONISATION uStatus = OPCUA_P_MUTEX_CREATE(&OpcUa_Trace_s_pLock); OpcUa_ReturnErrorIfBad(uStatus); #endif /* OPCUA_USE_SYNCHRONISATION */ uStatus = OPCUA_P_TRACE_INITIALIZE(); return uStatus; }
/** @brief Initializes all internal variables of the list. Do not call this function twice. Memory leaks will be created. @return OpcUa_BadInvalidArgument if a_pList is null @return OpcUa_Good on success @param a_pList [in] Location of the list */ OpcUa_StatusCode OpcUa_List_Initialize(OpcUa_List* a_pList) { OpcUa_StatusCode uStatus = OpcUa_Good; OpcUa_DeclareErrorTraceModule(OpcUa_Module_List); OpcUa_ReturnErrorIfArgumentNull(a_pList); a_pList->pMutex = OpcUa_Null; a_pList->currtElement = OpcUa_Null; a_pList->firstElement = OpcUa_Null; a_pList->lastElement = OpcUa_Null; a_pList->uintNbElements = 0; uStatus = OPCUA_P_MUTEX_CREATE(&(a_pList->pMutex)); OpcUa_ReturnErrorIfBad(uStatus); return OpcUa_Good; }
/** @brief Adds a new element with the given data into the List. The element is inserted as the first element. @return OpcUa_Good on success @return OpcUa_BadOutOfMemory if the allocation of the new element fails @return OpcUa_BadInvalidArgument if a_pList is null @return OpcUa_BadInvalidArgument if a_pElementData is null @param a_pList [in] Location of list @param a_pElementData [in] Location of user data */ OpcUa_StatusCode OpcUa_List_AddElement(OpcUa_List* a_pList, OpcUa_Void* a_pElementData) { OpcUa_StatusCode uStatus = OpcUa_Good; OpcUa_ListElement* pNewElement = OpcUa_Null; OpcUa_DeclareErrorTraceModule(OpcUa_Module_List); OpcUa_GotoErrorIfArgumentNull(a_pList); OpcUa_GotoErrorIfArgumentNull(a_pElementData); uStatus = OpcUa_ListElement_Create(&pNewElement); OpcUa_ReturnErrorIfBad(uStatus); if(pNewElement == OpcUa_Null) { return OpcUa_BadOutOfMemory; } pNewElement->data = a_pElementData; if(a_pList->firstElement) { a_pList->firstElement->prevElement = pNewElement; pNewElement->nextElement = a_pList->firstElement; } a_pList->firstElement = pNewElement; if(a_pList->lastElement == OpcUa_Null) { a_pList->lastElement = pNewElement; } a_pList->uintNbElements++; return OpcUa_Good; Error: return uStatus; }
/** * @brief Initializes the given parameter and allocates embedded objects. * If embedded objects cannot be allocated, the given pointer is set to OpcUa_Null. * * @return Bad status on fail; Good status on success; */ OpcUa_StatusCode OpcUa_TcpListener_Connection_Initialize(OpcUa_TcpListener_Connection* a_pConnection) { OpcUa_StatusCode uStatus = OpcUa_Good; OpcUa_DeclareErrorTraceModule(OpcUa_Module_TcpListener); OpcUa_ReturnErrorIfArgumentNull(a_pConnection); OpcUa_MemSet(&(a_pConnection->ConnectTime), 0, sizeof(OpcUa_DateTime)); OpcUa_MemSet(&(a_pConnection->DisconnectTime), 0, sizeof(OpcUa_DateTime)); a_pConnection->Socket = OpcUa_Null; #if OPCUA_P_SOCKETGETPEERINFO_V2 OpcUa_MemSet(a_pConnection->achPeerInfo, 0, OPCUA_P_PEERINFO_MIN_SIZE); #else /* OPCUA_P_SOCKETGETPEERINFO_V2 */ a_pConnection->PeerPort = 0; a_pConnection->PeerIp = 0; #endif /* OPCUA_P_SOCKETGETPEERINFO_V2 */ a_pConnection->pListenerHandle = OpcUa_Null; a_pConnection->pInputStream = OpcUa_Null; a_pConnection->uNoOfRequestsTotal = 0; a_pConnection->bConnected = OpcUa_False; OpcUa_String_Initialize(&a_pConnection->sURL); uStatus = OPCUA_P_MUTEX_CREATE(&(a_pConnection->Mutex)); OpcUa_ReturnErrorIfBad(uStatus); a_pConnection->ReceiveBufferSize = 0; a_pConnection->SendBufferSize = 0; a_pConnection->pSendQueue = OpcUa_Null; a_pConnection->bCloseWhenDone = OpcUa_False; a_pConnection->bNoRcvUntilDone = OpcUa_False; a_pConnection->bRcvDataPending = OpcUa_False; return OpcUa_Good; }
/*============================================================================ * Create a client socket *===========================================================================*/ OpcUa_StatusCode OPCUA_DLLCALL OpcUa_P_SocketManager_CreateClient( OpcUa_SocketManager a_hSocketManager, OpcUa_StringA a_sRemoteAddress, OpcUa_UInt16 a_uLocalPort, OpcUa_Socket_EventCallback a_pfnSocketCallBack, OpcUa_Void* a_pCallbackData, OpcUa_Socket* a_pSocket) { OpcUa_InternalSocket* pNewClientSocket = OpcUa_Null; OpcUa_InternalSocketManager* pInternalSocketManager = OpcUa_Null; OpcUa_UInt16 uPort = 0; OpcUa_StringA sRemoteAdress = OpcUa_Null; OpcUa_InitializeStatus(OpcUa_Module_Socket, "CreateClient"); #if !OPCUA_MULTITHREADED if(a_hSocketManager == OpcUa_Null) { a_hSocketManager = OpcUa_Socket_g_SocketManager; } #endif OpcUa_ReturnErrorIfArgumentNull(a_hSocketManager); OpcUa_ReturnErrorIfArgumentNull(a_pSocket); OpcUa_ReturnErrorIfArgumentNull(a_sRemoteAddress); /* parse address */ uStatus = OpcUa_P_ParseUrl( a_sRemoteAddress, &sRemoteAdress, &uPort); OpcUa_ReturnErrorIfBad(uStatus); pInternalSocketManager = (OpcUa_InternalSocketManager*)a_hSocketManager; pNewClientSocket = (OpcUa_InternalSocket*)OpcUa_SocketManager_FindFreeSocket( (OpcUa_SocketManager)pInternalSocketManager, OpcUa_False); if (pNewClientSocket == OpcUa_Null) { OpcUa_P_Memory_Free(sRemoteAdress); uStatus = OpcUa_BadMaxConnectionsReached; /* no socket left in the list. */ goto Error; } /* Create client socket. */ pNewClientSocket->rawSocket = OpcUa_P_Socket_CreateClient( a_uLocalPort, uPort, sRemoteAdress, &uStatus); OpcUa_P_Memory_Free(sRemoteAdress); OpcUa_GotoErrorIfTrue( pNewClientSocket->rawSocket == (OpcUa_RawSocket)OPCUA_P_SOCKET_INVALID, OpcUa_BadCommunicationError); pNewClientSocket->pfnEventCallback = a_pfnSocketCallBack; pNewClientSocket->pvUserData = a_pCallbackData; pNewClientSocket->Flags.bOwnThread = OpcUa_False; pNewClientSocket->Flags.EventMask = OPCUA_SOCKET_READ_EVENT | OPCUA_SOCKET_EXCEPT_EVENT | OPCUA_SOCKET_CONNECT_EVENT | OPCUA_SOCKET_TIMEOUT_EVENT; OPCUA_SOCKET_SETVALID(pNewClientSocket); /* return the new client socket */ *a_pSocket = pNewClientSocket; /* break loop to add new socket into eventing */ uStatus = OpcUa_P_SocketManager_InterruptLoop( a_hSocketManager, OPCUA_SOCKET_RENEWLOOP_EVENT, OpcUa_False); OpcUa_GotoErrorIfBad(uStatus); OpcUa_ReturnStatusCode; OpcUa_BeginErrorHandling; if(pNewClientSocket != OpcUa_Null) { if(pNewClientSocket->rawSocket != (OpcUa_RawSocket)OPCUA_P_SOCKET_INVALID) { OpcUa_P_RawSocket_Close(pNewClientSocket->rawSocket); pNewClientSocket->rawSocket = (OpcUa_RawSocket)OPCUA_P_SOCKET_INVALID; } OPCUA_SOCKET_INVALIDATE(pNewClientSocket); } OpcUa_FinishErrorHandling; }
/*============================================================================ * Create a server socket *===========================================================================*/ OpcUa_StatusCode OPCUA_DLLCALL OpcUa_P_SocketManager_CreateServer( OpcUa_SocketManager a_pSocketManager, OpcUa_StringA a_sAddress, OpcUa_Boolean a_bListenOnAllInterfaces, OpcUa_Socket_EventCallback a_pfnSocketCallBack, OpcUa_Void* a_pCallbackData, OpcUa_Socket* a_pSocket) { OpcUa_InternalSocketManager* pInternalSocketManager = OpcUa_Null; OpcUa_UInt16 uPort = 0; OpcUa_StringA sRemoteAdress = OpcUa_Null; OpcUa_InitializeStatus(OpcUa_Module_Socket, "CreateServer"); #if !OPCUA_MULTITHREADED if(a_pSocketManager == OpcUa_Null) { a_pSocketManager = OpcUa_Socket_g_SocketManager; } #endif OpcUa_ReturnErrorIfArgumentNull(a_pSocketManager); OpcUa_ReturnErrorIfArgumentNull(a_pSocket); pInternalSocketManager = (OpcUa_InternalSocketManager*)a_pSocketManager; /* parse address */ uStatus = OpcUa_P_ParseUrl( a_sAddress, &sRemoteAdress, &uPort); OpcUa_ReturnErrorIfBad(uStatus); if(a_bListenOnAllInterfaces) { if(sRemoteAdress != OpcUa_Null) { OpcUa_P_Memory_Free(sRemoteAdress); sRemoteAdress = OpcUa_Null; } } uStatus = OpcUa_SocketManager_InternalCreateServer( pInternalSocketManager, sRemoteAdress, uPort, a_pfnSocketCallBack, a_pCallbackData, a_pSocket); OpcUa_GotoErrorIfBad(uStatus); OpcUa_P_SocketManager_InterruptLoop(pInternalSocketManager, OPCUA_SOCKET_RENEWLOOP_EVENT, OpcUa_False); if(sRemoteAdress != OpcUa_Null) { OpcUa_P_Memory_Free(sRemoteAdress); } OpcUa_ReturnStatusCode; OpcUa_BeginErrorHandling; if(sRemoteAdress != OpcUa_Null) { OpcUa_P_Memory_Free(sRemoteAdress); } OpcUa_FinishErrorHandling; }