Exemplo n.º 1
0
        /// <summary>
        /// Initialize a host that will accept connections on a particular address, or not accept connections.
        /// </summary>
        /// <param name="address">The address to listen on, or null to not accept connections.</param>
        /// <param name="peerLimit">The maximum number of peers for this host.</param>
        /// <param name="channelLimit">The maximum number of channels, or 0 to use the maximum possible (255).</param>
        /// <param name="incomingBandwidth">The maximum incoming rate of transfer, or 0 for no limit.</param>
        /// <param name="outgoingBandwidth">The maximum outgoing rate of transfer, or 0 for no limit.</param>
        /// <exception cref="ArgumentOutOfRangeException">
        ///     <paramref name="peerLimit"/> is less than 0 or greater than 4095,
        ///     <paramref name="channelLimit"/> is less than 0 or greater than 255,
        ///     <paramref name="incomingBandwidth"/> is less than 0, and/or
        ///     <paramref name="outgoingBandwidth"/> is less than 0.
        /// </exception>
        /// <exception cref="InvalidOperationException">The host is already initialized.</exception>
        /// <exception cref="ENetException">Failed to initialize the host.</exception>
        public void Initialize(IPEndPoint address, int peerLimit,
                               int channelLimit = 0, int incomingBandwidth = 0, int outgoingBandwidth = 0)
        {
            if (NativeData != null)
            {
                throw new InvalidOperationException("Already initialized.");
            }
            if (peerLimit < 0 || peerLimit > ENetApi.ENET_PROTOCOL_MAXIMUM_PEER_ID)
            {
                throw new ArgumentOutOfRangeException("peerLimit");
            }
            CheckChannelLimit(channelLimit);
            CheckBandwidthLimit(incomingBandwidth, outgoingBandwidth);

            if (address != null)
            {
                ENetAddress nativeAddress = (ENetAddress)address;
                NativeData = ENetApi.enet_host_create(ref nativeAddress, (IntPtr)peerLimit,
                                                      (IntPtr)channelLimit, (uint)incomingBandwidth, (uint)outgoingBandwidth);
            }
            else
            {
                NativeData = ENetApi.enet_host_create(null, (IntPtr)peerLimit,
                                                      (IntPtr)channelLimit, (uint)incomingBandwidth, (uint)outgoingBandwidth);
            }
            if (NativeData == null)
            {
                throw new ENetException("Host creation call failed.");
            }
        }
Exemplo n.º 2
0
        public Task <bool> ConnectAsync(
            string hostName, ushort port,
            uint channelLimit = NativeMethods.ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT,
            uint data         = 0)
        {
            if (channelLimit > NativeMethods.ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
            {
                throw new ArgumentOutOfRangeException("channelLimit");
            }

            var tcs     = new TaskCompletionSource <bool>();
            var address = new Address {
                HostName = hostName, Port = port
            };
            ENetAddress nativeAddress = address.Struct;

            this.peerPtr = NativeMethods.EnetHostConnect(
                this.service.HostPtr, ref nativeAddress, channelLimit, data);
            if (this.peerPtr == IntPtr.Zero)
            {
                throw new EException("host connect call failed.");
            }
            this.service.PeersManager.Add(this.peerPtr, this);
            this.Connected = eEvent =>
            {
                if (eEvent.EventState == EventState.DISCONNECTED)
                {
                    tcs.TrySetException(new EException("socket disconnected in connect"));
                }
                tcs.TrySetResult(true);
            };
            return(tcs.Task);
        }
Exemplo n.º 3
0
        public EService(
            string hostName, ushort port, uint peerLimit = NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID,
            uint channelLimit = 0, uint incomingBandwidth = 0, uint outgoingBandwidth = 0)
        {
            if (peerLimit > NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID)
            {
                throw new ArgumentOutOfRangeException(string.Format("peerLimit: {0}", peerLimit));
            }

            if (channelLimit > NativeMethods.ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
            {
                throw new ArgumentOutOfRangeException(string.Format("channelLimit: {0}", channelLimit));
            }

            var address = new Address {
                HostName = hostName, Port = port
            };
            ENetAddress nativeAddress = address.Struct;

            this.host = NativeMethods.EnetHostCreate(ref nativeAddress, peerLimit, channelLimit,
                                                     incomingBandwidth, outgoingBandwidth);

            if (this.host == IntPtr.Zero)
            {
                throw new EException("Host creation call failed.");
            }
        }
Exemplo n.º 4
0
        public void Create(Address?address, int peerLimit, int channelLimit, uint incomingBandwidth, uint outgoingBandwidth, int bufferSize)
        {
            if (nativeHost != IntPtr.Zero)
            {
                throw new InvalidOperationException("Host already created");
            }

            if (peerLimit < 0 || peerLimit > Library.maxPeers)
            {
                throw new ArgumentOutOfRangeException("peerLimit");
            }

            CheckChannelLimit(channelLimit);

            if (address != null)
            {
                ENetAddress nativeAddress = address.Value.NativeData;

                nativeHost = Native.enet_host_create(ref nativeAddress, (IntPtr)peerLimit, (IntPtr)channelLimit, incomingBandwidth, outgoingBandwidth, bufferSize);
            }
            else
            {
                nativeHost = Native.enet_host_create(IntPtr.Zero, (IntPtr)peerLimit, (IntPtr)channelLimit, incomingBandwidth, outgoingBandwidth, bufferSize);
            }

            if (nativeHost == IntPtr.Zero)
            {
                throw new InvalidOperationException("Host creation call failed");
            }
        }
Exemplo n.º 5
0
        public ServerHost(Address address,
                          uint peerLimit         = NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID,
                          uint channelLimit      = 0, uint incomingBandwidth = 0,
                          uint outgoingBandwidth = 0, bool enableCrc         = true)
        {
            if (peerLimit > NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID)
            {
                throw new ArgumentOutOfRangeException("peerLimit");
            }
            CheckChannelLimit(channelLimit);

            ENetAddress nativeAddress = address.Struct;

            this.host = NativeMethods.enet_host_create(
                ref nativeAddress, peerLimit,
                channelLimit, incomingBandwidth, outgoingBandwidth);

            if (this.host == IntPtr.Zero)
            {
                throw new ENetException(0, "Host creation call failed.");
            }

            if (enableCrc)
            {
                NativeMethods.enet_enable_crc(this.host);
            }
        }
Exemplo n.º 6
0
        public Peer Connect(Address address, int channelLimit, uint data)
        {
            CheckCreated();
            CheckChannelLimit(channelLimit);

            ENetAddress nativeAddress = address.NativeData;
            Peer        peer          = new Peer(Native.enet_host_connect(nativeHost, ref nativeAddress, (IntPtr)channelLimit, data));

            if (peer.NativeData == IntPtr.Zero)
            {
                throw new InvalidOperationException("Host connect call failed");
            }

            return(peer);
        }
Exemplo n.º 7
0
        public Task <Peer> ConnectAsync(
            Address address, uint channelLimit = NativeMethods.ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT, uint data = 0)
        {
            CheckChannelLimit(channelLimit);

            var         tcs           = new TaskCompletionSource <Peer>();
            ENetAddress nativeAddress = address.Struct;
            IntPtr      peerPtr       = NativeMethods.enet_host_connect(this.host, ref nativeAddress, channelLimit, data);

            if (peerPtr == IntPtr.Zero)
            {
                throw new ENetException(0, "Host connect call failed.");
            }
            var peer = new Peer(peerPtr);

            this.PeersManager.Add(peerPtr, peer);
            peer.PeerEvent.Connected += e => tcs.TrySetResult(peer);
            return(tcs.Task);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Connects to a remote computer at the given address.
        /// </summary>
        /// <param name="address">The address to connect to.</param>
        /// <param name="data">Data to send along with the connect packet.</param>
        /// <param name="channelLimit">The maximum number of channels, or 0 to use the maximum possible (255).</param>
        /// <returns>
        ///     The new peer. This method does not block: the connection will be established
        ///     when you receive a <see cref="EventType.Connect"/> event.
        /// </returns>
        /// <exception cref="ArgumentNullException"><paramref name="address"/> is null.</exception>
        /// <exception cref="ArgumentException"><paramref name="address"/> is not IPv4.</exception>
        /// <exception cref="ArgumentOutOfRangeException">
        ///     <paramref name="address"/>'s port is less than 0 or greater than 65535, and/or
        ///     <paramref name="channelLimit"/> is less than 0 or greater than 255.
        /// </exception>
        /// <exception cref="InvalidOperationException">The host is not initialized.</exception>
        /// <exception cref="ENetException">An error occured.</exception>
        public Peer Connect(IPEndPoint address, int data, int channelLimit = 0)
        {
            CheckInitialized(); ENetAddress nativeAddress = (ENetAddress)address;
            CheckChannelLimit(channelLimit);

            // For consistency with Connect() and SetChannelLimit(),
            if (channelLimit == 0)
            {
                channelLimit = (int)ENetApi.ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT;
            }

            Peer peer = new Peer(ENetApi.enet_host_connect(NativeData, ref nativeAddress, (IntPtr)channelLimit, (uint)data));

            if (peer.NativeData == null)
            {
                throw new ENetException("Host connect failed.");
            }
            return(peer);
        }
Exemplo n.º 9
0
 internal static extern int enet_address_set_hostname(ref ENetAddress address, string hostName);
Exemplo n.º 10
0
 internal static extern int enet_address_get_ip(ref ENetAddress address, StringBuilder ip, IntPtr ipLength);
Exemplo n.º 11
0
 public static extern ENetPeer *enet_host_connect(ENetHost *host, ref ENetAddress address, IntPtr channelCount, uint data);
Exemplo n.º 12
0
 internal static extern int enet_address_set_ip(ref ENetAddress address, string ip);
Exemplo n.º 13
0
		internal static extern IntPtr EnetHostConnect(
			IntPtr host, ref ENetAddress address, uint channelCount, uint data);
Exemplo n.º 14
0
 public static extern ENetHost *enet_host_create(ref ENetAddress address,
                                                 IntPtr peerLimit, IntPtr channelLimit, uint incomingBandwidth, uint outgoingBandwidth);
Exemplo n.º 15
0
 public static extern int enet_address_set_host(ref ENetAddress address, byte[] hostName);
Exemplo n.º 16
0
 internal static extern IntPtr enet_host_connect(IntPtr host, ref ENetAddress address, IntPtr channelCount, uint data);
Exemplo n.º 17
0
 internal static extern int enet_address_get_host_ip(ref ENetAddress address, StringBuilder hostIp, uint ipLength);
Exemplo n.º 18
0
 public Address(ENetAddress address)
 {
     nativeAddress = address;
 }
Exemplo n.º 19
0
		internal static extern int EnetAddressSetHost(ref ENetAddress address, string hostName);
Exemplo n.º 20
0
		internal static extern int EnetAddressGetHost(
			ref ENetAddress address, StringBuilder hostName, uint nameLength);
Exemplo n.º 21
0
		internal static extern int EnetAddressGetHostIp(
			ref ENetAddress address, StringBuilder hostIp, uint ipLength);
Exemplo n.º 22
0
 internal static extern int enet_address_get_hostname(ref ENetAddress address, StringBuilder hostName, IntPtr nameLength);
Exemplo n.º 23
0
 public static extern int enet_address_get_host(ref ENetAddress address, byte[] hostName, IntPtr nameLength);
Exemplo n.º 24
0
 internal static extern IntPtr enet_host_create(ref ENetAddress address, IntPtr peerLimit, IntPtr channelLimit, uint incomingBandwidth, uint outgoingBandwidth, int bufferSize);
Exemplo n.º 25
0
 public static extern int enet_address_get_host_ip(ref ENetAddress address, byte[] hostIP, IntPtr ipLength);
Exemplo n.º 26
0
 internal Address(ENetAddress address)
 {
     nativeAddress = address;
 }
Exemplo n.º 27
0
		internal static extern IntPtr EnetHostCreate(
			ref ENetAddress address, uint peerLimit, uint channelLimit, uint incomingBandwidth,
			uint outgoingBandwidth);