Ignore:
Timestamp:
Mar 18, 2008, 9:36:56 PM (14 years ago)
Author:
Sam Hocevar
Message:
  • Get rid of ugly tabs and trailing spaces everywhere.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • abuse/trunk/src/net/gserver.cpp

    r120 r124  
    4040game_server::game_server()
    4141{
    42         player_list = NULL;
    43         waiting_server_input = 1;
    44         reload_state = 0;
     42    player_list = NULL;
     43    waiting_server_input = 1;
     44    reload_state = 0;
    4545}
    4646
    4747int game_server::total_players()
    4848{
    49         player_client *fl = player_list;
    50         int total = 1;
    51         for( ; fl; fl = fl->next)
    52         {
    53                 total++;
    54         }
    55         return total;
     49    player_client *fl = player_list;
     50    int total = 1;
     51    for( ; fl; fl = fl->next)
     52    {
     53        total++;
     54    }
     55    return total;
    5656}
    5757
     
    7070      sprintf(msg,symbol_str("min_wait"),main_net_cfg->min_players-total_players());
    7171      stat=wm->new_window(100,50,-1,-1,new info_field(0, 0, ID_NULL,msg,
    72                                        new button(0, wm->font()->height()*2, ID_CANCEL,symbol_str("cancel_button"),NULL)  ));
     72                       new button(0, wm->font()->height()*2, ID_CANCEL,symbol_str("cancel_button"),NULL)  ));
    7373      wm->flush_screen();
    7474      last_count=total_players();
     
    7777    if (wm->event_waiting())
    7878    {
    79       do { wm->get_event(ev); }  while (ev.type==EV_MOUSE_MOVE && wm->event_waiting()); 
     79      do { wm->get_event(ev); }  while (ev.type==EV_MOUSE_MOVE && wm->event_waiting());
    8080      wm->flush_screen();
    8181      if (ev.type==EV_MESSAGE && ev.message.id==ID_CANCEL)
    8282        abort=1;
    8383    }
    84    
     84
    8585    service_net_request();
    8686  }
     
    8888  {
    8989    wm->close_window(stat);
    90     wm->flush_screen(); 
    91   }
    92 }
    93  
     90    wm->flush_screen();
     91  }
     92}
     93
    9494game_server::player_client::~player_client()
    9595{
     
    118118      if (c->delete_me())
    119119      {
    120         base->packet.write_uint8(SCMD_DELETE_CLIENT);
    121         base->packet.write_uint8(c->client_id);
    122         if (c->wait_reload())
    123         {
    124           c->set_wait_reload(0);
    125           check_reload_wait();
    126         }
    127 
    128         if (last) last->next=c->next;
    129         else player_list=c->next;
    130         player_client *d=c;
    131         c=c->next;
    132         delete d;
    133       } else 
    134       {
    135         last=c;
    136         c=c->next;
     120    base->packet.write_uint8(SCMD_DELETE_CLIENT);
     121    base->packet.write_uint8(c->client_id);
     122    if (c->wait_reload())
     123    {
     124      c->set_wait_reload(0);
     125      check_reload_wait();
     126    }
     127
     128    if (last) last->next=c->next;
     129    else player_list=c->next;
     130    player_client *d=c;
     131    c=c->next;
     132    delete d;
     133      } else
     134      {
     135    last=c;
     136    c=c->next;
    137137      }
    138138    }
     
    140140
    141141  if (got_all)    // see if we have input from everyone, if so send it out
    142   {   
     142  {
    143143    base->packet.calc_checksum();
    144144
     
    147147      if (c->has_joined())
    148148      {
    149         c->set_wait_input(1);     
    150         game_sock->write(base->packet.data,base->packet.packet_size()+base->packet.packet_prefix_size(),c->data_address);
     149    c->set_wait_input(1);
     150    game_sock->write(base->packet.data,base->packet.packet_size()+base->packet.packet_prefix_size(),c->data_address);
    151151
    152152      }
     
    156156    game_sock->read_unselectable();    // don't listen to this socket until we are prepared to read next tick's game data
    157157    waiting_server_input=1;
    158   } 
     158  }
    159159}
    160160
     
    181181{
    182182  player_client *d=player_list;
    183   for (;d;d=d->next) 
    184    if (d->wait_reload()) return ;    // we are still waiting for someone to reload the game 
     183  for (;d;d=d->next)
     184   if (d->wait_reload()) return ;    // we are still waiting for someone to reload the game
    185185  base->wait_reload=0;
    186186}
     
    198198
    199199      fprintf(stderr,"request for resend tick %d (game cur=%d, pack=%d, last=%d)\n",
    200               tick,base->current_tick,base->packet.tick_received(),base->last_packet.tick_received());
     200          tick,base->current_tick,base->packet.tick_received(),base->last_packet.tick_received());
    201201
    202202      if (tick==base->last_packet.tick_received())
    203203      {
    204         net_packet *pack=&base->last_packet;     
    205         game_sock->write(pack->data,pack->packet_size()+pack->packet_prefix_size(),c->data_address);
     204    net_packet *pack=&base->last_packet;
     205    game_sock->write(pack->data,pack->packet_size()+pack->packet_prefix_size(),c->data_address);
    206206      }
    207207      return 1;
     
    211211      if (reload_state)   // already in reload state, notify client ok to start reloading
    212212      {
    213         if (c->comm->write(&cmd,1)!=1) 
    214           c->set_delete_me(1);
     213        if (c->comm->write(&cmd,1)!=1)
     214      c->set_delete_me(1);
    215215      } else c->set_need_reload_start_ok(1);
    216216      return 1;
     
    252252      if (bytes_received==use->packet_size()+use->packet_prefix_size())
    253253      {
    254         uint16_t rec_crc=use->get_checksum();
    255         if (rec_crc==use->calc_checksum())
    256         {
    257           player_client *f=player_list,*found=NULL;
    258           for (;!found &&f;f=f->next)
    259           if (f->has_joined() && from->equal(f->data_address))
    260             found=f;
    261           if (found)
    262           {
    263             if (base->current_tick==use->tick_received())          
    264             {
    265               if (prot->debug_level(net_protocol::DB_MINOR_EVENT))
    266                 fprintf(stderr,"(got data from %d)",found->client_id);
    267 
    268 //            fprintf(stderr,"(got packet %d)\n",use->tick_received());
    269 //            { time_marker now,start; while (now.diff_time(&start)<5.0) now.get_time(); }
    270 
    271               if (base->input_state!=INPUT_RELOAD)
    272                 add_client_input((char *)use->packet_data(),use->packet_size(),found);
    273 
    274             }
    275             else if (use->tick_received()==base->last_packet.tick_received())
    276             {
    277               if (prot->debug_level(net_protocol::DB_IMPORTANT_EVENT))
    278                 fprintf(stderr,"(sending old %d)\n",use->tick_received());           
    279 
    280               // if they are sending stale data we need to send them the last packet so they can catchup
    281               net_packet *pack=&base->last_packet;     
    282               game_sock->write(pack->data,pack->packet_size()+pack->packet_prefix_size(),found->data_address);
    283 
    284             } else if (prot->debug_level(net_protocol::DB_MAJOR_EVENT))
    285               fprintf(stderr,"received stale packet (got %d, expected %d)\n",use->tick_received(),base->current_tick);
    286 
    287           } else
    288           {
    289             if (prot->debug_level(net_protocol::DB_MAJOR_EVENT))
    290             {
    291               fprintf(stderr,"received data from unknown client\n");
    292               printf("from address "); from->print();
    293               printf(" first addr "); player_list->data_address->print(); printf("\n");
    294             }
    295           }
    296 
    297         } else fprintf(stderr,"received packet with bad checksum\n");
     254    uint16_t rec_crc=use->get_checksum();
     255    if (rec_crc==use->calc_checksum())
     256    {
     257      player_client *f=player_list,*found=NULL;
     258      for (;!found &&f;f=f->next)
     259      if (f->has_joined() && from->equal(f->data_address))
     260        found=f;
     261      if (found)
     262      {
     263        if (base->current_tick==use->tick_received())     
     264        {
     265          if (prot->debug_level(net_protocol::DB_MINOR_EVENT))
     266            fprintf(stderr,"(got data from %d)",found->client_id);
     267
     268//          fprintf(stderr,"(got packet %d)\n",use->tick_received());
     269//          { time_marker now,start; while (now.diff_time(&start)<5.0) now.get_time(); }
     270
     271          if (base->input_state!=INPUT_RELOAD)
     272            add_client_input((char *)use->packet_data(),use->packet_size(),found);
     273
     274        }
     275        else if (use->tick_received()==base->last_packet.tick_received())
     276        {
     277          if (prot->debug_level(net_protocol::DB_IMPORTANT_EVENT))
     278            fprintf(stderr,"(sending old %d)\n",use->tick_received());   
     279
     280          // if they are sending stale data we need to send them the last packet so they can catchup
     281          net_packet *pack=&base->last_packet;
     282          game_sock->write(pack->data,pack->packet_size()+pack->packet_prefix_size(),found->data_address);
     283
     284        } else if (prot->debug_level(net_protocol::DB_MAJOR_EVENT))
     285          fprintf(stderr,"received stale packet (got %d, expected %d)\n",use->tick_received(),base->current_tick);
     286
     287      } else
     288      {
     289        if (prot->debug_level(net_protocol::DB_MAJOR_EVENT))
     290        {
     291          fprintf(stderr,"received data from unknown client\n");
     292          printf("from address "); from->print();
     293          printf(" first addr "); player_list->data_address->print(); printf("\n");
     294        }
     295      }
     296
     297    } else fprintf(stderr,"received packet with bad checksum\n");
    298298      } else fprintf(stderr,"received incomplete packet\n");
    299299    } else if (!from)
     
    309309  /**************************       Any client with commands?       **************************/
    310310  player_client *c;
    311   for (c=player_list;c;c=c->next) 
     311  for (c=player_list;c;c=c->next)
    312312    if (c->comm->error() || (c->comm->ready_to_read() && !process_client_command(c)))
    313313    {
     
    316316    }
    317317    else ret=1;
    318  
     318
    319319  return 1;
    320320}
     
    330330
    331331int game_server::end_reload(int disconnect)  // notify evryone you've reloaded the level (at server request)
    332 { 
     332{
    333333  player_client *c=player_list;
    334334  prot->select(0);
    335335
    336   for (;c;c=c->next) 
    337     if (!c->delete_me() && c->wait_reload()) 
     336  for (;c;c=c->next)
     337    if (!c->delete_me() && c->wait_reload())
    338338    {
    339339      if (disconnect)
     
    342342    }
    343343
    344   for (c=player_list;c;c=c->next)   
     344  for (c=player_list;c;c=c->next)
    345345    c->set_has_joined(1);
    346346  reload_state=0;
    347  
     347
    348348  return 1;
    349349}
     
    355355  prot->select(0);
    356356
    357   for (;c;c=c->next) 
     357  for (;c;c=c->next)
    358358  {
    359359    if (!c->delete_me() && c->need_reload_start_ok())    // if the client is already waiting for reload state to start, send ok
     
    374374  if (!client_id) return 1;
    375375  for (;c;c=c->next) if (c->client_id==client_id) return 1;
    376   return 0;   
     376  return 0;
    377377}
    378378
    379379int game_server::add_client(int type, net_socket *sock, net_address *from)
    380380{
    381         if( type == CLIENT_ABUSE )
    382         {
    383                 if( total_players() >= main_net_cfg->max_players )
    384                 {
    385                         uint8_t too_many = 2;
    386                         sock->write( &too_many, 1 );
    387                         return 0;
    388                 }
    389 
    390                 uint8_t reg = 1; // Of course the game is registered
    391                 if( sock->write( &reg, 1 ) != 1 )
    392                         return 0;
    393 
    394                 uint16_t our_port = lstl( main_net_cfg->port + 1 ), cport;
    395                 char name[256];
    396                 uint8_t len;
    397                 int16_t nkills=lstl(main_net_cfg->kills);
    398 
    399                 if( sock->read(&len,1)!=1 ||
    400                         sock->read(name,len)!=len ||
    401                         sock->read(&cport,2)!=2 ||
    402                         sock->write(&our_port,2)!=2 ||
    403                         sock->write(&nkills,2)!=2)
    404                 {
    405                         return 0;
    406                 }
    407 
    408                 cport=lstl(cport);
    409 
    410                 int f = -1, i;
    411                 for( i = 0; f == -1 && i < MAX_JOINERS; i++ )
    412                 {
    413                         if( !isa_client(i) )
    414                         {
    415                                 f = i;
    416                                 join_struct *j=base->join_list;
    417                                 for( ; j; j = j->next )
    418                                 {
    419                                         if( j->client_id == i )
    420                                                 f = -1;
    421                                 }
    422                         }
    423                 }
    424 
    425                 if( f == -1 )
    426                         return 0;
    427 
    428                 from->set_port( cport );
    429 
    430                 uint16_t client_id = lstl( f );
    431                 if( sock->write( &client_id, 2 ) != 2 )
    432                 {
    433                         return 0;
    434                 }
    435                 client_id=f;
    436 
    437                 join_array[client_id].next = base->join_list;
    438                 base->join_list = &join_array[client_id];
    439                 join_array[client_id].client_id = client_id;
    440                 strcpy( join_array[client_id].name, name );
    441                 player_list = new player_client( f, sock, from, player_list );
    442 
    443                 return 1;
    444         }
    445         else
    446         {
    447                 return 0;
    448         }
     381    if( type == CLIENT_ABUSE )
     382    {
     383        if( total_players() >= main_net_cfg->max_players )
     384        {
     385            uint8_t too_many = 2;
     386            sock->write( &too_many, 1 );
     387            return 0;
     388        }
     389
     390        uint8_t reg = 1; // Of course the game is registered
     391        if( sock->write( &reg, 1 ) != 1 )
     392            return 0;
     393
     394        uint16_t our_port = lstl( main_net_cfg->port + 1 ), cport;
     395        char name[256];
     396        uint8_t len;
     397        int16_t nkills=lstl(main_net_cfg->kills);
     398
     399        if( sock->read(&len,1)!=1 ||
     400            sock->read(name,len)!=len ||
     401            sock->read(&cport,2)!=2 ||
     402            sock->write(&our_port,2)!=2 ||
     403            sock->write(&nkills,2)!=2)
     404        {
     405            return 0;
     406        }
     407
     408        cport=lstl(cport);
     409
     410        int f = -1, i;
     411        for( i = 0; f == -1 && i < MAX_JOINERS; i++ )
     412        {
     413            if( !isa_client(i) )
     414            {
     415                f = i;
     416                join_struct *j=base->join_list;
     417                for( ; j; j = j->next )
     418                {
     419                    if( j->client_id == i )
     420                        f = -1;
     421                }
     422            }
     423        }
     424
     425        if( f == -1 )
     426            return 0;
     427
     428        from->set_port( cport );
     429
     430        uint16_t client_id = lstl( f );
     431        if( sock->write( &client_id, 2 ) != 2 )
     432        {
     433            return 0;
     434        }
     435        client_id=f;
     436
     437        join_array[client_id].next = base->join_list;
     438        base->join_list = &join_array[client_id];
     439        join_array[client_id].client_id = client_id;
     440        strcpy( join_array[client_id].name, name );
     441        player_list = new player_client( f, sock, from, player_list );
     442
     443        return 1;
     444    }
     445    else
     446    {
     447        return 0;
     448    }
    449449}
    450450
     
    455455    if (c->wait_input())
    456456      c->set_delete_me(1);
    457   check_collection_complete(); 
     457  check_collection_complete();
    458458  return 1;
    459459}
     
    475475game_server::~game_server()
    476476{
    477         quit();
    478 }
    479 
     477    quit();
     478}
     479
Note: See TracChangeset for help on using the changeset viewer.