[56] | 1 | /* |
---|
| 2 | * Abuse - dark 2D side-scrolling platform game |
---|
| 3 | * Copyright (c) 1995 Crack dot Com |
---|
[494] | 4 | * Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net> |
---|
[56] | 5 | * |
---|
| 6 | * This software was released into the Public Domain. As with most public |
---|
[555] | 7 | * domain software, no warranty is made or implied by Crack dot Com, by |
---|
| 8 | * Jonathan Clark, or by Sam Hocevar. |
---|
[56] | 9 | */ |
---|
[2] | 10 | |
---|
[555] | 11 | #if defined HAVE_CONFIG_H |
---|
| 12 | # include "config.h" |
---|
| 13 | #endif |
---|
[2] | 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 | |
---|
[512] | 24 | #include "common.h" |
---|
| 25 | |
---|
[481] | 26 | #include "netcfg.h" |
---|
| 27 | #include "gclient.h" |
---|
| 28 | #include "netface.h" |
---|
| 29 | #include "undrv.h" |
---|
| 30 | #include "timing.h" |
---|
[56] | 31 | |
---|
[2] | 32 | extern base_memory_struct *base; |
---|
| 33 | extern net_socket *comm_sock,*game_sock; |
---|
| 34 | extern net_protocol *prot; |
---|
| 35 | extern char lsf[256]; |
---|
| 36 | extern int start_running; |
---|
| 37 | |
---|
| 38 | int game_client::process_server_command() |
---|
| 39 | { |
---|
[17] | 40 | uint8_t cmd; |
---|
[2] | 41 | if (client_sock->read(&cmd,1)!=1) return 0; |
---|
| 42 | switch (cmd) |
---|
| 43 | { |
---|
| 44 | case CLCMD_REQUEST_RESEND : |
---|
| 45 | { |
---|
[17] | 46 | uint8_t tick; |
---|
[2] | 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", |
---|
[124] | 50 | tick,base->current_tick,base->packet.tick_received(),base->last_packet.tick_received()); |
---|
[2] | 51 | |
---|
[112] | 52 | // asking for this tick? make sure it is collected |
---|
| 53 | if (tick==base->packet.tick_received() && !wait_local_input) |
---|
[2] | 54 | { |
---|
[124] | 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); |
---|
[2] | 58 | |
---|
[124] | 59 | { time_marker now,start; while (now.diff_time(&start)<3.0) now.get_time(); } |
---|
[2] | 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 | |
---|
| 72 | int 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 | { |
---|
[17] | 84 | uint16_t rec_crc=tmp.get_checksum(); |
---|
[2] | 85 | if (rec_crc==tmp.calc_checksum()) |
---|
| 86 | { |
---|
[124] | 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); |
---|
[2] | 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 | |
---|
[124] | 117 | game_client::game_client(net_socket *client_sock, net_address *server_addr) : |
---|
[2] | 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 | |
---|
| 125 | int 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"); |
---|
[124] | 132 | // { time_marker now,start; while (now.diff_time(&start)<3.0) now.get_time(); } |
---|
[2] | 133 | |
---|
| 134 | /* |
---|
[494] | 135 | unsigned char pk[2]={ CLCMD_REQUEST_RESEND,base->packet.tick_received()}; |
---|
[2] | 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 | |
---|
| 141 | void 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 | |
---|
| 156 | int game_client::end_reload(int disconnect) // notify evryone you've reloaded the level (at server request) |
---|
| 157 | { |
---|
[17] | 158 | uint8_t cmd=CLCMD_RELOAD_END; |
---|
[2] | 159 | if (client_sock->write(&cmd,1)!=1) return 0; |
---|
| 160 | return 1; |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | int game_client::start_reload() |
---|
| 164 | { |
---|
[17] | 165 | uint8_t cmd=CLCMD_RELOAD_START; |
---|
[2] | 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 | |
---|
| 171 | int kill_net(); |
---|
| 172 | |
---|
| 173 | int 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 | |
---|
| 181 | int game_client::quit() |
---|
| 182 | { |
---|
[17] | 183 | uint8_t cmd=CLCMD_UNJOIN; |
---|
[2] | 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 | |
---|
| 193 | game_client::~game_client() |
---|
| 194 | { |
---|
| 195 | delete client_sock; |
---|
| 196 | delete server_data_port; |
---|
| 197 | } |
---|
| 198 | |
---|