Ejemplo n.º 1
0
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;
}
Ejemplo n.º 2
0
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;
}
Ejemplo n.º 3
0
extern int
proto_session_body_marshall_point(Proto_Session *s, Point *p)
{
  //printf("Got into marshall_point\n");
  int rc = -1;
  if (proto_session_body_marshall_int(s, p->x) != -1)
  {
    rc = proto_session_body_marshall_int(s, p->y);
  }
  return rc;
}
Ejemplo n.º 4
0
extern int
proto_session_body_marshall_cell(Proto_Session *s, Cell *c)
{
  printf("Got into marshall_cell\n");
  int rc;
  rc = proto_session_body_marshall_hammer(s, &(c->mjolnir));
  if (rc != -1) rc = proto_session_body_marshall_int(s, c->type);
  if (rc != -1) rc = proto_session_body_marshall_int(s, c->flag);
  if (rc != -1) rc = proto_session_body_marshall_int(s, c->playernum);
  return rc;
}
Ejemplo n.º 5
0
extern int
proto_session_body_marshall_player(Proto_Session *s, Player *p)
{
  //printf("Got into marshall_player\n");
  int rc;
  rc = proto_session_body_marshall_point(s, &(p->location));
  if (rc != -1) rc = proto_session_body_marshall_hammer(s, &(p->mjolnir));
  if (rc != -1) rc = proto_session_body_marshall_int(s, p->playernum);
  if (rc != -1) rc = proto_session_body_marshall_int(s, p->team);
  if (rc != -1) rc = proto_session_body_marshall_int(s, p->flag);
  return rc;
}
Ejemplo n.º 6
0
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;
}
Ejemplo n.º 7
0
extern int
maze_marshall_cell(Proto_Session *s, Cell *cell)
{
  int rc = proto_session_body_marshall_int(s, cell->x);
  if (rc != 1) return rc;

  rc = proto_session_body_marshall_int(s, cell->y);
  if (rc != 1) return rc;

  rc = proto_session_body_marshall_char(s, cell->type);
  if (rc != 1) return rc;

  rc = proto_session_body_marshall_int(s, cell->team);
  return rc;
}
Ejemplo n.º 8
0
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;
}
Ejemplo n.º 9
0
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;

}
Ejemplo n.º 10
0
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;
}
Ejemplo n.º 11
0
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;
}
Ejemplo n.º 12
0
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;
}
Ejemplo n.º 13
0
static int
do_numjail_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_numjail_rpc\n");

    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_numjail_rpc: proto_session_body_unmarshall_bytes failed\n");
        if (proto_debug())
            fprintf(stderr, "do_numjail_rpc: unmarshalled response rc = %d \n", rc);
    }
    else {
        c->session_lost_handler(s);
        close(s->fd);
    }
    return rc;
}
/*
 * dim_c
 * dim_r
 * num_t1_players
 * num_t2_players
 * t1_flag
 * t2_flag
 * t1_jack
 * t2_jack
 * player_t 400
 */
extern int
wrap_update(maze_t * maze, Proto_Session * s) {
    if (proto_session_body_marshall_int(s, maze->dim_c) == -1) {
        fprintf(stderr, "ERROR: marhsall dim_c\n");
        return -1;
    }
    if (proto_session_body_marshall_int(s, maze->dim_r) == -1) {
        fprintf(stderr, "ERROR: marhsall dim_r\n");
        return -1;
    }
    if (proto_session_body_marshall_int(s, maze->num_t1_players) == -1) {
        fprintf(stderr, "ERROR: marhsall num_t1_players\n");
        return -1;
    }
    if (proto_session_body_marshall_int(s, maze->num_t2_players) == -1) {
        fprintf(stderr, "ERROR: marhsall num_t2_players\n");
        return -1;
    }

    if (wrap_item(maze->t1_flag, s) == -1) {
        fprintf(stderr, "ERROR: marhsall t1_flag\n");
        return -1;
    }
    if (wrap_item(maze->t2_flag, s) == -1) {
        fprintf(stderr, "ERROR: marhsall t2_flag\n");
        return -1;
    }
    if (wrap_item(maze->t1_jack, s) == -1) {
        fprintf(stderr, "ERROR: marhsall t1_jack\n");
        return -1;
    }
    if (wrap_item(maze->t2_jack, s) == -1) {
        fprintf(stderr, "ERROR: marhsall t2_jack\n");
        return -1;
    }

    int i;
    for (i = 0; i < (maze->num_t1_players + maze->num_t2_players); i++) {
        if (wrap_player((maze->players[i]), s) == -1) {
            fprintf(stderr, "ERROR: marhsall player_t\n");
            return -1;
        }
    }

    return 1;
}
Ejemplo n.º 15
0
extern int object_marshall(Proto_Session *s, Object * object)
{
  int rc = proto_session_body_marshall_int(s, object->type);
  if (rc != 1) return rc;

  rc = proto_session_body_marshall_int(s, object->x);
  if (rc != 1) return rc;

  rc = proto_session_body_marshall_int(s, object->y);
  if (rc != 1) return rc;

  rc = proto_session_body_marshall_int(s, object->team);
  if (rc != 1) return rc;

  rc = proto_session_body_marshall_int(s, object->visible);
  return rc;
}
Ejemplo n.º 16
0
extern int
proto_session_body_marshall_hammer(Proto_Session *s, Hammer *h)
{
  //printf("Got into marshall_hammer\n");
  int rc = -1;
  printf("Received hammerID = %d\n", h->hammerID);
  if (proto_session_body_marshall_int(s, h->hammerID) != -1)
  {
    printf("Marshalled hammerID %d\n", h->hammerID);
    rc = proto_session_body_marshall_int(s, h->uses);
    if (rc != -1)
    {
      printf("Marshalled %d uses\n", h->uses);
    }
  }
  return rc;

}
Ejemplo n.º 17
0
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;
}
Ejemplo n.º 18
0
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);
}
Ejemplo n.º 19
0
extern int
proto_session_body_marshall_objectmap(Proto_Session *s, ObjectMap *o){
  int dim = o->dim;

  proto_session_body_marshall_int(s,dim);

  if (s && ((s->slen + sizeof(ObjectMap) + sizeof(Cell)*dim*dim) < PROTO_SESSION_BUF_SIZE)){
    memcpy(s->sbuf + s->slen, o, sizeof(ObjectMap));
    memcpy(s->sbuf + s->slen + sizeof(ObjectMap), o->objects, sizeof(Cell)*dim*dim);
    s->slen += (sizeof(ObjectMap) + sizeof(Cell)*dim*dim);
    return 1;
  }
  return -1;
}
Ejemplo n.º 20
0
extern int  
proto_session_body_marshall_gamestate(Proto_Session *s, Gamestate *g){
  int ncell, nplayers;
  int i = 0;

  if(proto_session_body_marshall_int(s,g->numCells)<0){
    return -1;
  }
  if(proto_session_body_marshall_int(s,g->numPlayers)<0){
    return -1;
  }

  ncell = g->numCells;
  while(ncell){
    if(g->clist[i] != NULL){
      if(proto_session_body_marshall_cell(s,g->clist[i])<0){
	return -1;
      }
      ncell--;
    }
    i++;
  }

  i = 0;
  nplayers = g->numPlayers;
  while(nplayers){
    if(g->plist[i] != NULL){
      if(proto_session_body_marshall_player(s,g->plist[i])<0){
	return -1;
      }
      nplayers--;
    }
    i++;
  }
  
  return 1;
}
Ejemplo n.º 21
0
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;
}
Ejemplo n.º 22
0
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;
}
Ejemplo n.º 23
0
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;
}
Ejemplo n.º 24
0
extern int
maze_marshall_board(Proto_Session *s)
{
  int rc = proto_session_body_marshall_int(s, Board.size);
  if (rc != 1) return rc;

  int x,y;

  for (y = 0; y < Board.size; y++)
  {
    for (x = 0; x < Board.size; x++)
    {
      rc = maze_marshall_cell(s, Board.cells[y][x]);
      if (rc != 1) return rc;
    }
  }

  return rc;
}
Ejemplo n.º 25
0
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;
}
Ejemplo n.º 26
0
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;
}
Ejemplo n.º 27
0
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;
}
Ejemplo n.º 28
0
extern int 
maze_marshall_row(Proto_Session *s, int section) {

  if (section == 0) {
    int rc = proto_session_body_marshall_int(s, Board.size);
    if (rc != 1) return rc;
  }

  int rc = 1;

  int x,y;

  //printf("Marshalling rows %d through %d\n",(section*20),((section+1)*20));
  for (y = section*20; y < (section+1)*20 && y < Board.size; y++) {
    //printf("Marshalling row %d\n",y);
    for (x = 0; x < Board.size; x++) {
      rc = maze_marshall_cell(s, Board.cells[y][x]);
      if(rc != 1) return rc;
    }
  }

  return rc;

}
Ejemplo n.º 29
0
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;
}