예제 #1
0
        public void StatusUpdateTest()
        {
            // sample message (this caused deserialization issues)
            var message = new StatusUpdateMessage()
            {
                InstanceHash  = new Hash(new byte[] { 1, 2, 3 }),
                KnownPackages = new PackageStatus[0],
                KnownPeers    = new DiscoveryPeerData[] {
                    new DiscoveryPeerData()
                    {
                        ServiceEndpoint = new IPEndPoint(IPAddress.Parse("192.168.0.110"), 1234),
                        PeerId          = new Hash(new byte[] { 4, 5, 6 })
                    }
                },
                ServicePort  = 5432,
                PeerEndpoint = new IPEndPoint(IPAddress.Parse("192.168.0.109"), 5678)
            };

            // serialize/deserialize
            IMessageSerializer serializer = new ProtoBufMessageSerializer();

            byte[] bytes = serializer.Serialize((object)message, typeof(StatusUpdateMessage));
            var    des   = serializer.Deserialize <StatusUpdateMessage>(bytes);

            // compare
            Assert.NotNull(des);
            Assert.Equal(message.ServicePort, des.ServicePort);
            Assert.Equal(message.InstanceHash, des.InstanceHash);
            Assert.Equal(message.PeerEndpoint, des.PeerEndpoint);
            Assert.NotNull(des.KnownPeers);
            Assert.Equal(message.KnownPeers, des.KnownPeers, DiscoveryPeerData.Comparer);
        }
예제 #2
0
        public void IPv4AddressTest()
        {
            IMessageSerializer serializer = new ProtoBufMessageSerializer();
            IPAddress          adr        = IPAddress.Parse("8.8.8.8");

            byte[] bytes  = serializer.Serialize(adr);
            var    result = serializer.Deserialize <IPAddress>(bytes);

            Assert.Equal(adr, result);
        }
예제 #3
0
        public void IPv6AddressTest()
        {
            IMessageSerializer serializer = new ProtoBufMessageSerializer();
            IPAddress          adr        = IPAddress.Parse("2001:db8::ff00:42:8329");

            byte[] bytes  = serializer.Serialize(adr);
            var    result = serializer.Deserialize <IPAddress>(bytes);

            Assert.Equal(adr, result);
        }
예제 #4
0
        public void IPEndpointTest()
        {
            IMessageSerializer serializer = new ProtoBufMessageSerializer();
            var endPoint = new IPEndPoint(IPAddress.Parse("1.2.3.4"), 567);

            byte[] bytes  = serializer.Serialize(endPoint);
            var    result = serializer.Deserialize <IPEndPoint>(bytes);

            Assert.Equal(endPoint.Port, result.Port);
            Assert.Equal(endPoint.Address, result.Address);
        }
예제 #5
0
        public void ShouldSerializeToProtoBuf()
        {
            IMessageSerializer serializer = new ProtoBufMessageSerializer();

            var message = new TestMessageProto {
                Id = 123, Name = "SomeName"
            };
            var serialize = serializer.Serialize(message);

            Assert.NotNull(serialize);
            Assert.Equal(serialize.Length, 25);
        }
예제 #6
0
        public void ShouldSerializeToProtoBuf()
        {
            IMessageSerializer serializer = new ProtoBufMessageSerializer();

            var message = new TestMessage {
                Id = 123, Name = "SomeName"
            };
            var serialize = serializer.Serialize(message);

            Assert.That(serialize, Is.Not.Null);
            Assert.That(serialize, Has.Length.EqualTo(25));
        }
예제 #7
0
        public void ShouldDeserializeFromProtoBuf()
        {
            IMessageSerializer serializer = new ProtoBufMessageSerializer();
            IMessageDeserializer <TestMessageProto> deserializer = new ProtoBufMessageDeserializer <TestMessageProto>();

            var message = new TestMessageProto {
                Id = 123, Name = "SomeName"
            };
            var serialize   = serializer.Serialize(message);
            var deserialize = deserializer.Deserialize(serialize);

            Assert.Equal(message, deserialize);
        }
예제 #8
0
        public void ThreadSafeTest()
        {
            IMessageSerializer serializer = new ProtoBufMessageSerializer();
            var r = Enumerable.Range(0, 50).AsParallel()
                    .WithDegreeOfParallelism(10)
                    .Select(i => serializer.Serialize(new IPEndPoint(IPAddress.Parse("1.2.3.4"), 567)))
                    .ToArray();

            Assert.All(r, ri =>
            {
                var result = serializer.Deserialize <IPEndPoint>(ri);
                Assert.Equal(567, result.Port);
                Assert.Equal(IPAddress.Parse("1.2.3.4"), result.Address);
            });
        }
예제 #9
0
        private void InitStates()
        {
            this.fsm = new FiniteStateMachine();
            this.fsm.AddAllStates(typeof(PacketReadState));
            this.fsm.AddStateTransition(PacketReadState.WAIT_NEW_PACKET, PacketReadState.WAIT_DATA_SIZE, PacketReadTransition.HeaderReceived);
            this.fsm.AddStateTransition(PacketReadState.WAIT_DATA_SIZE, PacketReadState.WAIT_DATA, PacketReadTransition.SizeReceived);
            this.fsm.AddStateTransition(PacketReadState.WAIT_DATA_SIZE, PacketReadState.WAIT_DATA_SIZE_FRAGMENT, PacketReadTransition.IncompleteSize);
            this.fsm.AddStateTransition(PacketReadState.WAIT_DATA_SIZE_FRAGMENT, PacketReadState.WAIT_DATA, PacketReadTransition.WholeSizeReceived);
            this.fsm.AddStateTransition(PacketReadState.WAIT_DATA, PacketReadState.WAIT_NEW_PACKET, PacketReadTransition.PacketFinished);
            this.fsm.AddStateTransition(PacketReadState.WAIT_DATA, PacketReadState.INVALID_DATA, PacketReadTransition.InvalidData);
            this.fsm.AddStateTransition(PacketReadState.INVALID_DATA, PacketReadState.WAIT_NEW_PACKET, PacketReadTransition.InvalidDataFinished);
            this.fsm.SetCurrentState(PacketReadState.WAIT_NEW_PACKET);

            //---------------------------------------------
            //Init protoBufserializer
            this.protoBufserializer = new ProtoBufMessageSerializer();
        }
예제 #10
0
 public RpcNetworkMessageManager()
 {
     _serializer = new ProtoBufMessageSerializer();
     _dispatcher = new RpcNetworkMessageGameDispatcher(_serializer);
 }