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

Last change on this file since 555 was 555, checked in by Sam Hocevar, 9 years ago

ps3: make everything compile on the PS3. Of course, nothing links yet
because so much support is missing.

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