Пример #1
0
 public void SubscribeQueue <T>(Queue <T> queue) where T : class, new()
 {
     // TODO: Optimize with a packet pool using queue max size.
     netPacketProcessor.Subscribe((T data) => {
         queue.Enqueue(data);
     }, () => { return(new T()); });
 }
        public void TestThingMessage()
        {
            // Test whether the serialization framework supports property inheritance.
            // ThingMessage.Create derives from CreateMessage which has an Id property.
            var createThingMessage = new ThingMessages.Create(1, new int[] { });

            var writer = new NetDataWriter();
            NetPacketProcessor processor = new NetPacketProcessor();

            processor.RegisterNestedType <DistributedId>();

            processor.Write(writer, createThingMessage);

            var reader = new NetDataReader(writer.CopyData());

            ThingMessages.Create readMessage = null;
            processor.Subscribe(
                createMessage => readMessage = createMessage,
                () => new ThingMessages.Create());

            processor.ReadAllPackets(reader);

            Assert.IsNotNull(readMessage);
            Assert.AreEqual(new DistributedId(1), readMessage.Id);
        }
        public void TestSocketAddress()
        {
            IPEndPoint    endPoint                = new IPEndPoint(new IPAddress(0), 1);
            SocketAddress socketAddress           = endPoint.Serialize();
            var           serializedSocketAddress = new SerializedSocketAddress(socketAddress);
            var           packet = new Packet {
                SerializedSocketAddress = serializedSocketAddress
            };

            var writer = new NetDataWriter();
            NetPacketProcessor processor = new NetPacketProcessor();

            SerializedSocketAddress.RegisterWith(processor);

            processor.Write(writer, packet);

            var    reader     = new NetDataReader(writer.CopyData());
            Packet readPacket = null;

            processor.Subscribe <Packet>(packet => readPacket = packet, () => new Packet());

            processor.ReadAllPackets(reader);

            Assert.IsNotNull(readPacket);
            Assert.AreEqual(serializedSocketAddress.SocketAddress, readPacket.SerializedSocketAddress.SocketAddress);
        }
Пример #4
0
        void Awake()
        {
            client = new NetManager(this);
            client.Start();

            processor = PacketUtils.CreateProcessor();

            processor.Subscribe <ClientID>(
                (packet) =>
            {
                clientID    = packet.ID;
                clientIDSet = true;
            },
                () => new ClientID()
                );
            processor.Subscribe <WorldInitPacket>(NotifyPacketListeners <WorldInitPacket>, () => new WorldInitPacket());
            processor.Subscribe <WorldChunkPacket>(NotifyPacketListeners <WorldChunkPacket>, () => new WorldChunkPacket());
            //processor.Subscribe<StateChangePacket>(NotifyPacketListeners<StateChangePacket>, () => new StateChangePacket());
            processor.Subscribe <NetCreatePacket>(NotifyPacketListeners <NetCreatePacket>, () => new NetCreatePacket());
            processor.Subscribe <NetUpdatePacket>(NotifyPacketListeners <NetUpdatePacket>, () => new NetUpdatePacket());
            processor.Subscribe <NetDestroyPacket>(NotifyPacketListeners <NetDestroyPacket>, () => new NetDestroyPacket());

            //eventTable = new Dictionary<Type, List<Action<object>>>();
            eventTable = new EventTable <object>();
        }
Пример #5
0
        void Start()
        {
            writer = new NetDataWriter();

            // Start the server on port 5000
            server = new NetManager(this);
            server.Start(5000);
            server.DiscoveryEnabled = true;
            server.UpdateTime       = 15;

            connectedClients = new Dictionary <int, NetPeer>();
            clientVersions   = new Dictionary <int, int>();

            stateManager = new WorldStateManager(this, Constants.WORLD_SIZE);

            processor = PacketUtils.CreateProcessor();
            //processor.Subscribe<UpdatePacket, NetPeer>(OnUpdatePacket, () => new UpdatePacket());
            processor.Subscribe <ClientRequestPacket, NetPeer>(OnClientRequest, () => new ClientRequestPacket());
        }
Пример #6
0
        public NetworkConnection(SkirmishGame game)
        {
            _numberOfOtherPlayers = game.Slots.Count(s => s.State == SkirmishSlotState.Human) - 1;
            _listener             = new EventBasedNetListener();
            _manager = new NetManager(_listener);

            if (Debugger.IsAttached)
            {
                _manager.DisconnectTimeout = 600000;
            }

            _listener.PeerConnectedEvent    += peer => Logger.Trace($"{peer.EndPoint} connected");
            _listener.PeerDisconnectedEvent += (peer, info) => Logger.Trace($"{peer.EndPoint} disconnected with reason {info.Reason}");
            _listener.NetworkReceiveEvent   += (NetPeer peer, NetPacketReader reader, DeliveryMethod deliveryMethod) => _processor.ReadAllPackets(reader, peer);

            _writer    = new NetDataWriter();
            _processor = new NetPacketProcessor();
            _processor.RegisterNestedType <Order>(WriteOrder, ReadOrder);
            _processor.Subscribe <SkirmishOrderPacket, NetPeer>(ReceiveOrderPacket, () => new SkirmishOrderPacket());
        }
Пример #7
0
 public TestBroadcastNetEventListener()
 {
     ReceivedMessages   = new ConcurrentQueue <object>();
     netPacketProcessor = new NetPacketProcessor();
     netPacketProcessor.Subscribe(message => ReceivedMessages.Enqueue(message), () => new AnnounceMessage());
 }