00001 #include "NativeFeatureIncludes.h"
00002 #if _RAKNET_SUPPORT_UDPProxyServer==1
00003
00004 #include "UDPProxyServer.h"
00005 #include "BitStream.h"
00006 #include "UDPProxyCommon.h"
00007 #include "RakPeerInterface.h"
00008 #include "MessageIdentifiers.h"
00009
00010 using namespace RakNet;
00011
00012 UDPProxyServer::UDPProxyServer()
00013 {
00014 resultHandler=0;
00015 }
00016 UDPProxyServer::~UDPProxyServer()
00017 {
00018
00019 }
00020 void UDPProxyServer::SetResultHandler(UDPProxyServerResultHandler *rh)
00021 {
00022 resultHandler=rh;
00023 }
00024 bool UDPProxyServer::LoginToCoordinator(RakNet::RakString password, SystemAddress coordinatorAddress)
00025 {
00026 DataStructures::DefaultIndexType insertionIndex;
00027 insertionIndex = loggingInCoordinators.GetInsertionIndex(coordinatorAddress);
00028 if (insertionIndex==(DataStructures::DefaultIndexType)-1)
00029 return false;
00030 if (loggedInCoordinators.GetInsertionIndex(coordinatorAddress)==(DataStructures::DefaultIndexType)-1)
00031 return false;
00032 RakNet::BitStream outgoingBs;
00033 outgoingBs.Write((MessageID)ID_UDP_PROXY_GENERAL);
00034 outgoingBs.Write((MessageID)ID_UDP_PROXY_LOGIN_REQUEST_FROM_SERVER_TO_COORDINATOR);
00035 outgoingBs.Write(password);
00036 rakPeerInterface->Send(&outgoingBs, MEDIUM_PRIORITY, RELIABLE_ORDERED, 0, coordinatorAddress, false);
00037 loggingInCoordinators.InsertAtIndex(coordinatorAddress, insertionIndex, __FILE__, __LINE__ );
00038 return true;
00039 }
00040 void UDPProxyServer::Update(void)
00041 {
00042 udpForwarder.Update();
00043 }
00044 PluginReceiveResult UDPProxyServer::OnReceive(Packet *packet)
00045 {
00046
00047
00048 if (packet->data[0]==ID_UDP_PROXY_GENERAL && packet->length>1)
00049 {
00050 switch (packet->data[1])
00051 {
00052 case ID_UDP_PROXY_FORWARDING_REQUEST_FROM_COORDINATOR_TO_SERVER:
00053 if (loggedInCoordinators.GetIndexOf(packet->systemAddress)!=(DataStructures::DefaultIndexType)-1)
00054 {
00055 OnForwardingRequestFromCoordinatorToServer(packet);
00056 return RR_STOP_PROCESSING_AND_DEALLOCATE;
00057 }
00058 break;
00059 case ID_UDP_PROXY_NO_PASSWORD_SET_FROM_COORDINATOR_TO_SERVER:
00060 case ID_UDP_PROXY_WRONG_PASSWORD_FROM_COORDINATOR_TO_SERVER:
00061 case ID_UDP_PROXY_ALREADY_LOGGED_IN_FROM_COORDINATOR_TO_SERVER:
00062 case ID_UDP_PROXY_LOGIN_SUCCESS_FROM_COORDINATOR_TO_SERVER:
00063 {
00064 DataStructures::DefaultIndexType removalIndex = loggingInCoordinators.GetIndexOf(packet->systemAddress);
00065 if (removalIndex!=(DataStructures::DefaultIndexType)-1)
00066 {
00067 loggingInCoordinators.RemoveAtKey(packet->systemAddress, false, __FILE__, __LINE__ );
00068
00069 RakNet::BitStream incomingBs(packet->data, packet->length, false);
00070 incomingBs.IgnoreBytes(2);
00071 RakNet::RakString password;
00072 incomingBs.Read(password);
00073 switch (packet->data[1])
00074 {
00075 case ID_UDP_PROXY_NO_PASSWORD_SET_FROM_COORDINATOR_TO_SERVER:
00076 if (resultHandler)
00077 resultHandler->OnNoPasswordSet(password, this);
00078 break;
00079 case ID_UDP_PROXY_WRONG_PASSWORD_FROM_COORDINATOR_TO_SERVER:
00080 if (resultHandler)
00081 resultHandler->OnWrongPassword(password, this);
00082 break;
00083 case ID_UDP_PROXY_ALREADY_LOGGED_IN_FROM_COORDINATOR_TO_SERVER:
00084 if (resultHandler)
00085 resultHandler->OnAlreadyLoggedIn(password, this);
00086 break;
00087 case ID_UDP_PROXY_LOGIN_SUCCESS_FROM_COORDINATOR_TO_SERVER:
00088 RakAssert(loggedInCoordinators.GetIndexOf(packet->systemAddress)==(unsigned int)-1);
00089 loggedInCoordinators.Push(packet->systemAddress, __FILE__, __LINE__);
00090 if (resultHandler)
00091 resultHandler->OnLoginSuccess(password, this);
00092 break;
00093 }
00094 }
00095
00096
00097 return RR_STOP_PROCESSING_AND_DEALLOCATE;
00098 }
00099 }
00100 }
00101 return RR_CONTINUE_PROCESSING;
00102 }
00103 void UDPProxyServer::OnClosedConnection(SystemAddress systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason )
00104 {
00105 (void) lostConnectionReason;
00106 (void) rakNetGUID;
00107
00108 loggingInCoordinators.RemoveAtKey(systemAddress,false, __FILE__, __LINE__ );
00109 loggedInCoordinators.RemoveAtKey(systemAddress,false, __FILE__, __LINE__ );
00110 }
00111 void UDPProxyServer::OnRakPeerStartup(void)
00112 {
00113 udpForwarder.Startup();
00114 }
00115 void UDPProxyServer::OnRakPeerShutdown(void)
00116 {
00117 udpForwarder.Shutdown();
00118 loggingInCoordinators.Clear(true,__FILE__,__LINE__);
00119 loggedInCoordinators.Clear(true,__FILE__,__LINE__);
00120 }
00121 void UDPProxyServer::OnAttach(void)
00122 {
00123 if (rakPeerInterface->IsActive())
00124 OnRakPeerStartup();
00125 }
00126 void UDPProxyServer::OnDetach(void)
00127 {
00128 OnRakPeerShutdown();
00129 }
00130 void UDPProxyServer::OnForwardingRequestFromCoordinatorToServer(Packet *packet)
00131 {
00132 SystemAddress sourceAddress, targetAddress;
00133 RakNet::BitStream incomingBs(packet->data, packet->length, false);
00134 incomingBs.IgnoreBytes(2);
00135 incomingBs.Read(sourceAddress);
00136 incomingBs.Read(targetAddress);
00137 RakNetTimeMS timeoutOnNoDataMS;
00138 incomingBs.Read(timeoutOnNoDataMS);
00139 RakAssert(timeoutOnNoDataMS > 0 && timeoutOnNoDataMS < UDP_FORWARDER_MAXIMUM_TIMEOUT);
00140
00141 unsigned short srcToDestPort;
00142 unsigned short destToSourcePort;
00143 UDPForwarderResult success = udpForwarder.StartForwarding(sourceAddress, targetAddress, timeoutOnNoDataMS, 0, &srcToDestPort, &destToSourcePort, 0, 0);
00144 RakNet::BitStream outgoingBs;
00145 outgoingBs.Write((MessageID)ID_UDP_PROXY_GENERAL);
00146 outgoingBs.Write((MessageID)ID_UDP_PROXY_FORWARDING_REPLY_FROM_SERVER_TO_COORDINATOR);
00147 outgoingBs.Write(sourceAddress);
00148 outgoingBs.Write(targetAddress);
00149 outgoingBs.Write((unsigned char) success);
00150 if (success==UDPFORWARDER_SUCCESS)
00151 {
00152 outgoingBs.Write(srcToDestPort);
00153 outgoingBs.Write(destToSourcePort);
00154 }
00155 rakPeerInterface->Send(&outgoingBs, MEDIUM_PRIORITY, RELIABLE_ORDERED, 0, packet->systemAddress, false);
00156 }
00157
00158 #endif // _RAKNET_SUPPORT_*