/** * \brief This function is never ending loop of server state STREAM_OPEN. * This loop is used, when Verse server uses TCP for data exchange (not * UDP nor WebSocket) */ int vs_STREAM_OPEN_tcp_loop(struct vContext *C) { struct VS_CTX *vs_ctx = CTX_server_ctx(C); struct VSession *vsession = CTX_current_session(C); struct IO_CTX *io_ctx = CTX_io_ctx(C); struct timeval tv; fd_set set; int flag, ret, error; /* Set socket non-blocking */ flag = fcntl(io_ctx->sockfd, F_GETFL, 0); if( (fcntl(io_ctx->sockfd, F_SETFL, flag | O_NONBLOCK)) == -1) { if(is_log_level(VRS_PRINT_ERROR)) v_print_log(VRS_PRINT_ERROR, "fcntl(): %s\n", strerror(errno)); return -1; } /* "Never ending" loop */ while(1) { FD_ZERO(&set); FD_SET(io_ctx->sockfd, &set); /* Use negotiated FPS */ tv.tv_sec = 0; tv.tv_usec = 1000000/vsession->fps_host; /* Wait for recieved data */ if( (ret = select(io_ctx->sockfd+1, &set, NULL, NULL, &tv)) == -1) { if(is_log_level(VRS_PRINT_ERROR)) v_print_log(VRS_PRINT_ERROR, "%s:%s():%d select(): %s\n", __FILE__, __FUNCTION__, __LINE__, strerror(errno)); goto end; /* Was event on the listen socket */ } else if(ret>0 && FD_ISSET(io_ctx->sockfd, &set)) { /* Try to receive data through SSL connection */ if( v_tcp_read(io_ctx, &error) <= 0 ) { goto end; } if(v_STREAM_handle_messages(C) == 0) { goto end; } /* When some payload data were received, then poke data thread */ sem_post(vs_ctx->data.sem); } if( (ret = v_STREAM_pack_message(C)) == 0 ) { goto end; } /* Send command to the client */ if(ret == 1) { if( v_tcp_write(io_ctx, &error) <= 0) { goto end; } } } end: /* Set socket blocking again */ flag = fcntl(io_ctx->sockfd, F_GETFL, 0); if( (fcntl(io_ctx->sockfd, F_SETFL, flag & ~O_NONBLOCK)) == -1) { if(is_log_level(VRS_PRINT_ERROR)) v_print_log(VRS_PRINT_ERROR, "fcntl(): %s\n", strerror(errno)); return -1; } return 0; }
/** * \brief Main function for new thread. This thread is created for new * connection with client. This thread will try to authenticate new user * and negotiate new udp port. */ void *vs_tcp_conn_loop(void *arg) { struct vContext *C = (struct vContext*)arg; struct VS_CTX *vs_ctx = CTX_server_ctx(C); struct VSession *vsession = CTX_current_session(C); struct IO_CTX *io_ctx = CTX_io_ctx(C); struct VStreamConn *stream_conn = CTX_current_stream_conn(C); struct VMessage *r_message=NULL, *s_message=NULL; struct timeval tv; fd_set set; int error, ret; void *udp_thread_result; unsigned int int_size; /* Try to get size of TCP buffer */ int_size = sizeof(int_size); getsockopt(io_ctx->sockfd, SOL_SOCKET, SO_RCVBUF, (void *)&stream_conn->socket_buffer_size, &int_size); #ifdef WITH_OPENSSL /* Try to do TLS handshake with client */ if(vs_TLS_handshake(C)!=1) { goto end; } #endif r_message = (struct VMessage*)calloc(1, sizeof(struct VMessage)); s_message = (struct VMessage*)calloc(1, sizeof(struct VMessage)); CTX_r_message_set(C, r_message); CTX_s_message_set(C, s_message); stream_conn->host_state = TCP_SERVER_STATE_RESPOND_METHODS; if(is_log_level(VRS_PRINT_DEBUG_MSG)) { printf("%c[%d;%dm", 27, 1, 31); v_print_log(VRS_PRINT_DEBUG_MSG, "Server TCP state: RESPOND_methods\n"); printf("%c[%dm", 27, 0); } /* "Never ending" loop */ while(1) { FD_ZERO(&set); FD_SET(io_ctx->sockfd, &set); tv.tv_sec = VRS_TIMEOUT; /* User have to send something in 30 seconds */ tv.tv_usec = 0; if( (ret = select(io_ctx->sockfd+1, &set, NULL, NULL, &tv)) == -1) { if(is_log_level(VRS_PRINT_ERROR)) v_print_log(VRS_PRINT_ERROR, "%s:%s():%d select(): %s\n", __FILE__, __FUNCTION__, __LINE__, strerror(errno)); goto end; /* Was event on the listen socket */ } else if(ret>0 && FD_ISSET(io_ctx->sockfd, &set)) { /* Try to receive data through TCP connection */ if( v_tcp_read(io_ctx, &error) <= 0 ) { goto end; } /* Handle verse handshake at TCP connection */ if( (ret = vs_handle_handshake(C)) == -1) { goto end; } /* When there is something to send, then send it to peer */ if( ret == 1 ) { /* Send response message to the client */ if( (ret = v_tcp_write(io_ctx, &error)) <= 0) { goto end; } } } else { if(is_log_level(VRS_PRINT_ERROR)) v_print_log(VRS_PRINT_ERROR, "No response in %d seconds\n", VRS_TIMEOUT); goto end; } } end: if(is_log_level(VRS_PRINT_DEBUG_MSG)) { printf("%c[%d;%dm", 27, 1, 31); v_print_log(VRS_PRINT_DEBUG_MSG, "Server TCP state: CLOSING\n"); printf("%c[%dm", 27, 0); } /* Set up TCP CLOSING state (non-blocking) */ vs_CLOSING(C); /* Receive and Send messages are not neccessary any more */ if(r_message != NULL) { free(r_message); r_message = NULL; CTX_r_message_set(C, NULL); } if(s_message != NULL) { free(s_message); s_message = NULL; CTX_s_message_set(C, NULL); } /* TCP connection is considered as CLOSED, but it is not possible to use * this connection for other client */ stream_conn->host_state = TCP_SERVER_STATE_CLOSED; /* NULL pointer at stream connection */ CTX_current_stream_conn_set(C, NULL); /* Set TCP connection to CLOSED */ if(is_log_level(VRS_PRINT_DEBUG_MSG)) { printf("%c[%d;%dm", 27, 1, 31); v_print_log(VRS_PRINT_DEBUG_MSG, "Server TCP state: CLOSED\n"); printf("%c[%dm", 27, 0); } /* Was udp thread created? */ if(vsession->udp_thread != 0 ) { /* Wait for UDP thread (this is blocking operation) */ v_print_log(VRS_PRINT_DEBUG_MSG, "Waiting for join with UDP thread ...\n"); if(pthread_join(vsession->udp_thread, &udp_thread_result) != 0) { v_print_log(VRS_PRINT_DEBUG_MSG, "UDP thread was not joined\n"); } } pthread_mutex_lock(&vs_ctx->data.mutex); /* Unsubscribe this session (this avatar) from all nodes */ vs_node_free_avatar_reference(vs_ctx, vsession); /* Try to destroy avatar node */ vs_node_destroy_avatar_node(vs_ctx, vsession); pthread_mutex_unlock(&vs_ctx->data.mutex); /* This session could be used again for authentication */ stream_conn->host_state = TCP_SERVER_STATE_LISTEN; /* Clear session flags */ vsession->flags = 0; if(vsession->peer_cookie.str != NULL) { free(vsession->peer_cookie.str); vsession->peer_cookie.str = NULL; } if(vsession->ded.str != NULL) { free(vsession->ded.str); vsession->ded.str = NULL; } if(vsession->client_name != NULL) { free(vsession->client_name); vsession->client_name = NULL; } if(vsession->client_version != NULL) { free(vsession->client_version); vsession->client_version = NULL; } if(is_log_level(VRS_PRINT_DEBUG_MSG)) { printf("%c[%d;%dm", 27, 1, 31); v_print_log(VRS_PRINT_DEBUG_MSG, "Server TCP state: LISTEN\n"); printf("%c[%dm", 27, 0); } free(C); C = NULL; pthread_exit(NULL); return NULL; }