source: abuse/trunk/src/net/gclient.cpp @ 2

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