OpenMoHAA 0.82.0
Loading...
Searching...
No Matches
gt2Main.h
1/*
2GameSpy GT2 SDK
3Dan "Mr. Pants" Schoenblum
4dan@gamespy.com
5
6Copyright 2002 GameSpy Industries, Inc
7
8devsupport@gamespy.com
9*/
10
11#ifndef _GT2_MAIN_H_
12#define _GT2_MAIN_H_
13
14#include "gt2.h"
15#include "../darray.h"
16#include "../hashtable.h"
17#include "gt2Auth.h"
18
19/*************************
20** CONFIGURABLE DEFINES **
21*************************/
22
23// these defines are internal to GT2 and are NOT guaranteed to persist from version to version.
24
25
26// If set, this will convert all big endian vars to little endian before sending accross the net
27// And on big endian machines, convert little endian to big endian on recv
28//#define _GT2_ENDIAN_CONVERT_ENABLE // add this to your compiler pre-processor options
29
30#if defined GSI_BIG_ENDIAN && defined _GT2_ENDIAN_CONVERT_ENABLE
31 #define _GT2_ENDIAN_CONVERT
32#endif
33
34
35
36// any unreliable application message that starts with this magic string will have extra overhead.
37// the string can be changed to something that your application will not use, or not use frequently.
38// the only impact of this change will be to make your application incomatible with other application's
39// using either the original or another different magic string.
40// the string can consist of any number of characters, as long as there's at least one character, and the
41// length define matches the string's length.
42#define GTI2_MAGIC_STRING "\xFE\xFE"
43#define GTI2_MAGIC_STRING_LEN 2
44
45// the size of the buffer into which GT2 directly receives messages. this buffer is declared on the stack,
46// and so can be fairly large on most systems without having any impact. however, on some systems with small
47// stacks, this size can overflow the stack, in which case it should be lowered.
48// note, this buffer size only needs to be slighty larger than the largest message that will be sent ("slighty
49// larger" due to overhead with reliable messages, and unreliable messages starting with the magic string).
50#if defined(_PS2) && defined(INSOCK)
51 #define GTI2_STACK_RECV_BUFFER_SIZE NETBUFSIZE // Max for Insock. Otherwise SOCKET_ERROR
52#elif defined(_NITRO)
53 #define GTI2_STACK_RECV_BUFFER_SIZE 1500
54#elif defined (_XBOX) // Xbox packets are 1304,
55 #define GTI2_STACK_RECV_BUFFER_SIZE 4096 // when using VDP sockets, 2 bytes are used for data length
56#else
57 #define GTI2_STACK_RECV_BUFFER_SIZE 65535
58#endif
59
60// a server will disconnect a client that doesn't not successfully connect within this time (in milliseconds).
61// if the connectAttemptCallback has been called, and GT2 is awaiting an accept/reject, the attempt will
62// not be timed-out (although the client may abort the attempt at any time).
63#define GTI2_SERVER_TIMEOUT (1 * 60 * 1000)
64// the time (in milliseconds) GT2 waits between resending a message whose delivery has not yet been confirmed.
65#define GTI2_RESEND_TIME 1000
66// the time (in milliseconds) GT2 waits after receiving a message it must acknowledge before it actually sends
67// the ack. this allows it to combine acks, or include acks as part of other reliable messages it sends.
68// if an ack is pending, a new incoming message does not reset this timer.
69#define GTI2_PENDING_ACK_TIME 100
70// if GT2 does not send a message for this amount of time (in milliseconds), it sends a keep-alive message.
71#define GTI2_KEEP_ALIVE_TIME (30 * 1000)
72// if this is defined, it sets the percentage of sent datagrams to drop. this is good for simulating what will
73// happen on a high packet loss connection.
74//#define GTI2_DROP_SEND_RATE 30
75typedef enum
76{
77 GTI2UdpProtocol, // UDP socket type for standard sockets
78 GTI2VdpProtocol = 2, // VDP socket type only used for Xbox VDP sockets
79 GTI2AdHocProtocol = 3 // socket type only used for PSP Adhoc sockets
80} GTI2ProtocolType;
81
82// The Maximum offset of eiter UDP or VDP
83// measured in bytes
84// used as a buffer offset
85#define MAX_PROTOCOL_OFFSET 2
86
87/**********
88** TYPES **
89**********/
90
91typedef enum
92{
93 // client-only states
94 GTI2AwaitingServerChallenge, // sent challenge, waiting for server's challenge
95 GTI2AwaitingAcceptance, // sent response, waiting for accept/reject from server
96
97 // server-only states
98 GTI2AwaitingClientChallenge, // receiving challenge from a new client
99 GTI2AwaitingClientResponse, // sent challenge, waiting for client's response
100 GTI2AwaitingAcceptReject, // got client's response, waiting for app to accept/reject
101
102 // post-negotiation states
103 GTI2Connected, // connected
104 GTI2Closing, // sent a close message (GTI2Close or GTI2Reject), waiting for confirmation
105 GTI2Closed // connection has been closed, free it as soon as possible
106} GTI2ConnectionState;
107
108// message types
109typedef enum
110{
111 // reliable messages
112 // all start with <magic-string> <type> <serial-number> <expected-serial-number>
113 // type is 1 bytes, SN and ESN are 2 bytes each
114 GTI2MsgAppReliable, // reliable application message
115 GTI2MsgClientChallenge, // client's challenge to the server (initial connection request)
116 GTI2MsgServerChallenge, // server's response to the client's challenge, and his challenge to the client
117 GTI2MsgClientResponse, // client's response to the server's challenge
118 GTI2MsgAccept, // server accepting client's connection attempt
119 GTI2MsgReject, // server rejecting client's connection attempt
120 GTI2MsgClose, // message indicating the connection is closing
121 GTI2MsgKeepAlive, // keep-alive used to help detect dropped connections
122
123 GTI2NumReliableMessages,
124
125 // unreliable messages
126 GTI2MsgAck = 100, // acknowledge receipt of reliable message(s)
127 GTI2MsgNack, // alert sender to missing reliable message(s)
128 GTI2MsgPing, // used to determine latency
129 GTI2MsgPong, // a reply to a ping
130 GTI2MsgClosed // confirmation of connection closure (GTI2MsgClose or GTI2MsgReject) - also sent in response to bad messages from unknown addresses
131
132 // unreliable messages don't really have a message type, just the magic string repeated at the start
133} GTI2MessageType;
134
135/***************
136** STRUCTURES **
137***************/
138
139typedef struct GTI2Buffer
140{
141 GT2Byte * buffer; // The buffer's bytes.
142 int size; // Number of bytes in buffer.
143 int len; // Length of actual data in buffer.
144} GTI2Buffer;
145
147{
148 int start; // the start of the message
149 int len; // the length of the message
150 GTI2MessageType type; // the type
151 unsigned short serialNumber; // the serial number
153
155{
156 int start; // the start of the message
157 int len; // the length of the message
158 unsigned short serialNumber; // the serial number
159 gsi_time lastSend; // last time this message was sent
161
162typedef struct GTI2Socket
163{
164 SOCKET socket; // the network socket used for all network communication
165
166 unsigned int ip; // the ip this socket is bound to
167 unsigned short port; // the port this socket is bound to
168
169 HashTable connections; // the connections that are using this socket
170 DArray closedConnections; // connections that are closed no longer get a spot in the hash table
171
172 GT2Bool close; // if true, a close was attempted inside a callback, and it should be closed as soon as possible
173 GT2Bool error; // if true, there was a socket error using this socket
174
175 int callbackLevel; // if >0, then we're inside a callback (or recursive callbacks)
176 gt2ConnectAttemptCallback connectAttemptCallback; // if set, callback used to handle incoming connection attempts
177 gt2SocketErrorCallback socketErrorCallback; // if set, call this in case of an error
178 gt2DumpCallback sendDumpCallback; // if set, gets called for every datagram sent
179 gt2DumpCallback receiveDumpCallback; // if set, gets called for every datagram and connection reset received
180 gt2UnrecognizedMessageCallback unrecognizedMessageCallback; // if set, gets called for all unrecognized messages
181
182 void * data; // user data
183
184 int outgoingBufferSize; // per-connection buffer sizes
185 int incomingBufferSize;
186
187 GTI2ProtocolType protocolType; // set to UDP or VDP protocol depending on the call to create socket
188 // also used as an offset for VDP sockets
189 int protocolOffset;
190 GT2Bool broadcastEnabled; // set to true if the socket has already been broadcast enabled
191} GTI2Socket;
192
193typedef struct GTI2Connection
194{
195 // ip and port uniquely identify this connection on this socket
196 unsigned int ip; // the ip on the other side of this connection (network byte order)
197 unsigned short port; // the port on the other side of this connection (host byte order)
198
199 GTI2Socket * socket; // the parent socket
200
201 GTI2ConnectionState state; // connection state
202
203 GT2Bool initiated; // if true, the local side of the connection initiated the connection (client)
204
205 GT2Bool freeAtAcceptReject; // if true, don't free the connection until accept/reject is called
206
207 GT2Result connectionResult; // the result of the connect attempt
208
209 gsi_time startTime; // the time the connection was created
210 gsi_time timeout; // the timeout value passed into gt2Connect
211
212 int callbackLevel; // if >0, then we're inside a callback (or recursive callbacks)
213 GT2ConnectionCallbacks callbacks; // connection callbacks
214
215 char * initialMessage; // this is the initial message for the client
216 int initialMessageLen; // the initial message length
217
218 void * data; // user data
219
220 GTI2Buffer incomingBuffer; // buffer for incoming data
221 GTI2Buffer outgoingBuffer; // buffer for outgoing data
222 DArray incomingBufferMessages; // identifies incoming messages stored in the buffer
223 DArray outgoingBufferMessages; // identifies outgoing messages stored in the buffer
224
225 unsigned short serialNumber; // serial number of the next message to be sent out
226 unsigned short expectedSerialNumber; // the next serial number we're expecting from the remote side
227
228 char response[GTI2_RESPONSE_LEN]; // after the challenge is sent during negotiation, this is the response we're expecting
229
230 gsi_time lastSend; // the last time something was sent on this connection
231 gsi_time challengeTime; // the time the challenge was sent
232
233 GT2Bool pendingAck; // if true, there is an ack waiting to go out, either on its own or as part of a reliable message
234
235 gsi_time pendingAckTime; // the time at which the pending ack was first set
236
237 DArray sendFilters; // filters that apply to outgoing data
238 DArray receiveFilters; // filters that apply to incoming data
239
241
242// store last 32 ip's in a ring buffer
243#define MAC_TABLE_SIZE 32 // must be power of 2
244typedef struct
245{
246 gsi_u32 ip;
247 char mac[6];
249
250#ifdef GSI_ADHOC
251static int lastmactableentry = 0;
252static GTI2MacEntry MacTable[MAC_TABLE_SIZE];
253#endif // GSI_ADHOC
254
255#endif
Definition gt2.h:230
Definition gt2Main.h:140
Definition gt2Main.h:194
Definition gt2Main.h:147
Definition gt2Main.h:245
Definition gt2Main.h:155
Definition gt2Main.h:163