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; } }
void SendPossession() { var pawnIdx = byte.MaxValue; var pawnsOnReplica = Pawn.Replica.GetComponentsInChildren <Pawn>(); for (int i = 0; i < pawnsOnReplica.Length; ++i) { var pawnOnReplica = pawnsOnReplica[i]; if (Pawn == pawnOnReplica) { pawnIdx = (byte)i; break; } } Assert.IsTrue(pawnIdx != byte.MaxValue); var bs = BitStreamPool.Create(); bs.Write((byte)MessageId.PossessPawn); bs.Write(Pawn.Replica.Id); bs.Write(pawnIdx); Pawn.server.NetworkInterface.SendBitStream(bs, PacketReliability.ReliableSequenced, Connection); }
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); } }
public override void Dispose() { BitStreamPool.Recycle(this); }