int Login_Client_Messager::connect_login(int cid, int msg_id, Block_Buffer &buf){ MSG_100001 msg; msg.deserialize(buf); Login_Player *player = dynamic_cast<Login_Player*>(LOGIN_MANAGER->find_account_player(msg.account)); if (!player) { int status = LOGIN_MANAGER->client_login(msg.account, msg.password); if (status < 0) { LOG_ERROR("connect login fail, account:%s, password:%d, status:%d", msg.account.c_str(), msg.password.c_str(), status); LOGIN_MANAGER->close_client(0, cid, ERROR_LOGIN_FAIL); return -1; } //success MSG_500001 res_msg; res_msg.reset(); LOGIN_MANAGER->get_gate_ip(msg.account, res_msg.gate_ip, res_msg.gate_port); make_session(msg.account, res_msg.session); player = LOGIN_MANAGER->pop_player(); if (!player) { LOG_ERROR("login_player_pool_ pop error"); return -1; } player->reset(); player->set_player_cid(cid); player->set_account(msg.account); Session_Info session_info; session_info.session = res_msg.session; session_info.session_tick = Time_Value::gettimeofday(); session_info.gate_ip = res_msg.gate_ip; session_info.gate_port = res_msg.gate_port; player->set_session_info(session_info); LOGIN_MANAGER->bind_cid_player(cid, player); LOGIN_MANAGER->bind_account_player(msg.account, player); Block_Buffer res_buf; res_buf.make_server_message(RES_CONNECT_LOGIN, 0); res_msg.serialize(res_buf); res_buf.finish_message(); LOGIN_MANAGER->send_to_client(cid, res_buf); LOG_INFO("connect login, account:%s, gate_ip:%s, gate_port:%d, session:%s", msg.account.c_str(), res_msg.gate_ip.c_str(), res_msg.gate_port, res_msg.session.c_str()); } else {//fail LOG_ERROR("connect login, player have login, account:%s, password:%d", msg.account.c_str(), msg.password.c_str()); LOGIN_MANAGER->close_client(0, cid, ERROR_LOGIN_FAIL); } return 0; }
/* could implement failure on self-referential redirects, but * realistically, the application must implement a max-redirs count * check, so it's kind of redundant. Mozilla takes this approach. */ static int fail_loop(void) { ne_session *sess; CALL(make_session(&sess, serve_redir, "http://localhost:7777/foo/bar")); ne_redirect_register(sess); ONN("followed looping redirect", any_request(sess, "/foo/bar") != NE_ERROR); ne_session_destroy(sess); return OK; }
static int test_acl(const char *uri, ne_acl_entry *es, int nume) { ne_session *sess; CALL(make_session(&sess, single_serve_string, "HTTP/1.1 200 OK\r\n" "Connection: close\r\n\r\n")); ON(ne_acl_set(sess, uri, es, nume)); CALL(await_server()); ne_session_destroy(sess); return OK; }
void server::handle_accept(session::pointer new_session, const boost::system::error_code& error) { if (!error) { // Check whether the server was stopped by a signal before this // completion handler had a chance to run. if (!acceptor_.is_open()) { return; } new_session->start(); new_session = make_session(); auto accept_callback = boost::bind(&server::handle_accept, this, new_session, boost::asio::placeholders::error); acceptor_.async_accept(new_session->get_socket(), accept_callback); } }
server::server(boost::asio::io_service& io_service, const std::string& address, const std::string& port) : io_service_(io_service), signals_(io_service), acceptor_(io_service), session_manager_(rng_), credentials_(rng_) { // Register to handle the signals that indicate when the server should exit. // It is safe to register for the same signal multiple times in a program, // provided all registration for the specified signal is made through Asio. signals_.add(SIGINT); signals_.add(SIGTERM); #if defined(SIGQUIT) signals_.add(SIGQUIT); #endif signals_.async_wait( [this](boost::system::error_code /*ec*/, int /*signo*/) { // The server is stopped by cancelling all outstanding asynchronous // operations. Once all operations have finished the io_service::run() // call will exit. acceptor_.close(); }); // Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR). boost::asio::ip::tcp::resolver resolver(io_service_); boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve({address, port}); acceptor_.open(endpoint.protocol()); acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true)); acceptor_.bind(endpoint); acceptor_.listen(); session::pointer new_session = make_session(); acceptor_.async_accept(new_session->get_socket(), boost::bind(&server::handle_accept, this, new_session, boost::asio::placeholders::error)); }
static int check_redir(struct redir_args *args, const char *expect) { ne_session *sess; const ne_uri *loc; char *unp; CALL(make_session(&sess, serve_redir, args)); ne_redirect_register(sess); CALL(process_redir(sess, args->path, &loc)); ONN("redirect location was NULL", loc == NULL); unp = ne_uri_unparse(loc); ONV(strcmp(unp, expect), ("redirected to `%s' not `%s'", unp, expect)); ne_free(unp); ne_session_destroy(sess); CALL(await_server()); return OK; }
/* sends a small segment of the file from a high offset. */ static int send_high_offset(void) { int ret, fd = open64(SPARSE, O_RDONLY); ne_session *sess; ne_request *req; ONN("could not open sparse file", fd < 0); CALL(make_session(&sess, serve_check_body, NULL)); req = ne_request_create(sess, "PUT", "/sparse"); ne_set_request_body_fd64(req, fd, point, strlen(data)); ret = ne_request_dispatch(req); CALL(await_server()); ONV(ret != NE_OK || ne_get_status(req)->klass != 2, ("request failed: %s", ne_get_error(sess))); ne_request_destroy(req); ne_session_destroy(sess); close(fd); return OK; }
static int read_large_response(void) { ne_session *sess; ne_request *req; off64_t count = 0; int ret; char buf[8192]; CALL(make_session(&sess, serve_large_response, NULL)); req = ne_request_create(sess, "GET", "/foo"); #ifdef NE_DEBUGGING ne_debug_init(ne_debug_stream, ne_debug_mask & ~(NE_DBG_HTTPBODY|NE_DBG_HTTP)); #endif ret = ne_begin_request(req); if (ret == NE_OK) { while ((ret = ne_read_response_block(req, buf, sizeof buf)) > 0) count += ret; if (ret == NE_OK) ret = ne_end_request(req); } #ifdef NE_DEBUGGING ne_debug_init(ne_debug_stream, ne_debug_mask & (NE_DBG_HTTPBODY|NE_DBG_HTTP)); #endif ONV(ret, ("request failed: %s", ne_get_error(sess))); ONV(count != RESPSIZE, ("response body was %" NE_FMT_OFF64_T " not %" NE_FMT_OFF64_T, count, RESPSIZE)); ne_request_destroy(req); CALL(any_2xx_request(sess, "/bar")); CALL(await_server()); ne_session_destroy(sess); return OK; }
/** * Main interpreter loop. Runs the steps of the test. * * @param cls NULL * @param tc unused */ static void interpreter (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { struct Command *cmd; interpreter_task = NULL; while (1) { cmd = &test_commands[off]; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "#%u: %d %s\n", off, (int) cmd->code, (NULL != cmd->label) ? cmd->label : ""); switch (cmd->code) { case CMD_END_PASS: ret = 0; GNUNET_SCHEDULER_shutdown (); return; case CMD_ADD_ADDRESS: { struct GNUNET_HELLO_Address *addr; struct GNUNET_ATS_Session *session; addr = make_address (cmd->details.add_address.pid, cmd->details.add_address.addr_num, cmd->details.add_address.addr_flags); session = make_session (cmd->details.add_address.session); if (cmd->details.add_address.expect_fail) GNUNET_log_skip (1, GNUNET_NO); cmd->details.add_address.ar = GNUNET_ATS_address_add (sched_ats, addr, session, &cmd->details.add_address.properties); GNUNET_free (addr); if (cmd->details.add_address.expect_fail) { GNUNET_log_skip (0, GNUNET_YES); } else if (NULL == cmd->details.add_address.ar) { GNUNET_break (0); GNUNET_SCHEDULER_shutdown (); return; } off++; break; } case CMD_DEL_ADDRESS: { struct Command *add; add = find_command (CMD_ADD_ADDRESS, cmd->details.del_address.add_label); GNUNET_assert (NULL != add->details.add_address.ar); GNUNET_ATS_address_destroy (add->details.add_address.ar); add->details.add_address.ar = NULL; off++; break; } case CMD_AWAIT_ADDRESS_SUGGESTION: { struct GNUNET_PeerIdentity pid; struct GNUNET_HELLO_Address *addr; struct Command *add; struct AddressSuggestData *asd; int done; make_peer (cmd->details.await_address_suggestion.pid, &pid); asd = find_address_suggestion (&pid); if (NULL == asd) return; if (GNUNET_NO == asd->active) return; /* last suggestion was to disconnect, wait longer */ done = GNUNET_YES; if (NULL != cmd->details.await_address_suggestion.add_label) { done = GNUNET_NO; add = find_command (CMD_ADD_ADDRESS, cmd->details.await_address_suggestion.add_label); addr = make_address (add->details.add_address.pid, add->details.add_address.addr_num, add->details.add_address.addr_flags); if ( (asd->session == make_session (add->details.add_address.session)) && (0 == GNUNET_HELLO_address_cmp (addr, asd->address)) ) done = GNUNET_YES; GNUNET_free (addr); } if (GNUNET_NO == done) return; off++; break; } case CMD_AWAIT_DISCONNECT_SUGGESTION: { struct GNUNET_PeerIdentity pid; struct AddressSuggestData *asd; make_peer (cmd->details.await_disconnect_suggestion.pid, &pid); asd = find_address_suggestion (&pid); if (NULL == asd) return; /* odd, no suggestion at all yet!? */ if (GNUNET_YES == asd->active) return; /* last suggestion was to activate, wait longer */ /* last suggestion was to deactivate, condition satisfied! */ off++; break; } case CMD_REQUEST_CONNECTION_START: { struct GNUNET_PeerIdentity pid; make_peer (cmd->details.request_connection_start.pid, &pid); cmd->details.request_connection_start.csh = GNUNET_ATS_connectivity_suggest (con_ats, &pid, 1); off++; break; } case CMD_REQUEST_CONNECTION_STOP: { struct Command *start; start = find_command (CMD_REQUEST_CONNECTION_START, cmd->details.request_connection_stop.connect_label); GNUNET_ATS_connectivity_suggest_cancel (start->details.request_connection_start.csh); start->details.request_connection_start.csh = NULL; off++; break; } case CMD_AWAIT_ADDRESS_INFORMATION: { struct AddressInformationData *aid; struct Command *add; struct Command *update; struct GNUNET_HELLO_Address *addr; const struct GNUNET_ATS_Properties *cmp; add = find_command (CMD_ADD_ADDRESS, cmd->details.await_address_information.add_label); update = find_command (CMD_UPDATE_ADDRESS, cmd->details.await_address_information.update_label); addr = make_address (add->details.add_address.pid, add->details.add_address.addr_num, add->details.add_address.addr_flags); aid = find_address_information (addr); GNUNET_free (addr); if (NULL == update) cmp = &add->details.add_address.properties; else cmp = &update->details.update_address.properties; if ( (NULL != aid) && (cmp->delay.rel_value_us == aid->properties.delay.rel_value_us) && (cmp->utilization_out == aid->properties.utilization_out) && (cmp->utilization_in == aid->properties.utilization_in) && (cmp->distance == aid->properties.distance) && (cmp->scope == aid->properties.scope) ) { off++; break; } return; } case CMD_UPDATE_ADDRESS: { struct Command *add; add = find_command (CMD_ADD_ADDRESS, cmd->details.update_address.add_label); GNUNET_assert (NULL != add->details.add_address.ar); GNUNET_ATS_address_update (add->details.add_address.ar, &cmd->details.update_address.properties); off++; break; } case CMD_ADD_SESSION: { struct Command *add; struct GNUNET_ATS_Session *session; add = find_command (CMD_ADD_ADDRESS, cmd->details.add_session.add_label); session = make_session (cmd->details.add_session.session); GNUNET_assert (NULL != add->details.add_address.ar); GNUNET_ATS_address_add_session (add->details.add_address.ar, session); off++; break; } case CMD_DEL_SESSION: { struct Command *add_address; struct Command *add_session; struct GNUNET_ATS_Session *session; add_session = find_command (CMD_ADD_SESSION, cmd->details.del_session.add_session_label); add_address = find_command (CMD_ADD_ADDRESS, add_session->details.add_session.add_label); GNUNET_assert (NULL != add_address->details.add_address.ar); session = make_session (add_session->details.add_session.session); GNUNET_ATS_address_del_session (add_address->details.add_address.ar, session); off++; break; } case CMD_CHANGE_PREFERENCE: { struct GNUNET_PeerIdentity pid; make_peer (cmd->details.change_preference.pid, &pid); GNUNET_ATS_performance_change_preference (perf_ats, &pid, GNUNET_ATS_PREFERENCE_END); off++; break; } case CMD_PROVIDE_FEEDBACK: { struct GNUNET_PeerIdentity pid; make_peer (cmd->details.provide_feedback.pid, &pid); GNUNET_ATS_performance_give_feedback (perf_ats, &pid, cmd->details.provide_feedback.scope, GNUNET_ATS_PREFERENCE_END); off++; break; } case CMD_LIST_ADDRESSES: { struct GNUNET_PeerIdentity pid; make_peer (cmd->details.list_addresses.pid, &pid); cmd->details.list_addresses.alh = GNUNET_ATS_performance_list_addresses (perf_ats, &pid, cmd->details.list_addresses.all, &info_cb, cmd); return; } case CMD_RESERVE_BANDWIDTH: { struct GNUNET_PeerIdentity pid; make_peer (cmd->details.reserve_bandwidth.pid, &pid); cmd->details.reserve_bandwidth.rc = GNUNET_ATS_reserve_bandwidth (perf_ats, &pid, cmd->details.reserve_bandwidth.amount, &reservation_cb, cmd); return; } case CMD_SLEEP: off++; interpreter_task = GNUNET_SCHEDULER_add_delayed (cmd->details.sleep.delay, &interpreter, NULL); return; } /* end switch */ } /* end while(1) */ }