00001 #include "NativeFeatureIncludes.h"
00002 #if _RAKNET_SUPPORT_RakNetTransport==1
00003
00004 #include "RakNetTransport.h"
00005 #include "RakNetworkFactory.h"
00006 #include "RakPeerInterface.h"
00007 #include "BitStream.h"
00008 #include "MessageIdentifiers.h"
00009 #include <stdio.h>
00010 #include <string.h>
00011 #include <stdarg.h>
00012 #include "LinuxStrings.h"
00013
00014 #ifdef _MSC_VER
00015 #pragma warning( push )
00016 #endif
00017
00018 RakNetTransportCommandParser::RakNetTransportCommandParser()
00019 {
00020 RegisterCommand(1, "SetPassword","Changes the console password to whatever.");
00021 RegisterCommand(0, "ClearPassword","Removes the console passwords.");
00022 RegisterCommand(0, "GetPassword","Gets the console password.");
00023 }
00024 RakNetTransportCommandParser::~RakNetTransportCommandParser()
00025 {
00026 }
00027 bool RakNetTransportCommandParser::OnCommand(const char *command, unsigned numParameters, char **parameterList, TransportInterface *transport, SystemAddress systemAddress, const char *originalString)
00028 {
00029 (void) originalString;
00030 (void) numParameters;
00031
00032 RakNetTransport *rnt = (RakNetTransport*) transport;
00033 if (strcmp(command, "SetPassword")==0)
00034 {
00035 rnt->SetIncomingPassword(parameterList[0]);
00036 rnt->Send(systemAddress, "Password changed to %s\r\n", parameterList[0]);
00037 }
00038 else if (strcmp(command, "ClearPassword")==0)
00039 {
00040 rnt->SetIncomingPassword(0);
00041 rnt->Send(systemAddress, "Password cleared\r\n");
00042 }
00043 else if (strcmp(command, "GetPassword")==0)
00044 {
00045 char *password;
00046 password=rnt->GetIncomingPassword();
00047 if (password[0])
00048 rnt->Send(systemAddress, "Password is %s\r\n",password);
00049 else
00050 rnt->Send(systemAddress, "No password is set.\r\n");
00051 }
00052 return true;
00053 }
00054 const char *RakNetTransportCommandParser::GetName(void) const
00055 {
00056 return "RakNetTransport";
00057 }
00058 void RakNetTransportCommandParser::SendHelp(TransportInterface *transport, SystemAddress systemAddress)
00059 {
00060 transport->Send(systemAddress, "Provides a secure connection between your console\r\n");
00061 transport->Send(systemAddress, "and the console server. Used to modify the console password.\r\n");
00062 }
00063 RakNetTransport::RakNetTransport()
00064 {
00065 rakPeer=0;
00066 }
00067 RakNetTransport::~RakNetTransport()
00068 {
00069 if (rakPeer)
00070 RakNetworkFactory::DestroyRakPeerInterface(rakPeer);
00071 }
00072 bool RakNetTransport::Start(unsigned short port, bool serverMode)
00073 {
00074 AutoAllocate();
00075 rakPeer->InitializeSecurity(0,0,0,0);
00076
00077 if (serverMode)
00078 {
00079
00080 rakPeer->SetMaximumIncomingConnections(8);
00081 }
00082
00083 SocketDescriptor socketDescriptor(port,0);
00084 return rakPeer->Startup(8, 250, &socketDescriptor, 1);
00085 }
00086 void RakNetTransport::Stop(void)
00087 {
00088 if (rakPeer==0) return;
00089 rakPeer->Shutdown(1000, 0);
00090 newConnections.Clear(__FILE__, __LINE__);
00091 lostConnections.Clear(__FILE__, __LINE__);
00092 }
00093 void RakNetTransport::Send( SystemAddress systemAddress, const char *data, ... )
00094 {
00095 if (rakPeer==0) return;
00096 if (data==0 || data[0]==0) return;
00097
00098 char text[REMOTE_MAX_TEXT_INPUT];
00099 va_list ap;
00100 va_start(ap, data);
00101 _vsnprintf(text, REMOTE_MAX_TEXT_INPUT, data, ap);
00102 va_end(ap);
00103 text[REMOTE_MAX_TEXT_INPUT-1]=0;
00104
00105 RakNet::BitStream str;
00106 str.Write((MessageID)ID_TRANSPORT_STRING);
00107 str.Write(text, (int) strlen(text));
00108 str.Write((unsigned char) 0);
00109 rakPeer->Send(&str, MEDIUM_PRIORITY, RELIABLE_ORDERED, 0, systemAddress, (systemAddress==UNASSIGNED_SYSTEM_ADDRESS)!=0);
00110 }
00111 void RakNetTransport::CloseConnection( SystemAddress systemAddress )
00112 {
00113 rakPeer->CloseConnection(systemAddress, true, 0);
00114 }
00115 Packet* RakNetTransport::Receive( void )
00116 {
00117 if (rakPeer==0) return 0;
00118 Packet *p;
00119 p=rakPeer->Receive();
00120 if (p==0)
00121 return 0;
00122 if (p->data[0]==ID_TRANSPORT_STRING)
00123 {
00124 p->data++;
00125 return p;
00126 }
00127 if (p->data[0]==ID_NEW_INCOMING_CONNECTION)
00128 {
00129 newConnections.Push(p->systemAddress, __FILE__, __LINE__ );
00130 }
00131 else if (p->data[0]==ID_DISCONNECTION_NOTIFICATION || p->data[0]==ID_CONNECTION_LOST)
00132 {
00133 lostConnections.Push(p->systemAddress, __FILE__, __LINE__ );
00134 }
00135 rakPeer->DeallocatePacket(p);
00136
00137 return 0;
00138 }
00139 SystemAddress RakNetTransport::HasNewIncomingConnection(void)
00140 {
00141 if (newConnections.Size())
00142 return newConnections.Pop();
00143 return UNASSIGNED_SYSTEM_ADDRESS;
00144 }
00145 SystemAddress RakNetTransport::HasLostConnection(void)
00146 {
00147 if (lostConnections.Size())
00148 return lostConnections.Pop();
00149 return UNASSIGNED_SYSTEM_ADDRESS;
00150 }
00151 void RakNetTransport::SetIncomingPassword(const char *password)
00152 {
00153 if (password)
00154 rakPeer->SetIncomingPassword(password, (int) strlen(password)+1);
00155 else
00156 rakPeer->SetIncomingPassword(0, 0);
00157 }
00158 char * RakNetTransport::GetIncomingPassword(void)
00159 {
00160 static char password[256];
00161 int passwordLength=255;
00162 rakPeer->GetIncomingPassword((char*)password, &passwordLength);
00163 password[passwordLength]=0;
00164 return (char*) password;
00165 }
00166 void RakNetTransport::DeallocatePacket( Packet *packet )
00167 {
00168 if (rakPeer==0) return;
00169 packet->data--;
00170 rakPeer->DeallocatePacket(packet);
00171 }
00172 void RakNetTransport::AutoAllocate(void)
00173 {
00174 if (rakPeer==0)
00175 rakPeer=RakNetworkFactory::GetRakPeerInterface();
00176 }
00177 CommandParserInterface* RakNetTransport::GetCommandParser(void)
00178 {
00179 return &rakNetTransportCommandParser;
00180 }
00181
00182 #ifdef _MSC_VER
00183 #pragma warning( pop )
00184 #endif
00185
00186 #endif // _RAKNET_SUPPORT_*