void redrobd_rc_net_server_thread::shutdown_server(void) { // Initiate a controlled server shutdown m_shutdown_requested = true; ///////////////////////////////////// // Server socket ///////////////////////////////////// if (!m_server_closed) { // Shutdown server socket if (shutdown_socket(m_server_sd, true, true) != SOCKET_SUPPORT_SUCCESS) { THROW_EXP(REDROBD_INTERNAL_ERROR, REDROBD_SOCKET_OPERATION_FAILED, "Shutdown (requested) server socket failed in thread %s", get_name().c_str()); } // Close server socket if (close_socket(m_server_sd) != SOCKET_SUPPORT_SUCCESS) { THROW_EXP(REDROBD_INTERNAL_ERROR, REDROBD_SOCKET_OPERATION_FAILED, "Close (requested) server socket failed in thread %s", get_name().c_str()); } m_server_closed = true; } ///////////////////////////////////// // Client socket ///////////////////////////////////// if (m_client_connected) { // Shutdown client socket if (shutdown_socket(m_client_sd, true, true) != SOCKET_SUPPORT_SUCCESS) { THROW_EXP(REDROBD_INTERNAL_ERROR, REDROBD_SOCKET_OPERATION_FAILED, "Shutdown (requested) client socket failed in thread %s", get_name().c_str()); } // Close client socket if (close_socket(m_client_sd) != SOCKET_SUPPORT_SUCCESS) { THROW_EXP(REDROBD_INTERNAL_ERROR, REDROBD_SOCKET_OPERATION_FAILED, "Close (requested) client socket failed in thread %s", get_name().c_str()); } } }
long redrobd_rc_net_server_thread::cleanup(void) { try { redrobd_log_writeln(get_name() + " : cleanup started"); /////////////////////////////////////////////// // If no controlled shutdown was requested, // proceed with cleanup /////////////////////////////////////////////// if (!m_shutdown_requested) { // Shutdown server socket if (shutdown_socket(m_server_sd, true, true) != SOCKET_SUPPORT_SUCCESS) { THROW_EXP(REDROBD_INTERNAL_ERROR, REDROBD_SOCKET_OPERATION_FAILED, "Shutdown server socket failed in thread %s", get_name().c_str()); } // Close server socket if (close_socket(m_server_sd) != SOCKET_SUPPORT_SUCCESS) { THROW_EXP(REDROBD_INTERNAL_ERROR, REDROBD_SOCKET_OPERATION_FAILED, "Close server socket failed in thread %s", get_name().c_str()); } m_server_closed = true; } redrobd_log_writeln(get_name() + " : cleanup done"); return THREAD_SUCCESS; } catch (excep &exp) { syslog_error(redrobd_error_syslog_string(exp).c_str()); return THREAD_INTERNAL_ERROR; } catch (...) { syslog_error("redrobd_rc_net_server_thread::cleanup->Unexpected exception"); return THREAD_INTERNAL_ERROR; } }
int main(int argc, char**argv){ char *host,*port; char* messages[5]; messages[0] = "C is quirky flawed and an enormous success - Dennis M. Ritchie"; messages[1] = "The question of whether computers can think is like the question of whether submarines can swim - Edsger W. Dijkstra"; messages[2] = "Typing is no substitute for thinking. - Dartmouth Basic Manual"; messages[3] = "Any C programmer here ? Yeah i got a C in programming class. That counts ? - Unknown"; messages[4] = "The Internet? Is that thing still around? - Homer Simpson"; int num_msgs = 5; if(argc < 3 || argc > 3){ printf("Incorrect number of args.\n"); print_use_and_exit(); }else{ host = argv[1]; port = argv[2]; } CONN_INFO* connection = setup_socket(host, port); if(connection == NULL) print_use_and_exit(); for(int i = 0; i < num_msgs; i++){ MESSAGE*msg = malloc(sizeof(MESSAGE)); msg->buffer = messages[i]; msg->length = strlen(messages[i])+1; rtp_send_message(connection, msg); printf("Sent message %i to server...\n", i); MESSAGE*response = rtp_receive_message(connection); if(response) printf("Received reponse:\n\t%s\n\n", response->buffer); else{ perror("Error must have occurred while receiving response. No message returned\n"); free(connection); exit(EXIT_FAILURE); } free(msg); } shutdown_socket(connection); free(connection); return 0; }
void redrobd_rc_net_server_thread::handle_clients(void) { long rc; socket_address client_sa; char client_ip[DOTTED_IP_ADDR_LEN]; ostringstream oss_msg; while (1) { oss_msg << "Wait for client on port:" << dec << m_server_port; redrobd_log_writeln(get_name() + " : " + oss_msg.str()); oss_msg.str(""); // Wait for client to connect rc = accept_socket(m_server_sd, &m_client_sd, &client_sa); // Check if controlled server shutdown if ( (rc != SOCKET_SUPPORT_SUCCESS) && (m_shutdown_requested) ) { // This was a controlled shutdown. // Quit server thread with no error. break; } else if (rc != SOCKET_SUPPORT_SUCCESS) { // This was not a controlled shutdown. // Quit server thread with error. THROW_EXP(REDROBD_INTERNAL_ERROR, REDROBD_SOCKET_OPERATION_FAILED, "Accept server socket failed in thread %s", get_name().c_str()); } else { m_client_connected = true; } // Get address info for connected client if (to_ip_address(client_sa.net_addr, client_ip, DOTTED_IP_ADDR_LEN) != SOCKET_SUPPORT_SUCCESS) { THROW_EXP(REDROBD_INTERNAL_ERROR, REDROBD_SOCKET_OPERATION_FAILED, "Client address for server socket failed in thread %s", get_name().c_str()); } oss_msg << "Client connected => " << client_ip << ", port:" << dec << client_sa.port; redrobd_log_writeln(get_name() + " : " + oss_msg.str()); oss_msg.str(""); // Handle client commands bool handle_command = true; while (handle_command) { try { uint16_t client_command; // Wait for command recv_client((void *)&client_command, sizeof(client_command)); ntoh16(&client_command); // Handle command if (client_command == CLI_CMD_STEER) { uint8_t steer_code; // Get steer code recv_client((void *)&steer_code, sizeof(steer_code)); // Update latest steer code pthread_mutex_lock(&m_steer_code_mutex); m_steer_code = steer_code; pthread_mutex_unlock(&m_steer_code_mutex); } else if (client_command == CLI_CMD_GET_VOLTAGE) { uint16_t voltage; // Reply with latest voltage pthread_mutex_lock(&m_voltage_mutex); voltage = m_voltage; pthread_mutex_unlock(&m_voltage_mutex); hton16(&voltage); send_client((void *)&voltage, sizeof(voltage)); } else if (client_command == CLI_CMD_CAMERA) { uint8_t camera_code; // Get camera code recv_client((void *)&camera_code, sizeof(camera_code)); // Update latest camera code pthread_mutex_lock(&m_camera_code_mutex); m_camera_code = camera_code; pthread_mutex_unlock(&m_camera_code_mutex); } else if (client_command == CLI_CMD_GET_SYS_STATS) { RC_NET_SYS_STAT sys_stat; // Reply with latest system statistics pthread_mutex_lock(&m_sys_stat_mutex); memcpy(&sys_stat, &m_sys_stat, sizeof(m_sys_stat)); pthread_mutex_unlock(&m_sys_stat_mutex); hton32(&sys_stat.mem_used); hton16(&sys_stat.irq); hton32(&sys_stat.uptime); hton32(&sys_stat.cpu_temp); hton16(&sys_stat.cpu_voltage); hton16(&sys_stat.cpu_freq); send_client((void *)&sys_stat, sizeof(sys_stat)); } else { oss_msg << "Unknown client command : 0x" << hex << (unsigned)client_command; redrobd_log_writeln(get_name() + " : " + oss_msg.str()); oss_msg.str(""); handle_command = false; } } catch (...) { handle_command = false; } } if (m_shutdown_requested) { // This was a controlled shutdown. // Quit server thread with no error. break; } // Shutdown client socket if (shutdown_socket(m_client_sd, true, true) != SOCKET_SUPPORT_SUCCESS) { THROW_EXP(REDROBD_INTERNAL_ERROR, REDROBD_SOCKET_OPERATION_FAILED, "Shutdown client socket failed in thread %s", get_name().c_str()); } // Close client socket if (close_socket(m_client_sd) != SOCKET_SUPPORT_SUCCESS) { THROW_EXP(REDROBD_INTERNAL_ERROR, REDROBD_SOCKET_OPERATION_FAILED, "Close client socket failed in thread %s", get_name().c_str()); } m_client_connected = false; } }
static int test(int close_socket) { LOGINREC *login; DBPROCESS *dbproc; RETCODE ret; int expected_error = -1; printf("Starting %s\n", UNITTEST); dbinit(); dberrhandle(syb_err_handler); dbmsghandle(syb_msg_handler); printf("About to logon\n"); login = dblogin(); DBSETLPWD(login, PASSWORD); DBSETLUSER(login, USER); DBSETLAPP(login, "t0020"); printf("About to open\n"); dbproc = dbopen(login, SERVER); dbsetuserdata(dbproc, (BYTE*) &expected_error); if (strlen(DATABASE)) dbuse(dbproc, DATABASE); dbloginfree(login); dbcmd(dbproc, "select * from sysobjects"); printf("dbsqlexec should not hang.\n"); ret = dbsettime(15); if (ret != SUCCEED) { fprintf(stderr, "Failed. Error setting timeout.\n"); return 1; } if (!shutdown_socket(dbproc)) { fprintf(stderr, "Error shutting down connection\n"); return 1; } if (close_socket) close(end_socket); alarm(20); expected_error = close_socket ? 20006 : 20003; ret = dbsqlexec(dbproc); alarm(0); if (ret != FAIL) { fprintf(stderr, "Failed. Expected FAIL to be returned.\n"); return 1; } dbsetuserdata(dbproc, NULL); if (!close_socket) close(end_socket); dbexit(); printf("dblib okay on %s\n", __FILE__); return 0; }
int mcl_shutdown_rw(MCL_SOCKET sockfd) { return shutdown_socket(sockfd, SHUT_RDWR); }
int mcl_shutdown_wr(MCL_SOCKET sockfd) { return shutdown_socket(sockfd, SHUT_WR); }