Hey Folks,
some time ago i made a C++ plugin which integrates an easy interface to RakNet (C++ UDP networking library). I wondered if there is interest in the community for this?
To make it short here are some examples how to use it in Unity’s C#:
Here we start a server and a client which connects to it (if server started successfully) on one machine:
if( false == GabNetAP.IsServer() false == GabNetAP.IsClient() )
{
// 32 players max., port 1234
if( GabNetAP.StartServer( 32, 1234 ) )
{
GabNetAP.StartClient( "localhost", 1234 );
}
}
This is how the client sends some packets (the server would do it quite the same):
GabNetAP.NewOutPacket( GabNetAP.eMsgID.ID_MSG_CS_TEST, true, GabNetAP.fromClient );
byte testbyte = 4;
GabNetAP.AddByteToOutPacket( testbyte ); // there are functions for: byte, short, int, string, float ....
// "-1": client id isnt neccessary for clients (they always send to server) - server would define receiver with this arg
GabNetAP.SendOutPacket( GabNetAP.ePriority.MEDIUM, GabNetAP.eReliability.UNRELIABLE, -1 );
This is how the server would read this package (the client would do it quite the same):
int serverPacketCount = GabNetAP.GetPacketCount( GabNetAP.fromServer );
for( int i=0; i<serverPacketCount; ++i )
{
switch( (GabNetAP.eMsgID)GabNetAP.GetMessageIdFromPacket( i, GabNetAP.fromServer ) )
{
case GabNetAP.eMsgID.ID_MSG_CS_TEST:
{
// this is standard info sent with the packet
short clientId = GabNetAP.GetClientIdFromPacket( i, GabNetAP.fromServer );
double timestamp = GabNetAP.GetTimestampFromPacket( i, GabNetAP.fromServer );
// custom packet info (this is what we added on the client)
byte testbyte = GabNetAP.GetByteFromPacket( i, GabNetAP.fromServer );
}
break;
}
}
Of course there are also some helper functions like:
// am i a server ?
GabNetAP.IsServer()
// and/or am i client which has an open connection to a server?
GabNetAP.IsClientConnected()
// oooor am i client which still waits for the server to respond to his connection attempt?
GabNetAP.IsClient()
… and many more.
I optimized all internal packeting and stuff for maximum speed and minimum packet size. All clients automatically get a client id from the server for their session, which is needed for the communication.
If there is enough interest i might release the full C++ source and the unity3d stuff as product in the Asset Store or on my shop (see signature) or both …
(This wouldnt include a RakNet licence though)
… anyone?
Cheers
Ethan