tnet_transport_t* tnet_transport_create(const char* host, tnet_port_t port, tnet_socket_type_t type, const char* description) { tnet_transport_t* transport; if((transport = tsk_object_new(tnet_transport_def_t))){ transport->description = tsk_strdup(description); transport->local_host = tsk_strdup(host); transport->req_local_port = port; transport->type = type; transport->context = tnet_transport_context_create(); if((transport->master = tnet_socket_create(transport->local_host, transport->req_local_port, transport->type))){ transport->local_ip = tsk_strdup(transport->master->ip); transport->bind_local_port = transport->master->port; } else{ TSK_DEBUG_ERROR("Failed to create master socket"); TSK_OBJECT_SAFE_FREE(transport); } if(_tnet_transport_ssl_init(transport) != 0){ TSK_DEBUG_ERROR("Failed to initialize TLS and/or DTLS caps"); TSK_OBJECT_SAFE_FREE(transport); } // set priority tsk_runnable_set_priority(TSK_RUNNABLE(transport), TSK_THREAD_PRIORITY_TIME_CRITICAL); } return transport; }
int tnet_transport_prepare(tnet_transport_t *transport) { int ret = -1; transport_context_t *context; if (!transport || !transport->context) { TSK_DEBUG_ERROR("Invalid parameter."); return -1; } else { context = transport->context; } if (transport->prepared) { TSK_DEBUG_ERROR("Transport already prepared."); return -2; } /* Prepare master */ if (!transport->master) { if ((transport->master = tnet_socket_create(transport->local_host, transport->req_local_port, transport->type))) { tsk_strupdate(&transport->local_ip, transport->master->ip); transport->bind_local_port = transport->master->port; } else { TSK_DEBUG_ERROR("Failed to create master socket"); return -3; } } /* Start listening */ if (TNET_SOCKET_TYPE_IS_STREAM(transport->master->type)) { if ((ret = tnet_sockfd_listen(transport->master->fd, WSA_MAXIMUM_WAIT_EVENTS))) { TNET_PRINT_LAST_ERROR("listen have failed."); goto bail; } } /* Add the master socket to the context. */ // don't take ownership: will be closed by the dctor() when refCount==0 // otherwise will be closed twice: dctor() and removeSocket() if ((ret = addSocket(transport->master->fd, transport->master->type, transport, tsk_false, tsk_false, tsk_null))) { TSK_DEBUG_ERROR("Failed to add master socket"); goto bail; } /* set events on master socket */ if ((ret = WSAEventSelect(transport->master->fd, context->events[context->count - 1], FD_ALL_EVENTS) == SOCKET_ERROR)) { TNET_PRINT_LAST_ERROR("WSAEventSelect have failed."); goto bail; } transport->prepared = tsk_true; bail: return ret; }
int tnet_transport_prepare(tnet_transport_t *transport) { int ret = -1; transport_context_t *context; tnet_fd_t pipes[2]; TSK_DEBUG_INFO("tnet_transport_prepare()"); if(!transport || !transport->context) { TSK_DEBUG_ERROR("Invalid parameter."); return -1; } else { context = transport->context; } if(transport->prepared) { TSK_DEBUG_ERROR("Transport already prepared."); return -2; } /* Prepare master */ if(!transport->master) { if((transport->master = tnet_socket_create(transport->local_host, transport->req_local_port, transport->type))) { tsk_strupdate(&transport->local_ip, transport->master->ip); transport->bind_local_port = transport->master->port; } else { TSK_DEBUG_ERROR("Failed to create master socket"); return -3; } } /* Start listening */ if(TNET_SOCKET_TYPE_IS_STREAM(transport->master->type)) { if((ret = tnet_sockfd_listen(transport->master->fd, TNET_MAX_FDS))) { TNET_PRINT_LAST_ERROR("listen have failed."); goto bail; } } /* Create and add pipes to the fd_set */ if((ret = pipe(pipes))) { TNET_PRINT_LAST_ERROR("Failed to create new pipes."); goto bail; } /* set both R and W sides */ context->pipeR = pipes[0]; context->pipeW = pipes[1]; /* add R side */ TSK_DEBUG_INFO("pipeR fd=%d, pipeW=%d", context->pipeR, context->pipeW); if((ret = addSocket(context->pipeR, transport->master->type, transport, tsk_true, tsk_false, tsk_null))) { goto bail; } /* Add the master socket to the context. */ TSK_DEBUG_INFO("master fd=%d", transport->master->fd); // don't take ownership: will be closed by the dctor() when refCount==0 // otherwise will be closed twice: dctor() and removeSocket() if((ret = addSocket(transport->master->fd, transport->master->type, transport, tsk_false, tsk_false, tsk_null))) { TSK_DEBUG_ERROR("Failed to add master socket"); goto bail; } transport->prepared = tsk_true; bail: return ret; }
int tnet_transport_prepare(tnet_transport_t *transport) { int ret = -1; transport_context_t *context; tnet_fd_t pipes[2]; if(!transport || !transport->context){ TSK_DEBUG_ERROR("Invalid parameter."); return -1; } else{ context = transport->context; } if(transport->prepared){ TSK_DEBUG_ERROR("Transport already prepared."); return -2; } /* Prepare master */ if(!transport->master){ if((transport->master = tnet_socket_create(transport->local_host, transport->req_local_port, transport->type))){ tsk_strupdate(&transport->local_ip, transport->master->ip); transport->bind_local_port = transport->master->port; } else{ TSK_DEBUG_ERROR("Failed to create master socket"); return -3; } } /* set events */ context->events = TNET_POLLIN | TNET_POLLNVAL | TNET_POLLERR; if(TNET_SOCKET_TYPE_IS_STREAM(transport->master->type)){ context->events |= TNET_POLLOUT // emulate WinSock2 FD_CONNECT event //#if !defined(ANDROID) // | TNET_POLLHUP /* FIXME: always present */ //#endif ; } /* Start listening */ if(TNET_SOCKET_TYPE_IS_STREAM(transport->master->type)){ if((ret = tnet_sockfd_listen(transport->master->fd, TNET_MAX_FDS))){ TNET_PRINT_LAST_ERROR("listen have failed."); goto bail; } } /* Create and add pipes to the fd_set */ if((ret = pipe(pipes))){ TNET_PRINT_LAST_ERROR("Failed to create new pipes."); goto bail; } /* set both R and W sides */ context->pipeR = pipes[0]; context->pipeW = pipes[1]; /* add R side */ TSK_DEBUG_INFO("pipeR fd=%d", context->pipeR); if((ret = addSocket(context->pipeR, transport->master->type, transport, tsk_true, tsk_false))){ goto bail; } /* Add the master socket to the context. */ TSK_DEBUG_INFO("master fd=%d", transport->master->fd); // don't take ownership: will be closed by the dctor() // otherwise will be closed twice: dctor() and removeSocket() if((ret = addSocket(transport->master->fd, transport->master->type, transport, tsk_false, tsk_false))){ TSK_DEBUG_ERROR("Failed to add master socket"); goto bail; } transport->prepared = tsk_true; bail: return ret; }