Пример #1
0
        static void Server()
        {
            using (ENet.Host host = new ENet.Host())
            {
                Console.WriteLine("Initializing server...");

                host.InitializeServer(5000, 1);
                ENet.Peer peer = new ENet.Peer();

                while (true)
                {
                    ENet.Event @event;

                    if (host.Service(15000, out @event))
                    {
                        do
                        {
                            switch (@event.Type)
                            {
                            case ENet.EventType.Connect:
                                peer = @event.Peer;
                                // If you are using ENet 1.3.4 or newer, the following two methods will work:
                                //peer.SetPingInterval(1000);
                                //peer.SetTimeouts(8, 5000, 60000);
                                Console.WriteLine("Connected to client at IP/port {0}.", peer.GetRemoteAddress());
                                for (int i = 0; i < 200; i++)
                                {
                                    ENet.Packet packet = new ENet.Packet();
                                    packet.Initialize(new byte[] { 0, 0 }, 0, 2, ENet.PacketFlags.Reliable);
                                    packet.SetUserData(i);
                                    packet.SetUserData("Test", i * i);
                                    packet.Freed += p =>
                                    {
                                        Console.WriteLine("Initial packet freed (channel {0}, square of channel {1})",
                                                          p.GetUserData(),
                                                          p.GetUserData("Test"));
                                    };
                                    peer.Send((byte)i, packet);
                                }
                                break;

                            case ENet.EventType.Receive:
                                byte[] data  = @event.Packet.GetBytes();
                                ushort value = BitConverter.ToUInt16(data, 0);
                                if (value % 1000 == 1)
                                {
                                    Console.WriteLine("  Server: Ch={0} Recv={1}", @event.ChannelID, value);
                                }
                                value++; peer.Send(@event.ChannelID, BitConverter.GetBytes(value), ENet.PacketFlags.Reliable);
                                @event.Packet.Dispose();
                                break;
                            }
                        }while (host.CheckEvents(out @event));
                    }
                }
            }
        }
Пример #2
0
        public NetServer(string upnpIdent, int cport, int maxConnections = 64, bool enableUPnP = true)
        {
            m_enableUPnP     = enableUPnP;
            m_maxConnections = maxConnections;

            UPnPOpenPort(cport);

            host = new ENet.Host();
            host.InitializeServer(cport, maxConnections);
            listenPort = cport;

            OnData          += Server_OnData;
            OnStatusChanged += Server_OnStatusChanged;
        }
Пример #3
0
        static void Main(string[] args)
        {
            var host = new ENet.Host();
            host.InitializeServer(111, 500);
            Task.Factory.StartNew(server, host);

            var peers = new ENet.Host[500];
            for (int i = 0; i < peers.Length; i++)
            {
                var peer = (peers[i] = new ENet.Host());
                peer.InitializeClient(1);
                peer.Connect("localhost", 111, 0);
                Task.Factory.StartNew(client, peer);
            }

            Console.Read();
        }
Пример #4
0
        static void Main(string[] args)
        {
            var host = new ENet.Host();

            host.InitializeServer(111, 500);
            Task.Factory.StartNew(server, host);

            var peers = new ENet.Host[500];

            for (int i = 0; i < peers.Length; i++)
            {
                var peer = (peers[i] = new ENet.Host());
                peer.InitializeClient(1);
                peer.Connect("localhost", 111, 0);
                Task.Factory.StartNew(client, peer);
            }

            Console.Read();
        }
Пример #5
0
        static void Server()
        {
            using (ENet.Host host = new ENet.Host())
            {
                Console.WriteLine("Initializing server...");

                host.InitializeServer(5000, 1);
                ENet.Peer peer = new ENet.Peer();

                while (true)
                {
                    ENet.Event @event;

                    if (host.Service(15000, out @event))
                    {
                        do
                        {
                            switch (@event.Type)
                            {
                                case ENet.EventType.Connect:
                                    peer = @event.Peer;
                                    // If you are using ENet 1.3.4 or newer, the following two methods will work:
                                    //peer.SetPingInterval(1000);
                                    //peer.SetTimeouts(8, 5000, 60000);
                                    Console.WriteLine("Connected to client at IP/port {0}.", peer.GetRemoteAddress());
                                    for (int i = 0; i < 200; i++)
                                    {
                                        ENet.Packet packet = new ENet.Packet();
                                        packet.Initialize(new byte[] { 0, 0 }, 0, 2, ENet.PacketFlags.Reliable);
                                        packet.SetUserData(i);
                                        packet.SetUserData("Test", i * i);
                                        packet.Freed += p =>
                                            {
                                                Console.WriteLine("Initial packet freed (channel {0}, square of channel {1})",
                                                    p.GetUserData(),
                                                    p.GetUserData("Test"));
                                            };
                                        peer.Send((byte)i, packet);
                                    }
                                    break;

                                case ENet.EventType.Receive:
                                    byte[] data = @event.Packet.GetBytes();
                                    ushort value = BitConverter.ToUInt16(data, 0);
                                    if (value % 1000 == 1) { Console.WriteLine("  Server: Ch={0} Recv={1}", @event.ChannelID, value); }
                                    value++; peer.Send(@event.ChannelID, BitConverter.GetBytes(value), ENet.PacketFlags.Reliable);
                                    @event.Packet.Dispose();
                                    break;
                            }
                        }
                        while (host.CheckEvents(out @event));
                    }
                }
            }
        }