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

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

imlib: use vec2i for image::size and unroll all necessary changes
everywhere else in the code.

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