extern int object_unmarshall(Proto_Session *s, int offset, Object * object) { int type,x,y,team,visible; offset = proto_session_body_unmarshall_int(s, offset, &type); if (offset < 0) return offset; offset = proto_session_body_unmarshall_int(s, offset, &x); if (offset < 0) return offset; offset = proto_session_body_unmarshall_int(s, offset, &y); if (offset < 0) return offset; offset = proto_session_body_unmarshall_int(s, offset, &team); if (offset < 0) return offset; offset = proto_session_body_unmarshall_int(s, offset, &visible); if (offset < 0) return offset; object->type = (Object_Type) type; object->x = x; object->y = y; object->team = team; object->visible = visible; // printf("Unmarshalled "); // object_dump(object); return offset; }
extern int proto_client_cell_info(Proto_Client_Handle ch, int x, int y, int * buf) { int team, oc, rc; char cell_type; Proto_Session *s; Proto_Client *c = ch; s = &(c->rpc_session); marshall_mtonly(s, PROTO_MT_REQ_BASE_GET_CELL_INFO); proto_session_body_marshall_int(s, x); proto_session_body_marshall_int(s, y); rc = proto_session_rpc(s); if (rc == 1) { proto_session_body_unmarshall_char(s, 0, &cell_type); if(cell_type == 'i') return -1; proto_session_body_unmarshall_int(s, sizeof(char), &team); proto_session_body_unmarshall_int(s, sizeof(char) + sizeof(int), &oc); buf[0] = (int)cell_type; buf[1] = team; buf[2] = oc; } else c->session_lost_handler(s); return rc; }
extern int proto_session_body_unmarshall_gamestate(Proto_Session *s, int offset, Gamestate *g){ Player *p; Cell *c; int nplayers; int ncells; offset= proto_session_body_unmarshall_int(s,offset,&ncells); offset= proto_session_body_unmarshall_int(s,offset,&nplayers); while(ncells){ c = (Cell *)malloc(sizeof(Cell)); if((offset=proto_session_body_unmarshall_cell(s,offset,c))<0){ free(c); return -1; } gamestate_add_cell(g,c); ncells--; } while(nplayers){ p = (Player *)malloc(sizeof(Player)); if((offset=proto_session_body_unmarshall_player(s,offset,p))<0){ free(p); return -1; } gamestate_add_player(g,p); nplayers--; } return offset; }
extern int proto_session_body_unmarshall_hammer(Proto_Session *s, int offset, Hammer *h) { //printf("Got into unmarshall_hammer\n"); offset = proto_session_body_unmarshall_int(s, offset, &(h->hammerID)); if (offset != -1) offset = proto_session_body_unmarshall_int(s, offset, &(h->uses)); return offset; }
extern int proto_session_body_unmarshall_point(Proto_Session *s, int offset, Point *p) { //printf("Got into unmarshall_point\n"); offset = proto_session_body_unmarshall_int(s, offset, &(p->x)); if (offset != -1) offset = proto_session_body_unmarshall_int(s, offset, &(p->y)); return offset; }
extern int proto_session_body_unmarshall_cell(Proto_Session *s, int offset, Cell *c) { printf("Got into unmarshall_cell\n"); offset = proto_session_body_unmarshall_hammer(s, offset, &(c->mjolnir)); if (offset != -1) offset = proto_session_body_unmarshall_int(s, offset, &(c->type)); if (offset != -1) offset = proto_session_body_unmarshall_int(s, offset, &(c->flag)); if (offset != -1) offset = proto_session_body_unmarshall_int(s, offset, &(c->playernum)); return offset; }
extern int proto_session_body_unmarshall_player(Proto_Session *s, int offset, Player *p) { //printf("Got into unmarshall_player\n"); offset = proto_session_body_unmarshall_point(s, offset, &(p->location)); if (offset != -1) offset = proto_session_body_unmarshall_hammer(s, offset, &(p->mjolnir)); if (offset != -1) offset = proto_session_body_unmarshall_int(s, offset, &(p->playernum)); if (offset != -1) offset = proto_session_body_unmarshall_int(s, offset, &(p->team)); if (offset != -1) offset = proto_session_body_unmarshall_int(s, offset, &(p->flag)); return offset; }
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; }
extern int proto_client_pickup_shovel(Proto_Client_Handle ch) { int rc, offset; Player *p = ClientGameState.me; Proto_Session *s; Proto_Client *c = ch; s = &(c->rpc_session); marshall_mtonly(s, PROTO_MT_REQ_BASE_PICKUP_SHOVEL); player_marshall(s,p); rc = proto_session_rpc(s); if (rc != 1) { c->session_lost_handler(s); return rc; } else { offset = proto_session_body_unmarshall_int(s, 0, &rc); player_unmarshall(s, offset, ClientGameState.me); } return rc; }
extern int proto_client_drop_flag(Proto_Client_Handle ch) { Player *p = ClientGameState.me; int rc, offset; Proto_Session *s; Proto_Client *c = ch; s = &(c->rpc_session); marshall_mtonly(s, PROTO_MT_REQ_BASE_DROP_FLAG); player_marshall(s,p); rc = proto_session_rpc(s); if (rc != 1) { c->session_lost_handler(s); return rc; } else { offset = proto_session_body_unmarshall_int(s, 0, &rc); player_unmarshall(s, offset, ClientGameState.me); } return rc; }
static int do_dump_rpc(Proto_Client_Handle ch, Proto_Msg_Types mt) { int rc; Proto_Session *s; Proto_Client *c = ch; if (proto_debug()) fprintf(stderr, "do_dump_rpc \n"); s = &(c->rpc_session); marshall_mtonly(s, mt); rc = proto_session_rpc(s); if (rc == 1) { proto_session_body_unmarshall_int(s, 0, &rc); } else { c->session_lost_handler(s); close(s->fd); } return rc; }
extern int proto_client_move(Proto_Client_Handle ch, Player_Move direction) { int rc = 1, offset = 0; Proto_Session *s; Proto_Client *c = ch; s = &(c->rpc_session); //printf("Sending move command to server...\n\n"); marshall_mtonly(s, PROTO_MT_REQ_BASE_MOVE); player_marshall(s, ClientGameState.me); proto_session_body_marshall_int(s, direction); rc = proto_session_rpc(s); if (rc != 1) { c->session_lost_handler(s); return rc; } else { offset = proto_session_body_unmarshall_int(s, 0, &rc); player_unmarshall(s, offset, ClientGameState.me); } if (rc < 0) printf("player move Error!\n"); // else if (rc == 0) printf("player move rejected by server!\n"); // else printf("Player move successful.\n"); return rc; }
// all rpc's are assume to only reply only with a return code in the body // eg. like the null_mes static int do_generic_dummy_rpc(Proto_Client_Handle ch, Proto_Msg_Types mt) { int rc; Proto_Session *s; Proto_Client *c = ch; s = &(c->rpc_session); // marshall marshall_mtonly(s, mt); rc = proto_session_rpc(s); if (rc == 1) { proto_session_body_unmarshall_int(s, 0, &rc); } else { c->session_lost_handler(s); close(s->fd); } return rc; }
// Assigment 3 rpc static int do_numhome_rpc(Proto_Client_Handle ch, Proto_Msg_Types mt, int data) { int rc; Proto_Session *s; Proto_Client *c = ch; Proto_Msg_Hdr h; if (proto_debug()) fprintf(stderr, "do_numhome_rpc (team = %d)\n", data); s = &(c->rpc_session); bzero(&h, sizeof(h)); h.type = mt; proto_session_hdr_marshall(s, &h); if (proto_session_body_marshall_int(s, data) < 0) fprintf(stderr, "get_numHome_rpc: proto_session_body_marshall_char failed. " "Not enough available sbufer space\n"); rc = proto_session_rpc(s); if (rc == 1) { if(proto_session_body_unmarshall_int(s, 0, &rc) < 0) fprintf(stderr, "do_numhome_rpc: proto_session_body_unmarshall_bytes failed\n"); if (proto_debug()) fprintf(stderr, "do_numhome_rpc: unmarshalled response rc = %d \n", rc); } else { c->session_lost_handler(s); close(s->fd); } 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 do_numwall_rpc(Proto_Client_Handle ch, Proto_Msg_Types mt) { int rc; Proto_Session *s; Proto_Client *c = ch; Proto_Msg_Hdr h; if (proto_debug()) fprintf(stderr, "do_numwall_rpc\n"); s = &(c->rpc_session); bzero(&h, sizeof(h)); h.type = mt; proto_session_hdr_marshall(s, &h); rc = proto_session_rpc(s); if (rc == 1) { if(proto_session_body_unmarshall_int(s, 0, &rc) < 0) fprintf(stderr, "get_numwall_rpc: proto_session_body_unmarshall_bytes failed\n"); if (proto_debug()) fprintf(stderr, "do_numwall_rpc: unmarshalled response rc = %d \n", rc); } else { c->session_lost_handler(s); close(s->fd); } return rc; }
static int proto_server_mt_numjail_handler(Proto_Session *s) { int numjail, teamNo, rc; Proto_Msg_Hdr h; if (proto_debug()) { fprintf(stderr, "proto_server_mt_move_handler: invoked for session:\n"); //proto_session_dump(s); } // read msg here proto_session_body_unmarshall_int(s, 0, &teamNo); if (proto_debug()) fprintf(stderr, "proto_server_mt_numjail_handler: requested teamNo = %d\n", teamNo); numjail = findNumJail(teamNo);//numjail = 8; // call mze.c numjail(teamNo) func if (proto_debug()) fprintf(stderr, "proto_server_mt_numjail_handler: maze.c::func returned = %d\n", numjail); // 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, numjail) < 0 ) fprintf(stderr, "proto_server_mt_numjail_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 get_int(Proto_Client_Handle ch, int offset, int* result) { int rc; Proto_Session *s; Proto_Client *c = ch; s = &(c->rpc_session); rc = proto_session_body_unmarshall_int(s, offset, result); return rc; }
extern int proto_session_body_unmarshall_objectmap(Proto_Session *s, int offset, ObjectMap *o){ int dim; offset = proto_session_body_unmarshall_int(s,offset,&dim); if (s && ((s->rlen - (offset + sizeof(ObjectMap) + sizeof(Cell)*dim*dim) >= 0))){ memcpy(o, s->rbuf + offset, sizeof(ObjectMap)); memcpy(o->objects, s->rbuf + offset+ sizeof(ObjectMap), sizeof(Cell)*dim*dim); return offset + sizeof(Map) + sizeof(Cell)*dim*dim; } return -1; }
static int do_dim_rpc(Proto_Client_Handle ch, Proto_Msg_Types mt) { int rc, x, y; Proto_Session *s; Proto_Client *c = ch; Proto_Msg_Hdr h; if (proto_debug()) fprintf(stderr, "do_dim_rpc\n"); s = &(c->rpc_session); bzero(&h, sizeof(h)); h.type = mt; proto_session_hdr_marshall(s, &h); rc = proto_session_rpc(s); if (rc == 1) { if (proto_session_body_unmarshall_int(s, 0, &x) < 0) fprintf(stderr, "do_dim_rpc: proto_session_body_unmarshall_bytes failed\n"); if (proto_session_body_unmarshall_int(s, sizeof(int), &y) < 0) fprintf(stderr, "do_dim_rpc: proto_session_body_unmarshall_bytes failed\n"); if (proto_debug()) fprintf(stderr, "do_dim_rpc: unmarshalled response x = %X \n", x); if (proto_debug()) fprintf(stderr, "do_dim_rpc: unmarshalled response y = %X \n", y); rc = (x << 16) | y; if (proto_debug()) fprintf(stderr, "do_dim_rpc: return value = %X \n", rc); } else { c->session_lost_handler(s); close(s->fd); } return rc; }
extern int maze_unmarshall_cell(Proto_Session *s, int offset) { int x, y, team; char type; offset = proto_session_body_unmarshall_int(s, offset, &x); if (offset < 0) return offset; offset = proto_session_body_unmarshall_int(s, offset, &y); if (offset < 0) return offset; offset = proto_session_body_unmarshall_char(s, offset, &type); if (offset < 0) return offset; offset = proto_session_body_unmarshall_int(s, offset, &team); if (offset < 0) return offset; // If the cell at that location existed, free the memory if (Board.cells[y][x] != NULL) free(Board.cells[y][x]); // After all the primitives are unmarshalled correctly, // allocate a new cell, and put it's pointer in the board Board.cells[y][x] = (Cell *) malloc(sizeof(Cell)); // Populate the values Board.cells[y][x]->x = x; Board.cells[y][x]->y = y; Board.cells[y][x]->type = type; Board.cells[y][x]->team = team; // printf("Done with cell [%d][%d]\n",x,y); return offset; }
static int do_item_action_rpc(Proto_Client_Handle ch, Proto_Msg_Types mt, char item, char action) { int rc; Proto_Session *s; Proto_Client *c = ch; Proto_Msg_Hdr h; // prepare message s = &(c->rpc_session); bzero(&h, sizeof(h)); h.type = mt; proto_session_hdr_marshall(s, &h); if (proto_debug()) fprintf(stderr, "do_item_action: pId: %d, item: #%c#, action = #%c#\n", playerdata.id, item, action); if (proto_session_body_marshall_int(s, playerdata.id) < 0) fprintf(stderr, "do_item_action_rpc: proto_session_body_marshall_int failed. " "Not enough available sbufer space\n"); if (proto_session_body_marshall_char(s, item) < 0) fprintf(stderr, "do_item_action: proto_session_body_marshall_char failed. " "Not enough available sbufer space\n"); if (proto_session_body_marshall_char(s, action) < 0) fprintf(stderr, "do_item_action: proto_session_body_marshall_char failed. " "Not enough available sbufer space\n"); rc = proto_session_rpc(s); // process response if (rc == 1) { proto_session_hdr_unmarshall(s, &h); wait_for_event(h.sver.raw); if (proto_session_body_unmarshall_int(s, 0, &rc) < 0) fprintf(stderr, "do_item_action: proto_session_body_unmarshall_int failed\n"); if (proto_debug()) fprintf(stderr, "do_item_action: unmarshalled response rc = %d, game version = %llu, game state = %d \n", rc, h.sver.raw, h.gstate.v0.raw); } else { c->session_lost_handler(s); close(s->fd); } return rc; }
extern int proto_client_dump_maze(Proto_Client_Handle ch) { int rc; Proto_Session *s; Proto_Client *c = ch; s = &(c->rpc_session); marshall_mtonly(s, PROTO_MT_REQ_BASE_DUMP); rc = proto_session_rpc(s); if (rc == 1) proto_session_body_unmarshall_int(s, 0, &rc); else c->session_lost_handler(s); return rc; }
extern int maze_unmarshall_board(Proto_Session *s, int offset) { offset = proto_session_body_unmarshall_int(s, offset, &(Board.size)); if (offset < 0) return offset; int x,y; for (y = 0; y < Board.size; y++) { for (x = 0; x < Board.size; x++) { offset = maze_unmarshall_cell(s, offset); if (offset < 0) return offset; } } return offset; }
static int do_move_rpc(Proto_Client_Handle ch, Proto_Msg_Types mt, char data) { int rc, temp_version; Proto_Session *s; Proto_Client *c = ch; Proto_Msg_Hdr h; // prepare message: pID, move_command s = &(c->rpc_session); bzero(&h, sizeof(h)); h.type = mt; proto_session_hdr_marshall(s, &h); if (proto_debug()) fprintf(stderr, "do_move_rpc:\n pId: %d\n move: #%c#\n", playerdata.id, data); if (proto_session_body_marshall_int(s, playerdata.id) < 0) fprintf(stderr, "do_move_rpc: proto_session_body_marshall_int failed. " "Not enough available sbufer space\n"); if (proto_session_body_marshall_char(s, data) < 0) fprintf(stderr, "do_move_rpc: proto_session_body_marshall_char failed. " "Not enough available sbufer space\n"); rc = proto_session_rpc(s); // process reply if (rc == 1) { proto_session_hdr_unmarshall(s, &h); // wait until we have recieved the envent update wait_for_event(h.sver.raw); if (proto_session_body_unmarshall_int(s, 0, &rc) < 0) fprintf(stderr, "do_move_rpc: proto_session_body_unmarshall_int failed\n"); if (proto_debug()) fprintf(stderr, "do_move_rpc: unmarshalled response rc = %d, game version = %llu, game state = %d \n", rc, h.sver.raw, h.gstate.v0.raw); } else { c->session_lost_handler(s); close(s->fd); } return rc; }
extern int proto_client_maze_info(Proto_Client_Handle ch, char type) { int rc; Proto_Session *s; Proto_Client *c = ch; s = &(c->rpc_session); marshall_mtonly(s, PROTO_MT_REQ_BASE_GET_MAZE_INFO); proto_session_body_marshall_char(s, type); rc = proto_session_rpc(s); if (rc == 1) proto_session_body_unmarshall_int(s, 0, &rc); else c->session_lost_handler(s); return rc; }
extern int get_compress_from_body(Proto_Client_Handle ch,int offset ,int num_elements,int* alloc_pointer) { int ii, current_offset; int* ptr_to_array; current_offset = offset; ptr_to_array = alloc_pointer; Proto_Session *s; Proto_Client *c = ch; s = &(c->rpc_session); for(ii = 0; ii<num_elements;ii++) { current_offset = proto_session_body_unmarshall_int(s,current_offset,ptr_to_array); ptr_to_array++; } return current_offset; }
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 maze_unmarshall_row(Proto_Session *s, int offset, int section) { int x,y; if (section == 0) { offset = proto_session_body_unmarshall_int(s, offset, &(Board.size)); if (offset < 0) return offset; } //printf("Unmarshalling rows %d through %d\n", (section*20), ((section+1)*20)); for (y = section*20; y < (section+1)*20 && y < Board.size; y++) { //printf("Unmarshalling row %d\n",y); for (x = 0; x < Board.size; x++) { offset = maze_unmarshall_cell(s, offset); if (offset < 0) return offset; } } return offset; }
static int do_generic_dummy_rpc(Proto_Client_Handle ch, Proto_Msg_Types mt){ int rc; Proto_Session *s; Proto_Client *c = ch; s = &(c->rpc_session); marshall_mtonly(s, mt); rc = proto_session_rpc(s); if (rc==1) { if(mt == PROTO_MT_REQ_BASE_HELLO){ proto_session_hdr_unmarshall(s,&s->rhdr); } else proto_session_body_unmarshall_int(s, 0, &rc); } else { c->session_lost_handler(s); } return rc; }