Ejemplo n.º 1
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.º 2
0
        public bool Receive(out ENetPacket packet)
        {
            byte channel = 0;

            Native.ENetPacket *native = LibENet.PeerReceive(Unsafe, &channel);

            if (((IntPtr)native) == IntPtr.Zero)
            {
                packet = null;
                return(false);
            }

            packet = new ENetPacket(native, channel);
            LibENet.PacketDestroy(native);
            return(true);
        }
Ejemplo n.º 3
0
        public void Multicast(byte[] buffer, byte channel, ENetPacketFlags flags, IEnumerable <ENetPeer> peers, ENetPeer except)
        {
            CheckDispose();
            Native.ENetPacket *packet;

            fixed(byte *p = buffer)
            {
                packet = LibENet.PacketCreate((IntPtr)p, (UIntPtr)buffer.Length, flags & ~ENetPacketFlags.NoAllocate);
            }

            foreach (var peer in peers)
            {
                if (peer == null)
                {
                    throw new NullReferenceException();
                }

                if (peer.Host != this)
                {
                    throw new ENetMulticastException("Speicfied peer is not of this host.", peer);
                }

                if (peer == except)
                {
                    continue;
                }

                if (peer.Unsafe->State != ENetPeerState.Connected)
                {
                    continue;
                }

                if (LibENet.PeerSend(peer.Unsafe, channel, packet) != 0)
                {
                    throw new ENetMulticastException("Failed to send packet to speicfied peer.", peer);
                }
            }

            if (packet->ReferenceCount.ToUInt32() == 0)
            {
                LibENet.PacketDestroy(packet);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Destroys packet without considering references count
        /// </summary>
        public void Destroy()
        {
            ThrowIfNull();

            LibENet.PacketDestroy(m_Native);
        }