int main(void) { int clientfd; setup_signals(); printf("*** Creating a server...\n"); if ((serverfd = new_server("server1.example.com")) < 0) { perror("Couldn't create server"); exit(EXIT_FAILURE); } printf("*** Created!\n" " This is an echo server.\n" " Anything received will be echoed back to the client.\n\n"); /* Accept a client */ if ((clientfd = accept_client(serverfd)) < 0) { perror("Couldn't accept client"); destroy_server(serverfd); exit(EXIT_FAILURE); } printf("*** Accepted incoming connection request.\n"); static char buf[512+1]; int n; while ((n = recv_info(clientfd, buf, sizeof(buf)-1)) > 0) { buf[n] = '\0'; if (!strcmp(buf, "__QUIT__NOW__\n")) { printf("*** Got disconnect request from client, leaving...\n"); disconnect_from(clientfd); break; } printf("*** Received: %s", buf); if (send_info(clientfd, buf, n) < 0) { perror("Error sending message to client"); } else { printf("*** Sent: %s", buf); } } printf("*** Goodbye\n"); destroy_server(serverfd); return 0; }
// main loop int main( int argc, char **argv ) { printf("Hello world\n"); Management manager = new_manager(); // add actions to the manager action_add_to_manager( manager ); // add rooms to manager room_add_to_manager( manager ); Server server = new_server(); if( start_server( server, 5555 ) < 0 ){ return 1; } environment_loop( server, manager ); /* --------------------------------------- * Clean up */ close_server( server ); destroy_server( server ); destroy_manager( manager ); return 0; }
int main(void) { SetConsoleTitle(TITLE); mtx_init(&gmutex, mtx_plain); cnd_init(&gcond); init_timer(); init_path(); init_strings(); init_server(); initpackets(); //test_map(1); thrd_create(&t1, initsocket, (void*)0); thrd_create(&t2, commands, (void*)0); server_loop(); destroy_server(); mtx_destroy(&gmutex); cnd_destroy(&gcond); #if _DEBUG VLDReportLeaks(); #endif exit(TRUE);// Exit program }
int main(int argc, char *argv[]) { signal(SIGINT, on_sigint); if (init_server(&srv) == 0) serve(&srv); destroy_server(&srv); return 0; }
/* Destroys server instances. */ static void grpc_rb_server_free(void* p) { grpc_rb_server* svr = NULL; gpr_timespec deadline; if (p == NULL) { return; }; svr = (grpc_rb_server*)p; deadline = gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_seconds(2, GPR_TIMESPAN)); destroy_server(svr, deadline); xfree(p); }
int main() { sys::CLogger* logger = new sys::CLogger; logger->create("../log", "general.log"); sys::g_logger = logger; mooon::CGeneralConfig* config = new mooon::CGeneralConfig; mooon::CGeneralFactory* factory = new mooon::CGeneralFactory; mooon::IServer* server = create_server(logger, config, factory); server->start(); sys::CSysUtil::millisleep(1000000); server->stop(); destroy_server(server); return 0; }
//defining s Constructor for every RHINO node that runs the ClusterServer Application int main(int argc, char *argv[]) { //defining variables int pid; int portno; char buffer[256]; RhinoT rhinoDevice, *rhinoDevicePtr; RegisterT *rhinoRegistersPtr, rhinoRegisters; server_stateT *st; /* Is the structure containing an internet address and is defined in netinet/in.h Will contain the address of the client and serv_addr will contain address of the server */ printf("Entering main\n"); rhinoDevicePtr = &rhinoDevice; rhinoRegistersPtr = &rhinoRegisters; //if no arguments have been passed in on the console if (argc < 2) { error("Error, no port provided\n"); } st = create_server(); if(st == NULL){ error("unable to allocate local server state"); } portno = atoi(argv[1]); //getting the port number from the console //MAYBE INITIALIZE THE RHINO HERE BEFORE USED initializeMethod(rhinoDevicePtr, rhinoRegistersPtr); if(connect_sockets(portno, rhinoDevicePtr,rhinoRegistersPtr, st) < 0) { error("Error connecting socket\n"); } destroy_server(st); return 0; //this terminates the whole main program but we never get here }
int main () { int state = zlog_init("../zlog.conf"); category = zlog_get_category("default"); zlog_info (category, "Server starts!"); init_server (&server); server.run (&server); while (1) { char c = getchar (); if (c == 'q') break; } destroy_server (&server); zlog_fini (); }
/* call-seq: server = Server.new({'arg1': 'value1'}) ... // do stuff with server ... ... // to shutdown the server server.destroy() ... // to shutdown the server with a timeout server.destroy(timeout) Destroys server instances. */ static VALUE grpc_rb_server_destroy(int argc, VALUE *argv, VALUE self) { VALUE timeout = Qnil; gpr_timespec deadline; grpc_rb_server *s = NULL; /* "01" == 0 mandatory args, 1 (timeout) is optional */ rb_scan_args(argc, argv, "01", &timeout); TypedData_Get_Struct(self, grpc_rb_server, &grpc_rb_server_data_type, s); if (TYPE(timeout) == T_NIL) { deadline = gpr_inf_future(GPR_CLOCK_REALTIME); } else { deadline = grpc_rb_time_timeval(timeout, /* absolute time*/ 0); } destroy_server(s, deadline); return Qnil; }
static void destroy_client_server(void) { if (client->thread) { client->quit = 1; pj_thread_join(client->thread); pj_thread_destroy(client->thread); } if (client->sess) pj_stun_session_destroy(client->sess); if (client->sock) pj_sock_close(client->sock); if (client->test_complete) pj_sem_destroy(client->test_complete); if (server) destroy_server(); }
/* Instantiate standard server */ static int create_std_server(pj_stun_auth_type auth_type, pj_bool_t responding) { pj_pool_t *pool; pj_stun_session_cb sess_cb; pj_stun_auth_cred cred; pj_status_t status; /* Create server */ pool = pj_pool_create(mem, "server", 1000, 1000, NULL); server = PJ_POOL_ZALLOC_T(pool, struct server); server->pool = pool; server->auth_type = auth_type; server->responding = responding; /* Create STUN session */ pj_bzero(&sess_cb, sizeof(sess_cb)); sess_cb.on_rx_request = &server_on_rx_request; sess_cb.on_send_msg = &server_send_msg; status = pj_stun_session_create(&stun_cfg, "server", &sess_cb, PJ_FALSE, NULL, &server->sess); if (status != PJ_SUCCESS) { destroy_server(); return -10; } /* Configure credential */ pj_bzero(&cred, sizeof(cred)); cred.type = PJ_STUN_AUTH_CRED_DYNAMIC; cred.data.dyn_cred.get_auth = &server_get_auth; cred.data.dyn_cred.get_password = &server_get_password; cred.data.dyn_cred.verify_nonce = &server_verify_nonce; status = pj_stun_session_set_credential(server->sess, auth_type, &cred); if (status != PJ_SUCCESS) { destroy_server(); return -20; } /* Create socket */ status = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &server->sock); if (status != PJ_SUCCESS) { destroy_server(); return -30; } /* Bind */ pj_sockaddr_in_init(&server->addr.ipv4, NULL, 0); status = pj_sock_bind(server->sock, &server->addr, pj_sockaddr_get_len(&server->addr)); if (status != PJ_SUCCESS) { destroy_server(); return -40; } else { /* Get the bound IP address */ int namelen = sizeof(server->addr); pj_sockaddr addr; status = pj_sock_getsockname(server->sock, &server->addr, &namelen); if (status != PJ_SUCCESS) { destroy_server(); return -43; } status = pj_gethostip(pj_AF_INET(), &addr); if (status != PJ_SUCCESS) { destroy_server(); return -45; } pj_sockaddr_copy_addr(&server->addr, &addr); } /* Create worker thread */ status = pj_thread_create(pool, "server", &server_thread, 0, 0, 0, &server->thread); if (status != PJ_SUCCESS) { destroy_server(); return -30; } return 0; }
/* * Keep-alive test. */ static int keep_alive_test(pj_stun_config *cfg) { struct stun_srv *srv; struct stun_client *client; pj_sockaddr_in mapped_addr; pj_stun_sock_info info; pj_str_t srv_addr; pj_time_val timeout, t; int i, ret = 0; pj_status_t status; PJ_LOG(3,(THIS_FILE, " normal operation")); status = create_client(cfg, &client, PJ_TRUE); if (status != PJ_SUCCESS) return -310; status = create_server(client->pool, cfg->ioqueue, RESPOND_STUN|WITH_XOR_MAPPED, &srv); if (status != PJ_SUCCESS) { destroy_client(client); return -320; } /* * Part 1: initial Binding resolution. */ PJ_LOG(3,(THIS_FILE, " initial Binding request")); srv_addr = pj_str("127.0.0.1"); status = pj_stun_sock_start(client->sock, &srv_addr, pj_ntohs(srv->addr.ipv4.sin_port), NULL); if (status != PJ_SUCCESS) { destroy_server(srv); destroy_client(client); return -330; } /* Wait until on_status() callback is called with success status */ pj_gettimeofday(&timeout); timeout.sec += 60; do { handle_events(cfg, 100); pj_gettimeofday(&t); } while (client->on_status_cnt==0 && PJ_TIME_VAL_LT(t, timeout)); /* Check that callback with correct operation is called */ if (client->last_op != PJ_STUN_SOCK_BINDING_OP) { PJ_LOG(3,(THIS_FILE, " error: expecting Binding operation status")); ret = -340; goto on_return; } if (client->last_status != PJ_SUCCESS) { PJ_LOG(3,(THIS_FILE, " error: expecting PJ_SUCCESS status")); ret = -350; goto on_return; } /* Check that client doesn't receive anything */ if (client->on_rx_data_cnt != 0) { PJ_LOG(3,(THIS_FILE, " error: client shouldn't have received anything")); ret = -370; goto on_return; } /* Get info */ pj_bzero(&info, sizeof(info)); pj_stun_sock_get_info(client->sock, &info); /* Check that we have server address */ if (!pj_sockaddr_has_addr(&info.srv_addr)) { PJ_LOG(3,(THIS_FILE, " error: missing server address")); ret = -380; goto on_return; } /* .. and bound address port must not be zero */ if (pj_sockaddr_get_port(&info.bound_addr)==0) { PJ_LOG(3,(THIS_FILE, " error: bound address is zero")); ret = -381; goto on_return; } /* .. and mapped address */ if (!pj_sockaddr_has_addr(&info.mapped_addr)) { PJ_LOG(3,(THIS_FILE, " error: missing mapped address")); ret = -382; goto on_return; } /* verify the mapped address */ pj_sockaddr_in_init(&mapped_addr, &srv->ip_to_send, srv->port_to_send); if (pj_sockaddr_cmp(&info.mapped_addr, &mapped_addr) != 0) { PJ_LOG(3,(THIS_FILE, " error: mapped address mismatched")); ret = -383; goto on_return; } /* .. and at least one alias */ if (info.alias_cnt == 0) { PJ_LOG(3,(THIS_FILE, " error: must have at least one alias")); ret = -384; goto on_return; } if (!pj_sockaddr_has_addr(&info.aliases[0])) { PJ_LOG(3,(THIS_FILE, " error: missing alias")); ret = -386; goto on_return; } /* * Part 2: sending and receiving data */ PJ_LOG(3,(THIS_FILE, " sending/receiving data")); /* Change server operation mode to echo back data */ srv->flag = ECHO; /* Reset server */ srv->rx_cnt = 0; /* Client sending data to echo server */ { char txt[100]; PJ_LOG(3,(THIS_FILE, " sending to %s", pj_sockaddr_print(&info.srv_addr, txt, sizeof(txt), 3))); } status = pj_stun_sock_sendto(client->sock, NULL, &ret, sizeof(ret), 0, &info.srv_addr, pj_sockaddr_get_len(&info.srv_addr)); if (status != PJ_SUCCESS && status != PJ_EPENDING) { app_perror(" error: server sending data", status); ret = -390; goto on_return; } /* Wait for a short period until client receives data. We can't wait for * too long otherwise the keep-alive will kick in. */ pj_gettimeofday(&timeout); timeout.sec += 1; do { handle_events(cfg, 100); pj_gettimeofday(&t); } while (client->on_rx_data_cnt==0 && PJ_TIME_VAL_LT(t, timeout)); /* Check that data is received in server */ if (srv->rx_cnt == 0) { PJ_LOG(3,(THIS_FILE, " error: server didn't receive data")); ret = -395; goto on_return; } /* Check that status is still OK */ if (client->last_status != PJ_SUCCESS) { app_perror(" error: client has failed", client->last_status); ret = -400; goto on_return; } /* Check that data has been received */ if (client->on_rx_data_cnt == 0) { PJ_LOG(3,(THIS_FILE, " error: client doesn't receive data")); ret = -410; goto on_return; } /* * Part 3: Successful keep-alive, */ PJ_LOG(3,(THIS_FILE, " successful keep-alive scenario")); /* Change server operation mode to normal mode */ srv->flag = RESPOND_STUN | WITH_XOR_MAPPED; /* Reset server */ srv->rx_cnt = 0; /* Reset client */ client->on_status_cnt = 0; client->last_status = PJ_SUCCESS; client->on_rx_data_cnt = 0; /* Wait for keep-alive duration to see if client actually sends the * keep-alive. */ pj_gettimeofday(&timeout); timeout.sec += (PJ_STUN_KEEP_ALIVE_SEC + 1); do { handle_events(cfg, 100); pj_gettimeofday(&t); } while (PJ_TIME_VAL_LT(t, timeout)); /* Check that server receives some packets */ if (srv->rx_cnt == 0) { PJ_LOG(3, (THIS_FILE, " error: no keep-alive was received")); ret = -420; goto on_return; } /* Check that client status is still okay and on_status() callback is NOT * called */ /* No longer valid due to this ticket: * http://trac.pjsip.org/repos/ticket/742 if (client->on_status_cnt != 0) { PJ_LOG(3, (THIS_FILE, " error: on_status() must not be called on successful" "keep-alive when mapped-address does not change")); ret = -430; goto on_return; } */ /* Check that client doesn't receive anything */ if (client->on_rx_data_cnt != 0) { PJ_LOG(3,(THIS_FILE, " error: client shouldn't have received anything")); ret = -440; goto on_return; } /* * Part 4: Successful keep-alive with IP address change */ PJ_LOG(3,(THIS_FILE, " mapped IP address change")); /* Change server operation mode to normal mode */ srv->flag = RESPOND_STUN | WITH_XOR_MAPPED; /* Change mapped address in the response */ srv->ip_to_send = pj_str("2.2.2.2"); srv->port_to_send++; /* Reset server */ srv->rx_cnt = 0; /* Reset client */ client->on_status_cnt = 0; client->last_status = PJ_SUCCESS; client->on_rx_data_cnt = 0; /* Wait for keep-alive duration to see if client actually sends the * keep-alive. */ pj_gettimeofday(&timeout); timeout.sec += (PJ_STUN_KEEP_ALIVE_SEC + 1); do { handle_events(cfg, 100); pj_gettimeofday(&t); } while (PJ_TIME_VAL_LT(t, timeout)); /* Check that server receives some packets */ if (srv->rx_cnt == 0) { PJ_LOG(3, (THIS_FILE, " error: no keep-alive was received")); ret = -450; goto on_return; } /* Check that on_status() callback is called (because mapped address * has changed) */ if (client->on_status_cnt != 1) { PJ_LOG(3, (THIS_FILE, " error: on_status() was not called")); ret = -460; goto on_return; } /* Check that callback was called with correct operation */ if (client->last_op != PJ_STUN_SOCK_MAPPED_ADDR_CHANGE) { PJ_LOG(3,(THIS_FILE, " error: expecting keep-alive operation status")); ret = -470; goto on_return; } /* Check that last status is still success */ if (client->last_status != PJ_SUCCESS) { PJ_LOG(3, (THIS_FILE, " error: expecting successful status")); ret = -480; goto on_return; } /* Check that client doesn't receive anything */ if (client->on_rx_data_cnt != 0) { PJ_LOG(3,(THIS_FILE, " error: client shouldn't have received anything")); ret = -490; goto on_return; } /* Get info */ pj_bzero(&info, sizeof(info)); pj_stun_sock_get_info(client->sock, &info); /* Check that we have server address */ if (!pj_sockaddr_has_addr(&info.srv_addr)) { PJ_LOG(3,(THIS_FILE, " error: missing server address")); ret = -500; goto on_return; } /* .. and mapped address */ if (!pj_sockaddr_has_addr(&info.mapped_addr)) { PJ_LOG(3,(THIS_FILE, " error: missing mapped address")); ret = -510; goto on_return; } /* verify the mapped address */ pj_sockaddr_in_init(&mapped_addr, &srv->ip_to_send, srv->port_to_send); if (pj_sockaddr_cmp(&info.mapped_addr, &mapped_addr) != 0) { PJ_LOG(3,(THIS_FILE, " error: mapped address mismatched")); ret = -520; goto on_return; } /* .. and at least one alias */ if (info.alias_cnt == 0) { PJ_LOG(3,(THIS_FILE, " error: must have at least one alias")); ret = -530; goto on_return; } if (!pj_sockaddr_has_addr(&info.aliases[0])) { PJ_LOG(3,(THIS_FILE, " error: missing alias")); ret = -540; goto on_return; } /* * Part 5: Failed keep-alive */ PJ_LOG(3,(THIS_FILE, " failed keep-alive scenario")); /* Change server operation mode to respond without attribute */ srv->flag = RESPOND_STUN; /* Reset server */ srv->rx_cnt = 0; /* Reset client */ client->on_status_cnt = 0; client->last_status = PJ_SUCCESS; client->on_rx_data_cnt = 0; /* Wait until on_status() is called with failure. */ pj_gettimeofday(&timeout); timeout.sec += (PJ_STUN_KEEP_ALIVE_SEC + PJ_STUN_TIMEOUT_VALUE + 5); do { handle_events(cfg, 100); pj_gettimeofday(&t); } while (client->on_status_cnt==0 && PJ_TIME_VAL_LT(t, timeout)); /* Check that callback with correct operation is called */ if (client->last_op != PJ_STUN_SOCK_KEEP_ALIVE_OP) { PJ_LOG(3,(THIS_FILE, " error: expecting keep-alive operation status")); ret = -600; goto on_return; } if (client->last_status == PJ_SUCCESS) { PJ_LOG(3,(THIS_FILE, " error: expecting failed keep-alive")); ret = -610; goto on_return; } /* Check that client doesn't receive anything */ if (client->on_rx_data_cnt != 0) { PJ_LOG(3,(THIS_FILE, " error: client shouldn't have received anything")); ret = -620; goto on_return; } on_return: destroy_server(srv); destroy_client(client); for (i=0; i<7; ++i) handle_events(cfg, 50); return ret; }
/* * Invalid response scenario: when server returns no MAPPED-ADDRESS or * XOR-MAPPED-ADDRESS attribute. */ static int missing_attr_test(pj_stun_config *cfg, pj_bool_t destroy_on_err) { struct stun_srv *srv; struct stun_client *client; pj_str_t srv_addr; pj_time_val timeout, t; int i, ret = 0; pj_status_t status; PJ_LOG(3,(THIS_FILE, " missing attribute test [%d]", destroy_on_err)); status = create_client(cfg, &client, destroy_on_err); if (status != PJ_SUCCESS) return -110; status = create_server(client->pool, cfg->ioqueue, RESPOND_STUN, &srv); if (status != PJ_SUCCESS) { destroy_client(client); return -120; } srv_addr = pj_str("127.0.0.1"); status = pj_stun_sock_start(client->sock, &srv_addr, pj_ntohs(srv->addr.ipv4.sin_port), NULL); if (status != PJ_SUCCESS) { destroy_server(srv); destroy_client(client); return -130; } /* Wait until on_status() callback is called with the failure */ pj_gettimeofday(&timeout); timeout.sec += 60; do { handle_events(cfg, 100); pj_gettimeofday(&t); } while (client->on_status_cnt==0 && PJ_TIME_VAL_LT(t, timeout)); /* Check that callback with correct operation is called */ if (client->last_op != PJ_STUN_SOCK_BINDING_OP) { PJ_LOG(3,(THIS_FILE, " error: expecting Binding operation status")); ret = -140; goto on_return; } if (client->last_status != PJNATH_ESTUNNOMAPPEDADDR) { PJ_LOG(3,(THIS_FILE, " error: expecting PJNATH_ESTUNNOMAPPEDADDR")); ret = -150; goto on_return; } /* Check that client doesn't receive anything */ if (client->on_rx_data_cnt != 0) { PJ_LOG(3,(THIS_FILE, " error: client shouldn't have received anything")); ret = -170; goto on_return; } on_return: destroy_server(srv); destroy_client(client); for (i=0; i<7; ++i) handle_events(cfg, 50); return ret; }
/* * Timeout test: scenario when no response is received from server */ static int timeout_test(pj_stun_config *cfg, pj_bool_t destroy_on_err) { struct stun_srv *srv; struct stun_client *client; pj_str_t srv_addr; pj_time_val timeout, t; int ret = 0; pj_status_t status; PJ_LOG(3,(THIS_FILE, " timeout test [%d]", destroy_on_err)); status = create_client(cfg, &client, destroy_on_err); if (status != PJ_SUCCESS) return -10; status = create_server(client->pool, cfg->ioqueue, 0, &srv); if (status != PJ_SUCCESS) { destroy_client(client); return -20; } srv_addr = pj_str("127.0.0.1"); status = pj_stun_sock_start(client->sock, &srv_addr, pj_ntohs(srv->addr.ipv4.sin_port), NULL); if (status != PJ_SUCCESS) { destroy_server(srv); destroy_client(client); return -30; } /* Wait until on_status() callback is called with the failure */ pj_gettimeofday(&timeout); timeout.sec += 60; do { handle_events(cfg, 100); pj_gettimeofday(&t); } while (client->on_status_cnt==0 && PJ_TIME_VAL_LT(t, timeout)); /* Check that callback with correct operation is called */ if (client->last_op != PJ_STUN_SOCK_BINDING_OP) { PJ_LOG(3,(THIS_FILE, " error: expecting Binding operation status")); ret = -40; goto on_return; } /* .. and with the correct status */ if (client->last_status != PJNATH_ESTUNTIMEDOUT) { PJ_LOG(3,(THIS_FILE, " error: expecting PJNATH_ESTUNTIMEDOUT")); ret = -50; goto on_return; } /* Check that server received correct retransmissions */ if (srv->rx_cnt != PJ_STUN_MAX_TRANSMIT_COUNT) { PJ_LOG(3,(THIS_FILE, " error: expecting %d retransmissions, got %d", PJ_STUN_MAX_TRANSMIT_COUNT, srv->rx_cnt)); ret = -60; goto on_return; } /* Check that client doesn't receive anything */ if (client->on_rx_data_cnt != 0) { PJ_LOG(3,(THIS_FILE, " error: client shouldn't have received anything")); ret = -70; goto on_return; } on_return: destroy_server(srv); destroy_client(client); return ret; }
int cliser_server_close(lua_State *L) { server_t *server = (server_t *)lua_touserdata(L, 1); return destroy_server(L, server); }
void shutdown_handler(int signo) { destroy_server(serverfd); exit(0); }