SLikeNet 0.1.3

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Router2.h
Go to the documentation of this file.
1 /*
2 * Original work: Copyright (c) 2014, Oculus VR, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * RakNet License.txt file in the licenses directory of this source tree. An additional grant
7 * of patent rights can be found in the RakNet Patents.txt file in the same directory.
8 *
9 *
10 * Modified work: Copyright (c) 2017, SLikeSoft UG (haftungsbeschränkt)
11 *
12 * This source code was modified by SLikeSoft. Modifications are licensed under the MIT-style
13 * license found in the license.txt file in the root directory of this source tree.
14 */
15
19
20
21 #include "NativeFeatureIncludes.h"
22 #if _RAKNET_SUPPORT_Router2==1 && _RAKNET_SUPPORT_UDPForwarder==1
23
24 #ifndef __ROUTER_2_PLUGIN_H
25 #define __ROUTER_2_PLUGIN_H
26
27 #include "types.h"
28 #include "PluginInterface2.h"
29 #include "PacketPriority.h"
30 #include "Export.h"
31 #include "UDPForwarder.h"
32 #include "MessageIdentifiers.h"
33 #include "DS_List.h"
34 #include "SimpleMutex.h"
35
36 namespace SLNet
37 {
39 class RakPeerInterface;
40
41 struct Router2DebugInterface
42 {
43 Router2DebugInterface() {}
44 virtual ~Router2DebugInterface() {}
45 virtual void ShowFailure(const char *message);
46 virtual void ShowDiagnostic(const char *message);
47 };
48
55
59 class RAK_DLL_EXPORT Router2 : public PluginInterface2
60 {
61 public:
62 // GetInstance() and DestroyInstance(instance*)
63 STATIC_FACTORY_DECLARATIONS(Router2)
64
65 Router2();
66 virtual ~Router2();
67
70 void SetSocketFamily(unsigned short _socketFamily);
71
90 void EstablishRouting(RakNetGUID endpointGuid);
91
94 void SetMaximumForwardingRequests(int max);
95
97 void SetDebugInterface(Router2DebugInterface *_debugInterface);
98
100 Router2DebugInterface *GetDebugInterface(void) const;
101
102 // --------------------------------------------------------------------------------------------
103 // Packet handling functions
104 // --------------------------------------------------------------------------------------------
105 virtual PluginReceiveResult OnReceive(Packet *packet);
106 virtual void Update(void);
107 virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason );
108 virtual void OnFailedConnectionAttempt(Packet *packet, PI2_FailedConnectionAttemptReason failedConnectionAttemptReason);
109 virtual void OnRakPeerShutdown(void);
110
111
112 enum Router2RequestStates
113 {
114 R2RS_REQUEST_STATE_QUERY_FORWARDING,
115 REQUEST_STATE_REQUEST_FORWARDING,
116 };
117
118 struct ConnectionRequestSystem
119 {
120 RakNetGUID guid;
121 int pingToEndpoint;
122 unsigned short usedForwardingEntries;
123 };
124
125 struct ConnnectRequest
126 {
127 ConnnectRequest();
128 ~ConnnectRequest();
129
130 DataStructures::List<ConnectionRequestSystem> connectionRequestSystems;
131 SimpleMutex connectionRequestSystemsMutex;
132 Router2RequestStates requestState;
133 SLNet::TimeMS pingTimeout;
134 RakNetGUID endpointGuid;
135 RakNetGUID lastRequestedForwardingSystem;
136 bool returnConnectionLostOnFailure;
137 unsigned int GetGuidIndex(RakNetGUID guid);
138 };
139
140 unsigned int GetConnectionRequestIndex(RakNetGUID endpointGuid);
141
142 struct MiniPunchRequest
143 {
144 RakNetGUID endpointGuid;
145 SystemAddress endpointAddress;
146 bool gotReplyFromEndpoint;
147 RakNetGUID sourceGuid;
148 SystemAddress sourceAddress;
149 bool gotReplyFromSource;
150 SLNet::TimeMS timeout;
151 SLNet::TimeMS nextAction;
152 unsigned short forwardingPort;
153 __UDPSOCKET__ forwardingSocket;
154 };
155
156 struct ForwardedConnection
157 {
158 RakNetGUID endpointGuid;
159 RakNetGUID intermediaryGuid;
160 SystemAddress intermediaryAddress;
161 bool returnConnectionLostOnFailure;
162 bool weInitiatedForwarding;
163 };
164
165 protected:
166
167 bool UpdateForwarding(ConnnectRequest* connectionRequest);
168 void RemoveConnectionRequest(unsigned int connectionRequestIndex);
169 void RequestForwarding(ConnnectRequest* connectionRequest);
170 void OnQueryForwarding(Packet *packet);
171 void OnQueryForwardingReply(Packet *packet);
172 void OnRequestForwarding(Packet *packet);
173 void OnRerouted(Packet *packet);
174 void OnMiniPunchReply(Packet *packet);
175 void OnMiniPunchReplyBounce(Packet *packet);
176 bool OnForwardingSuccess(Packet *packet);
177 int GetLargestPingAmongConnectedSystems(void) const;
178 void ReturnToUser(MessageID messageId, RakNetGUID endpointGuid, const SystemAddress &systemAddress, bool wasGeneratedLocally);
179 bool ConnectInternal(RakNetGUID endpointGuid, bool returnConnectionLostOnFailure);
180
181 UDPForwarder *udpForwarder;
182 int maximumForwardingRequests;
183 SimpleMutex connectionRequestsMutex, miniPunchesInProgressMutex, forwardedConnectionListMutex;
184 DataStructures::List<ConnnectRequest*> connectionRequests;
185 DataStructures::List<MiniPunchRequest> miniPunchesInProgress;
186 // Forwarding we have initiated
187 DataStructures::List<ForwardedConnection> forwardedConnectionList;
188
189 void ClearConnectionRequests(void);
190 void ClearMinipunches(void);
191 void ClearForwardedConnections(void);
192 void ClearAll(void);
193 int ReturnFailureOnCannotForward(RakNetGUID sourceGuid, RakNetGUID endpointGuid);
194 void SendFailureOnCannotForward(RakNetGUID sourceGuid, RakNetGUID endpointGuid);
195 void SendForwardingSuccess(MessageID messageId, RakNetGUID sourceGuid, RakNetGUID endpointGuid, unsigned short sourceToDstPort);
196 void SendOOBFromRakNetPort(OutOfBandIdentifiers oob, BitStream *extraData, SystemAddress sa);
197 void SendOOBFromSpecifiedSocket(OutOfBandIdentifiers oob, SystemAddress sa, __UDPSOCKET__ socket);
198 void SendOOBMessages(MiniPunchRequest *mpr);
199
200 Router2DebugInterface *debugInterface;
201 unsigned short socketFamily;
202 };
203
204 }
205
206 #endif
207
208 #endif // _RAKNET_SUPPORT_*