1 | // structure used to comminicate with the engine |
---|
2 | |
---|
3 | #ifndef __NETFACE_HPP_ |
---|
4 | #define __NETFACE_HPP_ |
---|
5 | |
---|
6 | #define PACKET_MAX_SIZE 1024 // this is a game data packet (udp/ipx) |
---|
7 | #define READ_PACKET_SIZE 1024 // this is a file service packet (tcp/spx) |
---|
8 | #define NET_CRC_FILENAME "#net_crc" |
---|
9 | #define NET_STARTFILE "netstart.spe" |
---|
10 | #include "indian.hpp" |
---|
11 | #include <string.h> |
---|
12 | |
---|
13 | |
---|
14 | // list of commands for general networking and file services |
---|
15 | |
---|
16 | enum { NFCMD_OPEN, |
---|
17 | NFCMD_CLOSE, |
---|
18 | NFCMD_READ, |
---|
19 | NFCMD_WRITE, |
---|
20 | NFCMD_SEEK, |
---|
21 | NFCMD_SIZE, |
---|
22 | NFCMD_TELL, |
---|
23 | NFCMD_SET_FS, // used to set the default (active) filesever |
---|
24 | NFCMD_CRCS_CALCED, // engine sends this to driver after crcs are saved |
---|
25 | NFCMD_REQUEST_LSF, // engine sends to driver with remote server name, returns 0 for failure or lsf name |
---|
26 | NFCMD_PROCESS_LSF, // remote engine sends to driver with lsf name, when get_lsf is set in base_mem |
---|
27 | NFCMD_REQUEST_ENTRY, // sent from joining client engine to driver, who then connects as client_abuse |
---|
28 | NFCMD_BECOME_SERVER, |
---|
29 | NFCMD_BLOCK, // used by UNIX version to have engine give it up it's time-slice |
---|
30 | NFCMD_RELOAD_START, |
---|
31 | NFCMD_RELOAD_END, |
---|
32 | NFCMD_SEND_INPUT, |
---|
33 | NFCMD_INPUT_MISSING, // when engine is waiting for input and suspects packets are missing |
---|
34 | NFCMD_KILL_SLACKERS, // when the user decides the clients are taking too long to respond |
---|
35 | EGCMD_DIE |
---|
36 | }; |
---|
37 | |
---|
38 | // client commands |
---|
39 | enum { CLCMD_JOIN_FAILED, |
---|
40 | CLCMD_JOIN_SUCCESS, |
---|
41 | CLCMD_RELOAD_START, // will you please load netstart.spe |
---|
42 | CLCMD_RELOAD_END, // netstart.spe has been loaded, please continue |
---|
43 | CLCMD_REQUEST_RESEND, // input didn't arrive, please resend |
---|
44 | CLCMD_UNJOIN // causes server to delete you (addes your delete command to next out packet) |
---|
45 | } ; |
---|
46 | |
---|
47 | |
---|
48 | // return codes for NFCMD_OPEN |
---|
49 | enum { NF_OPEN_FAILED, |
---|
50 | NF_OPEN_LOCAL_FILE, // should return path to local file as well |
---|
51 | NF_OPEN_REMOTE_FILE } ; // returned to engine for a filename |
---|
52 | |
---|
53 | |
---|
54 | // types of clients allowed to connect |
---|
55 | enum { CLIENT_NFS=50, // client can read one remote files |
---|
56 | CLIENT_ABUSE, // waits for entry into a game |
---|
57 | CLIENT_CRC_WAITER, // client waits for crcs to be saved |
---|
58 | CLIENT_LSF_WAITER // waits for lsf to be transmitted |
---|
59 | |
---|
60 | } ; |
---|
61 | |
---|
62 | // base->input_state will be one of the following |
---|
63 | |
---|
64 | enum { INPUT_COLLECTING, // waiting for driver to receive input from clients/server |
---|
65 | INPUT_PROCESSING, // waiting for engine to process input from last tick |
---|
66 | INPUT_RELOAD, // server is waiting on clients to reload, process game packets, but don't store them |
---|
67 | INPUT_NET_DEAD }; // net driver detected an unrecoverable net error, engine should shut down net services |
---|
68 | |
---|
69 | |
---|
70 | |
---|
71 | // the net driver should not use any of these except SCMD_DELETE_CLIENT (0) because |
---|
72 | // they are subject to change |
---|
73 | enum { |
---|
74 | SCMD_DELETE_CLIENT, |
---|
75 | SCMD_VIEW_RESIZE, |
---|
76 | SCMD_SET_INPUT, |
---|
77 | SCMD_WEAPON_CHANGE, |
---|
78 | SCMD_END_OF_PACKET, |
---|
79 | SCMD_RELOAD, |
---|
80 | SCMD_KEYPRESS, |
---|
81 | SCMD_KEYRELEASE, |
---|
82 | SCMD_EXT_KEYPRESS, |
---|
83 | SCMD_EXT_KEYRELEASE, |
---|
84 | SCMD_CHAT_KEYPRESS, |
---|
85 | SCMD_SYNC |
---|
86 | }; |
---|
87 | |
---|
88 | |
---|
89 | struct join_struct |
---|
90 | { |
---|
91 | int client_id; |
---|
92 | char name[100]; |
---|
93 | join_struct *next; |
---|
94 | } ; |
---|
95 | |
---|
96 | struct net_packet |
---|
97 | { |
---|
98 | unsigned char data[PACKET_MAX_SIZE]; |
---|
99 | int packet_prefix_size() { return 5; } // 2 byte size, 2 byte check sum, 1 byte packet order |
---|
100 | unsigned short packet_size() { unsigned short size=(*(unsigned short *)data); return lstl(size); } |
---|
101 | unsigned char tick_received() { return data[4]; } |
---|
102 | void set_tick_received(unsigned char x) { data[4]=x; } |
---|
103 | unsigned char *packet_data() { return data+packet_prefix_size(); } |
---|
104 | unsigned short get_checksum() { unsigned short cs=*((unsigned short *)data+1); return lstl(cs); } |
---|
105 | unsigned short calc_checksum() |
---|
106 | { |
---|
107 | *((unsigned short *)data+1)=0; |
---|
108 | int i,size=packet_prefix_size()+packet_size(); |
---|
109 | unsigned char c1=0,c2=0,*p=data; |
---|
110 | for (i=0;i<size;i++,p++) |
---|
111 | { |
---|
112 | c1+=*p; |
---|
113 | c2+=c1; |
---|
114 | } |
---|
115 | unsigned short cs=( (((unsigned short)c1)<<8) | c2); |
---|
116 | *((unsigned short *)data+1)=lstl(cs); |
---|
117 | return cs; |
---|
118 | } |
---|
119 | |
---|
120 | |
---|
121 | void packet_reset() { set_packet_size(0); } // 2 bytes for size, 1 byte for tick |
---|
122 | |
---|
123 | void add_to_packet(void *buf, int size) |
---|
124 | { |
---|
125 | if (size && size+packet_size()+packet_prefix_size()<PACKET_MAX_SIZE) |
---|
126 | { |
---|
127 | memcpy(data+packet_size()+packet_prefix_size(),buf,size); |
---|
128 | set_packet_size(packet_size()+size); |
---|
129 | } |
---|
130 | } |
---|
131 | void write_byte(unsigned char x) { add_to_packet(&x,1); } |
---|
132 | void write_short(unsigned short x) { x=lstl(x); add_to_packet(&x,2); } |
---|
133 | void write_long(unsigned long x) { x=lltl(x); add_to_packet(&x,4); } |
---|
134 | |
---|
135 | void set_packet_size(unsigned short x) { *((unsigned short *)data)=lstl(x); } |
---|
136 | |
---|
137 | |
---|
138 | } ; |
---|
139 | |
---|
140 | struct base_memory_struct |
---|
141 | { |
---|
142 | net_packet packet, // current tick data |
---|
143 | last_packet; // last tick data (in case a client misses input, we can resend) |
---|
144 | |
---|
145 | short mem_lock; |
---|
146 | short calc_crcs; |
---|
147 | short get_lsf; |
---|
148 | short wait_reload; |
---|
149 | short need_reload; |
---|
150 | short input_state; // COLLECTING or PROCESSING |
---|
151 | short current_tick; // set by engine, used by driver to confirm packet is not left over |
---|
152 | |
---|
153 | join_struct *join_list; |
---|
154 | } ; |
---|
155 | |
---|
156 | |
---|
157 | |
---|
158 | #endif |
---|