Пример #1
0
    public void Can_serialize_and_deserialize_entities()
    {
        var componentManager = new ComponentManager();
        var entityManager    = new EntityManager(componentManager);
        var entityFactory    = new EntityFactory();
        var serializer       = new Serializer();

        entityFactory.CreatePlayer(entityManager.Create(), new Vector2(200, 400), 1, PlayerIndex.One, default);
        entityFactory.CreatePlayer(entityManager.Create(), new Vector2(300, 400), 2, PlayerIndex.Two, default);

        var state = FullGameState.CreateFullGameState(componentManager, entityManager.Entities, 1, DateTime.UtcNow, DateTime.UtcNow.AddSeconds(1), 1);

        var writer = new NetDataWriter();

        writer.Put(serializer.Serialize(state));
        var bytes  = writer.CopyData();
        var reader = new NetDataReader(bytes);
        var target = serializer.Deserialize <FullGameState>(reader.GetRemainingBytes());

        target.Components.Should()
        .BeEquivalentTo(state.Components, o => o
                        .Excluding(c => c.Path.EndsWith(nameof(CollisionBody.OldPosition)) ||
                                   c.Path.EndsWith(nameof(PlayerState.LocalPlayerIndex)) ||
                                   c.Path.EndsWith(nameof(PlayerState.IsLocal))
                                   )
                        );
    }
 public override void ReadPayload(NetDataReader message)
 {
     base.ReadPayload(message);
     Bounds     = new Rectangle(message.GetInt(), message.GetInt(), message.GetInt(), message.GetInt());
     Fullscreen = message.GetBool();
     Image      = message.GetRemainingBytes();
 }
Пример #3
0
        private void HandleRecvData(byte[] buffer, int length, TransportEventData eventData)
        {
            recvReader.Clear();
            recvReader.SetSource(buffer, 0, length);
            eventData.type = (ENetworkEvent)recvReader.GetByte();
            switch (eventData.type)
            {
            case ENetworkEvent.ConnectEvent:
                // This must received at clients only, then create new kcp here
                clientConnId = recvReader.GetUInt();
                int remotePort = recvReader.GetInt();
                eventData.connectionId = clientConnId;
                eventQueue.Enqueue(eventData);
                kcpHandles[clientConnId] = CreateKcp(clientConnId, clientSetting);
                kcpHandles[clientConnId].remoteEndPoint = new IPEndPoint(eventData.endPoint.Address, remotePort);
                break;

            case ENetworkEvent.DataEvent:
                // Read remaining data
                eventData.reader = new NetDataReader(recvReader.GetRemainingBytes());
                eventQueue.Enqueue(eventData);
                break;

            case ENetworkEvent.DisconnectEvent:
                // This must received at clients only to force them to stop client
                eventQueue.Enqueue(eventData);
                break;
            }
        }
Пример #4
0
    public void OnNetworkReceive(NetPeer peer, NetDataReader reader)
    {
        if (!imageInitialized)
        {
            string s = reader.GetString(32);
            if (s != string.Empty)
            {
                print("received: " + s);
                int w = 0;
                if (int.TryParse(Utils.EatString(ref s), out w))
                {
                    int h;
                    if (int.TryParse(Utils.EatString(ref s), out h))
                    {
                        clientTex = new Texture2D(w, h, StreamingManager.Inst.textureFormat, false);
                        buffer    = new byte[w * h * Utils.SomeTextureFormatsToBytes(StreamingManager.Inst.textureFormat)];//where 16 is fixed render texture bit depth
                        clientRenderTex.texture = clientTex;
                    }
                }
                imageInitialized = true;
            }
            return;
        }
        //partially fill buffer at given index
        int             start    = reader.GetInt();
        CompressionMode compMode = (CompressionMode)reader.GetByte();

        byte[] payload = reader.GetRemainingBytes();
        if (compMode != CompressionMode.none)
        {
            payload = Compressor.UnPack(payload, compMode);
        }
        System.Array.Copy(payload, 0, buffer, start, payload.Length);
    }
Пример #5
0
 public void Deserialize(NetDataReader reader)
 {
     networkID    = reader.GetInt();
     sceneId      = reader.GetInt();
     position     = new Vector3(reader.GetFloat(), reader.GetFloat(), reader.GetFloat());
     initialState = reader.GetRemainingBytes();
 }
Пример #6
0
 public void Deserialize(NetDataReader reader)
 {
     networkID      = reader.GetInt();
     rpcMethodIndex = reader.GetInt();
     frameTick      = reader.GetUShort();
     parameters     = reader.GetRemainingBytes();
 }
Пример #7
0
 public override void ReadPayload(NetDataReader message)
 {
     base.ReadPayload(message);
     Folder = message.GetString(250);
     Name   = message.GetString(250);
     Data   = message.GetRemainingBytes();
 }
Пример #8
0
 public virtual void Deserialize(NetDataReader reader)
 {
     networkID    = reader.GetInt();
     assetIndex   = reader.GetInt();
     position     = new Vector3(reader.GetFloat(), reader.GetFloat(), reader.GetFloat());
     frameTick    = reader.GetUShort();
     initialState = reader.GetRemainingBytes();
 }
Пример #9
0
        public void Route(NetPeer peer, NetDataReader netDataReader)
        {
            DataReader dataReader = new DataReader(netDataReader.GetRemainingBytes());

            while (dataReader.AvailableBytes > 0)
            {
                RouteParentPacket(peer, dataReader);
            }
        }
Пример #10
0
        public static IdentificationPacket GetIdentification(byte[] data)
        {
            IdentificationPacket p = new IdentificationPacket();
            NetDataReader        r = new NetDataReader(data);

            p.name = r.GetString(r.GetInt());
            p.id   = new Guid(r.GetRemainingBytes());
            return(p);
        }
Пример #11
0
    public Snapshot ReadSnapshot(NetDataReader reader, out int ackedTick)
    {
        lastReceivedPacketTime = DateTimeOffset.Now.ToUnixTimeMilliseconds();

        var snapshot = compressionManager.DecodeSnapshot(reader.GetRemainingBytes(), out ackedTick);

        if (LastAckedSnapshot < snapshot.Tick)
        {
            LastAckedSnapshot = snapshot.Tick;
        }

        return(snapshot);
    }
Пример #12
0
 public void ReadData(NetDataReader reader)
 {
     posX = reader.GetFloat();
     posY = reader.GetFloat();
     posZ = reader.GetFloat();
     rotX = reader.GetFloat();
     rotY = reader.GetFloat();
     rotZ = reader.GetFloat();
     rotW = reader.GetFloat();
     if (!reader.EndOfData)
     {
         extraData = reader.GetRemainingBytes();
     }
 }
Пример #13
0
        public void Deserialize(NetDataReader reader)
        {
            playerControllerId = reader.GetShort();
            msgData            = reader.GetRemainingBytes();

            if (msgData == null)
            {
                msgSize = 0;
            }
            else
            {
                msgSize = msgData.Length;
            }
        }
Пример #14
0
        /// <summary>
        /// Wrapper callback for incoming packets to translate from network library-specific classes to more generic ones.
        /// Called whenever any packet type with a registered handler is received to adapt the callback values.
        /// </summary>
        /// <param name="peer">Peer the packet originated from</param>
        /// <param name="reader">Data reader containing the payload</param>
        private void packetHandlerCallbackWrapper(NetPeer peer, NetDataReader reader)
        {
            // Get the connection ID by converting LiteNetLib's connection id long to a hex string
            string connectionId = peer.ConnectId.ToString("X");

            // Get the module string and message data
            string type = reader.GetString();

            byte[] data = reader.GetRemainingBytes();

            // Invoke the packet handler responsible for this packet type
            if (registeredPacketHandlers.ContainsKey(type))
            {
                registeredPacketHandlers[type].Invoke(type, connectionId, data);
            }
        }
Пример #15
0
 internal ConnectionRequest(
     long connectionId,
     byte connectionNumber,
     ConnectionRequestType type,
     NetDataReader netDataReader,
     NetPeer peer,
     IConnectionRequestListener listener)
 {
     ConnectionId     = connectionId;
     ConnectionNumber = connectionNumber;
     Type             = type;
     Peer             = peer;
     // var dd = netDataReader.GetString();
     Data      = netDataReader.GetRemainingBytes();
     _listener = listener;
 }
Пример #16
0
 public virtual void Deserialize(NetDataReader reader)
 {
     networkID = reader.GetInt();
     dirtyFlag = reader.GetInt();
     state     = reader.GetRemainingBytes();
 }