Пример #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());
            }
        }
Пример #2
0
        public unsafe ENetEvent(NativeENetEvent native)
        {
            Type      = native.Type;
            ChannelId = native.ChannelID;
            Data      = native.Data;

            if (native.Peer != null)
            {
                Peer = new ENetPeer(native.Peer);
            }
            else
            {
                Peer = default;
            }

            if (native.Packet != null)
            {
                Packet = new ENetPacket(native.Packet);
            }
            else
            {
                Packet = default;
            }
        }
 public ENetMulticastException(string message, ENetPeer peer) : base(message)
 {
     Peer = peer;
 }
Пример #4
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);
            }
        }
Пример #5
0
 public void Multicast(byte[] buffer, Enum channel, ENetPacketFlags flags, IEnumerable <ENetPeer> peers, ENetPeer except) =>
 Multicast(buffer, Convert.ToByte(channel), flags, peers, except);
Пример #6
0
        /// <summary>
        /// An extension method for clearing the user-data.
        /// </summary>
        /// <param name="peer">The peer that holds the user-data.</param>
        public static void UnsetUserData(this ENetPeer peer)
        {
            var native = peer.GetNativePointer();

            UnsetUserData(ref native->Data);
        }
Пример #7
0
        /// <summary>
        /// Tries to retrieve user-data from <see cref="ENetPeer"/> of given type.
        /// </summary>
        /// <typeparam name="TData">The data type.</typeparam>
        /// <param name="peer">The peer that holds the user-data.</param>
        /// <param name="data">The result user-data.</param>
        /// <returns>Returns true if the user-data found; otherwise false.</returns>
        /// <remarks>
        /// If the data type differs from what was supplied to <see cref="SetUserData{TData}(ENetPeer, TData)"/> then return value becomes false.
        /// </remarks>
        public static bool TryGetUserData <TData>(this ENetPeer peer, out TData data)
        {
            var native = peer.GetNativePointer();

            return(TryGetUserData(native->Data, out data));
        }
Пример #8
0
        /// <summary>
        /// Sets or overwrites the user-data of the given packet.
        /// </summary>
        /// <typeparam name="TData">The user-data type.</typeparam>
        /// <param name="peer">The peer to store user-data into it's <see cref="ENetPeer.UserData"/> field.</param>
        /// <param name="data">The user-data.</param>
        public static void SetUserData <TData>(this ENetPeer peer, TData data)
        {
            var native = peer.GetNativePointer();

            SetUserData(ref native->Data, data);
        }