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
UDPProxyServer.cpp
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
16 #include "slikenet/NativeFeatureIncludes.h"
17 #if _RAKNET_SUPPORT_UDPProxyServer==1 && _RAKNET_SUPPORT_UDPForwarder==1
18
19 #include "slikenet/UDPProxyServer.h"
20 #include "slikenet/BitStream.h"
21 #include "slikenet/UDPProxyCommon.h"
22 #include "slikenet/peerinterface.h"
23 #include "slikenet/MessageIdentifiers.h"
24
25 using namespace SLNet;
26
27 STATIC_FACTORY_DEFINITIONS(UDPProxyServer,UDPProxyServer);
28
29 UDPProxyServer::UDPProxyServer()
30 {
31 resultHandler=0;
32 socketFamily=AF_INET;
33 }
34 UDPProxyServer::~UDPProxyServer()
35 {
36
37 }
38 void UDPProxyServer::SetSocketFamily(unsigned short _socketFamily)
39 {
40 socketFamily=_socketFamily;
41 }
42 void UDPProxyServer::SetResultHandler(UDPProxyServerResultHandler *rh)
43 {
44 resultHandler=rh;
45 }
46 bool UDPProxyServer::LoginToCoordinator(SLNet::RakString password, SystemAddress coordinatorAddress)
47 {
48 unsigned int insertionIndex;
49 bool objectExists;
50 insertionIndex=loggingInCoordinators.GetIndexFromKey(coordinatorAddress,&objectExists);
51 if (objectExists==true)
52 return false;
53 loggedInCoordinators.GetIndexFromKey(coordinatorAddress,&objectExists);
54 if (objectExists==true)
55 return false;
56 SLNet::BitStream outgoingBs;
57 outgoingBs.Write((MessageID)ID_UDP_PROXY_GENERAL);
58 outgoingBs.Write((MessageID)ID_UDP_PROXY_LOGIN_REQUEST_FROM_SERVER_TO_COORDINATOR);
59 outgoingBs.Write(password);
60 rakPeerInterface->Send(&outgoingBs, MEDIUM_PRIORITY, RELIABLE_ORDERED, 0, coordinatorAddress, false);
61 loggingInCoordinators.InsertAtIndex(coordinatorAddress, insertionIndex, _FILE_AND_LINE_ );
62 return true;
63 }
64 void UDPProxyServer::SetServerPublicIP(RakString ip)
65 {
66 serverPublicIp = ip;
67 }
68 void UDPProxyServer::Update(void)
69 {
70 }
71 PluginReceiveResult UDPProxyServer::OnReceive(Packet *packet)
72 {
73 // Make sure incoming messages from from UDPProxyCoordinator
74
75 if (packet->data[0]==ID_UDP_PROXY_GENERAL && packet->length>1)
76 {
77 bool objectExists;
78
79 switch (packet->data[1])
80 {
81 case ID_UDP_PROXY_FORWARDING_REQUEST_FROM_COORDINATOR_TO_SERVER:
82 if (loggedInCoordinators.GetIndexFromKey(packet->systemAddress, &objectExists)!=(unsigned int)-1)
83 {
84 OnForwardingRequestFromCoordinatorToServer(packet);
85 return RR_STOP_PROCESSING_AND_DEALLOCATE;
86 }
87 break;
88 case ID_UDP_PROXY_NO_PASSWORD_SET_FROM_COORDINATOR_TO_SERVER:
89 case ID_UDP_PROXY_WRONG_PASSWORD_FROM_COORDINATOR_TO_SERVER:
90 case ID_UDP_PROXY_ALREADY_LOGGED_IN_FROM_COORDINATOR_TO_SERVER:
91 case ID_UDP_PROXY_LOGIN_SUCCESS_FROM_COORDINATOR_TO_SERVER:
92 {
93 unsigned int removalIndex = loggingInCoordinators.GetIndexFromKey(packet->systemAddress, &objectExists);
94 if (objectExists)
95 {
96 loggingInCoordinators.RemoveAtIndex(removalIndex);
97
98 SLNet::BitStream incomingBs(packet->data, packet->length, false);
99 incomingBs.IgnoreBytes(2);
100 SLNet::RakString password;
101 incomingBs.Read(password);
102 switch (packet->data[1])
103 {
104 case ID_UDP_PROXY_NO_PASSWORD_SET_FROM_COORDINATOR_TO_SERVER:
105 if (resultHandler)
106 resultHandler->OnNoPasswordSet(password, this);
107 break;
108 case ID_UDP_PROXY_WRONG_PASSWORD_FROM_COORDINATOR_TO_SERVER:
109 if (resultHandler)
110 resultHandler->OnWrongPassword(password, this);
111 break;
112 case ID_UDP_PROXY_ALREADY_LOGGED_IN_FROM_COORDINATOR_TO_SERVER:
113 if (resultHandler)
114 resultHandler->OnAlreadyLoggedIn(password, this);
115 break;
116 case ID_UDP_PROXY_LOGIN_SUCCESS_FROM_COORDINATOR_TO_SERVER:
117 // RakAssert(loggedInCoordinators.GetIndexOf(packet->systemAddress)==(unsigned int)-1);
118 loggedInCoordinators.Insert(packet->systemAddress, packet->systemAddress, true, _FILE_AND_LINE_);
119 if (resultHandler)
120 resultHandler->OnLoginSuccess(password, this);
121 break;
122 }
123 }
124
125
126 return RR_STOP_PROCESSING_AND_DEALLOCATE;
127 }
128 }
129 }
130 return RR_CONTINUE_PROCESSING;
131 }
132 void UDPProxyServer::OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason )
133 {
134 (void) lostConnectionReason;
135 (void) rakNetGUID;
136
137 loggingInCoordinators.RemoveIfExists(systemAddress);
138 loggedInCoordinators.RemoveIfExists(systemAddress);
139 }
140 void UDPProxyServer::OnRakPeerStartup(void)
141 {
142 udpForwarder.Startup();
143 }
144 void UDPProxyServer::OnRakPeerShutdown(void)
145 {
146 udpForwarder.Shutdown();
147 loggingInCoordinators.Clear(true,_FILE_AND_LINE_);
148 loggedInCoordinators.Clear(true,_FILE_AND_LINE_);
149 }
150 void UDPProxyServer::OnAttach(void)
151 {
152 if (rakPeerInterface->IsActive())
153 OnRakPeerStartup();
154 }
155 void UDPProxyServer::OnDetach(void)
156 {
157 OnRakPeerShutdown();
158 }
159 void UDPProxyServer::OnForwardingRequestFromCoordinatorToServer(Packet *packet)
160 {
161 SystemAddress sourceAddress, targetAddress;
162 SLNet::BitStream incomingBs(packet->data, packet->length, false);
163 incomingBs.IgnoreBytes(2);
164 incomingBs.Read(sourceAddress);
165 incomingBs.Read(targetAddress);
166 SLNet::TimeMS timeoutOnNoDataMS;
167 incomingBs.Read(timeoutOnNoDataMS);
168 RakAssert(timeoutOnNoDataMS > 0 && timeoutOnNoDataMS <= UDP_FORWARDER_MAXIMUM_TIMEOUT);
169
170 unsigned short forwardingPort=0;
171 UDPForwarderResult success = udpForwarder.StartForwarding(sourceAddress, targetAddress, timeoutOnNoDataMS, 0, socketFamily, &forwardingPort, 0);
172 SLNet::BitStream outgoingBs;
173 outgoingBs.Write((MessageID)ID_UDP_PROXY_GENERAL);
174 outgoingBs.Write((MessageID)ID_UDP_PROXY_FORWARDING_REPLY_FROM_SERVER_TO_COORDINATOR);
175 outgoingBs.Write(sourceAddress);
176 outgoingBs.Write(targetAddress);
177 outgoingBs.Write(serverPublicIp);
178 outgoingBs.Write((unsigned char) success);
179 outgoingBs.Write(forwardingPort);
180 rakPeerInterface->Send(&outgoingBs, MEDIUM_PRIORITY, RELIABLE_ORDERED, 0, packet->systemAddress, false);
181 }
182
183 #endif // _RAKNET_SUPPORT_*