void proto_server_post_event(void) { int i; int num; pthread_mutex_lock(&Proto_Server.EventSubscribersLock); i = 0; num = Proto_Server.EventNumSubscribers; while (num > 0) { Proto_Server.EventSession.fd = Proto_Server.EventSubscribers[i]; if (Proto_Server.EventSession.fd != -1) { num--; if (proto_session_send_msg(&Proto_Server.EventSession, 0) < 0) { // must have lost an event connection close(Proto_Server.EventSession.fd); Proto_Server.EventSubscribers[i] = -1; Proto_Server.EventNumSubscribers--; Proto_Server.session_lost_handler(&Proto_Server.EventSession); } // FIXME: add ack message here to ensure that game is updated // correctly everywhere... at the risk of making server dependent // on client behaviour (use time out to limit impact... drop // clients that misbehave but be carefull of introducing deadlocks } i++; } proto_session_reset_send(&Proto_Server.EventSession); pthread_mutex_unlock(&Proto_Server.EventSubscribersLock); }
static int proto_server_mt_leave_game_handler(Proto_Session *s) { Proto_Msg_Hdr h; int rc; int dummy_reply = 1; if (proto_debug()) fprintf(stderr, "proto_server_mt_leave_game_handler: invoked for session:\n"); //proto_session_dump(s); // Call game logic: remove player from game // TODO: update game version if necesary // pthread_mutex_lock(&server_data_mutex); // pthread_mutex_unlock(&server_data_mutex); if (proto_debug()) fprintf(stderr, "proto_server_mt_leave_game_handler: remove player reply:%d\n", 1); bzero(&h, sizeof(h)); h.type = proto_session_hdr_unmarshall_type(s); h.type += PROTO_MT_REP_BASE_RESERVED_FIRST; h.sver.raw = server_gameData.version; h.gstate.v0.raw = server_gameData.state; proto_session_hdr_marshall(s, &h); proto_session_body_marshall_int(s, dummy_reply); rc = proto_session_send_msg(s, 1); // TODO: update subscribers // doUpdateClientsGame(0); return rc; }
static int proto_server_mt_numhome_handler(Proto_Session *s) { int numhome, rc, teamNo; Proto_Msg_Hdr h; if (proto_debug()) { fprintf(stderr, "proto_server_mt_move_handler: invoked for session:\n"); //proto_session_dump(s); } proto_session_body_unmarshall_int(s, 0, &teamNo); if (proto_debug()) fprintf(stderr, "proto_server_mt_numhome_handler: requested teamNo = %d\n", teamNo); numhome = findNumHome(teamNo); //numhome = 5; //pthread_mutex_lock(&game_mutex); //pthread_mutex_unlock(&game_mutex); if (proto_debug()) fprintf(stderr, "proto_server_mt_numhome_handler: maze.c func returned = %d\n", numhome); // create replay message bzero(&h, sizeof(h)); h.type = proto_session_hdr_unmarshall_type(s); h.type += PROTO_MT_REP_BASE_RESERVED_FIRST; proto_session_hdr_marshall(s, &h); if (proto_session_body_marshall_int(s, numhome) < 0 ) fprintf(stderr, "proto_server_mt_numhome_handler: " "proto_session_body_marshall_bytes failed\n"); rc = proto_session_send_msg(s, 1); // TODO: return failed if an error occours return rc; }
static int proto_server_mt_numfloor_handler(Proto_Session *s) { int numfloor, rc; Proto_Msg_Hdr h; if (proto_debug()) { fprintf(stderr, "proto_server_mt_numfloor_handler: invoked for session:\n"); //proto_session_dump(s); } numfloor = findNumFloor();//numfloor = 12345; // call mze.c numjail(teamNo) func if (proto_debug()) fprintf(stderr, "proto_server_mt_numfloor_handler: maze.c::func returned = %d\n", numfloor); // create replay message bzero(&h, sizeof(h)); h.type = proto_session_hdr_unmarshall_type(s); h.type += PROTO_MT_REP_BASE_RESERVED_FIRST; proto_session_hdr_marshall(s, &h); if (proto_session_body_marshall_int(s, numfloor) < 0 ) fprintf(stderr, "proto_server_mt_numfloor_handler: " "proto_session_body_marshall_bytes failed\n"); rc = proto_session_send_msg(s, 1); // TODO: return failed if an error occours return rc; }
static int proto_server_pickup_handler(Proto_Session *s){ int id,rc,ret; Proto_Msg_Hdr sh; Proto_Msg_Hdr rh; Player* p; Proto_Session *us; bzero(&sh, sizeof(sh)); bzero(&rh, sizeof(rh)); sh.type = proto_session_hdr_unmarshall_type(s); sh.type += PROTO_MT_REP_BASE_RESERVED_FIRST; proto_session_hdr_unmarshall(s, &rh); id = rh.pstate.v0.raw; p = gamestate_get_player(Server_Gamestate,id); ret = player_obj_pickup(p,Server_ObjectMap,Server_Gamestate); sh.pstate.v0.raw = id;//same header for both reply and update sh.pstate.v1.raw = ret; //sending back the actual thing picked up proto_session_hdr_marshall(s, &sh); rc = proto_session_send_msg(s,1); if(ret >= 0){ us = proto_server_event_session(); sh.type = PROTO_MT_EVENT_BASE_MOVE; proto_session_hdr_marshall(us,&sh); proto_server_post_event(); } return rc; }
static int proto_server_mt_dump_handler(Proto_Session *s) { int rc; Proto_Msg_Hdr h; if (proto_debug()) { fprintf(stderr, "proto_server_mt_dump_handler: invoked for session:\n"); //proto_session_dump(s); } dumpMap(); // create replay message bzero(&h, sizeof(h)); h.type = proto_session_hdr_unmarshall_type(s); h.type += PROTO_MT_REP_BASE_RESERVED_FIRST; proto_session_hdr_marshall(s, &h); rc = 1; if (proto_session_body_marshall_int(s, rc) < 0 ) fprintf(stderr, "proto_server_mt_dump_handler: " "proto_session_body_marshall_bytes failed\n"); rc = proto_session_send_msg(s, 1); // TODO: return failed if an error occours return rc; }
extern int proto_session_rpc(Proto_Session *s){ int rc=0; proto_session_send_msg(s, 1); rc = proto_session_rcv_msg(s); return rc; }
extern int proto_session_rpc(Proto_Session *s) { int rc; rc = proto_session_send_msg(s,1); if (rc==1) { rc = proto_session_rcv_msg(s); } return rc; }
static int proto_server_mt_cinfo_handler(Proto_Session *s) { int in_x, in_y, rc; char cell, team, occupied; Proto_Msg_Hdr h; if (proto_debug()) { fprintf(stderr, "proto_server_mt_cinfo_handler: invoked for session:\n"); //proto_session_dump(s); } // read msg here proto_session_body_unmarshall_int(s, 0, &in_x); proto_session_body_unmarshall_int(s, sizeof(int), &in_y); if (proto_debug()) fprintf(stderr, "proto_server_mt_numjail_handler: requested cellinfo( %d, %d )\n", in_x, in_y); //cell = '#';//cell = (char)0xCC; // call mze.c numjail(teamNo) func //team = '1';//team = (char)0xDD; //occupied = 'Y';//occupied = (char)0xEE; cell = get_cell_type( in_x, in_y ); team = get_cell_team( in_x, in_y ); occupied = is_cell_occupied( in_x, in_y ); //findCInfo(in_x, in_y); if (proto_debug()) fprintf(stderr, "proto_server_mt_numjail_handler: maze.c::func returned\n" " cell='%c', team='%c', occupied='%c'\n", cell, team, occupied); // create replay message bzero(&h, sizeof(h)); h.type = proto_session_hdr_unmarshall_type(s); h.type += PROTO_MT_REP_BASE_RESERVED_FIRST; proto_session_hdr_marshall(s, &h); if (proto_session_body_marshall_char(s, cell) < 0 ) fprintf(stderr, "proto_server_mt_cinfo_handler: " "proto_session_body_marshall_bytes failed\n"); if (proto_session_body_marshall_char(s, team) < 0 ) fprintf(stderr, "proto_server_mt_cinfo_handler: " "proto_session_body_marshall_bytes failed\n"); if (proto_session_body_marshall_char(s, occupied) < 0 ) fprintf(stderr, "proto_server_mt_cinfo_handler: " "proto_session_body_marshall_bytes failed\n"); rc = proto_session_send_msg(s, 1); // TODO: return failed if an error occours return rc; }
static int proto_server_dump_handler(Proto_Session *s){ Proto_Msg_Hdr h; int i; map_dump(Server_Map); bzero(&h, sizeof(h)); h.type = PROTO_MT_REP_BASE_DUMP; proto_session_hdr_marshall(s, &h); proto_session_body_marshall_int(s, 1); return proto_session_send_msg(s,1); }
static int proto_server_mt_join_game_handler(Proto_Session *s) { int rc, player, dimy, dimx; Proto_Msg_Hdr h; player = 1; char dummy_maze[] = {'1','2','3','4','5','6','7','8','9'}; if (proto_debug()) fprintf(stderr, "proto_server_mt_join_game_handler: invoked for session:\n"); //proto_session_dump(s); // prepare reply message bzero(&h, sizeof(s)); h.type = proto_session_hdr_unmarshall_type(s); h.type += PROTO_MT_REP_BASE_RESERVED_FIRST; // send to client the current state and version h.sver.raw = server_gameData.version; h.gstate.v0.raw = server_gameData.state; proto_session_hdr_marshall(s, &h); // TODO: call game logic dimx = 3;//dimx = get_maze_dimx(); dimy = 3;//dimy = get_maze_dimy(); // TODO: update game version and state if necesary // pthread_mutex_lock(&server_data_mutex); // pthread_mutex_unlock(&server_data_mutex); // relpy: pID, xdim, ydim, maze if (proto_session_body_marshall_int(s, player) < 0) fprintf(stderr, "proto_server_mt_join_game_handler: " "proto_session_body_marshall_int failed\n"); if (proto_session_body_marshall_int(s, dimx) < 0 ) fprintf(stderr, "proto_server_mt_join_game_handler: " "proto_session_body_marshall_int failed\n"); if (proto_session_body_marshall_int(s, dimy) < 0 ) fprintf(stderr, "proto_server_mt_join_game_handler: " "proto_session_body_marshall_int failed\n"); if (proto_session_body_marshall_bytes(s, dimx*dimy, &dummy_maze[0]) < 0) fprintf(stderr, "proto_server_mt_join_game_handler: " "proto_session_body_marshall_bytes failed\n"); rc = proto_session_send_msg(s,1); // TODO: update subscribers doUpdateClientsGame(0); return rc; }
extern int proto_session_rpc(Proto_Session *s) { int rc; // HACK // fprintf(stderr, "Sent bytes:\n", ); // print_mem(&s->shdr, sizeof(Proto_Msg_Hdr)); rc = proto_session_send_msg(s, 0); rc = proto_session_rcv_msg(s); // fprintf(stderr, "Rcv msg returns : %d\n", rc); return rc; }
extern int proto_session_rpc(Proto_Session *s) { int rc; if (proto_session_send_msg(s, 1) != -1) { rc = proto_session_rcv_msg(s); } else { rc = -1; } return rc; }
//This function is used for RPC's that do not contain anything in the body //Parameter: Proto_Msg_Hdr ch - that contains all necessary information for the RPC // Proto_Client_Handle *h - Handle to the Proto_Client //Returns: Return Code - as specified in Game_Error_Types in types.h extern int do_no_body_rpc(Proto_Client_Handle ch, Proto_Msg_Hdr * h) { int rc; Proto_Session *s; Proto_Client *c = ch; s = &(c->rpc_session); proto_session_hdr_marshall(s,h); rc = proto_session_send_msg(s,1); rc = proto_session_rcv_msg(s); if(rc<=0) rc =-1; return rc; }
static int proto_server_mt_item_action_handler(Proto_Session *s) { int rc, player_id; char item, action; int dummy_reply = 1; Proto_Msg_Hdr h; if (proto_debug()) fprintf(stderr, "proto_server_mt_item_action_handler: invoked for session:\n"); // proto_session_dump(s); // Read rpc message: pID, item, action proto_session_body_unmarshall_int(s, 0, &player_id); proto_session_body_unmarshall_char(s, sizeof(int), &item); proto_session_body_unmarshall_char(s, sizeof(int)+sizeof(char), &action); if (proto_debug()) fprintf(stderr, "proto_server_mt_move_handler: Recieved:\n" " pId: %d\n item #%c#\n action #%c#\n", player_id, item, action); // TODO: call game logic // TODO: update game version if necesary // pthread_mutex_lock(&server_data_mutex); // pthread_mutex_unlock(&server_data_mutex); if (proto_debug()) fprintf(stderr, "proto_server_mt_move_handler: Send: game logic_reply: %d\n", dummy_reply); // rpc reply bzero(&h, sizeof(h)); h.type = proto_session_hdr_unmarshall_type(s); h.type += PROTO_MT_REP_BASE_RESERVED_FIRST; h.sver.raw = server_gameData.version; h.gstate.v0.raw = server_gameData.state; proto_session_hdr_marshall(s, &h); proto_session_body_marshall_int(s, dummy_reply); rc = proto_session_send_msg(s, 1); // TODO: update subscribers // doUpdateClientsGame(0); return rc; }
extern int proto_session_rpc(Proto_Session *s) { int rc; // ADD CODE int n1; int n2; n1 = proto_session_send_msg(s, 1); if (n1 != 1) { printf("prote_session_rpc error: sending messge"); return -1; } n2 = proto_session_rcv_msg(s); if (n2 == 1) rc = 1; else rc = -1; return rc; }
//send back an ack reply then close session static int proto_server_goodbye_handler(Proto_Session *s){ Proto_Msg_Hdr h; Proto_Session *us; Player p; int rc=1; proto_session_body_unmarshall_player(s,0,&p); bzero(&h, sizeof(h)); h.type = PROTO_MT_REP_BASE_GOODBYE; proto_session_hdr_marshall(s, &h); rc = proto_session_send_msg(s,1); if(p.team == TEAM1){ numPlayers1--; if(p.state == SAFE) numplayershome1--; } else{ numPlayers2--; if(p.state == SAFE) numplayershome2--; } gamestate_remove_player(Server_Gamestate,p.id); pidx = player_find_next_id(Server_Gamestate->plist); if(pidx<0) gamefull = 1; else gamefull = 0; us = proto_server_event_session(); h.type = PROTO_MT_EVENT_BASE_PLAYER_QUIT; proto_session_hdr_marshall(us,&h); proto_session_body_marshall_player(us, &p); proto_server_post_event(); return rc; }
//This function is used for RPC's that do not contain anything in the body //Parameter: Proto_Msg_Hdr *h - that contains all necessary information for the RPC // Proto_Client_Handle ch - Handle to the Proto_Client // Pos current - current position of the player // Pos next - next position that the players wants to move to //Returns: Return Code - as specified in Game_Error_Types in types.h extern int do_action_request_rpc(Proto_Client_Handle ch, Proto_Msg_Hdr * hdr,Pos current, Pos next) { int rc; Proto_Session *s; Proto_Client *c = ch; s = &(c->rpc_session); proto_session_hdr_marshall(s,hdr); if( hdr->gstate.v1.raw >= ACTION_MOVE && hdr->gstate.v1.raw <= ACTION_PICKUP_SHOVEL) { proto_session_body_marshall_bytes(s,sizeof(Pos),(char*)¤t); proto_session_body_marshall_bytes(s,sizeof(Pos),(char*)&next); } rc = proto_session_send_msg(s,1); rc = proto_session_rcv_msg(s); if(rc<=0) rc =-1; return rc; }
static int proto_server_mt_null_handler(Proto_Session *s){ int rc=1; Proto_Msg_Hdr h; fprintf(stderr, "proto_server_mt_null_handler: invoked for session:\n"); proto_session_dump(s); // setup dummy reply header : set correct reply message type and // everything else empty bzero(&h, sizeof(h)); h.type = proto_session_hdr_unmarshall_type(s); h.type += PROTO_MT_REP_BASE_RESERVED_FIRST; proto_session_hdr_marshall(s, &h); // setup a dummy body that just has a return code proto_session_body_marshall_int(s, 0xdeadbeef); rc=proto_session_send_msg(s,1); return rc; }
static int proto_server_mt_dim_handler(Proto_Session *s) { int dimx, dimy, rc; Proto_Msg_Hdr h; if (proto_debug()) { fprintf(stderr, "proto_server_mt_dim_handler: invoked for session:\n"); //proto_session_dump(s); } //dimx = 1;//dimx = 0x0000AAAA; // call mze.c numjail(teamNo) func //dimy = 2;//dimy = 0x0000BBBB; // call mze.c numjail(teamNo) func dimx = get_maze_dimx(); dimy = get_maze_dimy(); if (proto_debug()) fprintf(stderr, "proto_server_mt_dim_handler: maze.c::func returned x=%d, y=%d\n", dimx, dimy); // create replay message bzero(&h, sizeof(h)); h.type = proto_session_hdr_unmarshall_type(s); h.type += PROTO_MT_REP_BASE_RESERVED_FIRST; proto_session_hdr_marshall(s, &h); if (proto_session_body_marshall_int(s, dimx) < 0 ) fprintf(stderr, "proto_server_mt_dim_handler: " "proto_session_body_marshall_bytes failed\n"); if (proto_session_body_marshall_int(s, dimy) < 0 ) fprintf(stderr, "proto_server_mt_dim_handler: " "proto_session_body_marshall_bytes failed\n"); rc = proto_session_send_msg(s, 1); // TODO: return failed if an error occours return rc; }
void proto_server_post_event(void){ int i; int num; pthread_mutex_lock(&Proto_Server.EventSubscribersLock); i = 0; num = Proto_Server.EventNumSubscribers; while (num) { Proto_Server.EventSession.fd = Proto_Server.EventSubscribers[i]; if (Proto_Server.EventSession.fd != -1) { num--; if (proto_session_send_msg(&(Proto_Server.EventSession),0)<0) { printf("Lost a connection.\n"); // must have lost an event connection close(Proto_Server.EventSession.fd); Proto_Server.EventSubscribers[i]=-1; Proto_Server.EventNumSubscribers--; // NYI; //Proto_Server.ADD CODE //I Have no idea^^ -B 4/24/13 // may have to issue a remove player for this subscriber...-chris 4/24/13 } // FIXME: add ack message here to ensure that game is updated // correctly everywhere... at the risk of making server dependent // on client behaviour (use time out to limit impact... drop // clients that misbehave but be carefull of introducing deadlocks } i++; } proto_session_reset_send(&Proto_Server.EventSession); pthread_mutex_unlock(&Proto_Server.EventSubscribersLock); }
static int proto_server_move_handler(Proto_Session *s){ int i,rx,ry,id,rc, winner; dir_t dir; Cell *cell = malloc(sizeof(Cell)); Proto_Msg_Hdr sh; Proto_Msg_Hdr rh; Player* p; Proto_Session *us; int valid; int flagindex; object_t flag = -1; Proto_Session *fs; bzero(&sh, sizeof(sh)); bzero(&rh, sizeof(rh)); sh.type = proto_session_hdr_unmarshall_type(s); sh.type += PROTO_MT_REP_BASE_RESERVED_FIRST; proto_session_hdr_unmarshall(s, &rh); id = rh.pstate.v0.raw; dir = rh.pstate.v1.raw; p = gamestate_get_player(Server_Gamestate,id); valid = 0; flagindex = -1; valid = player_move(dir,p,Server_ObjectMap, Server_Gamestate); // printf("Valid bit%d\n", valid); if (valid>0) { sh.pstate.v3.raw = 1; printf("Player %d is moving to (%d,%d)\n",id,p->pcell->x,p->pcell->y); if(!DEBUG_MAP){ flagindex = objectmap_flag_visible(p,Server_ObjectMap); if(flagindex>=0) flag = Server_ObjectMap->objects[flagindex]->obj; if(flag == FLAG1){ if(!flag1found) flag1found = 1; else flagindex = -1; } if(flag == FLAG2){ if(!flag2found) flag2found = 1; else flagindex = -1; } } } else { sh.pstate.v3.raw = 0; printf("Player %d attemped an invalid move\n",id); } sh.pstate.v0.raw = p->id; sh.pstate.v1.raw = p->pcell->x; sh.pstate.v2.raw = p->pcell->y; proto_session_hdr_marshall(s, &sh); rc = proto_session_send_msg(s,1); if(valid){ us = proto_server_event_session(); sh.type = PROTO_MT_EVENT_BASE_MOVE; proto_session_hdr_marshall(us,&sh); proto_server_post_event(); } bzero(&sh, sizeof(sh)); if(flagindex>=0){ fs = proto_server_event_session(); sh.type = PROTO_MT_EVENT_BASE_FLAG; //gstate.v0 holds the flag itself //gstate.v1 holds the index in the objectmap where that flag is located sh.gstate.v0.raw = Server_ObjectMap->objects[flagindex]->obj; sh.gstate.v1.raw = flagindex; proto_session_hdr_marshall(fs,&sh); proto_server_post_event(); } bzero(&sh, sizeof(sh)); if((winner = gamestate_team_wins()) >= 0){ sh.type = PROTO_MT_EVENT_BASE_WIN; sh.gstate.v0.raw = winner; proto_session_hdr_marshall(fs,&sh); proto_server_post_event(); } return rc; }
static int proto_server_hello_handler(Proto_Session *s){ int rc = 1; int x; Player* p; Proto_Msg_Hdr sh; Proto_Session *us; p = player_create(); bzero(&sh, sizeof(sh)); sh.type = proto_session_hdr_unmarshall_type(s); sh.type += PROTO_MT_REP_BASE_RESERVED_FIRST; x = player_find_empty_home(p,nextTeam, Server_ObjectMap, pidx); // x = !gamefull; if(x){ gamestate_add_player(Server_Gamestate,p); printf("New player joining:\n"); printf("Location: %d,%d\n", p->pcell->x, p->pcell->y); player_dump(p); sh.pstate.v0.raw = 1; sh.pstate.v1.raw = p->id; gamestate_dump(Server_Gamestate); proto_session_body_marshall_gamestate(s,Server_Gamestate); // proto_session_body_marshall_map(s,Server_Map); if(nextTeam == TEAM1){ nextTeam = TEAM2; numPlayers1++; } else{ nextTeam = TEAM1; numPlayers2++; } pidx = player_find_next_id(Server_Gamestate->plist); if(pidx<0) gamefull = 1; s->player = p; } else { sh.pstate.v0.raw = 0; } proto_session_hdr_marshall(s, &sh); // proto_dump_msghdr(&(s->shdr)); rc = proto_session_send_msg(s,1); if(x){ //if new player joining, tell everyone us = proto_server_event_session(); bzero(&sh, sizeof(sh)); sh.type = PROTO_MT_EVENT_BASE_PLAYER_JOIN; sh.pstate.v0.raw = p->id; sh.pstate.v1.raw = p->pcell->x; sh.pstate.v2.raw = p->pcell->y; proto_session_hdr_marshall(us, &sh); proto_session_body_marshall_player(us,p); proto_server_post_event(); } return rc; }