-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathHost.cs
205 lines (176 loc) · 5.88 KB
/
Host.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#region
using System;
#endregion
namespace ENet
{
public unsafe class Host : IDisposable
{
private Native.ENetHost* _host;
public bool IsSet
{
get { return _host != null; }
}
public Native.ENetHost* NativeData
{
get { return _host; }
set { _host = value; }
}
#region IDisposable Members
public void Dispose()
{
Dispose(true);
}
#endregion
~Host()
{
Dispose(false);
}
private void CheckChannelLimit(int channelLimit)
{
if (channelLimit < 0 || channelLimit > Native.ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
{
throw new ArgumentOutOfRangeException("channelLimit");
}
}
private void CheckCreated()
{
if (_host == null)
{
throw new InvalidOperationException("Not created.");
}
}
public void Create(ushort port, int peerLimit)
{
var address = new Address();
address.Port = port;
Create(address, peerLimit);
}
public void Create(Address? address, int peerLimit)
{
Create(address, peerLimit, 0);
}
public void Create(Address? address, int peerLimit, int channelLimit)
{
Create(address, peerLimit, channelLimit, 0, 0);
}
public void Create(Address? address, int peerLimit, int channelLimit, uint incomingBandwidth, uint outgoingBandwidth)
{
if (_host != null)
{
throw new InvalidOperationException("Already created.");
}
if (peerLimit < 0 || peerLimit > Native.ENET_PROTOCOL_MAXIMUM_PEER_ID)
{
throw new ArgumentOutOfRangeException("peerLimit");
}
CheckChannelLimit(channelLimit);
if (address != null)
{
var nativeAddress = address.Value.NativeData;
_host = Native.enet_host_create(ref nativeAddress, (IntPtr) peerLimit,
(IntPtr) channelLimit, incomingBandwidth, outgoingBandwidth);
}
else
{
_host = Native.enet_host_create(null, (IntPtr) peerLimit,
(IntPtr) channelLimit, incomingBandwidth, outgoingBandwidth);
}
if (_host == null)
{
throw new ENetException(0, "Host creation call failed.");
}
}
protected virtual void Dispose(bool disposing)
{
if (_host != null)
{
Native.enet_host_destroy(_host);
_host = null;
}
}
public void Broadcast(byte channelID, ref Packet packet)
{
CheckCreated();
packet.CheckCreated();
Native.enet_host_broadcast(_host, channelID, packet.NativeData);
packet.NativeData = null; // Broadcast automatically clears this.
}
public void CompressWithRangeCoder()
{
CheckCreated();
Native.enet_host_compress_with_range_coder(_host);
}
public void DoNotCompress()
{
CheckCreated();
Native.enet_host_compress(_host, null);
}
public int CheckEvents(out Event @event)
{
CheckCreated();
Native.ENetEvent nativeEvent;
var ret = Native.enet_host_check_events(_host, out nativeEvent);
if (ret <= 0)
{
@event = new Event();
return ret;
}
@event = new Event(nativeEvent);
return ret;
}
public Peer Connect(Address address, int channelLimit, uint data)
{
CheckCreated();
CheckChannelLimit(channelLimit);
var nativeAddress = address.NativeData;
var peer = new Peer(Native.enet_host_connect(_host, ref nativeAddress, (IntPtr) channelLimit, data));
if (peer.NativeData == null)
{
throw new ENetException(0, "Host connect call failed.");
}
return peer;
}
public void Flush()
{
CheckCreated();
Native.enet_host_flush(_host);
}
public int Service(int timeout)
{
if (timeout < 0)
{
throw new ArgumentOutOfRangeException("timeout");
}
CheckCreated();
return Native.enet_host_service(_host, null, (uint) timeout);
}
public int Service(int timeout, out Event @event)
{
if (timeout < 0)
{
throw new ArgumentOutOfRangeException("timeout");
}
CheckCreated();
Native.ENetEvent nativeEvent;
var ret = Native.enet_host_service(_host, out nativeEvent, (uint) timeout);
if (ret <= 0)
{
@event = new Event();
return ret;
}
@event = new Event(nativeEvent);
return ret;
}
public void SetBandwidthLimit(uint incomingBandwidth, uint outgoingBandwidth)
{
CheckCreated();
Native.enet_host_bandwidth_limit(_host, incomingBandwidth, outgoingBandwidth);
}
public void SetChannelLimit(int channelLimit)
{
CheckChannelLimit(channelLimit);
CheckCreated();
Native.enet_host_channel_limit(_host, (IntPtr) channelLimit);
}
}
}