Refactored WebSocketServiceHost.cs
This commit is contained in:
parent
b02cfe1819
commit
25c103d51e
@ -59,11 +59,21 @@ namespace WebSocketSharp.Server
|
||||
|
||||
#endregion
|
||||
|
||||
#region Internal Properties
|
||||
|
||||
internal ServerState State {
|
||||
get {
|
||||
return Sessions.State;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Properties
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether the WebSocket service cleans up the inactive
|
||||
/// sessions periodically.
|
||||
/// Gets or sets a value indicating whether the WebSocket service cleans up
|
||||
/// the inactive sessions periodically.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// <c>true</c> if the service cleans up the inactive sessions periodically;
|
||||
@ -99,11 +109,27 @@ namespace WebSocketSharp.Server
|
||||
|
||||
#region Internal Methods
|
||||
|
||||
internal void Start ()
|
||||
{
|
||||
Sessions.Start ();
|
||||
}
|
||||
|
||||
internal void StartSession (WebSocketContext context)
|
||||
{
|
||||
CreateSession ().Start (context, Sessions);
|
||||
}
|
||||
|
||||
internal void Stop (ushort code, string reason)
|
||||
{
|
||||
var payload = new PayloadData (code.Append (reason));
|
||||
var e = new CloseEventArgs (payload);
|
||||
var bytes = !code.IsReserved ()
|
||||
? WebSocketFrame.CreateCloseFrame (Mask.Unmask, payload).ToByteArray ()
|
||||
: null;
|
||||
|
||||
Sessions.Stop (e, bytes);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Protected Methods
|
||||
|
@ -276,7 +276,7 @@ namespace WebSocketSharp.Server
|
||||
host.KeepClean = false;
|
||||
|
||||
if (_state == ServerState.Start)
|
||||
host.Sessions.Start ();
|
||||
host.Start ();
|
||||
|
||||
_hosts.Add (path, host);
|
||||
}
|
||||
@ -309,9 +309,8 @@ namespace WebSocketSharp.Server
|
||||
_hosts.Remove (path);
|
||||
}
|
||||
|
||||
if (host.Sessions.State == ServerState.Start)
|
||||
host.Sessions.Stop (
|
||||
((ushort) CloseStatusCode.Away).InternalToByteArray (ByteOrder.Big), true);
|
||||
if (host.State == ServerState.Start)
|
||||
host.Stop ((ushort) CloseStatusCode.Away, null);
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -320,7 +319,7 @@ namespace WebSocketSharp.Server
|
||||
{
|
||||
lock (_sync) {
|
||||
foreach (var host in _hosts.Values)
|
||||
host.Sessions.Start ();
|
||||
host.Start ();
|
||||
|
||||
_state = ServerState.Start;
|
||||
}
|
||||
@ -332,13 +331,13 @@ namespace WebSocketSharp.Server
|
||||
_state = ServerState.ShuttingDown;
|
||||
|
||||
var payload = new PayloadData (data);
|
||||
var args = new CloseEventArgs (payload);
|
||||
var e = new CloseEventArgs (payload);
|
||||
var bytes = send
|
||||
? WebSocketFrame.CreateCloseFrame (Mask.Unmask, payload).ToByteArray ()
|
||||
: null;
|
||||
|
||||
foreach (var host in _hosts.Values)
|
||||
host.Sessions.Stop (args, bytes);
|
||||
host.Sessions.Stop (e, bytes);
|
||||
|
||||
_hosts.Clear ();
|
||||
_state = ServerState.Stop;
|
||||
|
@ -345,25 +345,14 @@ namespace WebSocketSharp.Server
|
||||
}
|
||||
}
|
||||
|
||||
internal void Stop (byte[] data, bool send)
|
||||
{
|
||||
var payload = new PayloadData (data);
|
||||
var args = new CloseEventArgs (payload);
|
||||
var bytes = send
|
||||
? WebSocketFrame.CreateCloseFrame (Mask.Unmask, payload).ToByteArray ()
|
||||
: null;
|
||||
|
||||
Stop (args, bytes);
|
||||
}
|
||||
|
||||
internal void Stop (CloseEventArgs args, byte[] frameAsBytes)
|
||||
internal void Stop (CloseEventArgs e, byte[] frameAsBytes)
|
||||
{
|
||||
lock (_sync) {
|
||||
_state = ServerState.ShuttingDown;
|
||||
|
||||
_sweepTimer.Enabled = false;
|
||||
foreach (var session in _sessions.Values.ToList ())
|
||||
session.Context.WebSocket.Close (args, frameAsBytes, 1000);
|
||||
session.Context.WebSocket.Close (e, frameAsBytes, 1000);
|
||||
|
||||
_state = ServerState.Stop;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user