예제 #1
0
static int
proto_server_pickup_handler(Proto_Session *s){
  int id,rc,ret;
  Proto_Msg_Hdr sh;
  Proto_Msg_Hdr rh;
  Player* p;
  Proto_Session *us;

  bzero(&sh, sizeof(sh));
  bzero(&rh, sizeof(rh));

  sh.type = proto_session_hdr_unmarshall_type(s);
  sh.type += PROTO_MT_REP_BASE_RESERVED_FIRST;

  proto_session_hdr_unmarshall(s, &rh);
  id = rh.pstate.v0.raw;
  p = gamestate_get_player(Server_Gamestate,id);
  
  ret = player_obj_pickup(p,Server_ObjectMap,Server_Gamestate);
  sh.pstate.v0.raw = id;//same header for both reply and update
  sh.pstate.v1.raw = ret; //sending back the actual thing picked up
  proto_session_hdr_marshall(s, &sh);
  rc = proto_session_send_msg(s,1);
  
  if(ret >= 0){
    us = proto_server_event_session();
    sh.type = PROTO_MT_EVENT_BASE_MOVE;
    proto_session_hdr_marshall(us,&sh);
    proto_server_post_event();
  } 
  return rc;
}
예제 #2
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;
}
예제 #3
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;
}
예제 #4
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;
}
예제 #5
0
// 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;
}
예제 #6
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;
}
예제 #7
0
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;
}
예제 #8
0
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;
}
예제 #9
0
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;
}
예제 #10
0
// Broadcasts changes in the game to Subscribers
int
doUpdateClientsGame(int updateMapVersion)
{
    Proto_Session *s;
    Proto_Msg_Hdr hdr;

    if (proto_debug())
        fprintf(stderr, "doUpdateClientsGame called\n"); 

    bzero(&hdr, sizeof(hdr));

    s = proto_server_event_session();
    // set sver if nescesary
    hdr.type = PROTO_MT_EVENT_BASE_UPDATE;
    // pthread_spin_lock(&server_data_spinlock);
    hdr.sver.raw = server_gameData.version;
    hdr.gstate.v0.raw = server_gameData.state;
    // pthread_spin_unlock(&server_data_spinlock);
    proto_session_hdr_marshall(s, &hdr);

    // test
    proto_server_test_msg(s);
    proto_server_post_event();
    return 1;
}
예제 #11
0
static void
marshall_mtonly(Proto_Session *s, Proto_Msg_Types mt) {
  Proto_Msg_Hdr h;
  
  bzero(&h, sizeof(h));
  h.type = mt;
  proto_session_hdr_marshall(s, &h);
}
예제 #12
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;
}
예제 #13
0
int
doUpdateClients(void) {
    Proto_Session *s;
    Proto_Msg_Hdr hdr;

    s = proto_server_event_session();
    hdr.type = PROTO_MT_EVENT_BASE_UPDATE;
    proto_session_hdr_marshall(s, &hdr);
    proto_server_post();
    return 1;
}
예제 #14
0
static int proto_server_mt_cinfo_handler(Proto_Session *s)
{
    int in_x, in_y, rc;
    char cell, team, occupied;
    Proto_Msg_Hdr h;

    if (proto_debug()) {
        fprintf(stderr, "proto_server_mt_cinfo_handler: invoked for session:\n");
        //proto_session_dump(s);
    }

    // read msg here
    proto_session_body_unmarshall_int(s, 0, &in_x);
    proto_session_body_unmarshall_int(s, sizeof(int), &in_y);

    if (proto_debug()) 
        fprintf(stderr, "proto_server_mt_numjail_handler: requested cellinfo( %d, %d )\n", in_x, in_y);
    
    //cell = '#';//cell = (char)0xCC; // call mze.c numjail(teamNo) func
    //team = '1';//team = (char)0xDD;
    //occupied = 'Y';//occupied = (char)0xEE;

    cell = get_cell_type( in_x, in_y );
    team = get_cell_team( in_x, in_y );
    occupied = is_cell_occupied( in_x, in_y );
    //findCInfo(in_x, in_y);

    if (proto_debug())
        fprintf(stderr, "proto_server_mt_numjail_handler: maze.c::func returned\n"
                "     cell='%c', team='%c', occupied='%c'\n", cell, team, occupied);

    // 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_char(s, cell) < 0 )
        fprintf(stderr, "proto_server_mt_cinfo_handler: "
                "proto_session_body_marshall_bytes failed\n");

    if (proto_session_body_marshall_char(s, team) < 0 )
        fprintf(stderr, "proto_server_mt_cinfo_handler: "
                "proto_session_body_marshall_bytes failed\n");

    if (proto_session_body_marshall_char(s, occupied) < 0 )
        fprintf(stderr, "proto_server_mt_cinfo_handler: "
                "proto_session_body_marshall_bytes failed\n");

    rc = proto_session_send_msg(s, 1);
    // TODO: return failed if an error occours
    return rc;
}
예제 #15
0
//send back an ack reply then close session
static int
proto_server_goodbye_handler(Proto_Session *s){
  Proto_Msg_Hdr h;
  Proto_Session *us;
  Player p;
  int rc=1;
  
  proto_session_body_unmarshall_player(s,0,&p);

  bzero(&h, sizeof(h));
  h.type = PROTO_MT_REP_BASE_GOODBYE;
  proto_session_hdr_marshall(s, &h);

  rc = proto_session_send_msg(s,1);

  if(p.team == TEAM1){
    numPlayers1--;
    if(p.state == SAFE)
      numplayershome1--;
  } else{
    numPlayers2--;
    if(p.state == SAFE)
      numplayershome2--;
  }
  gamestate_remove_player(Server_Gamestate,p.id);
  pidx = player_find_next_id(Server_Gamestate->plist);
  if(pidx<0)
    gamefull = 1;
  else
    gamefull = 0;

  us = proto_server_event_session();
  h.type = PROTO_MT_EVENT_BASE_PLAYER_QUIT;
  proto_session_hdr_marshall(us,&h);
  proto_session_body_marshall_player(us, &p);
  proto_server_post_event();

  return rc;
}
예제 #16
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);
}
예제 #17
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;
}
예제 #18
0
//This function is used for RPC's that do not contain anything in the body
//Parameter: Proto_Msg_Hdr ch - that contains all necessary information for the RPC
//           Proto_Client_Handle *h - Handle to the Proto_Client 
//Returns:   Return Code - as specified in Game_Error_Types in types.h
extern int 
do_no_body_rpc(Proto_Client_Handle ch, Proto_Msg_Hdr * h)
{
  int rc;
  Proto_Session *s;
  Proto_Client *c = ch;
  
  s = &(c->rpc_session);
  proto_session_hdr_marshall(s,h);
  
  rc = proto_session_send_msg(s,1);
  rc = proto_session_rcv_msg(s);
  
  if(rc<=0) rc =-1;

  return rc;
}
예제 #19
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;
}
예제 #20
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;
}
예제 #21
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;
}
예제 #22
0
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;
}
예제 #23
0
//This function is used for RPC's that do not contain anything in the body
//Parameter: Proto_Msg_Hdr *h - that contains all necessary information for the RPC
//           Proto_Client_Handle ch - Handle to the Proto_Client 
//           Pos current - current position of the player
//           Pos next - next position that the players wants to move to
//Returns:   Return Code - as specified in Game_Error_Types in types.h
extern int 
do_action_request_rpc(Proto_Client_Handle ch, Proto_Msg_Hdr * hdr,Pos current, Pos next)
{
  int rc;
  Proto_Session *s;
  Proto_Client *c = ch;
  
  s = &(c->rpc_session);
  proto_session_hdr_marshall(s,hdr);
  if( hdr->gstate.v1.raw >= ACTION_MOVE && 
      hdr->gstate.v1.raw <= ACTION_PICKUP_SHOVEL)
  {
      proto_session_body_marshall_bytes(s,sizeof(Pos),(char*)&current);
      proto_session_body_marshall_bytes(s,sizeof(Pos),(char*)&next);
  }
  
  rc = proto_session_send_msg(s,1);
  rc = proto_session_rcv_msg(s);
  
  if(rc<=0) rc =-1;
  return rc;
}
예제 #24
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;
}
예제 #25
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;
}
예제 #26
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;
}
예제 #27
0
static int
proto_server_hello_handler(Proto_Session *s){
  int rc = 1;
  int x;
  Player* p;
  Proto_Msg_Hdr sh;
  Proto_Session *us;

  p = player_create();

  bzero(&sh, sizeof(sh));
  
  sh.type = proto_session_hdr_unmarshall_type(s);
  sh.type += PROTO_MT_REP_BASE_RESERVED_FIRST;

  x = player_find_empty_home(p,nextTeam, Server_ObjectMap, pidx);
  //  x = !gamefull;
  if(x){
    gamestate_add_player(Server_Gamestate,p);
    printf("New player joining:\n");
    printf("Location: %d,%d\n", p->pcell->x, p->pcell->y);
    player_dump(p);
    sh.pstate.v0.raw = 1;
    sh.pstate.v1.raw = p->id;
    gamestate_dump(Server_Gamestate);
    proto_session_body_marshall_gamestate(s,Server_Gamestate);
    //    proto_session_body_marshall_map(s,Server_Map);
    if(nextTeam == TEAM1){
      nextTeam = TEAM2;
      numPlayers1++;
    } else{
      nextTeam = TEAM1;
      numPlayers2++;
    }
    pidx = player_find_next_id(Server_Gamestate->plist);
    if(pidx<0)
      gamefull = 1;

    s->player = p;
  } else {
    sh.pstate.v0.raw = 0;
  }

  proto_session_hdr_marshall(s, &sh);
  
  //  proto_dump_msghdr(&(s->shdr));
  rc = proto_session_send_msg(s,1);
  
  if(x){ //if new player joining, tell everyone    
    us = proto_server_event_session();
    bzero(&sh, sizeof(sh));
    sh.type = PROTO_MT_EVENT_BASE_PLAYER_JOIN;
    sh.pstate.v0.raw = p->id;
    sh.pstate.v1.raw = p->pcell->x;
    sh.pstate.v2.raw = p->pcell->y;
    proto_session_hdr_marshall(us, &sh);
    proto_session_body_marshall_player(us,p);
    proto_server_post_event();
  }

  return rc;
}
예제 #28
0
static int
proto_server_move_handler(Proto_Session *s){
  int i,rx,ry,id,rc, winner;
  dir_t dir;
  Cell *cell = malloc(sizeof(Cell));
  Proto_Msg_Hdr sh;
  Proto_Msg_Hdr rh;
  Player* p;
  Proto_Session *us;
  int valid;
  int flagindex;
  object_t flag = -1;
  Proto_Session *fs;
  bzero(&sh, sizeof(sh));
  bzero(&rh, sizeof(rh));

  sh.type = proto_session_hdr_unmarshall_type(s);
  sh.type += PROTO_MT_REP_BASE_RESERVED_FIRST;

  proto_session_hdr_unmarshall(s, &rh);
  id = rh.pstate.v0.raw;
  dir = rh.pstate.v1.raw;
  p = gamestate_get_player(Server_Gamestate,id);
  valid = 0;
  flagindex = -1;
  valid = player_move(dir,p,Server_ObjectMap, Server_Gamestate);
  //  printf("Valid bit%d\n", valid);
  if (valid>0) {
    sh.pstate.v3.raw = 1;
    printf("Player %d is moving to (%d,%d)\n",id,p->pcell->x,p->pcell->y);
    if(!DEBUG_MAP){
      flagindex = objectmap_flag_visible(p,Server_ObjectMap);
      
      if(flagindex>=0)
	flag = Server_ObjectMap->objects[flagindex]->obj;
      
      if(flag == FLAG1){
	if(!flag1found)
	  flag1found = 1;
	else
	  flagindex = -1;
      }
      if(flag == FLAG2){
	if(!flag2found)
	  flag2found = 1;
	else
	  flagindex = -1;
      }
    }
  } else {
    sh.pstate.v3.raw = 0;    
    printf("Player %d attemped an invalid move\n",id);
  }
  sh.pstate.v0.raw = p->id;
  sh.pstate.v1.raw = p->pcell->x;
  sh.pstate.v2.raw = p->pcell->y;
 
  proto_session_hdr_marshall(s, &sh);
  
  rc = proto_session_send_msg(s,1);
  
  if(valid){
    us = proto_server_event_session();
    sh.type = PROTO_MT_EVENT_BASE_MOVE;
    proto_session_hdr_marshall(us,&sh);
    proto_server_post_event();
  }
  
  bzero(&sh, sizeof(sh));
  if(flagindex>=0){
    fs = proto_server_event_session();
    sh.type = PROTO_MT_EVENT_BASE_FLAG;
    
    //gstate.v0 holds the flag itself
    //gstate.v1 holds the index in the objectmap where that flag is located
    sh.gstate.v0.raw = Server_ObjectMap->objects[flagindex]->obj;
    sh.gstate.v1.raw = flagindex;
    proto_session_hdr_marshall(fs,&sh);
    proto_server_post_event();
  }
  
  bzero(&sh, sizeof(sh));
  if((winner = gamestate_team_wins()) >= 0){
    sh.type = PROTO_MT_EVENT_BASE_WIN;
    sh.gstate.v0.raw = winner;
    proto_session_hdr_marshall(fs,&sh);
    proto_server_post_event();
  }

  return rc;
}