Пример #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();
        }
Пример #3
0
 public UNetServer CreateServer(int port, out Guid guid)
 {
     guid = Guid.NewGuid();
     var srv = new UNetServer();
     var mainChannel = srv.CreateChannel<TcpServerChannel>();
     mainChannel.PacketProcessor = Globals.PacketProcessor;
     mainChannel.EnsurePacketIntegrity = false;
     mainChannel.Port = (uint)port;
     srv.Initialize(mainChannel);
     ActiveServers.Add(guid, srv);
     return ActiveServers[guid];
 }
Пример #4
0
        public UNetServer CreateServer(int port, out Guid guid)
        {
            guid = Guid.NewGuid();
            var srv         = new UNetServer();
            var mainChannel = srv.CreateChannel <TcpServerChannel>();

            mainChannel.PacketProcessor       = Globals.PacketProcessor;
            mainChannel.EnsurePacketIntegrity = false;
            mainChannel.Port = (uint)port;
            srv.Initialize(mainChannel);
            ActiveServers.Add(guid, srv);
            return(ActiveServers[guid]);
        }
Пример #5
0
        public static void Example3()
        {
            var srv           = new UNetServer();
            var mainChannel   = srv.CreateChannel <TcpServerChannel>();
            var secondChannel = srv.CreateChannel <TcpServerChannel>();


            //mainChannel.OnPeerConnected += (sender, e) =>
            //{
            //    _operations.Add(mainChannel.RegisterOperation<FileTransferOperation>(e.Peer.Identity.Guid));
            //    srv.AddPeerToChannel(secondChannel, p => p.Identity.Guid == e.Peer.Identity.Guid);
            //    count++;
            //    Console.WriteLine("Main channel connect");
            //};
            //secondChannel.OnPeerConnected += (sender, e) =>
            //{
            //    _operations.Add(secondChannel.RegisterOperation<FileTransferOperation>(e.Peer.Identity.Guid));
            //    count++;
            //    Console.WriteLine("second channel connect");
            //};
            //secondChannel.OnPacketReceived += (sender, e) =>
            //{
            //    Console.WriteLine("Received pingpacket from peer: {0} @ channel {1}", e.Peer.Identity.Guid, e.Channel.Id);
            //};

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

            while (count != 2)
            {
                Thread.Sleep(100);
            }


            _operations[1].SendPacket(new FileTransferInitiatePacket());
            Console.ReadLine();
        }
Пример #6
0
        // Simple example on how you can transfer a large file and track the progress
        public static void Example2()
        {
            var srv         = new UNetServer();
            var mainChannel = srv.CreateChannel <TcpServerChannel>(new StandardPacketProcessor());

            mainChannel.EnsurePacketIntegrity = true;
            mainChannel.IntegrityHash         = PacketIntegrityHash.Sha256;

            srv.Initialize(mainChannel);
            mainChannel.OnPeerConnected += (o, e) => {
                mainChannel.Send(new PingPacket(), e.Peer.Identity.Guid);
            };

            Console.ReadLine();
        }
Пример #7
0
        public static void Example5()
        {
            var srv         = new UNetServer();
            var mainChannel = srv.CreateChannel <TcpServerChannel, StandardPacketProcessor>();

            srv.Initialize(mainChannel);
            //srv.GetMainChannel().EnsurePacketIntegrity = true;
            //srv.GetMainChannel().IntegrityHash = PacketIntegrityHash.Sha256;

            srv.OnPeerConnected += (e, o) =>
            {
                srv.GetMainChannel().Send(new PingPacket(), o.Peer.Identity.Guid);
            };

            Console.ReadLine();
        }