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