Exemplo n.º 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));
                    }
                }
            }
        }
Exemplo n.º 2
0
        void ClientThread(HostInfo hostinfo)
        {
            _hostInfo = hostinfo;

            using (ENet.Host host = new ENet.Host())
            {
                Console.WriteLine("Initializing client...");
                host.Initialize(hostinfo.address, hostinfo.peerLimit,
                                hostinfo.channelLimit, hostinfo.incomingBandwidth, hostinfo.outgoingBandwidth);

                peer = host.Connect(hostinfo.hostName, hostinfo.port, hostinfo.data, hostinfo.channelLimit);
                hostinfo.isConnected = true;
                while (hostinfo.isConnected)
                {
                    ENet.Event @event;

                    if (host.Service(hostinfo.timeout, out @event))
                    {
                        do
                        {
                            switch (@event.Type)
                            {
                            case ENet.EventType.Connect:
                                Console.WriteLine("Connected to server at IP/port {0}.", peer.GetRemoteAddress());
                                break;

                            case ENet.EventType.Receive:
                                byte[] data = @event.Packet.GetBytes();

                                HandleMessage(data);

                                ushort value = BitConverter.ToUInt16(data, 0);
                                if (value % 1000 == 0)
                                {
                                    Console.WriteLine("  Client: Ch={0} Recv={1}", @event.ChannelID, value);
                                }
                                value++;
                                //  peer.Send(@event.ChannelID, BitConverter.GetBytes(value), ENet.PacketFlags.Reliable);
                                @event.Packet.Dispose();
                                break;

                            case ENet.EventType.Disconnect:
                                Console.WriteLine("Disconnected from server at IP/port {0}.", peer.GetRemoteAddress());
                                hostinfo.isConnected = false;
                                hostinfo.isLoggedIn  = false;
                                break;

                            default:
                                Console.WriteLine(@event.Type);
                                break;
                            }
                        }while (host.CheckEvents(out @event));
                    }
                }
            }
        }
Exemplo n.º 3
0
        static void Client()
        {
            using (ENet.Host host = new ENet.Host())
            {
                Console.WriteLine("Initializing client...");
                host.Initialize(null, 1);

                peer = host.Connect("127.0.0.1", 1234, 1234, 200);

                while (true)
                {
                    ENet.Event @event;

                    if (host.Service(15000, out @event))
                    {
                        do
                        {
                            switch (@event.Type)
                            {
                            case ENet.EventType.Connect:
                                Console.WriteLine("Connected to server at IP/port {0}.", peer.GetRemoteAddress());
                                break;

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

                            default:
                                Console.WriteLine(@event.Type);
                                break;
                            }
                        }while (host.CheckEvents(out @event));
                    }
                }
            }
        }
Exemplo n.º 4
0
        static void Client()
        {
            using (ENet.Host host = new ENet.Host())
            {
                Console.WriteLine("Initializing client...");
                host.Initialize(null, 1);

                ENet.Peer peer = host.Connect("127.0.0.1", 5000, 1234, 200);
                while (true)
                {
                    ENet.Event @event;

                    if (host.Service(15000, out @event))
                    {
                        do
                        {
                            switch (@event.Type)
                            {
                                case ENet.EventType.Connect:
                                    Console.WriteLine("Connected to server at IP/port {0}.", peer.GetRemoteAddress());
                                    break;

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

                                default:
                                    Console.WriteLine(@event.Type);
                                    break;
                            }
                        }
                        while (host.CheckEvents(out @event));
                    }
                }
            }
        }
Exemplo n.º 5
0
        private void i_checkStartENetThread()
        {
            if (m_ENetTask == null || m_ENetTask.IsCompleted)
            {
                m_ENetTask = Task.Run(() =>
                {
                    Thread.CurrentThread.Name = "ENet Thread";
                    while (!m_stopThread || m_disconnectionList.Count != 0)
                    {
                        using (MonitorLock.CreateLock(m_outgoingPacketListLocks[m_currentOutgoingPacketListIndex]))
                        {
                            foreach (var outgoingPacket in m_outgoingPacketLists[m_currentOutgoingPacketListIndex])
                            {
                                NetWrapConnection connection;
                                if (!m_connections.TryGetValue(outgoingPacket.ConnectionId, out connection))
                                {
                                    continue;
                                }

                                if (connection.Peer.State == ENet.PeerState.Connected)
                                {
                                    connection.Peer.Send(0, outgoingPacket.Message, outgoingPacket.MessageOffset, outgoingPacket.MessageCount, outgoingPacket.Flags);
                                }
                            }
                            m_outgoingPacketLists[m_currentOutgoingPacketListIndex].Clear();
                        }

                        using (MonitorLock.CreateLock(m_disconnectionListLock))
                        {
                            foreach (var connectionId in m_disconnectionList)
                            {
                                NetWrapConnection connection;
                                if (m_connections.TryRemove(connectionId, out connection) && connection.Peer.UserData != IntPtr.Zero)
                                {
                                    GCHandle.FromIntPtr(connection.Peer.UserData).Free();
                                    connection.Peer.UserData = IntPtr.Zero;
                                }
                                if (connection.Peer.IsInitialized && connection.Peer.State == ENet.PeerState.Connected)
                                {
                                    connection.Peer.DisconnectLater("disconnect".GetHashCode());
                                }
                            }
                            m_disconnectionList.Clear();
                        }

                        using (MonitorLock.CreateLock(m_enetEventListLocks[m_currentENetEventListIndex]))
                        {
                            ENet.Event enetEvent;
                            using (MonitorLock.CreateLock(hostLock))
                            {
                                if (host.Service(0, out enetEvent))
                                {
                                    do
                                    {
                                        m_enetEventLists[m_currentENetEventListIndex].Add(enetEvent);
                                    } while (host.CheckEvents(out enetEvent));
                                }
                            }
                        }

                        Thread.Sleep(25);
                    }

                    m_threadStoppedEvent.Set();
                });
            }
        }
Exemplo n.º 6
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));
                    }
                }
            }
        }