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; }
// 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; }
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_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_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; }
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; }
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 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; }
extern int proto_client_goodbye(Proto_Client_Handle ch, int id, Player * p) { int rc; Proto_Session *s; Proto_Client *c = ch; s = &(c->rpc_session); marshall_mtonly(s, PROTO_MT_REQ_BASE_GOODBYE); proto_session_body_marshall_int(s,id); player_marshall(s,p); rc = proto_session_rpc(s); close(&(c->rpc_session.fd)); close(&(c->event_session.fd)); return rc; }
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; }