home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / ABUSESRC.ZIP / AbuseSrc / macabuse / src / net / unix / gclient.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-20  |  5.1 KB  |  183 lines

  1. #include "system.h"
  2. #include "netcfg.hpp"
  3. #include "gclient.hpp"
  4. #include "netface.hpp"
  5. #include "undrv.hpp"
  6. #include "timing.hpp"
  7. #include "dprint.hpp"
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <fcntl.h>
  12. #include <unistd.h>
  13. #ifndef __MAC__
  14. #include <sys/stat.h>
  15. #include <sys/types.h>
  16. #endif
  17. #include <string.h>
  18. #include <signal.h>
  19.  
  20. extern base_memory_struct *base;
  21. extern net_socket *comm_sock,*game_sock;
  22. extern int registered;
  23. extern net_protocol *prot;
  24. extern char lsf[256];
  25. extern int start_running;
  26.  
  27. int game_client::process_server_command()
  28. {
  29.   uchar cmd;
  30.   if (client_sock->read(&cmd,1)!=1) return 0;
  31.   switch (cmd)
  32.   {
  33.     case CLCMD_REQUEST_RESEND :
  34.     {
  35.       uchar tick;
  36.       if (client_sock->read(&tick,1)!=1) return 0;
  37.  
  38.       dprintf("request for resend tick %d (game cur=%d, pack=%d, last=%d)\n",
  39.           tick,base->current_tick,base->packet.tick_received(),base->last_packet.tick_received());
  40.  
  41.       if (tick==base->packet.tick_received() && !wait_local_input)    // asking for this tick?  make sure is collected
  42.       {
  43.     dprintf("resending client packet %d to server\n");
  44.     net_packet *pack=&base->packet;
  45.     game_sock->write(pack->data,pack->packet_size()+pack->packet_prefix_size(),server_data_port); 
  46.  
  47.     { time_marker now,start; while (now.diff_time(&start)<3.0) now.get_time(); } 
  48.       }
  49.       return 1;
  50.     } break;
  51.     default :
  52.     {
  53.       dprintf("unknown command from server %d\n",cmd);
  54.       return 0;
  55.     }
  56.   }
  57.   return 0;
  58. }
  59.  
  60. int game_client::process_net()
  61. {
  62. //  if (client_sock->error())
  63. //    return 0;
  64.  
  65.   if (game_sock->ready_to_read())     // any game data comming in?
  66.   {
  67.     net_packet tmp;  // don't store in main packet in case something is wrong with this packet and server still needs old one
  68.  
  69.     int bytes_received=game_sock->read(tmp.data,PACKET_MAX_SIZE);
  70.     if (bytes_received==tmp.packet_size()+tmp.packet_prefix_size())   // was the packet complete?
  71.     {
  72.       unsigned short rec_crc=tmp.get_checksum();
  73.       if (rec_crc==tmp.calc_checksum())
  74.       {
  75.                 if (base->current_tick==tmp.tick_received())  
  76.                 {
  77.                   base->packet=tmp;
  78.                   wait_local_input=1;
  79.                   base->input_state=INPUT_PROCESSING;   // tell engine to start processing
  80.                 }
  81. //    else dprintf("received stale packet (got %d, expected %d)\n",tmp.tick_received(),base->current_tick);
  82.       } else dprintf("received packet with bad checksum\n");
  83.     } else dprintf("incomplete packet, read %d, should be %d\n",bytes_received,tmp.packet_size()+tmp.packet_prefix_size());
  84.  
  85.   }
  86.  
  87.   if (client_sock->ready_to_read())
  88.   {
  89.     if (!process_server_command())
  90.     {
  91.       main_net_cfg->state=net_configuration::RESTART_SINGLE;
  92.       start_running=0;
  93.       strcpy(lsf,"abuse.lsp");
  94.  
  95.       wait_local_input=1;
  96.       base->input_state=INPUT_PROCESSING;   // tell engine to start processing
  97.       return 0;
  98.  
  99.     }
  100.   }
  101.   return 1;
  102. }
  103.  
  104.  
  105. game_client::game_client(net_socket *client_sock, net_address *server_addr) : 
  106.   client_sock(client_sock)
  107. {
  108.  server_data_port=server_addr->copy();
  109.   client_sock->read_selectable();
  110.   wait_local_input=1;
  111. }
  112.  
  113. int game_client::input_missing()
  114. {
  115.   if (prot->debug_level(net_protocol::DB_IMPORTANT_EVENT))
  116.     dprintf("(resending %d)\n",base->packet.tick_received());
  117.   net_packet *pack=&base->packet;
  118.   game_sock->write(pack->data,pack->packet_size()+pack->packet_prefix_size(),server_data_port);
  119. //  dprintf("2");
  120. //  { time_marker now,start; while (now.diff_time(&start)<3.0) now.get_time(); } 
  121.  
  122. /*
  123.   unsigned char pk[2]={CLCMD_REQUEST_RESEND,base->packet.tick_received()};
  124.   if (client_sock->write(pk,2)!=2) return 0;
  125.   dprintf("sending retry request to server (tick %d, wait input=%d)\n",pk[1],wait_local_input); */
  126.   return 1;
  127. }
  128.  
  129. void game_client::add_engine_input()
  130. {
  131.   net_packet *pack=&base->packet;
  132.   base->input_state=INPUT_COLLECTING;
  133.   wait_local_input=0;
  134.   pack->set_tick_received(base->current_tick);
  135.   pack->calc_checksum();
  136.   game_sock->write(pack->data,pack->packet_size()+pack->packet_prefix_size(),server_data_port);
  137. //  data_sock->write(pack->data,pack->packet_size()+pack->packet_prefix_size());
  138. /*  dprintf("(sending %d)\n",base->packet.tick_received());
  139.  
  140.   { time_marker now,start; while (now.diff_time(&start)<5.0) now.get_time(); }  */
  141. }
  142.  
  143.  
  144. int game_client::end_reload(int disconnect)  // notify evryone you've reloaded the level (at server request)
  145. {
  146.   uchar cmd=CLCMD_RELOAD_END;
  147.   if (client_sock->write(&cmd,1)!=1) return 0;
  148.   return 1;
  149. }
  150.  
  151. int game_client::start_reload()
  152. {
  153.   uchar cmd=CLCMD_RELOAD_START;
  154.   if (client_sock->write(&cmd,1)!=1) return 0;
  155.   if (client_sock->read(&cmd,1)!=1) return 0;
  156.   return 1;
  157. }
  158.  
  159. int kill_net();
  160.  
  161. int game_client::kill_slackers()
  162. {
  163.   if (base->input_state==INPUT_COLLECTING)
  164.     base->input_state=INPUT_PROCESSING;
  165.   kill_net();
  166.   return 0;        // tell driver to delete us and replace with local client
  167. }
  168.  
  169. int game_client::quit()
  170. {
  171.   uchar cmd=CLCMD_UNJOIN;
  172.   if (client_sock->write(&cmd,1)!=1) return 0;
  173.   if (client_sock->read(&cmd,1)!=1) return 0;
  174.   return 1;
  175. }
  176.  
  177. game_client::~game_client()
  178. {
  179.   delete client_sock;
  180.   delete server_data_port;
  181. }
  182.  
  183.