Ignore:
Timestamp:
Mar 18, 2008, 9:36:56 PM (11 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/netdrv.cpp

    r112 r124  
    5252void die(int why)
    5353#endif
    54 { 
     54{
    5555  fprintf(stderr,"dieing\n");
    5656  if (driver) { delete driver; driver=NULL; }
     
    8888  if (shm_seg_id!=-1)
    8989    shmctl(shm_seg_id,IPC_RMID,NULL);
    90   if (shm_addr!=(void *)-1) 
     90  if (shm_addr!=(void *)-1)
    9191  {
    9292    shmdt((char *)shm_addr);
     
    110110  driver=this;
    111111  int catch_sigs[]={SIGHUP,SIGINT,SIGQUIT,SIGILL,SIGABRT,
    112                     SIGIOT,SIGFPE,SIGKILL,SIGUSR1,SIGSEGV,
    113                     SIGUSR2,SIGPIPE,SIGTERM,SIGCHLD,
    114                     SIGCONT,SIGSTOP,SIGTSTP,SIGTTIN,SIGTTOU,-1};
     112            SIGIOT,SIGFPE,SIGKILL,SIGUSR1,SIGSEGV,
     113            SIGUSR2,SIGPIPE,SIGTERM,SIGCHLD,
     114            SIGCONT,SIGSTOP,SIGTSTP,SIGTTIN,SIGTTOU,-1};
    115115
    116116  int i;
     
    128128
    129129  shm_addr=shmat(shm_seg_id,NULL,0);  // attach as read/write
    130   if (shm_addr==(void *)-1) 
     130  if (shm_addr==(void *)-1)
    131131    mdie("could not attach shm seg");
    132132
     
    153153    comm_failed();
    154154
    155  
     155
    156156  if (shmctl(shm_seg_id,IPC_RMID,NULL))  // remove the shm id
    157157    mdie("could not remove shm id");
     
    178178  int driver_out_fd=open(DOUT_NAME,O_RDWR);  // open the pipe
    179179  if (driver_out_fd<0)
    180   { perror(DOUT_NAME); 
     180  { perror(DOUT_NAME);
    181181    exit(1);
    182182  }
     
    184184  int driver_in_fd=open(DIN_NAME,O_RDWR);
    185185  if (driver_in_fd<0)
    186   { 
     186  {
    187187    close(driver_out_fd);
    188     perror(DIN_NAME); 
     188    perror(DIN_NAME);
    189189    exit(1);
    190190  }
     
    193193  in->read_selectable();
    194194  out=new unix_fd(driver_out_fd);
    195  
    196   if (in->read(&reg,sizeof(reg))!=sizeof(reg)) 
     195
     196  if (in->read(&reg,sizeof(reg))!=sizeof(reg))
    197197    mdie("unable to registration from engine");
    198198}
    199199
    200 net_driver::net_driver(int argc, char **argv, int comm_port, int game_port, net_protocol *proto) : 
     200net_driver::net_driver(int argc, char **argv, int comm_port, int game_port, net_protocol *proto) :
    201201  comm_port(comm_port), game_port(game_port), proto(proto)
    202202{
     
    234234      if (cmd<=EGCMD_DIE)
    235235      {
    236         char *cmds[]={"open","close","read","write","seek","size","tell","setfs","crc_calced","process_lsf","request_lfs",
    237                      "equest_entry","become_server","block","reload_start","reload_end","send_input","input_missing",
    238                       "kill_slackers","die"};
    239         fprintf(stderr,"engine cmd : %s\n",cmds[cmd]);
     236    char *cmds[]={"open","close","read","write","seek","size","tell","setfs","crc_calced","process_lsf","request_lfs",
     237             "equest_entry","become_server","block","reload_start","reload_end","send_input","input_missing",
     238              "kill_slackers","die"};
     239    fprintf(stderr,"engine cmd : %s\n",cmds[cmd]);
    240240      }
    241241    }
     
    245245      case EGCMD_DIE :
    246246      {
    247         cmd=game_face->quit();
    248         if (!out->write(&cmd,1)) { mdie("could not write block ack1"); }  // send something to unblock engine
    249         mdie("received die command");
     247    cmd=game_face->quit();
     248    if (!out->write(&cmd,1)) { mdie("could not write block ack1"); }  // send something to unblock engine
     249    mdie("received die command");
    250250      } break;
    251251
    252252      case NFCMD_RELOAD_START :
    253       {     
    254         cmd=game_face->start_reload();
    255         if (!out->write(&cmd,1)) { mdie("could not write start reload ack"); }  // send something to unblock engine     
     253      {
     254    cmd=game_face->start_reload();
     255    if (!out->write(&cmd,1)) { mdie("could not write start reload ack"); }  // send something to unblock engine   
    256256      } break;
    257257
    258258      case NFCMD_RELOAD_END :
    259       {     
    260         cmd=game_face->end_reload();
    261         if (!out->write(&cmd,1)) { mdie("could not write end reload ack"); }  // send something to unblock engine       
     259      {
     260    cmd=game_face->end_reload();
     261    if (!out->write(&cmd,1)) { mdie("could not write end reload ack"); }  // send something to unblock engine   
    262262      } break;
    263263
    264264      case NFCMD_BLOCK :
    265       {     
    266         if (!out->write(&cmd,1)) { mdie("could not write block ack1"); }  // send something to unblock engine
    267         if (!in->read(&cmd,1)) { mdie("could not read block ack1"); }  // send something to block ourself
     265      {
     266    if (!out->write(&cmd,1)) { mdie("could not write block ack1"); }  // send something to unblock engine
     267    if (!in->read(&cmd,1)) { mdie("could not read block ack1"); }  // send something to block ourself
    268268      } break;
    269269
    270270      case NFCMD_INPUT_MISSING :    // try to fetch the input via a loss-less net protocol
    271271      {
    272         game_face->input_missing();
    273         if (out->write(&cmd,1)!=1) { mdie("could not write block ack1"); }  // send something to unblock engine
     272    game_face->input_missing();
     273    if (out->write(&cmd,1)!=1) { mdie("could not write block ack1"); }  // send something to unblock engine
    274274      } break;
    275275      case NFCMD_KILL_SLACKERS :
    276276      {
    277         if (!game_face->kill_slackers())
    278         {
    279           delete game_face;
    280           game_face=new game_handler();
    281         }
    282         if (out->write(&cmd,1)!=1) { mdie("could not write block ack1"); }  // send something to unblock engine
     277    if (!game_face->kill_slackers())
     278    {
     279      delete game_face;
     280      game_face=new game_handler();
     281    }
     282    if (out->write(&cmd,1)!=1) { mdie("could not write block ack1"); }  // send something to unblock engine
    283283      } break;
    284284      case NFCMD_SEND_INPUT :
    285285      {
    286         game_face->add_engine_input();
    287         if (out->write(&cmd,1)!=1) { mdie("could not write send ack1"); }  // send something to unblock engine
    288         if (in->read(&cmd,1)!=1) { mdie("could not read send ack2"); }  // read something to block ourselves for engine
     286    game_face->add_engine_input();
     287    if (out->write(&cmd,1)!=1) { mdie("could not write send ack1"); }  // send something to unblock engine
     288    if (in->read(&cmd,1)!=1) { mdie("could not read send ack2"); }    // read something to block ourselves for engine
    289289      } break;
    290290
     
    292292      case NFCMD_REQUEST_ENTRY :
    293293      {
    294         uint8_t len;
    295         char name[256];
    296         if (in->read(&len,1)!=1) { mdie("could not read server name length"); }
    297         if (in->read(name,len)!=len) { mdie("could not read server name"); }
    298         uint16_t success=join_server(name);
    299         if (out->write(&success,2)!=2) mdie("cound not send lsf read failure");     
     294    uint8_t len;
     295    char name[256];
     296    if (in->read(&len,1)!=1) { mdie("could not read server name length"); }
     297    if (in->read(name,len)!=len) { mdie("could not read server name"); }
     298    uint16_t success=join_server(name);
     299    if (out->write(&success,2)!=2) mdie("cound not send lsf read failure");
    300300      } break;
    301301      case NFCMD_BECOME_SERVER :
    302302      {
    303         cmd=become_server();   
    304         if (out->write(&cmd,1)!=1) mdie("cound not send ok");
     303    cmd=become_server();   
     304    if (out->write(&cmd,1)!=1) mdie("cound not send ok");
    305305      } break;
    306306      case NFCMD_REQUEST_LSF :
    307307      {
    308         uint8_t len;
    309         char name[256];
    310         if (in->read(&len,1)!=1) { mdie("could not read lsf name length"); }
    311         if (in->read(name,len)!=len) { mdie("could not read lsf name"); }
    312         if (!get_lsf(name))
    313         {
    314           len=0;
    315           if (out->write(&len,1)!=1) mdie("cound not send lsf read failure");
    316         } else
    317         {
    318           len=strlen(name)+1;
    319           if (out->write(&len,1)!=1) mdie("cound not send lsf name len");
    320           if (out->write(name,len)!=len) mdie("cound not send lsf name");
    321         }     
     308    uint8_t len;
     309    char name[256];
     310    if (in->read(&len,1)!=1) { mdie("could not read lsf name length"); }
     311    if (in->read(name,len)!=len) { mdie("could not read lsf name"); }
     312    if (!get_lsf(name))
     313    {
     314      len=0;
     315      if (out->write(&len,1)!=1) mdie("cound not send lsf read failure");
     316    } else
     317    {
     318      len=strlen(name)+1;
     319      if (out->write(&len,1)!=1) mdie("cound not send lsf name len");
     320      if (out->write(name,len)!=len) mdie("cound not send lsf name");
     321    }
    322322      } break;
    323323
    324324      case NFCMD_PROCESS_LSF :
    325325      {
    326         uint8_t len,name[256];
    327         if (in->read(&len,1)!=1) { mdie("could not read lsf name length"); }
    328         if (in->read(name,len)!=len) { mdie("could not read lsf name"); }
    329 
    330         while (lsf_wait_list)
    331         {
    332           lsf_waiter *c=lsf_wait_list;
    333           lsf_wait_list=lsf_wait_list->next;
    334           uint8_t status=1;
    335           c->sock->write(&len,1);
    336           c->sock->write(name,len);
    337           delete c;
    338         }
    339       } break;
    340 
    341       case NFCMD_CRCS_CALCED : 
    342       {
    343         while (crc_wait_list)
    344         {
    345           crc_waiter *c=crc_wait_list;
    346           crc_wait_list=crc_wait_list->next;
    347           uint8_t status=1;
    348           c->sock->write(&status,1);
    349           delete c;
    350         }
     326    uint8_t len,name[256];
     327    if (in->read(&len,1)!=1) { mdie("could not read lsf name length"); }
     328    if (in->read(name,len)!=len) { mdie("could not read lsf name"); }
     329
     330    while (lsf_wait_list)
     331    {
     332      lsf_waiter *c=lsf_wait_list;
     333      lsf_wait_list=lsf_wait_list->next;
     334      uint8_t status=1;
     335      c->sock->write(&len,1);
     336      c->sock->write(name,len);
     337      delete c;
     338    }
     339      } break;
     340
     341      case NFCMD_CRCS_CALCED :
     342      {
     343    while (crc_wait_list)
     344    {
     345      crc_waiter *c=crc_wait_list;
     346      crc_wait_list=crc_wait_list->next;
     347      uint8_t status=1;
     348      c->sock->write(&status,1);
     349      delete c;
     350    }
    351351      } break;
    352352
    353353      case NFCMD_SET_FS :
    354354      {
    355         uint8_t size;
    356         char sn[256];
    357         if (in->read(&size,1)!=1) mdie("could not read filename length");
    358         if (in->read(sn,size)!=size) mdie("could not read server name");
    359         fman->set_default_fs_name(sn);
    360 
    361         size=fetch_crcs(sn);  // return success
    362         if (out->write(&size,1)!=1) mdie("could not send ok to engine");
    363       } break;   
     355    uint8_t size;
     356    char sn[256];
     357    if (in->read(&size,1)!=1) mdie("could not read filename length");
     358    if (in->read(sn,size)!=size) mdie("could not read server name");
     359    fman->set_default_fs_name(sn);
     360
     361    size=fetch_crcs(sn);  // return success
     362    if (out->write(&size,1)!=1) mdie("could not send ok to engine");
     363      } break;
    364364
    365365      case NFCMD_OPEN :
    366366      {
    367         uint8_t size[2];
    368         char filename[300],mode[20],*fn;
    369         fn=filename;
    370         if (in->read(size,2)!=2  ||
    371             in->read(filename,size[0])!=size[0] ||
    372             in->read(mode,size[1])!=size[1])
    373           mdie("incomplete open command from engine");
    374        
    375         int fd=fman->rf_open_file(fn,mode);
    376         if (fd==-2)
    377         {
    378           uint8_t st[2];
    379           st[0]=NF_OPEN_LOCAL_FILE;
    380           st[1]=strlen(fn)+1;
    381           if (out->write(st,2)!=2) comm_failed();
    382           if (out->write(fn,st[1])!=st[1]) comm_failed();
    383         } else if (fd==-1)
    384         {
    385           uint8_t st=NF_OPEN_FAILED;
    386           if (out->write(&st,1)!=1) comm_failed();
    387         } else
    388         {
    389           uint8_t st=NF_OPEN_REMOTE_FILE;
    390           if (out->write(&st,1)!=1) comm_failed();     
    391           if (out->write(&fd,sizeof(fd))!=sizeof(fd)) comm_failed();   
    392         }
     367    uint8_t size[2];
     368    char filename[300],mode[20],*fn;
     369    fn=filename;
     370    if (in->read(size,2)!=2  ||
     371        in->read(filename,size[0])!=size[0] ||
     372        in->read(mode,size[1])!=size[1])
     373      mdie("incomplete open command from engine");
     374   
     375    int fd=fman->rf_open_file(fn,mode);
     376    if (fd==-2)
     377    {
     378      uint8_t st[2];
     379      st[0]=NF_OPEN_LOCAL_FILE;
     380      st[1]=strlen(fn)+1;
     381      if (out->write(st,2)!=2) comm_failed();
     382      if (out->write(fn,st[1])!=st[1]) comm_failed();
     383    } else if (fd==-1)
     384    {
     385      uint8_t st=NF_OPEN_FAILED;
     386      if (out->write(&st,1)!=1) comm_failed();
     387    } else
     388    {
     389      uint8_t st=NF_OPEN_REMOTE_FILE;
     390      if (out->write(&st,1)!=1) comm_failed();     
     391      if (out->write(&fd,sizeof(fd))!=sizeof(fd)) comm_failed();     
     392    }
    393393      } break;
    394394      case NFCMD_CLOSE :
     
    398398      case NFCMD_READ :
    399399      {
    400         int fd;
    401         if (in->read(&fd,sizeof(fd))!=sizeof(fd)) comm_failed();
    402 
    403         switch (cmd)
    404         {
    405           case NFCMD_CLOSE :
    406           {
    407             fman->rf_close(fd);
    408             uint8_t st=1;
    409             if (out->write(&st,1)!=1) comm_failed();   
    410           } break;
    411           case NFCMD_SIZE  :
    412           {
    413             int32_t x=fman->rf_file_size(fd);
    414             if (out->write(&x,sizeof(x))!=sizeof(x)) comm_failed();              
    415           } break;
    416           case NFCMD_TELL :
    417           {
    418             int32_t offset=fman->rf_tell(fd);
    419             if (out->write(&offset,sizeof(offset))!=sizeof(offset)) comm_failed(); 
    420           } break;
    421           case NFCMD_SEEK :
    422           {
    423             int32_t offset;
    424             if (in->read(&offset,sizeof(offset))!=sizeof(offset)) comm_failed();
    425             offset=fman->rf_seek(fd,offset);
    426             if (out->write(&offset,sizeof(offset))!=sizeof(offset)) comm_failed(); 
    427           } break;
    428           case NFCMD_READ :
    429           {
    430             int32_t size;
    431             if (in->read(&size,sizeof(size))!=sizeof(size)) comm_failed();
    432             fman->rf_read(fd,out,size);
    433           } break;
    434         }
    435       } break;   
     400    int fd;
     401    if (in->read(&fd,sizeof(fd))!=sizeof(fd)) comm_failed();
     402
     403    switch (cmd)
     404    {
     405      case NFCMD_CLOSE :
     406      {
     407        fman->rf_close(fd);
     408        uint8_t st=1;
     409        if (out->write(&st,1)!=1) comm_failed();     
     410      } break;
     411      case NFCMD_SIZE  :
     412      {
     413        int32_t x=fman->rf_file_size(fd);
     414        if (out->write(&x,sizeof(x))!=sizeof(x)) comm_failed();        
     415      } break;
     416      case NFCMD_TELL :
     417      {
     418        int32_t offset=fman->rf_tell(fd);
     419        if (out->write(&offset,sizeof(offset))!=sizeof(offset)) comm_failed();
     420      } break;
     421      case NFCMD_SEEK :
     422      {
     423        int32_t offset;
     424        if (in->read(&offset,sizeof(offset))!=sizeof(offset)) comm_failed();
     425        offset=fman->rf_seek(fd,offset);
     426        if (out->write(&offset,sizeof(offset))!=sizeof(offset)) comm_failed();
     427      } break;
     428      case NFCMD_READ :
     429      {
     430        int32_t size;
     431        if (in->read(&size,sizeof(size))!=sizeof(size)) comm_failed();
     432        fman->rf_read(fd,out,size);
     433      } break;
     434    }
     435      } break;
    436436      default :
    437437      { fprintf(stderr,"net driver : unknown net command %d\n",cmd); die(0); }
    438     }   
     438    }
    439439    ret=1;
    440440  }
     
    452452  net_socket *sock=connect_to_server(server_name,DEFAULT_COMM_PORT,0);
    453453  if (!sock)
    454   { 
     454  {
    455455    fprintf(stderr,"unable to connect\n");
    456456    return 0;
     
    472472  {
    473473    fprintf(stderr,
    474             "This server is not running the registered version of abuse, and\n"
    475             "you are (thanks!).  So that there are no conflict between the two games\n"
    476             "please start with the -share option when connecting to this server\n"
    477             "example : abuse -net somewhere.someplace.net -share\n");
     474        "This server is not running the registered version of abuse, and\n"
     475        "you are (thanks!).  So that there are no conflict between the two games\n"
     476        "please start with the -share option when connecting to this server\n"
     477        "example : abuse -net somewhere.someplace.net -share\n");
    478478    delete sock;
    479479    return 0;
     
    487487
    488488  if (sock->write(&len,1)!=1 ||
    489       sock->write(uname,len)!=len || 
     489      sock->write(uname,len)!=len ||
    490490      sock->write(&port,2)!=2  ||            // send server out game port
    491491      sock->read(&port,2)!=2   ||            // read server's game port
     
    507507
    508508
    509 net_socket *net_driver::connect_to_server(char *&name, int port, int force_port, 
    510                                           net_socket::socket_type sock_type)
     509net_socket *net_driver::connect_to_server(char *&name, int port, int force_port,
     510                      net_socket::socket_type sock_type)
    511511{
    512512  char *oname=name;
    513513  net_address *addr=proto->get_node_address(name, port, force_port);
    514   if (!addr) 
     514  if (!addr)
    515515  {
    516516    if (debug) fprintf(stderr,"No IP address for name %s\n",oname);
     
    539539      sock->read(name_start,len)!=len)
    540540  {
    541     delete sock; 
     541    delete sock;
    542542    return 0;
    543   } 
     543  }
    544544
    545545  delete sock;
    546   return 1; 
     546  return 1;
    547547}
    548548
     
    555555  uint8_t cmd=CLIENT_CRC_WAITER;
    556556  if (sock->write(&cmd,1)!=1 ||
    557       sock->read(&cmd,1)!=1) 
     557      sock->read(&cmd,1)!=1)
    558558  { delete sock; return 0; }
    559559  delete sock;
    560   return cmd; 
     560  return cmd;
    561561}
    562562
     
    572572
    573573      crc_wait_list=new crc_waiter(sock,crc_wait_list);
    574       base->calc_crcs=1;     
     574      base->calc_crcs=1;
    575575      return 1;
    576576    } break;
     
    590590      return ret;
    591591    }
    592        
    593   } 
     592
     593  }
    594594  return 0;
    595595}
Note: See TracChangeset for help on using the changeset viewer.