Exemplo n.º 1
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.º 2
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.º 3
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));
                    }
                }
            }
        }