OpenMoHAA 0.82.0
Loading...
Searching...
No Matches
gsUdpEngine.h
1
3#ifndef __GS_UDP_ENGINE_H__
4#define __GS_UDP_ENGINE_H__
5
8// UDP Communication Engine
9//
10#include "gsCommon.h"
11#include "../gt2/gt2.h"
12#include "../darray.h"
13
16// Constants
17
18// The UDP Engine should try the MSS (Maximum Segment Size = IP Packet size - IP Header)
19// as a default size to keep the packets from being fragmented. Currently 1460 is the
20// MSS for windows. Consoles may have a different size.
21#define GS_UDP_DEFAULT_OUT_BUFFSIZE 1460
22#define GS_UDP_DEFAULT_IN_BUFFSIZE 1500
23
24// a default size for address strings
25#define GS_IP_ADDR_AND_PORT 22
26
27// A fixed header len. unless we require a bigger size, it will stay this size.
28// These need to be used for calculating the free space available on
29// the outgoing buffers for an IP and Port which represent the peer.
30#define GS_UDP_MSG_HEADER_LEN 16
31#define GS_UDP_RELIABLE_MSG_HEADER 7
32
33// The following error codes will be given back to the higher level app
34// or message handler.
35typedef enum _GSUdpErrorCode
36{
37 GS_UDP_NO_ERROR,
38 GS_UDP_NO_MEMORY,
39 GS_UDP_REJECTED,
40 GS_UDP_NETWORK_ERROR,
41 GS_UDP_ADDRESS_ERROR,
42 GS_UDP_ADDRESS_ALREADY_IN_USE,
43 GS_UDP_TIMED_OUT,
44 GS_UDP_REMOTE_ERROR,
45 GS_UDP_SEND_FAILED,
46 GS_UDP_INVALID_MESSAGE,
47 GS_UDP_PARAMETER_ERROR,
48 GS_UDP_NOT_INITIALIZED,
49 GS_UDP_MSG_TOO_BIG,
50 GS_UDP_UNKNOWN_ERROR,
51 // Add new errors before here
52 GS_UDP_NUM_ERROR_CODES
53} GSUdpErrorCode;
54
55// Used so that an app or message handler does
56// not need to request to start talking to a peer twice
57// Also lets higher level app or message handlers know about
58// if a communication channel to a peer has been broken.
59typedef enum _GSUdpPeerState
60{
61 GS_UDP_PEER_CONNECTING,
62 GS_UDP_PEER_CONNECTED,
63 GS_UDP_PEER_CLOSING,
64 GS_UDP_PEER_CLOSED,
65 // Add new connection state before here
66 GS_UDP_PEER_STATE_NUM
67} GSUdpPeerState;
68
69// When a communication channel to a peer is closed
70// the closed callback will let us know why it was closed
71typedef enum _GSUdpCloseReason
72{
73 GS_UDP_CLOSED_LOCALLY,
74 GS_UDP_CLOSED_REMOTELY,
75 // errors:
76 GS_UDP_CLOSED_BY_COMM_ERROR, // An invalid message was received (it was either unexpected or wrongly formatted).
77 GS_UDP_CLOSED_BY_LOW_MEM,
78 // Add new reasons before here
79 GS_UDP_CLOSED_NUM
80} GSUdpCloseReason;
81
84// Callbacks
85
86// Errors to give higher layers feedback
87typedef void (*gsUdpErrorCallback)(GSUdpErrorCode theCode, void *theUserData);
88
89
90// app Request attempt callback used to tell registered listeners about connection attempts
91typedef void (*gsUdpAppConnectAttemptCallback)(unsigned int theIp, unsigned short thePort,
92 int theLatency, unsigned char *theInitMsg,
93 unsigned int theInitMsgLen, void *theUserData);
94
95
96// peer communication channel callback types
97typedef void (*gsUdpConnClosedCallback)(unsigned int ip, unsigned short port, GSUdpCloseReason reason,
98 void *theUserData);
99typedef void (*gsUdpConnReceivedDataCallback)(unsigned int ip, unsigned short port,
100 unsigned char *message, unsigned int messageLength,
101 gsi_bool reliable, void *theUserData);
102typedef void (*gsUdpConnConnectedCallback)(unsigned int ip, unsigned short port,
103 GSUdpErrorCode error, gsi_bool rejected,
104 void *theUserData);
105typedef void (*gsUdpConnPingCallback)(unsigned int ip, unsigned short port, unsigned int latency,
106 void *theUserData);
107
108
109// Messages that cannot be interpreted are passed on to the higher level app
110typedef gsi_bool (*gsUdpUnknownMsgCallback)(unsigned int ip, unsigned short port,
111 unsigned char *message, unsigned int messageLength,
112 void *theUserData);
113
116// Public Functionality
117// Initialization and test functions
118gsi_bool gsUdpEngineIsInitialized();
119GSUdpErrorCode gsUdpEngineInitialize(unsigned short thePort, int theIncomingBufSize,
120 int theOutgoingBufSize, gsUdpErrorCallback theAppNetworkError,
121 gsUdpConnConnectedCallback theAppConnected,
122 gsUdpConnClosedCallback theAppClosed,
123 gsUdpConnPingCallback theAppPing,
124 gsUdpConnReceivedDataCallback theAppReceive,
125 gsUdpUnknownMsgCallback theAppUnownMsg,
126 gsUdpAppConnectAttemptCallback theAppConnectAttempt,
127 void *theAppUserData);
128// update and shutdown
129GSUdpErrorCode gsUdpEngineThink();
130GSUdpErrorCode gsUdpEngineShutdown();
131
132// Connectivity functions
133GSUdpErrorCode gsUdpEngineGetPeerState(unsigned int theIp, unsigned short thePort,
134 GSUdpPeerState *thePeerState);
135GSUdpErrorCode gsUdpEngineStartTalkingToPeer(unsigned int theIp, unsigned short thePort,
136 char theInitMsg[GS_UDP_MSG_HEADER_LEN], int timeOut);
137GSUdpErrorCode gsUdpEngineAcceptPeer(unsigned int theIp, unsigned short thePort);
138GSUdpErrorCode gsUdpEngineRejectPeer(unsigned int theIp, unsigned short thePort);
139
140// Sending functionality
141// WARNING: Messages should not be greater than the outgoing buffer size minus the header
142// and the 7 byte header for reliable messages (used for internal gt2 operations). Most
143// UDP fragmentation occurs if messages are bigger than 1500 bytes. Also, some routers are
144// known to drop those packets that are larger than 1500 bytes because of set MTU sizes.
145// The recommended outgoing buffer size is the default (1460). So take that, and subtract
146// 16 for message handler header and reliable message header (if sending data reliably).
147// freeSpace = 1460 - 16 - 7
148GSUdpErrorCode gsUdpEngineSendMessage(unsigned int theIp, unsigned short thePort,
149 char theHeader[GS_UDP_MSG_HEADER_LEN], unsigned char *theMsg,
150 unsigned int theMsgLen, gsi_bool theReliable);
151
152// This function should be called for those parts of the code that want specific handling of messages
153// Any call to send should include the header registered here.
154GSUdpErrorCode gsUdpEngineAddMsgHandler(char theInitMsg[GS_UDP_MSG_HEADER_LEN],
155 char theHeader[GS_UDP_MSG_HEADER_LEN],
156 gsUdpErrorCallback theMsgHandlerError,
157 gsUdpConnConnectedCallback theMsgHandlerConnected,
158 gsUdpConnClosedCallback theMsgHandlerClosed,
159 gsUdpConnPingCallback theMsgHandlerPing,
160 gsUdpConnReceivedDataCallback theMsgHandlerRecv,
161 void *theUserData);
162GSUdpErrorCode gsUdpEngineRemoveMsgHandler(char theHeader[GS_UDP_MSG_HEADER_LEN]);
165// Public Utility functionality
166SOCKET gsUdpEngineGetSocket();
167void gsUdpEngineAddrToString(unsigned int theIp, unsigned short thePort,
168 char addrstring[GS_IP_ADDR_AND_PORT]);
169unsigned int gsUdpEngineGetLocalAddr();
170unsigned short gsUdpEngineGetLocalPort();
171
172// lets the app or message handler know if it is able to shutdown the udp layer
173gsi_bool gsUdpEngineNoMoreMsgHandlers();
174gsi_bool gsUdpEngineNoApp();
175
176// check the remaining free space on the outgoing buffer for the peer based
177// IP and port
178int gsUdpEngineGetPeerOutBufferFreeSpace(unsigned int theIp, unsigned short thePort);
179
180#endif