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
NatPunchthroughServer.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_NatPunchthroughServer==1
23
24 #ifndef __NAT_PUNCHTHROUGH_SERVER_H
25 #define __NAT_PUNCHTHROUGH_SERVER_H
26
27 #include "types.h"
28 #include "Export.h"
29 #include "PluginInterface2.h"
30 #include "PacketPriority.h"
31 #include "SocketIncludes.h"
32 #include "DS_OrderedList.h"
33 #include "string.h"
34
35 namespace SLNet
36 {
38 class RakPeerInterface;
39 struct Packet;
40 #if _RAKNET_SUPPORT_PacketLogger==1
41 class PacketLogger;
42 #endif
43
48
50 struct RAK_DLL_EXPORT NatPunchthroughServerDebugInterface
51 {
52 NatPunchthroughServerDebugInterface() {}
53 virtual ~NatPunchthroughServerDebugInterface() {}
54 virtual void OnServerMessage(const char *msg)=0;
55 };
56
58 struct RAK_DLL_EXPORT NatPunchthroughServerDebugInterface_Printf : public NatPunchthroughServerDebugInterface
59 {
60 virtual void OnServerMessage(const char *msg);
61 };
62
63 #if _RAKNET_SUPPORT_PacketLogger==1
64
65 struct RAK_DLL_EXPORT NatPunchthroughServerDebugInterface_PacketLogger : public NatPunchthroughServerDebugInterface
66 {
67 // Set to non-zero to write to the packetlogger!
68 PacketLogger *pl;
69
70 NatPunchthroughServerDebugInterface_PacketLogger() {pl=0;}
71 ~NatPunchthroughServerDebugInterface_PacketLogger() {}
72 virtual void OnServerMessage(const char *msg);
73 };
74 #endif
75
83 class RAK_DLL_EXPORT NatPunchthroughServer : public PluginInterface2
84 {
85 public:
86
87 STATIC_FACTORY_DECLARATIONS(NatPunchthroughServer)
88
89 // Constructor
90 NatPunchthroughServer();
91
92 // Destructor
93 virtual ~NatPunchthroughServer();
94
97 void SetDebugInterface(NatPunchthroughServerDebugInterface *i);
98
100 virtual void Update(void);
101
103 virtual PluginReceiveResult OnReceive(Packet *packet);
104
106 virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason );
107 virtual void OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming);
108
109 // Each connected user has a ready state. Ready means ready for nat punchthrough.
110 struct User;
111 struct ConnectionAttempt
112 {
113 ConnectionAttempt() {sender=0; recipient=0; startTime=0; attemptPhase=NAT_ATTEMPT_PHASE_NOT_STARTED;}
114 User *sender, *recipient;
115 uint16_t sessionId;
116 SLNet::Time startTime;
117 enum
118 {
119 NAT_ATTEMPT_PHASE_NOT_STARTED,
120 NAT_ATTEMPT_PHASE_GETTING_RECENT_PORTS,
121 } attemptPhase;
122 };
123 struct User
124 {
125 RakNetGUID guid;
126 SystemAddress systemAddress;
127 unsigned short mostRecentPort;
128 bool isReady;
129 DataStructures::OrderedList<RakNetGUID,RakNetGUID> groupPunchthroughRequests;
130
131 DataStructures::List<ConnectionAttempt *> connectionAttempts;
132 bool HasConnectionAttemptToUser(User *user);
133 void DerefConnectionAttempt(ConnectionAttempt *ca);
134 void DeleteConnectionAttempt(ConnectionAttempt *ca);
135 void LogConnectionAttempts(SLNet::RakString &rs);
136 };
137 SLNet::Time lastUpdate;
138 static int NatPunchthroughUserComp( const RakNetGUID &key, User * const &data );
139 protected:
140 void OnNATPunchthroughRequest(Packet *packet);
141 DataStructures::OrderedList<RakNetGUID, User*, NatPunchthroughServer::NatPunchthroughUserComp> users;
142
143 void OnGetMostRecentPort(Packet *packet);
144 void OnClientReady(Packet *packet);
145
146 void SendTimestamps(void);
147 void StartPendingPunchthrough(void);
148 void StartPunchthroughForUser(User*user);
149 uint16_t sessionId;
150 NatPunchthroughServerDebugInterface *natPunchthroughServerDebugInterface;
151
152 SystemAddress boundAddresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS];
153 unsigned char boundAddressCount;
154
155 };
156
157 } // namespace SLNet
158
159 #endif
160
161 #endif // _RAKNET_SUPPORT_*