-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUNetServer.cs
307 lines (279 loc) · 11.9 KB
/
UNetServer.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
using System;
using System.Collections.Generic;
using uNet2.Channel;
using uNet2.Channel.Events;
using uNet2.Exceptions.Channel;
using uNet2.Exceptions.Server;
using uNet2.Network;
using uNet2.Packet;
using uNet2.SocketOperation;
namespace uNet2
{
/// <summary>
/// The server
/// </summary>
public sealed class UNetServer
{
/// <summary>
/// This event is raised each time a channel is created successfully
/// </summary>
public event ChannelEvents.OnChannelCreated OnChannelCreated;
/// <summary>
/// This event is raised each time a channel is disposed successfully
/// </summary>
public event ChannelEvents.OnChannelCreated OnChannelDisposed;
/// <summary>
/// This event is raised each time a peer connected to any channel
/// </summary>
public event ChannelEvents.OnPeerConnected OnPeerConnected;
private readonly ChannelManager _channelMgr;
public List<Peer.Peer> ConnectedPeers
{
get { return GetMainChannel().ConnectedPeers; }
}
/// <summary>
/// Use <see cref="Initialize(IServerChannel)"/> when initializing server
/// </summary>
public UNetServer()
{
_channelMgr = new ChannelManager();
}
/// <summary>
/// Use <see cref="Initialize()"/> when initializing server
/// </summary>
/// <param name="mainChannel">The server main channel to use</param>
public UNetServer(IServerChannel mainChannel)
{
_channelMgr = new ChannelManager();
mainChannel.IsMainChannel = true;
_channelMgr.UnsafeAddChannel(mainChannel);
mainChannel.HostServer = this;
}
/// <summary>
/// Initializes the server.
/// Only use this if you've set a main channel with <see cref="UNetServer(IServerChannel)"/>
/// </summary>
/// <remarks>
/// May generate a <see cref="ServerInitializationException"/> if used incorrectly
/// </remarks>
public void Initialize()
{
if (_channelMgr.GetChannel<IServerChannel>(ch => ch is IServerChannel && ch.IsMainChannel) == null)
throw new ServerInitializationException("Attempt to initialize server without a main channel");
_channelMgr.GetChannel<IServerChannel>(ch => ch is IServerChannel && ch.IsMainChannel)
.Start();
_channelMgr.GetChannel<IServerChannel>(ch => ch is IServerChannel && ch.IsMainChannel)
.OnPeerConnected +=
(sender, e) => OnPeerConnected.Raise(sender, e);
}
/// <summary>
/// Initializes the server.
/// Only use this if you haven't set a main channel with <see cref="UNetServer()"/>
/// </summary>
/// <param name="mainChannel">The server main channel to use</param>
public void Initialize(IServerChannel mainChannel)
{
if (_channelMgr.GetChannel<IServerChannel>(ch => ch.IsMainChannel) != null)
return;
mainChannel.IsMainChannel = true;
mainChannel.HostServer = this;
_channelMgr.UnsafeAddChannel(mainChannel);
_channelMgr.GetChannel<IServerChannel>(mainChannel.Id).Start();
_channelMgr.GetChannel<IServerChannel>(mainChannel.Id).OnPeerConnected +=
(sender, e) => OnPeerConnected.Raise(sender, e);
}
/// <summary>
/// Creates a channel and assigns a valid ID, port and manager to it
/// </summary>
/// <remarks>
/// It is recommended to use this method whenever you wish to create a new channel.
/// The only reason not to use this method is if you wish to create a channel with a specific ID
/// which may cause issues
/// </remarks>
/// <typeparam name="T">The type of channel to create</typeparam>
/// <returns>Returns a newly created channel ready to be added</returns>
public T CreateChannel<T>() where T : IServerChannel
{
return (T)_channelMgr.CreateChannel<T>();
}
/// <summary>
/// Creates a channel and assigns a valid ID, port and manager to it
/// </summary>
/// <remarks>
/// It is recommended to use this method whenever you wish to create a new channel.
/// The only reason not to use this method is if you wish to create a channel with a specific ID
/// which may cause issues
/// </remarks>
/// <typeparam name="T">The type of channel to create</typeparam>
/// <returns>Returns a newly created channel ready to be added</returns>
public T CreateChannel<T>(IPacketProcessor packetProcessor) where T : IServerChannel
{
var channel = (T) _channelMgr.CreateChannel<T>();
channel.PacketProcessor = packetProcessor;
return channel;
}
/// <summary>
/// Creates a channel and assigns a valid ID, port and manager to it
/// </summary>
/// <remarks>
/// It is recommended to use this method whenever you wish to create a new channel.
/// The only reason not to use this method is if you wish to create a channel with a specific ID
/// which may cause issues
/// </remarks>
/// <typeparam name="T">The type of channel to create</typeparam>
/// <param name="name">The name of the channel</param>
/// <returns>Returns a newly created channel ready to be added</returns>
public T CreateChannel<T>(string name) where T : IServerChannel
{
return (T)_channelMgr.CreateChannel<T>(name);
}
public T CreateChannel<T, TU>() where T : IServerChannel where TU : IPacketProcessor
{
return (T)_channelMgr.CreateChannel<T, TU>();
}
public IServerChannel GetMainChannel()
{
return _channelMgr.GetMainChannel() as IServerChannel;
}
/// <summary>
/// Adds a channel to this server
/// </summary>
/// <remarks>
/// May throw a <see cref="ChannelOperationException"/> if used incorrectly
/// </remarks>
/// <param name="channel">The channel to add</param>
/// <returns>Returns true if channel was added successfully</returns>
public bool AddChannel(IServerChannel channel)
{
if (!_channelMgr.AddChannel(channel))
return false;
OnChannelCreated.Raise(this, new ChannelEventArgs(channel));
channel.HostServer = this;
channel.OnPeerConnected += (sender, e) => OnPeerConnected.Raise(this, e);
channel.PacketProcessor = _channelMgr.GetMainChannel().PacketProcessor;
return true;
}
/// <summary>
/// Creates a channel using <see cref="ChannelManager.CreateChannel{T}()"/> and adds it to this server
/// </summary>
/// <remarks>
/// May throw a <see cref="ChannelOperationException"/> if used incorrectly
/// </remarks>
/// <code>
/// CreateAndAddChannel{T}(ch => {
/// ch.Name = "Example";
/// ch.Port = 1000;
/// ...
/// });
/// </code>
/// <typeparam name="T">The type of channel to create</typeparam>
/// <param name="channelAction">An action to set channel data</param>
/// <returns>Returns true if channel was created and added successfully</returns>
public bool CreateAndAddChannel<T>(Action<T> channelAction) where T : IServerChannel
{
var channel = (IServerChannel)_channelMgr.CreateChannel<T>();
channelAction((T)channel);
if (!_channelMgr.AddChannel(channel))
return false;
channel.HostServer = this;
OnChannelCreated.Raise(this, new ChannelEventArgs(channel));
channel.OnPeerConnected += OnPeerConnected;
return true;
}
/// <summary>
/// Disposes and removes a channel from this server
/// </summary>
/// <param name="id">The ID of the channel to remove</param>
public void DisposeChannel(int id)
{
_channelMgr.RemoveChannel(id);
OnChannelDisposed.Raise(this, new ChannelEventArgs(id));
}
/// <summary>
/// Disposes and removes a channel from this server
/// </summary>
/// <param name="channel">The channel to remove</param>
public void DisposeChannel(IServerChannel channel)
{
var id = _channelMgr.RemoveChannel(channel);
OnChannelDisposed.Raise(this, new ChannelEventArgs(id));
}
/// <summary>
/// Disposes and removes a channel from this server
/// </summary>
/// <param name="pred">The predicate used to determine what channel to dispose and remove</param>
public void DisposeChannel(Predicate<IChannel> pred)
{
var id = _channelMgr.RemoveChannel(pred);
OnChannelDisposed.Raise(this, new ChannelEventArgs(id));
}
/// <summary>
/// Retrieves a channel from this server
/// </summary>
/// <typeparam name="T">The type of channel to retrieve</typeparam>
/// <param name="id">The ID of the channel to retrieve</param>
/// <returns>Returns the channel requested</returns>
public T GetChannel<T>(int id) where T : IServerChannel
{
return _channelMgr.GetChannel<T>(id);
}
/// <summary>
/// Retrieves a channel from this server
/// </summary>
/// <typeparam name="T">The type of channel to retrieve</typeparam>
/// <param name="channel">The channel to retrieve</param>
/// <returns>Returns the channel requested</returns>
public T GetChannel<T>(IChannel channel) where T : IServerChannel
{
return _channelMgr.GetChannel<T>(channel.Id);
}
/// <summary>
/// Retrieves a channel from this server
/// </summary>
/// <typeparam name="T">The type of channel to retrieve</typeparam>
/// <param name="pred">The predicate used to determine what channel to retrieve</param>
/// <returns>Returns the channel requested</returns>
public T GetChannel<T>(Predicate<IChannel> pred) where T : IServerChannel
{
return _channelMgr.GetChannel<T>(pred);
}
public void AddPeerToChannel(IServerChannel channel, Predicate<Peer.Peer> pred)
{
_channelMgr.AddPeerToChannel(channel, pred);
}
public void AddPeerToChannel(IServerChannel channel, SocketIdentity identity)
{
_channelMgr.AddPeerToChannel(channel, identity);
}
/// <summary>
/// Broadcasts a packet to all connected peers in all channels
/// </summary>
/// <param name="data">The data to broadcast</param>
public void GlobalBroadcast(IDataPacket data)
{
}
/// <summary>
/// Broadcasts a packet to all connected peers in a specific channel
/// </summary>
/// <param name="channel">The channel to broadcast to</param>
/// <param name="data">The data to broadcast</param>
public void BroadcastToChannel(IServerChannel channel, IDataPacket data)
{
_channelMgr.GetChannel<IServerChannel>(channel.Id).Broadcast(data);
}
/// <summary>
/// Shuts down the server
/// </summary>
/// <remarks>
/// You will have to call <see cref="Initialize()"/> or <see cref="Initialize(IServerChannel)"/> to use it again/>
/// </remarks>
public void Shutdown()
{
_channelMgr.Reset();
}
public T CreateOperation<T, TU>(TU hostChannel) where T : ISocketOperation where TU : IChannel
{
return _channelMgr.GetChannel<TU>(hostChannel.Id).CreateOperation<T>();
}
}
}