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

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