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; }
static void * proto_client_event_dispatcher(void * arg) { Proto_Client *c; Proto_Session *s; Proto_Msg_Types mt; Proto_MT_Handler hdlr; pthread_detach(pthread_self()); /*NOT_IMPL;i*/ c = arg; s = &(c->event_session); for (;;) { if (proto_session_rcv_msg(s)==1) { mt = proto_session_hdr_unmarshall_type(s); if (mt > PROTO_MT_EVENT_BASE_RESERVED_FIRST && mt < PROTO_MT_EVENT_BASE_RESERVED_LAST) { hdlr = c->base_event_handlers[mt-PROTO_MT_EVENT_BASE_RESERVED_FIRST-1]; /*NOT_IMPL;//ADD CODE*/ if (hdlr(s)<0) goto leave; } } else { c->session_lost_handler(s); /*NOT_IMPL;//ADD CODE*/ goto leave; } } leave: close(s->fd); return NULL; }
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_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; }
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 do_cinfo_rpc(Proto_Client_Handle ch, int x, int y, Proto_Msg_Types mt){ int rc=0; Proto_Session *s; Proto_Client *c = ch; Proto_Msg_Hdr *hdr = malloc(sizeof(Proto_Msg_Hdr)); s = &(c->rpc_session); bzero(hdr, sizeof(Proto_Msg_Hdr)); hdr->pstate.v0.raw=x; hdr->pstate.v1.raw=y; hdr->type = mt; proto_session_hdr_marshall(s,hdr); proto_dump_msghdr(&(s->shdr)); rc = proto_session_rpc(s); if(rc==1){ proto_session_hdr_unmarshall(s,&s->rhdr); proto_dump_msghdr(&(s->rhdr)); } else c->session_lost_handler(s); 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; }
static int do_goodbye_rpc(Proto_Client_Handle ch, Player *p, Proto_Msg_Types mt){ int rc=0; Proto_Session *s; Proto_Client *c = ch; Proto_Msg_Hdr hdr; s = &(c->rpc_session); bzero(&hdr, sizeof(Proto_Msg_Hdr)); hdr.type = mt; proto_session_hdr_marshall(s,&hdr); proto_session_body_marshall_player(s,p); rc = proto_session_rpc(s); if(rc==1){ proto_session_hdr_unmarshall(s,&s->rhdr); // proto_dump_msghdr(&(s->rhdr)); } else c->session_lost_handler(s); 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; }
// 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; }
// 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; }
static int do_cinfo_rpc(Proto_Client_Handle ch, Proto_Msg_Types mt, int x, int y) { int rc; char cell, team, occupied; Proto_Session *s; Proto_Client *c = ch; Proto_Msg_Hdr h; s = &(c->rpc_session); bzero(&h, sizeof(h)); h.type = mt; proto_session_hdr_marshall(s, &h); if (proto_debug()) fprintf(stderr, "do_cinfo_rpc: cinfo(%d, %d) \n", x, y); if (proto_session_body_marshall_int(s, x) < 0) fprintf(stderr, "do_cinfo_rpc: proto_session_body_marshall_char failed. " "Not enough available sbufer space\n"); if (proto_session_body_marshall_int(s, y) < 0) fprintf(stderr, "do_cinfo_rpc: proto_session_body_marshall_char failed. " "Not enough available sbufer space\n"); rc = proto_session_rpc(s); if (rc == 1) { // unmarshall if (proto_session_body_unmarshall_char(s, 0, &cell) < 0) fprintf(stderr, "do_cinfo_rpc: proto_session_body_unmarshall_bytes failed\n"); if (proto_session_body_unmarshall_char(s, 1, &team) < 0) fprintf(stderr, "do_cinfo_rpc: proto_session_body_unmarshall_bytes failed\n"); if (proto_session_body_unmarshall_char(s, 2, &occupied) < 0) fprintf(stderr, "do_cinfo_rpc: proto_session_body_unmarshall_bytes failed\n"); if (proto_debug()) fprintf(stderr, "do_cinfo_rpc: unmarshalled response cell = %X, %c \n", cell & 0xFF, cell); if (proto_debug()) fprintf(stderr, "do_cinfo_rpc: unmarshalled response team = %X, %c \n", team & 0xFF, team); if (proto_debug()) fprintf(stderr, "do_cinfo_rpc: unmarshalled response occupied = %X, %c \n", occupied & 0xFF, occupied); rc = 0; rc =((cell & 0xFF)<<8) | ((team & 0xFF)<<16) | ((occupied & 0xFF)<<24); if (proto_debug()) fprintf(stderr, "do_cinfo_rpc: return value = %X \n", rc); } else { c->session_lost_handler(s); close(s->fd); } return rc; }
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; }
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_new_player(Proto_Client_Handle ch, int * id) { int rc = 1, offset = 0; Proto_Session *s; Proto_Client *c = ch; Player clientPlayer; memset(&clientPlayer, '\0', sizeof(clientPlayer)); s = &(c->rpc_session); //printf("Requesting to create a new player...\n\n"); marshall_mtonly(s, PROTO_MT_REQ_BASE_NEW_PLAYER); rc = proto_session_rpc(s); if (rc != 1) { printf("new player rpc returned -1\n"); c->session_lost_handler(s); return rc; } else { rc = player_unmarshall(s,0, &clientPlayer); *id = clientPlayer.fd; // put the client player in it's proper place based on team and id. player_copy(&(ClientGameState.players[clientPlayer.team][clientPlayer.id]),&clientPlayer); //player_dump(&(ClientGameState.players[clientPlayer.team][clientPlayer.id])); // set the ClientGameState.me pointer to this address. This is where our player will // be unmarshalled from now on, with every update from the server. ClientGameState.me = &(ClientGameState.players[clientPlayer.team][clientPlayer.id]); //player_dump(ClientGameState.me); } //printf("new player id = %d, team = %d\n",p->id,p->team); if (rc < 0) printf("Player_unmarshall Error!\n"); 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; }
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 proto_client_hello(Proto_Client_Handle ch) { int i = 0, rc = 1, offset = 0; Proto_Session *s; Proto_Client *c = ch; s = &(c->rpc_session); printf("Loading...\n\n"); marshall_mtonly(s, PROTO_MT_REQ_BASE_HELLO); proto_session_body_marshall_int(s,i); rc = proto_session_rpc(s); if (rc == 1) maze_unmarshall_row(s, offset, i); else c->session_lost_handler(s); if (Board.size / 20 > 0) { for (i = 1; i < Board.size / 20; i++) { proto_session_reset_send(s); marshall_mtonly(s, PROTO_MT_REQ_BASE_HELLO); proto_session_body_marshall_int(s,i); rc = proto_session_rpc(s); if (rc == 1) maze_unmarshall_row(s, 0, i); else c->session_lost_handler(s); } } //dump(); return i; }
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; }
static int do_leave_game_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_leave_rpc\n"); s = &(c->rpc_session); bzero(&h, sizeof(h)); marshall_mtonly(s, mt); rc = proto_session_rpc(s); if (rc == 1) { proto_session_hdr_unmarshall(s, &h); wait_for_event(h.sver.raw); // i dont think I need it here if (proto_session_body_unmarshall_int(s, 0, &rc) < 0) fprintf(stderr, "do_leave_game: proto_session_body_unmarshall_int failed\n"); if (proto_debug()) fprintf(stderr, "do_leave_game: 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; }
static int do_join_game_rpc(Proto_Client_Handle ch, Proto_Msg_Types mt) { int rc, X, Y, ii, size; Proto_Session *s; Proto_Client *c = ch; Proto_Msg_Hdr h; if (proto_debug()) fprintf(stderr, "do_join_game_rpc started.\n"); // prepare msessage 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", playerdata.id); if (proto_session_body_marshall_int(s, playerdata.id) < 0) fprintf(stderr, "do_join_game_rpc: proto_session_body_marshall_int failed. " "Not enough available sbufer space\n"); // sned message rc = proto_session_rpc(s); // process reply: pID, xdim, ydim, maze if (rc == 1) { proto_session_hdr_unmarshall(s, &h); if (proto_session_body_unmarshall_int(s, 0, &rc) < 0) fprintf(stderr, "do_join_game_rpc: proto_session_body_unmarshall_int failed\n"); if (rc < 0) { if (proto_debug()) fprintf(stderr, "do_join_game_rpc: returned player id = %d\n", rc); return rc; } if (proto_session_body_unmarshall_int(s, sizeof(int), &X) < 0) fprintf(stderr, "do_join_game_rpc: proto_session_body_unmarshall_int failed\n"); if (proto_session_body_unmarshall_int(s, 2*sizeof(int), &Y) < 0) fprintf(stderr, "do_join_game_rpc: proto_session_body_unmarshall_int failed\n"); pthread_mutex_lock(&client_data_mutex); if ( gamedata.game_version == 0 ) { // initialize game version and state playerdata.id = rc; gamedata.game_state = h.gstate.v0.raw; gamedata.game_version = h.sver.raw; themaze.rows = Y; themaze.columns = X; size = X*Y; themaze.maze = (char*) malloc( size*sizeof(char) + 1 ); themaze.cell_version = (unsigned long long*) malloc(size*sizeof(unsigned long long)); if (proto_session_body_unmarshall_bytes(s, 3*sizeof(int), size, themaze.maze) < 0) fprintf(stderr, "do_join_game_rpc: proto_session_body_unmarshall_bytes failed\n"); for (ii = 0; ii < size; ii++) themaze.cell_version[ii] = h.sver.raw; themaze.maze[X*Y] = 0; if (proto_debug()) fprintf(stderr, "do_join_game_rpc: unmarshalled response\n" " game version = %llu\n trs=%llu\n game state = %d\n player id = %d\n" " dimx = %d\n dimy = %d\n", h.sver.raw, h.gstate.v1.raw, h.gstate.v0.raw, rc, X, Y); } else fprintf(stderr, "do_join_game_rpc: WARNING: gamedata had been initialized before\n"); pthread_mutex_unlock(&client_data_mutex); } else { c->session_lost_handler(s); close(s->fd); } return rc; }