Modified closing

This commit is contained in:
sta 2013-09-05 21:58:57 +09:00
parent e0a7e74d0a
commit b7313955c9
8 changed files with 36 additions and 32 deletions

View File

@ -462,9 +462,9 @@ namespace WebSocketSharp.Server
return; return;
} }
_serviceHosts.Stop (data);
_listener.Close (); _listener.Close ();
_receiveRequestThread.Join (5 * 1000); _receiveRequestThread.Join (5 * 1000);
_serviceHosts.Stop (code, reason);
_listening = false; _listening = false;
} }
@ -579,9 +579,9 @@ namespace WebSocketSharp.Server
if (!_listening) if (!_listening)
return; return;
_serviceHosts.Stop ();
_listener.Close (); _listener.Close ();
_receiveRequestThread.Join (5 * 1000); _receiveRequestThread.Join (5 * 1000);
_serviceHosts.Stop ();
_listening = false; _listening = false;
} }

View File

@ -201,14 +201,11 @@ namespace WebSocketSharp.Server
void Stop (); void Stop ();
/// <summary> /// <summary>
/// Stops the WebSocket service host with the specified <see cref="ushort"/> and <see cref="string"/>. /// Stops the WebSocket service host with the specified array of <see cref="byte"/>.
/// </summary> /// </summary>
/// <param name="code"> /// <param name="data">
/// A <see cref="ushort"/> that contains a status code indicating the reason for stop. /// An array of <see cref="byte"/> that contains the reason for stop.
/// </param> /// </param>
/// <param name="reason"> void Stop (byte [] data);
/// A <see cref="string"/> that contains the reason for stop.
/// </param>
void Stop (ushort code, string reason);
} }
} }

View File

@ -207,7 +207,7 @@ namespace WebSocketSharp.Server
} }
base.Stop (); base.Stop ();
_serviceHosts.Stop (code, reason); _serviceHosts.Stop (data);
} }
#endregion #endregion

View File

@ -211,6 +211,11 @@ namespace WebSocketSharp.Server
_websocket.SendAsync (data, completed); _websocket.SendAsync (data, completed);
} }
internal void Stop (byte [] data)
{
_websocket.Close (data);
}
#endregion #endregion
#region Protected Methods #region Protected Methods

View File

@ -280,7 +280,7 @@ namespace WebSocketSharp.Server
} }
base.Stop (); base.Stop ();
_sessions.Stop (code, reason); _sessions.Stop (data);
} }
#endregion #endregion
@ -765,19 +765,15 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Stops receiving the WebSocket connection requests with the specified <see cref="ushort"/> and /// Stops the WebSocket service host with the specified array of <see cref="byte"/>.
/// <see cref="string"/>.
/// </summary> /// </summary>
/// <param name="code"> /// <param name="data">
/// A <see cref="ushort"/> that contains a status code indicating the reason for stop. /// An array of <see cref="byte"/> that contains the reason for stop.
/// </param> /// </param>
/// <param name="reason"> void IWebSocketServiceHost.Stop (byte [] data)
/// A <see cref="string"/> that contains the reason for stop.
/// </param>
void IWebSocketServiceHost.Stop (ushort code, string reason)
{ {
base.Stop (); base.Stop ();
_sessions.Stop (code, reason); _sessions.Stop (data);
} }
#endregion #endregion

View File

@ -193,7 +193,7 @@ namespace WebSocketSharp.Server
_serviceHosts.Remove (servicePath); _serviceHosts.Remove (servicePath);
} }
host.Stop ((ushort) CloseStatusCode.AWAY, String.Empty); host.Stop (((ushort) CloseStatusCode.AWAY).ToByteArray (ByteOrder.BIG));
return true; return true;
} }
@ -208,12 +208,12 @@ namespace WebSocketSharp.Server
} }
} }
internal void Stop (ushort code, string reason) internal void Stop (byte [] data)
{ {
lock (_sync) lock (_sync)
{ {
foreach (var host in _serviceHosts.Values) foreach (var host in _serviceHosts.Values)
host.Stop (code, reason); host.Stop (data);
_serviceHosts.Clear (); _serviceHosts.Clear ();
} }

View File

@ -508,7 +508,7 @@ namespace WebSocketSharp.Server
} }
} }
internal void Stop (ushort code, string reason) internal void Stop (byte [] data)
{ {
stopSweepTimer (); stopSweepTimer ();
lock (_sync) lock (_sync)
@ -518,7 +518,7 @@ namespace WebSocketSharp.Server
_stopped = true; _stopped = true;
foreach (var service in copy ().Values) foreach (var service in copy ().Values)
service.Stop (code, reason); service.Stop (data);
} }
} }
@ -588,7 +588,7 @@ namespace WebSocketSharp.Server
{ {
var state = service.WebSocket.ReadyState; var state = service.WebSocket.ReadyState;
if (state == WebSocketState.OPEN) if (state == WebSocketState.OPEN)
service.Stop (CloseStatusCode.ABNORMAL, String.Empty); service.Stop (((ushort) CloseStatusCode.ABNORMAL).ToByteArray (ByteOrder.BIG));
else if (state == WebSocketState.CLOSING) else if (state == WebSocketState.CLOSING)
continue; continue;
else else

View File

@ -586,9 +586,9 @@ namespace WebSocketSharp
private void close (ushort code, string reason) private void close (ushort code, string reason)
{ {
var data = code.Append (reason); var data = code.Append (reason);
if (data.Length > 125) var msg = data.CheckIfValidCloseData ();
if (msg != null)
{ {
var msg = "The payload length of a Close frame must be 125 bytes or less.";
_logger.Error (String.Format ("{0}\ncode: {1}\nreason: {2}", msg, code, reason)); _logger.Error (String.Format ("{0}\ncode: {1}\nreason: {2}", msg, code, reason));
error (msg); error (msg);
@ -1314,6 +1314,12 @@ namespace WebSocketSharp
#region Internal Methods #region Internal Methods
// As server
internal void Close (byte [] data)
{
close (new PayloadData (data));
}
// As server // As server
internal void Close (HttpStatusCode code) internal void Close (HttpStatusCode code)
{ {
@ -1346,7 +1352,7 @@ namespace WebSocketSharp
/// </param> /// </param>
public void Close (ushort code) public void Close (ushort code)
{ {
Close (code, String.Empty); Close (code, "");
} }
/// <summary> /// <summary>
@ -1358,7 +1364,7 @@ namespace WebSocketSharp
/// </param> /// </param>
public void Close (CloseStatusCode code) public void Close (CloseStatusCode code)
{ {
close ((ushort) code, String.Empty); close (new PayloadData (((ushort) code).ToByteArray (ByteOrder.BIG)));
} }
/// <summary> /// <summary>
@ -1377,9 +1383,9 @@ namespace WebSocketSharp
/// </param> /// </param>
public void Close (ushort code, string reason) public void Close (ushort code, string reason)
{ {
if (!code.IsCloseStatusCode ()) var msg = code.CheckIfValidCloseStatusCode ();
if (msg != null)
{ {
var msg = "Invalid close status code.";
_logger.Error (String.Format ("{0}\ncode: {1}", msg, code)); _logger.Error (String.Format ("{0}\ncode: {1}", msg, code));
error (msg); error (msg);