Ejemplo n.º 1
0
        public ENetEvent CheckEvents()
        {
            CheckDispose();

            var native = new Native.ENetEvent();

            LibENet.HostCheckEvents(Pointer, &native);
            return(NativeToManagedEvent(ref native));
        }
Ejemplo n.º 2
0
        private ENetEvent NativeToManagedEvent(ref Native.ENetEvent native)
        {
            switch (native.Type)
            {
            case ENetEventType.None:
                return(null);

            case ENetEventType.Connect:
                var connect = new ENetConnectEventArgs();
                if (native.Peer->Data == IntPtr.Zero)
                {
                    connect.Peer = new ENetPeer(this, native.Peer);
                }
                else
                {
                    connect.Peer = ENetPeer.FromPtr(native.Peer->Data);
                }
                connect.Peer.RemoteEndPoint = native.Peer->Address.ToEndPoint();
                connect.Data = native.Data;
                return(connect);

            case ENetEventType.Disconnect:
                if (native.Peer->Data == IntPtr.Zero)
                {
                    throw new NullReferenceException("Peer->Data");
                }

                var disconnect = new ENetDisconnectEventArgs();
                disconnect.Peer = ENetPeer.FromPtr(native.Peer->Data);
                disconnect.Data = native.Data;
                disconnect.Peer.FreeHandle();
                return(disconnect);

            case ENetEventType.Receive:
                if (native.Peer->Data == IntPtr.Zero)
                {
                    throw new NullReferenceException("Peer->Data");
                }

                var receive = new ENetReceiveEventArgs();
                receive.Peer   = ENetPeer.FromPtr(native.Peer->Data);
                receive.Packet = new ENetPacket(native.Packet, native.ChannelID);
                LibENet.PacketDestroy(native.Packet);
                return(receive);

            default:
                throw new NotImplementedException(native.Type.ToString());
            }
        }
Ejemplo n.º 3
0
        public bool Service(out ENetEvent e, uint timeout)
        {
            CheckDispose();
            var native = new Native.ENetEvent();
            int result;

            result = LibENet.HostService(Pointer, &native, timeout);

            if (result < 0)
            {
                throw new Exception("Service failure.");
            }

            if (result == 0)
            {
                e = new ENetNoneEventArgs();
            }
            else
            {
                e = NativeToManagedEvent(ref native);
            }

            return(true);
        }