/// <summary>
        /// Instantiates <see cref="ENetHost"/> by creating native ENet host
        /// </summary>
        /// <param name="address">The address at which other peers may connect to this host. If null, then no peers may connect to the host.</param>
        /// <param name="peers">Maximum number of peers</param>
        /// <param name="channels">Maximum number of channels (0 = <see cref="MaximumPeers"/>)</param>
        /// <param name="incomingBandwidth">Maximum incoming bandwidth (0 = unlimited)</param>
        /// <param name="outgoingBandwidth">Maximum outgoing bandwidth (0 = unlimited)</param>
        public ENetHost(IPEndPoint?address, int peers, byte channels, long incomingBandwidth = 0, long outgoingBandwidth = 0)
        {
            if (incomingBandwidth < uint.MinValue || incomingBandwidth > uint.MaxValue)
            {
                throw new ArgumentOutOfRangeException(nameof(incomingBandwidth));
            }

            if (outgoingBandwidth < uint.MinValue || outgoingBandwidth > uint.MaxValue)
            {
                throw new ArgumentOutOfRangeException(nameof(outgoingBandwidth));
            }

            if (peers < 0 || peers > MaximumPeers)
            {
                throw new ArgumentOutOfRangeException(nameof(peers));
            }

            var nativeAddress = NativeENetAddress.FromIPEndPoint(address ?? new IPEndPoint(IPAddress.Any, 0));

            m_Pointer = LibENet.HostCreate(&nativeAddress, (UIntPtr)peers, (UIntPtr)channels, (uint)incomingBandwidth, (uint)outgoingBandwidth);

            if (m_Pointer == IntPtr.Zero)
            {
                ThrowHelper.ThrowENetCreateHostFailed();
            }
Exemple #2
0
        public ENetHost(IPEndPoint address, int peers, byte channels, long incomingBandwidth, long outgoingBandwidth)
        {
            if (incomingBandwidth < uint.MinValue || incomingBandwidth > uint.MaxValue)
            {
                throw new ArgumentOutOfRangeException("incomingBandwidth");
            }

            if (outgoingBandwidth < uint.MinValue || outgoingBandwidth > uint.MaxValue)
            {
                throw new ArgumentOutOfRangeException("outgoingBandwidth");
            }

            if (peers < uint.MinValue || peers > LibENet.ENET_PROTOCOL_MAXIMUM_PEER_ID)
            {
                throw new ArgumentOutOfRangeException(string.Format("Maximum peers count is {0}", LibENet.ENET_PROTOCOL_MAXIMUM_PEER_ID));
            }

            var enetAddress = Native.ENetAddress.FromEndPoint(address);

            Pointer = LibENet.HostCreate(&enetAddress, (UIntPtr)peers, (UIntPtr)channels, (uint)incomingBandwidth, (uint)outgoingBandwidth);
            if (Pointer == IntPtr.Zero)
            {
                throw new Exception("Failed to create ENet host.");
            }

            m_pInterceptCallback    = (IntPtr *)IntPtr.Add(Pointer, Native.ENetHostOffset.InterceptOffset);
            m_pChecksumCallback     = (IntPtr *)IntPtr.Add(Pointer, Native.ENetHostOffset.ChecksumOffset);
            m_pTotalSentData        = (uint *)IntPtr.Add(Pointer, Native.ENetHostOffset.TotalSentDataOffset);
            m_pTotalSentPackets     = (uint *)IntPtr.Add(Pointer, Native.ENetHostOffset.TotalSentPacketsOffset);
            m_pTotalReceivedData    = (uint *)IntPtr.Add(Pointer, Native.ENetHostOffset.TotalReceivedDataOffset);
            m_pTotalReceivedPackets = (uint *)IntPtr.Add(Pointer, Native.ENetHostOffset.TotalReceivedPacketsOffset);
            m_pReceivedAddress      = (Native.ENetAddress *)IntPtr.Add(Pointer, Native.ENetHostOffset.ReceivedAddressOffset);
            m_pReceivedData         = (IntPtr *)IntPtr.Add(Pointer, Native.ENetHostOffset.ReceivedDataOffset);
            m_pReceivedDataLength   = (UIntPtr *)IntPtr.Add(Pointer, Native.ENetHostOffset.ReceivedDataLengthOffset);
            m_pConnectedPeers       = (UIntPtr *)IntPtr.Add(Pointer, Native.ENetHostOffset.ConnectedPeersOffset);

            ChecksumDelegate    = new Native.ENetChecksumCallback(ChecksumCallback);
            m_InterceptDelegate = new Native.ENetInterceptCallback(InterceptCallback);
        }