|
|
@@ -24,20 +24,26 @@ using System.Threading.Tasks;
|
|
|
|
|
|
namespace ENet
|
|
|
{
|
|
|
- public unsafe class Peer : IDisposable
|
|
|
+ public unsafe class ENetPeer : IDisposable
|
|
|
{
|
|
|
+ private readonly ENetHost host;
|
|
|
private Native.ENetPeer* peer;
|
|
|
+ private Action<ENetEvent> connected;
|
|
|
+ private Action<ENetEvent> received;
|
|
|
+ private Action<ENetEvent> disconnect;
|
|
|
|
|
|
- public Peer(Native.ENetPeer* peer)
|
|
|
+ public ENetPeer(ENetHost host, Native.ENetPeer* peer)
|
|
|
{
|
|
|
if (peer == null)
|
|
|
{
|
|
|
throw new InvalidOperationException("No native peer.");
|
|
|
}
|
|
|
this.peer = peer;
|
|
|
+ this.host = host;
|
|
|
+ this.host.Peers.Add(this);
|
|
|
}
|
|
|
|
|
|
- ~Peer()
|
|
|
+ ~ENetPeer()
|
|
|
{
|
|
|
this.Dispose(false);
|
|
|
}
|
|
|
@@ -58,63 +64,114 @@ namespace ENet
|
|
|
if (disposing)
|
|
|
{
|
|
|
Native.enet_peer_reset(this.peer);
|
|
|
- this.peer = null;
|
|
|
}
|
|
|
+ this.host.Peers.Remove((int)this.peer->data);
|
|
|
+ this.peer = null;
|
|
|
}
|
|
|
|
|
|
- public void ConfigureThrottle(uint interval, uint acceleration, uint deceleration)
|
|
|
+ // peer连接上了调用该回调方法
|
|
|
+ public Action<ENetEvent> Connected
|
|
|
{
|
|
|
- Native.enet_peer_throttle_configure(this.peer, interval, acceleration, deceleration);
|
|
|
+ get
|
|
|
+ {
|
|
|
+ if (connected == null)
|
|
|
+ {
|
|
|
+ return e => { };
|
|
|
+ }
|
|
|
+ return connected;
|
|
|
+ }
|
|
|
+ set
|
|
|
+ {
|
|
|
+ connected = value;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- public void Disconnect(uint data)
|
|
|
+ public Action<ENetEvent> Received
|
|
|
{
|
|
|
- Native.enet_peer_disconnect(this.peer, data);
|
|
|
+ get
|
|
|
+ {
|
|
|
+ if (received == null)
|
|
|
+ {
|
|
|
+ return e => { };
|
|
|
+ }
|
|
|
+ return received;
|
|
|
+ }
|
|
|
+ set
|
|
|
+ {
|
|
|
+ received = value;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- public void DisconnectLater(uint data)
|
|
|
+ public Action<ENetEvent> Disconnect
|
|
|
{
|
|
|
- Native.enet_peer_disconnect_later(this.peer, data);
|
|
|
+ get
|
|
|
+ {
|
|
|
+ if (disconnect == null)
|
|
|
+ {
|
|
|
+ return e => { };
|
|
|
+ }
|
|
|
+ return disconnect;
|
|
|
+ }
|
|
|
+ set
|
|
|
+ {
|
|
|
+ disconnect = value;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- public void DisconnectNow(uint data)
|
|
|
+ public void ConfigureThrottle(uint interval, uint acceleration, uint deceleration)
|
|
|
{
|
|
|
- Native.enet_peer_disconnect_now(this.peer, data);
|
|
|
+ Native.enet_peer_throttle_configure(this.peer, interval, acceleration, deceleration);
|
|
|
}
|
|
|
|
|
|
- public void Ping()
|
|
|
+ public void Send(byte channelID, byte[] data)
|
|
|
{
|
|
|
- Native.enet_peer_ping(this.peer);
|
|
|
+ this.Send(channelID, data, 0, data.Length);
|
|
|
}
|
|
|
|
|
|
- public bool Receive(out byte channelID, out Packet packet)
|
|
|
+ public void Send(byte channelID, byte[] data, int offset, int length)
|
|
|
{
|
|
|
- Native.ENetPacket* nativePacket = Native.enet_peer_receive(this.peer, out channelID);
|
|
|
- if (nativePacket == null)
|
|
|
+ using (var packet = new ENetPacket(data, offset, length))
|
|
|
{
|
|
|
- packet = new Packet();
|
|
|
- return false;
|
|
|
+ this.Send(channelID, packet);
|
|
|
}
|
|
|
- packet = new Packet(nativePacket);
|
|
|
- return true;
|
|
|
}
|
|
|
|
|
|
- public void Send(byte channelID, byte[] data)
|
|
|
+ public void Send(byte channelID, ENetPacket packet)
|
|
|
{
|
|
|
- this.Send(channelID, data, 0, data.Length);
|
|
|
+ Native.enet_peer_send(this.peer, channelID, packet.NativeData);
|
|
|
}
|
|
|
|
|
|
- public void Send(byte channelID, byte[] data, int offset, int length)
|
|
|
+ public Task<ENetPacket> ReceiveAsync()
|
|
|
{
|
|
|
- using (var packet = new Packet(data, offset, length))
|
|
|
- {
|
|
|
- this.Send(channelID, packet);
|
|
|
- }
|
|
|
+ var tcs = new TaskCompletionSource<ENetPacket>();
|
|
|
+ this.Received = e => tcs.TrySetResult(e.Packet);
|
|
|
+ return tcs.Task;
|
|
|
}
|
|
|
|
|
|
- public void Send(byte channelID, Packet packet)
|
|
|
+ public Task<bool> DisconnectAsync(uint data)
|
|
|
{
|
|
|
- Native.enet_peer_send(this.peer, channelID, packet.NativeData);
|
|
|
+ var tcs = new TaskCompletionSource<bool>();
|
|
|
+ this.Disconnect = e => tcs.TrySetResult(true);
|
|
|
+ Native.enet_peer_disconnect(this.peer, data);
|
|
|
+ return tcs.Task;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Task<bool> DisconnectLaterAsync(uint data)
|
|
|
+ {
|
|
|
+ var tcs = new TaskCompletionSource<bool>();
|
|
|
+ this.Disconnect = e => tcs.TrySetResult(true);
|
|
|
+ Native.enet_peer_disconnect_later(this.peer, data);
|
|
|
+ return tcs.Task;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void DisconnectNow(uint data)
|
|
|
+ {
|
|
|
+ Native.enet_peer_disconnect_now(this.peer, data);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void Ping()
|
|
|
+ {
|
|
|
+ Native.enet_peer_ping(this.peer);
|
|
|
}
|
|
|
|
|
|
public Native.ENetPeer* NativeData
|
|
|
@@ -148,13 +205,5 @@ namespace ENet
|
|
|
this.peer->data = value;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- public uint ConnectID
|
|
|
- {
|
|
|
- get
|
|
|
- {
|
|
|
- return this.peer->connectID;
|
|
|
- }
|
|
|
- }
|
|
|
}
|
|
|
}
|