public override void OnSerializeData(BitStream bitStream, ulong guid, int packet_size) { CustomIDs packet_id = (CustomIDs)bitStream.ReadByte();//the first byte of the packet is id, the rest is data switch (packet_id) { case CustomIDs.CLIENT_DATA: Debug.Log("[Server] Received client data from " + peer.GetAddress(guid, true) + " with guid " + guid); //creating a client data structure and putting it in a dictionary Clients.Add(guid, new ClientData(guid, bitStream.ReadString())); /* we inform the client that his data is accepted, we send it via a reliable channel */ using (PooledBitStream bsIn = BitStreamPool.GetBitStream()) { //be sure to reset the bitstream! If you do not do this the old recorded data will be sent bsIn.Reset(); //Write the packet id as a byte so that the client knows how to process the packet bsIn.Write((byte)CustomIDs.CLIENT_DATA_ACCEPTED); //As an example, we will send the text to the client in the load bsIn.Write("RakNet top... SosiPisos"); //we send data from bitstream to the client using its unique guid SendToClient(bitStream, guid, PacketPriority.IMMEDIATE_PRIORITY, PacketReliability.RELIABLE, 0); } break; } }
public static PooledBitStream GetBitStream() { PooledBitStream writer = Take(); writer.Reset(); return(writer); }
public override void OnConnected() { Debug.LogWarning("[Client] Connected to server"); //example using poolable bitStream using (PooledBitStream bitStream = BitStreamPool.GetBitStream()) { bitStream.Reset(); bitStream.Write((byte)CustomIDs.CLIENT_DATA); bitStream.Write(username); SendToServer(bitStream, PacketPriority.IMMEDIATE_PRIORITY, PacketReliability.RELIABLE, 0); } }