Esempio n. 1
0
extern void get_pos(Proto_Client_Handle ch, Pos* current)
{
  Proto_Session *s;
  Proto_Client *c = ch;
  s = &(c->rpc_session);
  proto_session_body_unmarshall_bytes(s,0,sizeof(Pos),(char*)current);
}
Esempio n. 2
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;
}