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; }
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 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 void proto_session_hdr_unmarshall_mapBody(Proto_Session *s, char *mapString){ int i; for (i=0; i<s->rhdr.blen; i++) { char a; proto_session_body_unmarshall_char(s, i, &a); mapString[i] = a; } mapString[i] = '\0'; // fprintf(stderr, "Final map: %s\n", mapString); }
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; }