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

Last change on this file since 494 was 494, checked in by Sam Hocevar, 11 years ago

style: remove trailing spaces, fix copyright statements.

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