Inheritance: NetBase
Ejemplo n.º 1
0
        public void Run()
        {
            EventBasedNetListener netListener = new EventBasedNetListener();
            EventBasedNatPunchListener natPunchListener1 = new EventBasedNatPunchListener();
            EventBasedNatPunchListener natPunchListener2 = new EventBasedNatPunchListener();

            netListener.PeerConnectedEvent += peer =>
            {
                Console.WriteLine("PeerConnected: " + peer.EndPoint.ToString());
            };

            netListener.PeerDisconnectedEvent += (peer, reason, error) =>
            {
                Console.WriteLine("PeerDisconnected: " + reason);
            };

            natPunchListener1.NatIntroductionSuccess += (point, token) =>
            {
                Console.WriteLine("Success C1. Connecting to C2: {0}", point);
                _c1.Connect(point);
            };

            natPunchListener2.NatIntroductionSuccess += (point, token) =>
            {
                Console.WriteLine("Success C2. Connecting to C1: {0}", point);
                _c2.Connect(point);
            };

            _c1 = new NetClient(netListener, "gamekey");
            _c1.PeerToPeerMode = true;
            _c1.NatPunchEnabled = true;
            _c1.NatPunchModule.Init(natPunchListener1);
            _c1.Start();

            _c2 = new NetClient(netListener, "gamekey");
            _c2.PeerToPeerMode = true;
            _c2.NatPunchEnabled = true;
            _c2.NatPunchModule.Init(natPunchListener2);
            _c2.Start();

            _puncher = new NetClient(netListener, "notneed");
            _puncher.Start(ServerPort);
            _puncher.NatPunchEnabled = true;
            _puncher.NatPunchModule.Init(this);

            _c1.NatPunchModule.SendNatIntroduceRequest(new NetEndPoint("::1", ServerPort), "token1");
            _c2.NatPunchModule.SendNatIntroduceRequest(new NetEndPoint("::1", ServerPort), "token1");

            // keep going until ESCAPE is pressed
            Console.WriteLine("Press ESC to quit");

            while (true)
            {
                if (Console.KeyAvailable)
                {
                    var key = Console.ReadKey().Key;
                    if (key == ConsoleKey.Escape)
                    {
                        break;
                    }
                    else if (key == ConsoleKey.A)
                    {
                        Console.WriteLine("C1 stopped");
                        _c1.Stop();
                    }
                }
                
                DateTime nowTime = DateTime.Now;

                _c1.NatPunchModule.PollEvents();
                _c2.NatPunchModule.PollEvents();
                _puncher.NatPunchModule.PollEvents();
                _c1.PollEvents();
                _c2.PollEvents();

                //check old peers
                foreach (var waitPeer in _waitingPeers)
                {
                    if (nowTime - waitPeer.Value.RefreshTime > KickTime)
                    {
                        _peersToRemove.Add(waitPeer.Key);
                    }
                }

                //remove
                for (int i = 0; i < _peersToRemove.Count; i++)
                {
                    Console.WriteLine("Kicking peer: " + _peersToRemove[i]);
                    _waitingPeers.Remove(_peersToRemove[i]);
                }
                _peersToRemove.Clear();

                Thread.Sleep(10);
            }

            _c1.Stop();
            _c2.Stop();
            _puncher.Stop();
        }
Ejemplo n.º 2
0
        public void Run()
        {
            //Server
            _serverListener = new ServerListener();

            NetServer server = new NetServer(_serverListener, 2, "myapp1");
            if (!server.Start(9050))
            {
                Console.WriteLine("Server start failed");
                Console.ReadKey();
                return;
            }
            _serverListener.Server = server;

            //Client
            _clientListener = new ClientListener();

            NetClient client1 = new NetClient(_clientListener, "myapp1");
            //client1.SimulateLatency = true;
            client1.SimulationMaxLatency = 1500;
            client1.MergeEnabled = true;
            if (!client1.Start())
            {
                Console.WriteLine("Client1 start failed");
                return;
            }
            client1.Connect("127.0.0.1", 9050);

            NetClient client2 = new NetClient(_clientListener, "myapp1");
            //client2.SimulateLatency = true;
            client2.SimulationMaxLatency = 1500;
            client2.Start();
            client2.Connect("::1", 9050);

            while (!Console.KeyAvailable)
            {
                client1.PollEvents();
                client2.PollEvents();
                server.PollEvents();
                Thread.Sleep(15);
            }

            client1.Stop();
            client2.Stop();
            server.Stop();
            Console.ReadKey();
            Console.WriteLine("ServStats:\n BytesReceived: {0}\n PacketsReceived: {1}\n BytesSent: {2}\n PacketsSent: {3}", 
                server.BytesReceived, 
                server.PacketsReceived, 
                server.BytesSent, 
                server.PacketsSent);
            Console.WriteLine("Client1Stats:\n BytesReceived: {0}\n PacketsReceived: {1}\n BytesSent: {2}\n PacketsSent: {3}",
                client1.BytesReceived,
                client1.PacketsReceived,
                client1.BytesSent,
                client1.PacketsSent);
            Console.WriteLine("Client2Stats:\n BytesReceived: {0}\n PacketsReceived: {1}\n BytesSent: {2}\n PacketsSent: {3}",
                client2.BytesReceived,
                client2.PacketsReceived,
                client2.BytesSent,
                client2.PacketsSent);
            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }
Ejemplo n.º 3
0
        public void Run()
        {
            //Server
            _serverListener = new ServerListener();

            NetServer server = new NetServer(_serverListener, 2, "myapp1");
            server.DiscoveryEnabled = true;
            if (!server.Start(9050))
            {
                Console.WriteLine("Server start failed");
                Console.ReadKey();
                return;
            }
            _serverListener.Server = server;

            //Client
            _clientListener1 = new ClientListener();

            NetClient client1 = new NetClient(_clientListener1, "myapp1");
            _clientListener1.Client = client1;
            client1.SimulateLatency = true;
            client1.SimulationMaxLatency = 1500;
            if (!client1.Start())
            {
                Console.WriteLine("Client1 start failed");

                return;
            }

            _clientListener2 = new ClientListener();
            NetClient client2 = new NetClient(_clientListener2, "myapp1");
            _clientListener2.Client = client2;
            client2.SimulateLatency = true;
            client2.SimulationMaxLatency = 1500;
            client2.Start();

            //Send broadcast
            NetDataWriter writer = new NetDataWriter();

            writer.Put("CLIENT 1 DISCOVERY REQUEST");
            client1.SendDiscoveryRequest(writer, 9050);
            writer.Reset();

            writer.Put("CLIENT 2 DISCOVERY REQUEST");
            client2.SendDiscoveryRequest(writer, 9050);

            while (!Console.KeyAvailable)
            {
                client1.PollEvents();
                client2.PollEvents();
                server.PollEvents();
                Thread.Sleep(15);
            }

            client1.Stop();
            client2.Stop();
            server.Stop();
            Console.ReadKey();
            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }