Пример #1
0
        public static void Example4()
        {
            var srv           = new UNetServer();
            var mainChannel   = srv.CreateChannel <TcpServerChannel, StandardPacketProcessor>();
            var secondChannel = srv.CreateChannel <TcpServerChannel, StandardPacketProcessor>();

            mainChannel.OnPeerConnected += (sender, e) => srv.AddPeerToChannel(secondChannel, p => p.Identity.Equals(e.Peer.Identity));

            secondChannel.OnPeerConnected += (sender, e) =>
            {
                secondChannel.OnPacketReceived += (sender2, e2) =>
                {
                    if (e2.Packet.PacketId == 1)
                    {
                        Console.WriteLine("Received ping packet from {0} @ second channel", e2.Peer.Identity.Guid);
                    }
                };
                Console.WriteLine("Peer has connected to second channel");
                secondChannel.Send(new PingPacket(), e.Peer.Identity.Guid);
            };


            srv.Initialize(mainChannel);
            srv.AddChannel(secondChannel);

            Console.ReadLine();
        }
Пример #2
0
        // Simple example on how multiple channels can be handled
        public static void Example1()
        {
            var srv = new UNetServer();

            // Register global events across all channels
            srv.OnPeerConnected +=
                (sender, e) => Console.WriteLine("A peer has connected to channel: {0} with GUID: {1}", e.Channel.Name,
                                                 e.Peer.Identity.Guid);
            srv.OnChannelCreated +=
                (sender, e) => Console.WriteLine("A channel was created with name: {0}", e.Channel.Name);

            // Create a main channel for the server
            var mainChannel = srv.CreateChannel <TcpServerChannel>();

            // Register events on main channel exclusively
            mainChannel.OnPeerConnected +=
                (sender, e) =>
            {
                Console.WriteLine("A peer has connected to the mainchannel with GUID: {0}",
                                  e.Peer.Identity.Guid);
                srv.AddPeerToChannel(srv.GetChannel <TcpServerChannel>(ch => ch.Name == "MySecondChannel"), e.Peer.Identity);
            };
            mainChannel.OnPacketReceived +=
                (sender, e) =>
                Console.WriteLine("A packet was received on the mainchannel from peer with GUID: {0}",
                                  e.Peer.Identity.Guid);

            // Initialize the server and start listening on the main channel
            srv.Initialize(mainChannel);
            // Create and add a second channel to the server
            srv.CreateAndAddChannel <TcpServerChannel>(ch => ch.Name = "MySecondChannel");

            Console.ReadLine();
        }