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