void tearDown(void) { if (FileDesc >= 0) { LOG0("Shutting down socket..."); KineticSocket_Close(FileDesc); FileDesc = 0; } KineticLogger_Close(); }
KineticStatus KineticSession_Connect(KineticSession * const session) { if (session == NULL) { return KINETIC_STATUS_SESSION_EMPTY; } // Establish the connection KINETIC_ASSERT(strlen(session->config.host) > 0); session->socket = KineticSocket_Connect( session->config.host, session->config.port); if (session->socket == KINETIC_SOCKET_DESCRIPTOR_INVALID) { LOG0("Session connection failed!"); session->socket = KINETIC_SOCKET_DESCRIPTOR_INVALID; session->connected = false; return KINETIC_STATUS_CONNECTION_ERROR; } session->connected = true; bus_socket_t socket_type = session->config.useSsl ? BUS_SOCKET_SSL : BUS_SOCKET_PLAIN; session->si = calloc(1, sizeof(socket_info) + 2 * PDU_PROTO_MAX_LEN); if (session->si == NULL) { return KINETIC_STATUS_MEMORY_ERROR; } bool success = Bus_RegisterSocket(session->messageBus, socket_type, session->socket, session); if (!success) { LOG0("Failed registering connection with client!"); goto connection_error_cleanup; } // Wait for initial unsolicited status to be received in order to obtain connection ID success = KineticResourceWaiter_WaitTilAvailable(&session->connectionReady, KINETIC_CONNECTION_TIMEOUT_SECS); if (!success) { LOG0("Timed out waiting for connection ID from device!"); goto connection_error_cleanup; } LOGF1("Received connection ID %lld for session %p", (long long)KineticSession_GetConnectionID(session), (void*)session); return KINETIC_STATUS_SUCCESS; connection_error_cleanup: if (session->si != NULL) { free(session->si); session->si = NULL; } if (session->socket != KINETIC_SOCKET_DESCRIPTOR_INVALID) { KineticSocket_Close(session->socket); session->socket = KINETIC_SOCKET_DESCRIPTOR_INVALID; } session->connected = false; return KINETIC_STATUS_CONNECTION_ERROR; }
KineticStatus KineticSession_Disconnect(KineticSession * const session) { if (session == NULL) { return KINETIC_STATUS_SESSION_EMPTY; } if (!session->connected || session->socket < 0) { return KINETIC_STATUS_CONNECTION_ERROR; } // Close the connection KineticSocket_Close(session->socket); Bus_ReleaseSocket(session->messageBus, session->socket, NULL); free(session->si); session->si = NULL; session->socket = KINETIC_SOCKET_INVALID; session->connected = false; pthread_mutex_destroy(&session->sendMutex); return KINETIC_STATUS_SUCCESS; }