/// <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."); } }
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); }
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."); } }
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"); } }
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); } }
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); }
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); }
/// <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); }
internal static extern int enet_address_set_hostname(ref ENetAddress address, string hostName);
internal static extern int enet_address_get_ip(ref ENetAddress address, StringBuilder ip, IntPtr ipLength);
public static extern ENetPeer *enet_host_connect(ENetHost *host, ref ENetAddress address, IntPtr channelCount, uint data);
internal static extern int enet_address_set_ip(ref ENetAddress address, string ip);
internal static extern IntPtr EnetHostConnect( IntPtr host, ref ENetAddress address, uint channelCount, uint data);
public static extern ENetHost *enet_host_create(ref ENetAddress address, IntPtr peerLimit, IntPtr channelLimit, uint incomingBandwidth, uint outgoingBandwidth);
public static extern int enet_address_set_host(ref ENetAddress address, byte[] hostName);
internal static extern IntPtr enet_host_connect(IntPtr host, ref ENetAddress address, IntPtr channelCount, uint data);
internal static extern int enet_address_get_host_ip(ref ENetAddress address, StringBuilder hostIp, uint ipLength);
public Address(ENetAddress address) { nativeAddress = address; }
internal static extern int EnetAddressSetHost(ref ENetAddress address, string hostName);
internal static extern int EnetAddressGetHost( ref ENetAddress address, StringBuilder hostName, uint nameLength);
internal static extern int EnetAddressGetHostIp( ref ENetAddress address, StringBuilder hostIp, uint ipLength);
internal static extern int enet_address_get_hostname(ref ENetAddress address, StringBuilder hostName, IntPtr nameLength);
public static extern int enet_address_get_host(ref ENetAddress address, byte[] hostName, IntPtr nameLength);
internal static extern IntPtr enet_host_create(ref ENetAddress address, IntPtr peerLimit, IntPtr channelLimit, uint incomingBandwidth, uint outgoingBandwidth, int bufferSize);
public static extern int enet_address_get_host_ip(ref ENetAddress address, byte[] hostIP, IntPtr ipLength);
internal Address(ENetAddress address) { nativeAddress = address; }
internal static extern IntPtr EnetHostCreate( ref ENetAddress address, uint peerLimit, uint channelLimit, uint incomingBandwidth, uint outgoingBandwidth);