/* * Invoke this in place of libstartup's start_network(). * * XXX Note that this code has not been tested after start_network()'s * internals were changed. This *should* work, but hey that's never * happened in my lifetime... */ void start_network_with_dump(void (*dump_func)(void *cookie, void *p, unsigned len)) { the_dump_func = dump_func; start_network(); }
int run() { timer_srv.reset(new boost::asio::io_service()); the_net.reset(new net_server<koko_socket>(2)); boost::system::error_code ec; the_config.load_from_file(); db_.reset(new utf8_data_base(the_config.accdb_host_, the_config.accdb_user_,the_config.accdb_pwd_, the_config.accdb_name_)); if(!db_->grabdb()){ glb_log.write_log("database start failed!!!!"); return -1; } db_delay_helper_[0].set_db_ptr(db_); if (start_network() != 0){ goto _EXIT; } { boost::shared_ptr<boost::thread> td(new boost::thread(boost::bind(db_thread_func, db_delay_helper_))); cache_persistent_threads.push_back(td); } int idle_count = 0; task_on_5sec* ptask = new task_on_5sec(*timer_srv); task.reset(ptask); ptask->schedule(1000); ptask->routine(); while(!glb_exit) { bool busy = false; timer_srv->reset(); timer_srv->poll(); the_net->ios_.reset(); the_net->ios_.poll(); handle_pending_logins(); pickup_player_msgs(busy); handle_player_msgs(); update_players(); smsg_ptr pmsg; broadcast_msg_.pop_front(pmsg); if(pmsg.get()) broadcast_msg(pmsg); boost::posix_time::milliseconds ms(20); boost::this_thread::sleep(ms); } _EXIT: return 0; }
bool command_executor::execute_command(const hotkey_command& cmd, int /*index*/, bool press) { // hotkey release handling if (!press) { switch(cmd.id) { // release a scroll key, un-apply scrolling in the given direction case HOTKEY_SCROLL_UP: scroll_up(false); break; case HOTKEY_SCROLL_DOWN: scroll_down(false); break; case HOTKEY_SCROLL_LEFT: scroll_left(false); break; case HOTKEY_SCROLL_RIGHT: scroll_right(false); break; default: return false; // nothing else handles a hotkey release } return true; } // hotkey press handling switch(cmd.id) { case HOTKEY_SCROLL_UP: scroll_up(true); break; case HOTKEY_SCROLL_DOWN: scroll_down(true); break; case HOTKEY_SCROLL_LEFT: scroll_left(true); break; case HOTKEY_SCROLL_RIGHT: scroll_right(true); break; case HOTKEY_CYCLE_UNITS: cycle_units(); break; case HOTKEY_CYCLE_BACK_UNITS: cycle_back_units(); break; case HOTKEY_ENDTURN: end_turn(); break; case HOTKEY_UNIT_HOLD_POSITION: unit_hold_position(); break; case HOTKEY_END_UNIT_TURN: end_unit_turn(); break; case HOTKEY_LEADER: goto_leader(); break; case HOTKEY_UNDO: undo(); break; case HOTKEY_REDO: redo(); break; case HOTKEY_TERRAIN_DESCRIPTION: terrain_description(); break; case HOTKEY_UNIT_DESCRIPTION: unit_description(); break; case HOTKEY_RENAME_UNIT: rename_unit(); break; case HOTKEY_SAVE_GAME: save_game(); break; case HOTKEY_SAVE_REPLAY: save_replay(); break; case HOTKEY_SAVE_MAP: save_map(); break; case HOTKEY_LOAD_GAME: load_game(); break; case HOTKEY_TOGGLE_ELLIPSES: toggle_ellipses(); break; case HOTKEY_TOGGLE_GRID: toggle_grid(); break; case HOTKEY_STATUS_TABLE: status_table(); break; case HOTKEY_RECALL: recall(); break; case HOTKEY_LABEL_SETTINGS: label_settings(); break; case HOTKEY_RECRUIT: recruit(); break; case hotkey::HOTKEY_REPEAT_RECRUIT: repeat_recruit(); break; case HOTKEY_SPEAK: speak(); break; case HOTKEY_SPEAK_ALLY: whisper(); break; case HOTKEY_SPEAK_ALL: shout(); break; case HOTKEY_CREATE_UNIT: create_unit(); break; case HOTKEY_CHANGE_SIDE: change_side(); break; case HOTKEY_KILL_UNIT: kill_unit(); break; case HOTKEY_PREFERENCES: preferences(); break; case HOTKEY_OBJECTIVES: objectives(); break; case HOTKEY_UNIT_LIST: unit_list(); break; case HOTKEY_STATISTICS: show_statistics(); break; case HOTKEY_STOP_NETWORK: stop_network(); break; case HOTKEY_START_NETWORK: start_network(); break; case HOTKEY_LABEL_TEAM_TERRAIN: label_terrain(true); break; case HOTKEY_LABEL_TERRAIN: label_terrain(false); break; case HOTKEY_CLEAR_LABELS: clear_labels(); break; case HOTKEY_SHOW_ENEMY_MOVES: show_enemy_moves(false); break; case HOTKEY_BEST_ENEMY_MOVES: show_enemy_moves(true); break; case HOTKEY_DELAY_SHROUD: toggle_shroud_updates(); break; case HOTKEY_UPDATE_SHROUD: update_shroud_now(); break; case HOTKEY_CONTINUE_MOVE: continue_move(); break; case HOTKEY_SEARCH: search(); break; case HOTKEY_HELP: show_help(); break; case HOTKEY_CHAT_LOG: show_chat_log(); break; case HOTKEY_USER_CMD: user_command(); break; case HOTKEY_CUSTOM_CMD: custom_command(); break; case HOTKEY_AI_FORMULA: ai_formula(); break; case HOTKEY_CLEAR_MSG: clear_messages(); break; case HOTKEY_LANGUAGE: change_language(); break; case HOTKEY_REPLAY_PLAY: play_replay(); break; case HOTKEY_REPLAY_RESET: reset_replay(); break; case HOTKEY_REPLAY_STOP: stop_replay(); break; case HOTKEY_REPLAY_NEXT_TURN: replay_next_turn(); break; case HOTKEY_REPLAY_NEXT_SIDE: replay_next_side(); break; case HOTKEY_REPLAY_NEXT_MOVE: replay_next_move(); break; case HOTKEY_REPLAY_SHOW_EVERYTHING: replay_show_everything(); break; case HOTKEY_REPLAY_SHOW_EACH: replay_show_each(); break; case HOTKEY_REPLAY_SHOW_TEAM1: replay_show_team1(); break; case HOTKEY_REPLAY_SKIP_ANIMATION: replay_skip_animation(); break; case HOTKEY_REPLAY_EXIT: replay_exit(); break; case HOTKEY_WB_TOGGLE: whiteboard_toggle(); break; case HOTKEY_WB_EXECUTE_ACTION: whiteboard_execute_action(); break; case HOTKEY_WB_EXECUTE_ALL_ACTIONS: whiteboard_execute_all_actions(); break; case HOTKEY_WB_DELETE_ACTION: whiteboard_delete_action(); break; case HOTKEY_WB_BUMP_UP_ACTION: whiteboard_bump_up_action(); break; case HOTKEY_WB_BUMP_DOWN_ACTION: whiteboard_bump_down_action(); break; case HOTKEY_WB_SUPPOSE_DEAD: whiteboard_suppose_dead(); break; case HOTKEY_SELECT_HEX: select_hex(); break; case HOTKEY_DESELECT_HEX: deselect_hex(); break; case HOTKEY_MOVE_ACTION: move_action(); break; case HOTKEY_SELECT_AND_ACTION: select_and_action(); break; case HOTKEY_ACCELERATED: toggle_accelerated_speed(); break; case LUA_CONSOLE: lua_console(); break; case HOTKEY_ZOOM_IN: zoom_in(); break; case HOTKEY_ZOOM_OUT: zoom_out(); break; case HOTKEY_ZOOM_DEFAULT: zoom_default(); break; case HOTKEY_MAP_SCREENSHOT: map_screenshot(); break; case HOTKEY_QUIT_TO_DESKTOP: quit_confirmation::quit_to_desktop(); break; case HOTKEY_QUIT_GAME: quit_confirmation::quit_to_title(); break; default: return false; } return true; }
TCP::TCP(bool server_mode) : sock(-1), server_mode(server_mode), thread(0), mutex(0) { id = 0; #ifdef WIN32 start_network(); #endif }
/* * the initialization follows the ping_reply example... */ int main(int argc, char **argv) { short port = 3000; int dateserverseconds = 60; #ifndef KNIT #ifdef OSKIT #ifdef OSKIT_UNIX /* * OSKit/unix needs the threaded_fd support in order for the * timeout (alarm) in dateserver to work. */ oskit_clientos_init_pthreads(); start_clock(); start_pthreads(); /* * Build using emulated filesystem/sockets, or real drivers. * The real drivers require a second ethernet interface (you * must setenv ETHERIF and ETHERADDR), and a raw disk * partition (either a real one or a pseudo one created with * the vnconfig tools). */ #ifdef REALSTUFF start_fs_pthreads("/dev/oskitfs", 0); start_network_pthreads(); #else start_fs_native_pthreads("/tmp"); start_network_native_pthreads(); #endif #else oskit_clientos_init(); start_clock(); start_fs_bmod(); start_network(); #endif /* OSKIT_UNIX */ #endif /* OSKIT */ #endif /* !KNIT */ /* * Accept -s command line parameter for setting * dateserverseconds. */ if (argc > 1) { if (!strcmp(argv[1],"-s")) { dateserverseconds = atoi(argv[2]); } } /* * if you want to test pingreply, choose the first piece of code */ #if 0 fingerclient("oskit", "155.99.212.1"); /* fast.cs.utah.edu */ fingerclient("ftp", "155.99.212.1"); /* fast */ #endif printf("responding to date requests for %d seconds\n", dateserverseconds); gethostname(IPADDR, sizeof IPADDR); printf("type 'telnet %s %d' to test\n", IPADDR, port); dateserver(port, dateserverseconds); exit(0); }
bool command_executor::execute_command(HOTKEY_COMMAND command, int /*index*/) { switch(command) { case HOTKEY_CYCLE_UNITS: cycle_units(); break; case HOTKEY_CYCLE_BACK_UNITS: cycle_back_units(); break; case HOTKEY_ENDTURN: end_turn(); break; case HOTKEY_UNIT_HOLD_POSITION: unit_hold_position(); break; case HOTKEY_END_UNIT_TURN: end_unit_turn(); break; case HOTKEY_LEADER: goto_leader(); break; case HOTKEY_UNDO: undo(); break; case HOTKEY_REDO: redo(); break; case HOTKEY_UNIT_DESCRIPTION: unit_description(); break; case HOTKEY_RENAME_UNIT: rename_unit(); break; case HOTKEY_SAVE_GAME: save_game(); break; case HOTKEY_SAVE_REPLAY: save_replay(); break; case HOTKEY_SAVE_MAP: save_map(); break; case HOTKEY_LOAD_GAME: load_game(); break; case HOTKEY_TOGGLE_ELLIPSES: toggle_ellipses(); break; case HOTKEY_TOGGLE_GRID: toggle_grid(); break; case HOTKEY_STATUS_TABLE: status_table(); break; case HOTKEY_RECALL: recall(); break; case HOTKEY_RECRUIT: recruit(); break; case hotkey::HOTKEY_REPEAT_RECRUIT: repeat_recruit(); break; case HOTKEY_SPEAK: speak(); break; case HOTKEY_SPEAK_ALLY: whisper(); break; case HOTKEY_SPEAK_ALL: shout(); break; case HOTKEY_CREATE_UNIT: create_unit(); break; case HOTKEY_CHANGE_SIDE: change_side(); break; case HOTKEY_PREFERENCES: preferences(); break; case HOTKEY_OBJECTIVES: objectives(); break; case HOTKEY_UNIT_LIST: unit_list(); break; case HOTKEY_STATISTICS: show_statistics(); break; case HOTKEY_STOP_NETWORK: stop_network(); break; case HOTKEY_START_NETWORK: start_network(); break; case HOTKEY_LABEL_TEAM_TERRAIN: label_terrain(true); break; case HOTKEY_LABEL_TERRAIN: label_terrain(false); break; case HOTKEY_CLEAR_LABELS: clear_labels(); break; case HOTKEY_SHOW_ENEMY_MOVES: show_enemy_moves(false); break; case HOTKEY_BEST_ENEMY_MOVES: show_enemy_moves(true); break; case HOTKEY_DELAY_SHROUD: toggle_shroud_updates(); break; case HOTKEY_UPDATE_SHROUD: update_shroud_now(); break; case HOTKEY_CONTINUE_MOVE: continue_move(); break; case HOTKEY_SEARCH: search(); break; case HOTKEY_HELP: show_help(); break; case HOTKEY_CHAT_LOG: show_chat_log(); break; case HOTKEY_USER_CMD: user_command(); break; case HOTKEY_CUSTOM_CMD: custom_command(); break; case HOTKEY_AI_FORMULA: ai_formula(); break; case HOTKEY_CLEAR_MSG: clear_messages(); break; case HOTKEY_LANGUAGE: change_language(); break; case HOTKEY_PLAY_REPLAY: play_replay(); break; case HOTKEY_RESET_REPLAY: reset_replay(); break; case HOTKEY_STOP_REPLAY: stop_replay(); break; case HOTKEY_REPLAY_NEXT_TURN: replay_next_turn(); break; case HOTKEY_REPLAY_NEXT_SIDE: replay_next_side(); break; case HOTKEY_REPLAY_SHOW_EVERYTHING: replay_show_everything(); break; case HOTKEY_REPLAY_SHOW_EACH: replay_show_each(); break; case HOTKEY_REPLAY_SHOW_TEAM1: replay_show_team1(); break; case HOTKEY_REPLAY_SKIP_ANIMATION: replay_skip_animation(); break; case HOTKEY_WB_TOGGLE: whiteboard_toggle(); break; case HOTKEY_WB_EXECUTE_ACTION: whiteboard_execute_action(); break; case HOTKEY_WB_EXECUTE_ALL_ACTIONS: whiteboard_execute_all_actions(); break; case HOTKEY_WB_DELETE_ACTION: whiteboard_delete_action(); break; case HOTKEY_WB_BUMP_UP_ACTION: whiteboard_bump_up_action(); break; case HOTKEY_WB_BUMP_DOWN_ACTION: whiteboard_bump_down_action(); break; case HOTKEY_WB_SUPPOSE_DEAD: whiteboard_suppose_dead(); break; case HOTKEY_LEFT_MOUSE_CLICK: left_mouse_click(); break; case HOTKEY_RIGHT_MOUSE_CLICK: right_mouse_click(); break; default: return false; } return true; }
/* * Hauptprogramm des Servers */ int main(int argc, char **argv) { unsigned short port = 54321; argc_cp = argc; argv_cp = argv; pid_t forkResult; char pfad[51]; char catalog[51]; /* * Semaphore initialisieren */ sem_init(&uno_semaphore, 0, 0); set_pid(); if(detect_para(&port, pfad, catalog) == -1) { exit(0); } /* *Verhalten des Servers bei Strg+C */ signal(SIGINT,sigfunc); infoPrint("gewählter Port: %d", port); infoPrint("gewählter Pfad: %s", pfad); infoPrint("gewählter Katalog: %s", catalog); /* *Den Programmnamen setzen */ setProgName(argv[0]); infoPrint("Server Gruppe 6/n"); infoPrint("Info: Der Server kann mit STRG-C beendet werden."); infoPrint("--------------------------------------------------------------------------------------------------------------\n"); /* *Socket erstellen */ start_network(port); /* *Pipe erstellen */ if(pipe(stdinPipe) == -1 || pipe(stdoutPipe) == -1) { perror("pipe"); } /* *Kindprozess erstellen */ forkResult = fork(); if(forkResult < 0) { perror("fork"); } /* *Im Kindprozess */ else if(forkResult == 0) { /* *Standardeingabekanal mit dem Pipekanal stdinPipe[0] verbinden */ if(dup2(stdinPipe[0], STDIN_FILENO) == -1) { perror("dup2(stdinPipe[0], STDIN_FILENO)"); } /*Standardausgabekanal mit dem Pipekanal stdinPipe[1] verbinden*/ if(dup2(stdoutPipe[1], STDOUT_FILENO) == -1) { perror("dup2(stdoutPipe[1], STDOUT_FILENO)"); } /* Schließen aller Pipe-Deskriptoren. * Nach dem exec kennt der Kindprozess diese nicht mehr und spricht * die Pipes selbst über stdin und stdout an. */ close(stdinPipe[0]); close(stdinPipe[1]); close(stdoutPipe[0]); close(stdoutPipe[1]); /* *Neues Programm läuft... */ execl("./loader", "loader", "-d", catalog, NULL); /* * ...oder auch nicht, dann war's aber ein Fehler */ perror("exec"); } close(stdinPipe[0]); close(stdoutPipe[1]); pthread_t login_thread_id; /* *Login Thread starten */ if((pthread_create(&login_thread_id, 0, (void*)&login_loop, &socket_desc)) != 0) { perror("Thread_create steht in main.c"); } pthread_t spielstand_thread_id; infoPrint("NACH LOGIN!!!"); /* *Spielstand Thread starten */ if((pthread_create(&spielstand_thread_id, NULL, (void*)&spielstand_thread_main, NULL)) != 0) { perror("Thread_create steht in main.c"); } /* * Warten auf das Ende des Spielstandthreads */ pthread_join(spielstand_thread_id, 0); /* *Server beenden */ quitServer(NO_ERROR, 0); return 0; }
/********************************************************* ****************** Main Function ****************** *********************************************************/ int main(int agrc, char *agrv[]) { /** * cmd parameter */ int help_flag = 0; int ipv6_flag = 0; int ser_flag = 0; int cli_flag = 0; int ser_or_cli_flag = -1; int socket_protocol = -1; int socket_type = -1; int net_type = AF_INET; int times = 1; char *protocol = NULL; char *message = NULL; char *ip = NULL; int port = 0; int rt = 0; int local_ip_flag = 0; char local_ip[256] = {0}; int gateway_ip_flag = {0}; char gateway_ip[128] = {0}; char *ifname = NULL; unsigned char local_mac[6] = {0}; char *remote_mac = NULL; char remote_ip[20] = {0}; struct options opts[] = { {"-h", "--help" , 0, RET_INT, ADDR_ADDR(help_flag) }, {"-6", NULL , 0, RET_INT, ADDR_ADDR(ipv6_flag) }, {"-s", "--server" , 0, RET_INT, ADDR_ADDR(ser_flag) }, {"-c", "--client" , 0, RET_INT, ADDR_ADDR(cli_flag) }, {"-a", "--agreement" , 1, RET_STR, ADDR_ADDR(protocol) }, {"-i", "--ip" , 1, RET_STR, ADDR_ADDR(ip) }, {"-p", "--port" , 1, RET_INT, ADDR_ADDR(port) }, {"-t", "--times" , 1, RET_INT, ADDR_ADDR(times) }, {"-m", "--message" , 1, RET_STR, ADDR_ADDR(message) }, {"-l", "--localip" , 0, RET_INT, ADDR_ADDR(local_ip_flag) }, {"-g", "--gatewayip" , 0, RET_INT, ADDR_ADDR(gateway_ip_flag)}, {NULL, "--mac" , 1, RET_STR, ADDR_ADDR(ifname) }, {"-r", "--remoteip" , 1, RET_STR, ADDR_ADDR(remote_mac) }, {NULL, NULL} }; struct usage help_usage[] = { {"-s, --server", "Create a socket server"}, {"-c, --client", "Create a socket client"}, {"-a, --agreement [Agreement]", "Agreement of networking. Agreement can be \"[u udp t tcp]\""}, {"-i, --ip [ip address]", "IP address"}, {"-p, --port [port]", "Port"}, {"-t, --times [times]", "Times of sending message"}, {"-m, --message [message]", "Message of sending"}, {"-l, --localip", "Local ip address"}, {"-g, --gatewayip", "Gateway ip address"}, {"--mac [interface name]", "Get mac address by interface name."}, {"-r, --remoteip [mac address]", "Get remote ip address."}, {"-h, --help", "Program usage"}, {NULL, NULL} }; /** * check count of cmdline arguemnts */ set_print_usage_width(60); if (agrc <= 1) { print_usage(help_usage); exit(-1); } /** * parser args */ get_args(agrc, agrv, opts); if(help_flag > 0) { print_usage(help_usage); exit(1); } /** * get mac addree by interface name */ if (ifname) { if (get_mac(ifname, local_mac, sizeof(local_mac)) < 0) return -1; print_mac(local_mac, NULL); return 0; } /** * get remote ip address by mac address */ if (remote_mac) { if (get_remote_ip_by_mac(remote_mac, remote_ip, sizeof(remote_ip), 0) < 0) return -1; printf("%s\n", remote_ip); return 0; } /** * Get Local IP Address */ if (ipv6_flag > 0) net_type = AF_INET6; if (local_ip_flag) { if (get_local_ip(net_type, NULL, local_ip, sizeof(local_ip)) < 0) { exit(-1); } printf("%s\n", local_ip); exit(0); } /** * get gateway ip address */ if (gateway_ip_flag) { if (get_gateway(gateway_ip, sizeof(gateway_ip)) < 0) { exit(-1); } printf("%s\n", gateway_ip); exit(0); } /** * socket server or client */ if (ser_flag > 0) ser_or_cli_flag = SOCKET_SERVER; else if (cli_flag > 0) ser_or_cli_flag = SOCKET_CLIENT; /** * socket protocol */ if (protocol != NULL) { if (!strncmp("u", protocol, sizeof("u")) || !strncmp("udp", protocol, sizeof("udp"))) { socket_protocol = IPPROTO_UDP; socket_type = SOCK_DGRAM; } else if (!strncmp("t", protocol, sizeof("t")) || !strncmp("tcp", protocol, sizeof("tcp"))) { socket_protocol = IPPROTO_TCP; socket_type = SOCK_STREAM; } else { fprintf(stderr, "Invalid arguemnt\n"); exit(1); } } /** * start up socket */ start_network(ser_or_cli_flag, net_type, socket_type, socket_protocol, ip, port, times, message); return rt; }