Skip to content

Commit 79abc9a

Browse files
committed
Modified broadcasting
1 parent 5d8de64 commit 79abc9a

File tree

3 files changed

+223
-62
lines changed

3 files changed

+223
-62
lines changed

websocket-sharp/Server/WebSocketServiceHostManager.cs

Lines changed: 68 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@
2828

2929
using System;
3030
using System.Collections.Generic;
31+
using System.IO;
3132
using System.Linq;
3233
using System.Text;
34+
using System.Threading;
3335
using WebSocketSharp.Net;
3436

3537
namespace WebSocketSharp.Server
@@ -195,6 +197,59 @@ public IEnumerable<string> ServicePaths {
195197

196198
#region Private Methods
197199

200+
private void broadcast (Opcode opcode, byte [] data)
201+
{
202+
WaitCallback callback = state =>
203+
{
204+
var cache = new Dictionary<CompressionMethod, byte []> ();
205+
try {
206+
foreach (var host in ServiceHosts)
207+
{
208+
if (_state != ServerState.START)
209+
break;
210+
211+
host.Sessions.BroadcastInternally (opcode, data, cache);
212+
}
213+
}
214+
catch (Exception ex) {
215+
_logger.Fatal (ex.ToString ());
216+
}
217+
finally {
218+
cache.Clear ();
219+
}
220+
};
221+
222+
ThreadPool.QueueUserWorkItem (callback);
223+
}
224+
225+
private void broadcast (Opcode opcode, Stream stream)
226+
{
227+
WaitCallback callback = state =>
228+
{
229+
var cache = new Dictionary<CompressionMethod, Stream> ();
230+
try {
231+
foreach (var host in ServiceHosts)
232+
{
233+
if (_state != ServerState.START)
234+
break;
235+
236+
host.Sessions.BroadcastInternally (opcode, stream, cache);
237+
}
238+
}
239+
catch (Exception ex) {
240+
_logger.Fatal (ex.ToString ());
241+
}
242+
finally {
243+
foreach (var cached in cache.Values)
244+
cached.Dispose ();
245+
246+
cache.Clear ();
247+
}
248+
};
249+
250+
ThreadPool.QueueUserWorkItem (callback);
251+
}
252+
198253
private Dictionary<string, Dictionary<string, bool>> broadping (byte [] frameAsBytes, int timeOut)
199254
{
200255
var result = new Dictionary<string, Dictionary<string, bool>> ();
@@ -316,13 +371,10 @@ public void Broadcast (byte [] data)
316371
return;
317372
}
318373

319-
foreach (var host in ServiceHosts)
320-
{
321-
if (_state != ServerState.START)
322-
break;
323-
324-
host.Sessions.BroadcastInternally (data);
325-
}
374+
if (data.LongLength <= WebSocket.FragmentLength)
375+
broadcast (Opcode.BINARY, data);
376+
else
377+
broadcast (Opcode.BINARY, new MemoryStream (data));
326378
}
327379

328380
/// <summary>
@@ -341,13 +393,11 @@ public void Broadcast (string data)
341393
return;
342394
}
343395

344-
foreach (var host in ServiceHosts)
345-
{
346-
if (_state != ServerState.START)
347-
break;
348-
349-
host.Sessions.BroadcastInternally (data);
350-
}
396+
var rawData = Encoding.UTF8.GetBytes (data);
397+
if (rawData.LongLength <= WebSocket.FragmentLength)
398+
broadcast (Opcode.TEXT, rawData);
399+
else
400+
broadcast (Opcode.TEXT, new MemoryStream (rawData));
351401
}
352402

353403
/// <summary>
@@ -362,7 +412,7 @@ public void Broadcast (string data)
362412
/// </param>
363413
public void BroadcastTo (byte [] data, string servicePath)
364414
{
365-
var msg = _state.CheckIfStarted () ?? data.CheckIfValidSendData () ?? servicePath.CheckIfValidServicePath ();
415+
var msg = _state.CheckIfStarted () ?? servicePath.CheckIfValidServicePath ();
366416
if (msg != null)
367417
{
368418
_logger.Error (msg);
@@ -376,7 +426,7 @@ public void BroadcastTo (byte [] data, string servicePath)
376426
return;
377427
}
378428

379-
host.Sessions.BroadcastInternally (data);
429+
host.Sessions.Broadcast (data);
380430
}
381431

382432
/// <summary>
@@ -391,7 +441,7 @@ public void BroadcastTo (byte [] data, string servicePath)
391441
/// </param>
392442
public void BroadcastTo (string data, string servicePath)
393443
{
394-
var msg = _state.CheckIfStarted () ?? data.CheckIfValidSendData () ?? servicePath.CheckIfValidServicePath ();
444+
var msg = _state.CheckIfStarted () ?? servicePath.CheckIfValidServicePath ();
395445
if (msg != null)
396446
{
397447
_logger.Error (msg);
@@ -405,7 +455,7 @@ public void BroadcastTo (string data, string servicePath)
405455
return;
406456
}
407457

408-
host.Sessions.BroadcastInternally (data);
458+
host.Sessions.Broadcast (data);
409459
}
410460

411461
/// <summary>

websocket-sharp/Server/WebSocketSessionManager.cs

Lines changed: 66 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@
2828

2929
using System;
3030
using System.Collections.Generic;
31+
using System.IO;
3132
using System.Linq;
3233
using System.Text;
34+
using System.Threading;
3335
using System.Timers;
3436

3537
namespace WebSocketSharp.Server
@@ -47,7 +49,7 @@ public class WebSocketSessionManager
4749
private Dictionary<string, WebSocketService> _sessions;
4850
private volatile ServerState _state;
4951
private volatile bool _sweeping;
50-
private Timer _sweepTimer;
52+
private System.Timers.Timer _sweepTimer;
5153
private object _sync;
5254

5355
#endregion
@@ -229,7 +231,7 @@ private static string createID ()
229231

230232
private void setSweepTimer (double interval)
231233
{
232-
_sweepTimer = new Timer (interval);
234+
_sweepTimer = new System.Timers.Timer (interval);
233235
_sweepTimer.Elapsed += (sender, e) =>
234236
{
235237
Sweep ();
@@ -254,32 +256,69 @@ internal string Add (WebSocketService session)
254256
}
255257
}
256258

257-
internal void BroadcastInternally (byte [] data)
259+
internal void BroadcastInternally (Opcode opcode, byte [] data)
258260
{
259-
var services = ServiceInstances.GetEnumerator ();
260-
Action completed = null;
261-
completed = () =>
261+
WaitCallback callback = state =>
262262
{
263-
if (_state == ServerState.START && services.MoveNext ())
264-
services.Current.SendAsync (data, completed);
263+
var cache = new Dictionary<CompressionMethod, byte []> ();
264+
try {
265+
BroadcastInternally (opcode, data, cache);
266+
}
267+
catch (Exception ex) {
268+
_logger.Fatal (ex.ToString ());
269+
}
270+
finally {
271+
cache.Clear ();
272+
}
265273
};
266274

267-
if (_state == ServerState.START && services.MoveNext ())
268-
services.Current.SendAsync (data, completed);
275+
ThreadPool.QueueUserWorkItem (callback);
269276
}
270277

271-
internal void BroadcastInternally (string data)
278+
internal void BroadcastInternally (Opcode opcode, Stream stream)
272279
{
273-
var services = ServiceInstances.GetEnumerator ();
274-
Action completed = null;
275-
completed = () =>
280+
WaitCallback callback = state =>
276281
{
277-
if (_state == ServerState.START && services.MoveNext ())
278-
services.Current.SendAsync (data, completed);
282+
var cache = new Dictionary <CompressionMethod, Stream> ();
283+
try {
284+
BroadcastInternally (opcode, stream, cache);
285+
}
286+
catch (Exception ex) {
287+
_logger.Fatal (ex.ToString ());
288+
}
289+
finally {
290+
foreach (var cached in cache.Values)
291+
cached.Dispose ();
292+
293+
cache.Clear ();
294+
}
279295
};
280296

281-
if (_state == ServerState.START && services.MoveNext ())
282-
services.Current.SendAsync (data, completed);
297+
ThreadPool.QueueUserWorkItem (callback);
298+
}
299+
300+
internal void BroadcastInternally (
301+
Opcode opcode, byte [] data, Dictionary<CompressionMethod, byte []> cache)
302+
{
303+
foreach (var session in ServiceInstances)
304+
{
305+
if (_state != ServerState.START)
306+
break;
307+
308+
session.Context.WebSocket.Send (opcode, data, cache);
309+
}
310+
}
311+
312+
internal void BroadcastInternally (
313+
Opcode opcode, Stream stream, Dictionary <CompressionMethod, Stream> cache)
314+
{
315+
foreach (var session in ServiceInstances)
316+
{
317+
if (_state != ServerState.START)
318+
break;
319+
320+
session.Context.WebSocket.Send (opcode, stream, cache);
321+
}
283322
}
284323

285324
internal Dictionary<string, bool> BroadpingInternally ()
@@ -367,7 +406,10 @@ public void Broadcast (byte [] data)
367406
return;
368407
}
369408

370-
BroadcastInternally (data);
409+
if (data.LongLength <= WebSocket.FragmentLength)
410+
BroadcastInternally (Opcode.BINARY, data);
411+
else
412+
BroadcastInternally (Opcode.BINARY, new MemoryStream (data));
371413
}
372414

373415
/// <summary>
@@ -385,7 +427,11 @@ public void Broadcast (string data)
385427
return;
386428
}
387429

388-
BroadcastInternally (data);
430+
var rawData = Encoding.UTF8.GetBytes (data);
431+
if (rawData.LongLength <= WebSocket.FragmentLength)
432+
BroadcastInternally (Opcode.TEXT, rawData);
433+
else
434+
BroadcastInternally (Opcode.TEXT, new MemoryStream (rawData));
389435
}
390436

391437
/// <summary>

0 commit comments

Comments
 (0)