Exemplo n.º 1
0
        /// <summary>
        /// Queues a packet to be sent.
        /// </summary>
        /// <param name="channelId">Destination channel Id</param>
        /// <param name="packet">Packet to be queued</param>
        /// <remarks>
        /// This method will destroy the packet if its <see cref="ENetPacket.ReferenceCount"/> is zero
        /// </remarks>
        public void Send(byte channelId, ENetPacket packet)
        {
            ThrowIfNull();

            if (packet.IsNull)
            {
                throw new ArgumentNullException(nameof(packet));
            }

            if (LibENet.PeerSend(m_Native, channelId, packet.GetNativePointer()) < 0)
            {
                ThrowHelper.ThrowENetPeerSendFailed();
            }
        }
Exemplo 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);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Attempts to dequeue any incoming queued packet.
        /// </summary>
        /// <param name="packet">Received packet if return value is true</param>
        /// <param name="channelId">Receiver channel if return value is true</param>
        /// <returns>Return true if packet received otherwise false</returns>
        public bool TryReceive(out ENetPacket packet, out byte channelId)
        {
            ThrowIfNull();

            byte chnl         = 0;
            var  resultPacket = LibENet.PeerReceive(m_Native, &chnl);

            if (resultPacket == null)
            {
                channelId = 0;
                packet    = default;
                return(false);
            }
            else
            {
                channelId = chnl;
                packet    = new ENetPacket(resultPacket);
                return(true);
            }
        }
Exemplo n.º 4
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;
            }
        }
Exemplo n.º 5
0
 internal void RaiseReceiveEvent(ENetPacket packet) => OnReceive?.Invoke(this, packet);
Exemplo n.º 6
0
        /// <summary>
        /// An extension method for clearing the user-data.
        /// </summary>
        /// <param name="packet">The packet that holds the user-data.</param>
        public static void UnsetUserData(this ENetPacket packet)
        {
            var native = packet.GetNativePointer();

            UnsetUserData(ref native->UserData);
        }
Exemplo n.º 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="packet">The packet 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}(ENetPacket, TData)"/> then return value becomes false.
        /// </remarks>
        public static bool TryGetUserData <TData>(this ENetPacket packet, out TData data)
        {
            var native = packet.GetNativePointer();

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

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