• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

RPC4Plugin.cpp

Go to the documentation of this file.
00001 #include "NativeFeatureIncludes.h"
00002 #if _RAKNET_SUPPORT_RPC4Plugin==1
00003 
00004 #include "RPC4Plugin.h"
00005 #include "MessageIdentifiers.h"
00006 #include "RakPeerInterface.h"
00007 #include "PacketizedTCP.h"
00008 
00009 using namespace RakNet;
00010 
00011 RPC4Plugin::RPC4Plugin()
00012 {
00013 
00014 }
00015 RPC4Plugin::~RPC4Plugin()
00016 {
00017 
00018 }
00019 bool RPC4Plugin::RegisterFunction(const char* uniqueID, void ( *functionPointer ) ( RakNet::BitStream *userData, Packet *packet ))
00020 {
00021         DataStructures::StringKeyedHashIndex skhi = registeredFunctions.GetIndexOf(uniqueID);
00022         if (skhi.IsInvalid()==false)
00023                 return false;
00024 
00025         registeredFunctions.Push(uniqueID,functionPointer,__FILE__,__LINE__);
00026         return true;
00027 }
00028 bool RPC4Plugin::UnregisterFunction(const char* uniqueID)
00029 {
00030         void ( *f ) ( RakNet::BitStream *, Packet * );
00031         return registeredFunctions.Pop(f,uniqueID,__FILE__,__LINE__);
00032 }
00033 void RPC4Plugin::CallLoopback( const char* uniqueID, RakNet::BitStream * bitStream )
00034 {
00035         Packet *p=0;
00036 
00037         DataStructures::StringKeyedHashIndex skhi = registeredFunctions.GetIndexOf(uniqueID);
00038 
00039         if (skhi.IsInvalid()==true)
00040         {
00041                 if (rakPeerInterface) 
00042                         p=rakPeerInterface->AllocatePacket(sizeof(MessageID)+sizeof(unsigned char)+strlen(uniqueID)+1);
00043 #if _RAKNET_SUPPORT_PacketizedTCP==1
00044                 else
00045                         p=packetizedTCP->AllocatePacket(sizeof(MessageID)+sizeof(unsigned char)+strlen(uniqueID)+1);
00046 #endif
00047 
00048                 if (rakPeerInterface)
00049                         p->guid=rakPeerInterface->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS);
00050 #if _RAKNET_SUPPORT_PacketizedTCP==1
00051                 else
00052                         p->guid=UNASSIGNED_RAKNET_GUID;
00053 #endif
00054 
00055                 p->systemAddress=UNASSIGNED_SYSTEM_ADDRESS;
00056                 p->systemAddress.systemIndex=(SystemIndex)-1;
00057                 p->data[0]=ID_RPC_REMOTE_ERROR;
00058                 p->data[1]=RPC_ERROR_FUNCTION_NOT_REGISTERED;
00059                 strcpy((char*) p->data+2, uniqueID);
00060                 
00061                 PushBackPacketUnified(p,false);
00062 
00063                 return;
00064         }
00065 
00066         RakNet::BitStream out;
00067         out.Write((MessageID) ID_RPC_4_PLUGIN);
00068         out.WriteCompressed(uniqueID);
00069         if (bitStream)
00070         {
00071                 bitStream->ResetReadPointer();
00072                 out.Write(bitStream);
00073         }
00074         if (rakPeerInterface) 
00075                 p=rakPeerInterface->AllocatePacket(out.GetNumberOfBytesUsed());
00076 #if _RAKNET_SUPPORT_PacketizedTCP==1
00077         else
00078                 p=packetizedTCP->AllocatePacket(out.GetNumberOfBytesUsed());
00079 #endif
00080 
00081         if (rakPeerInterface)
00082                 p->guid=rakPeerInterface->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS);
00083 #if _RAKNET_SUPPORT_PacketizedTCP==1
00084         else
00085                 p->guid=UNASSIGNED_RAKNET_GUID;
00086 #endif
00087         p->systemAddress=UNASSIGNED_SYSTEM_ADDRESS;
00088         p->systemAddress.systemIndex=(SystemIndex)-1;
00089         memcpy(p->data,out.GetData(),out.GetNumberOfBytesUsed());
00090         PushBackPacketUnified(p,false);
00091         return;
00092 }
00093 void RPC4Plugin::Call( const char* uniqueID, RakNet::BitStream * bitStream, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast )
00094 {
00095         RakNet::BitStream out;
00096         out.Write((MessageID) ID_RPC_4_PLUGIN);
00097         out.WriteCompressed(uniqueID);
00098         if (bitStream)
00099         {
00100                 bitStream->ResetReadPointer();
00101                 out.Write(bitStream);
00102         }
00103         SendUnified(&out,priority,reliability,orderingChannel,systemIdentifier,broadcast);
00104 }
00105 PluginReceiveResult RPC4Plugin::OnReceive(Packet *packet)
00106 {
00107         if (packet->data[0]==ID_RPC_4_PLUGIN)
00108         {
00109                 RakNet::BitStream bsIn(packet->data,packet->length,false);
00110                 bsIn.IgnoreBytes(1);
00111                 RakNet::RakString functionName;
00112                 bsIn.ReadCompressed(functionName);
00113                 DataStructures::StringKeyedHashIndex skhi = registeredFunctions.GetIndexOf(functionName.C_String());
00114                 if (skhi.IsInvalid())
00115                 {
00116                         RakNet::BitStream bsOut;
00117                         bsOut.Write((unsigned char) ID_RPC_REMOTE_ERROR);
00118                         bsOut.Write((unsigned char) RPC_ERROR_FUNCTION_NOT_REGISTERED);
00119                         bsOut.Write(functionName.C_String(),functionName.GetLength()+1);
00120                         SendUnified(&bsOut,HIGH_PRIORITY,RELIABLE_ORDERED,0,packet->systemAddress,false);
00121                         return RR_STOP_PROCESSING_AND_DEALLOCATE;
00122                 }
00123 
00124                 void ( *fp ) ( RakNet::BitStream *, Packet * );
00125                 fp = registeredFunctions.ItemAtIndex(skhi);
00126                 fp(&bsIn,packet);
00127                 return RR_STOP_PROCESSING_AND_DEALLOCATE;
00128         }
00129 
00130         return RR_CONTINUE_PROCESSING;
00131 }
00132 
00133 #endif // _RAKNET_SUPPORT_*

Generated on Thu Sep 30 2010 01:27:28 for RakNet by  doxygen 1.7.1