|
|
@@ -27,44 +27,22 @@ namespace ENet
|
|
|
{
|
|
|
private Native.ENetHost* host;
|
|
|
|
|
|
- ~Host()
|
|
|
- {
|
|
|
- this.Dispose(false);
|
|
|
- }
|
|
|
-
|
|
|
- private static void CheckChannelLimit(int channelLimit)
|
|
|
- {
|
|
|
- if (channelLimit < 0 || channelLimit > Native.ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
|
|
|
- {
|
|
|
- throw new ArgumentOutOfRangeException("channelLimit");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private void CheckCreated()
|
|
|
- {
|
|
|
- if (this.host == null)
|
|
|
- {
|
|
|
- throw new InvalidOperationException("Not created.");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public void Create(ushort port, int peerLimit)
|
|
|
+ public Host(ushort port, int peerLimit):
|
|
|
+ this(new Address { Port = port }, peerLimit)
|
|
|
{
|
|
|
- var address = new Address {Port = port};
|
|
|
- Create(address, peerLimit);
|
|
|
}
|
|
|
|
|
|
- public void Create(Address? address, int peerLimit)
|
|
|
+ public Host(Address? address, int peerLimit):
|
|
|
+ this(address, peerLimit, 0)
|
|
|
{
|
|
|
- this.Create(address, peerLimit, 0);
|
|
|
}
|
|
|
|
|
|
- public void Create(Address? address, int peerLimit, int channelLimit)
|
|
|
+ public Host(Address? address, int peerLimit, int channelLimit):
|
|
|
+ this(address, peerLimit, channelLimit, 0, 0)
|
|
|
{
|
|
|
- this.Create(address, peerLimit, channelLimit, 0, 0);
|
|
|
}
|
|
|
|
|
|
- public void Create(Address? address, int peerLimit, int channelLimit, uint incomingBandwidth, uint outgoingBandwidth)
|
|
|
+ public Host(Address? address, int peerLimit, int channelLimit, uint incomingBandwidth, uint outgoingBandwidth)
|
|
|
{
|
|
|
if (this.host != null)
|
|
|
{
|
|
|
@@ -80,14 +58,14 @@ namespace ENet
|
|
|
{
|
|
|
Native.ENetAddress nativeAddress = address.Value.NativeData;
|
|
|
this.host = Native.enet_host_create(
|
|
|
- ref nativeAddress, (IntPtr) peerLimit, (IntPtr) channelLimit, incomingBandwidth,
|
|
|
- outgoingBandwidth);
|
|
|
+ ref nativeAddress, (IntPtr)peerLimit, (IntPtr)channelLimit, incomingBandwidth,
|
|
|
+ outgoingBandwidth);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
this.host = Native.enet_host_create(
|
|
|
- null, (IntPtr) peerLimit, (IntPtr) channelLimit, incomingBandwidth,
|
|
|
- outgoingBandwidth);
|
|
|
+ null, (IntPtr)peerLimit, (IntPtr)channelLimit, incomingBandwidth,
|
|
|
+ outgoingBandwidth);
|
|
|
}
|
|
|
if (this.host == null)
|
|
|
{
|
|
|
@@ -95,12 +73,28 @@ namespace ENet
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public void Dispose()
|
|
|
+ ~Host()
|
|
|
+ {
|
|
|
+ this.Dispose();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void CheckChannelLimit(int channelLimit)
|
|
|
+ {
|
|
|
+ if (channelLimit < 0 || channelLimit > Native.ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
|
|
|
+ {
|
|
|
+ throw new ArgumentOutOfRangeException("channelLimit");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void CheckCreated()
|
|
|
{
|
|
|
- this.Dispose(true);
|
|
|
+ if (this.host == null)
|
|
|
+ {
|
|
|
+ throw new InvalidOperationException("Not created.");
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- private void Dispose(bool disposing)
|
|
|
+ public void Dispose()
|
|
|
{
|
|
|
if (this.host == null)
|
|
|
{
|
|
|
@@ -133,13 +127,9 @@ namespace ENet
|
|
|
public int CheckEvents(out Event e)
|
|
|
{
|
|
|
this.CheckCreated();
|
|
|
+
|
|
|
Native.ENetEvent nativeEvent;
|
|
|
int ret = Native.enet_host_check_events(this.host, out nativeEvent);
|
|
|
- if (ret <= 0)
|
|
|
- {
|
|
|
- e = new Event();
|
|
|
- return ret;
|
|
|
- }
|
|
|
e = new Event(nativeEvent);
|
|
|
return ret;
|
|
|
}
|
|
|
@@ -150,11 +140,12 @@ namespace ENet
|
|
|
CheckChannelLimit(channelLimit);
|
|
|
|
|
|
Native.ENetAddress nativeAddress = address.NativeData;
|
|
|
- var peer = new Peer(Native.enet_host_connect(this.host, ref nativeAddress, (IntPtr) channelLimit, data));
|
|
|
- if (peer.NativeData == null)
|
|
|
+ Native.ENetPeer* p = Native.enet_host_connect(this.host, ref nativeAddress, (IntPtr) channelLimit, data);
|
|
|
+ if (p == null)
|
|
|
{
|
|
|
throw new ENetException(0, "Host connect call failed.");
|
|
|
}
|
|
|
+ var peer = new Peer(p);
|
|
|
return peer;
|
|
|
}
|
|
|
|
|
|
@@ -184,11 +175,6 @@ namespace ENet
|
|
|
Native.ENetEvent nativeEvent;
|
|
|
|
|
|
int ret = Native.enet_host_service(this.host, out nativeEvent, (uint) timeout);
|
|
|
- if (ret <= 0)
|
|
|
- {
|
|
|
- e = new Event();
|
|
|
- return ret;
|
|
|
- }
|
|
|
e = new Event(nativeEvent);
|
|
|
return ret;
|
|
|
}
|
|
|
@@ -205,25 +191,5 @@ namespace ENet
|
|
|
this.CheckCreated();
|
|
|
Native.enet_host_channel_limit(this.host, (IntPtr) channelLimit);
|
|
|
}
|
|
|
-
|
|
|
- public bool IsSet
|
|
|
- {
|
|
|
- get
|
|
|
- {
|
|
|
- return this.host != null;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public Native.ENetHost* NativeData
|
|
|
- {
|
|
|
- get
|
|
|
- {
|
|
|
- return this.host;
|
|
|
- }
|
|
|
- set
|
|
|
- {
|
|
|
- this.host = value;
|
|
|
- }
|
|
|
- }
|
|
|
}
|
|
|
}
|