Refactored a few for WebSocket.cs

This commit is contained in:
sta 2015-04-26 16:50:09 +09:00
parent 59effc31ce
commit 371ac36dd9

View File

@ -248,12 +248,13 @@ namespace WebSocketSharp
#region Public Properties #region Public Properties
/// <summary> /// <summary>
/// Gets or sets the compression method used to compress the message on the WebSocket /// Gets or sets the compression method used to compress the message on
/// connection. /// the WebSocket connection.
/// </summary> /// </summary>
/// <value> /// <value>
/// One of the <see cref="CompressionMethod"/> enum values, indicates the compression method /// One of the <see cref="CompressionMethod"/> enum values, indicates
/// used to compress the message. The default value is <see cref="CompressionMethod.None"/>. /// the compression method used to compress the message. The default value is
/// <see cref="CompressionMethod.None"/>.
/// </value> /// </value>
public CompressionMethod Compression { public CompressionMethod Compression {
get { get {
@ -295,8 +296,8 @@ namespace WebSocketSharp
/// Gets the credentials for the HTTP authentication (Basic/Digest). /// Gets the credentials for the HTTP authentication (Basic/Digest).
/// </summary> /// </summary>
/// <value> /// <value>
/// A <see cref="NetworkCredential"/> that represents the credentials for the authentication. /// A <see cref="NetworkCredential"/> that represents the credentials for
/// The default value is <see langword="null"/>. /// the authentication. The default value is <see langword="null"/>.
/// </value> /// </value>
public NetworkCredential Credentials { public NetworkCredential Credentials {
get { get {
@ -391,8 +392,8 @@ namespace WebSocketSharp
} }
/// <summary> /// <summary>
/// Gets or sets the value of the HTTP Origin header to send with the WebSocket connection /// Gets or sets the value of the HTTP Origin header to send with
/// request to the server. /// the WebSocket connection request to the server.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// The <see cref="WebSocket"/> sends the Origin header if this property has any. /// The <see cref="WebSocket"/> sends the Origin header if this property has any.
@ -460,8 +461,8 @@ namespace WebSocketSharp
/// Gets the state of the WebSocket connection. /// Gets the state of the WebSocket connection.
/// </summary> /// </summary>
/// <value> /// <value>
/// One of the <see cref="WebSocketState"/> enum values, indicates the state of the WebSocket /// One of the <see cref="WebSocketState"/> enum values, indicates the state of
/// connection. The default value is <see cref="WebSocketState.Connecting"/>. /// the WebSocket connection. The default value is <see cref="WebSocketState.Connecting"/>.
/// </value> /// </value>
public WebSocketState ReadyState { public WebSocketState ReadyState {
get { get {
@ -590,9 +591,9 @@ namespace WebSocketSharp
_protocol = null; _protocol = null;
if (!_ignoreExtensions) { if (!_ignoreExtensions) {
var extensions = _context.Headers["Sec-WebSocket-Extensions"]; var exts = _context.Headers["Sec-WebSocket-Extensions"];
if (extensions != null && extensions.Length > 0) if (exts != null && exts.Length > 0)
processSecWebSocketExtensionsHeader (extensions); processSecWebSocketExtensionsHeader (exts);
} }
return sendHttpResponse (createHandshakeResponse ()); return sendHttpResponse (createHandshakeResponse ());
@ -676,7 +677,7 @@ namespace WebSocketSharp
_readyState = WebSocketState.Closing; _readyState = WebSocketState.Closing;
} }
_logger.Trace ("Start closing the connection."); _logger.Trace ("Begin closing the connection.");
e.WasClean = closeHandshake ( e.WasClean = closeHandshake (
send ? WebSocketFrame.CreateCloseFrame (e.PayloadData, _client).ToByteArray () : null, send ? WebSocketFrame.CreateCloseFrame (e.PayloadData, _client).ToByteArray () : null,
@ -718,11 +719,11 @@ namespace WebSocketSharp
_exitReceiving = null; _exitReceiving = null;
} }
var res = sent && received; var ret = sent && received;
_logger.Debug ( _logger.Debug (
String.Format ("Was clean?: {0}\nsent: {1} received: {2}", res, sent, received)); String.Format ("Was clean?: {0}\n sent: {1}\n received: {2}", ret, sent, received));
return res; return ret;
} }
private bool concatenateFragmentsInto (Stream destination) private bool concatenateFragmentsInto (Stream destination)
@ -773,7 +774,7 @@ namespace WebSocketSharp
return processUnsupportedFrame ( return processUnsupportedFrame (
frame, frame,
CloseStatusCode.IncorrectData, CloseStatusCode.IncorrectData,
"An incorrect data has been received while receiving the fragmented data."); "Incorrect data has been received while receiving the fragmented data.");
} }
return true; return true;
@ -811,10 +812,10 @@ namespace WebSocketSharp
var buff = new StringBuilder (80); var buff = new StringBuilder (80);
if (_compression != CompressionMethod.None) { if (_compression != CompressionMethod.None) {
var c = _compression.ToExtensionString ( var str = _compression.ToExtensionString (
"server_no_context_takeover", "client_no_context_takeover"); "server_no_context_takeover", "client_no_context_takeover");
buff.AppendFormat ("{0}, ", c); buff.AppendFormat ("{0}, ", str);
} }
var len = buff.Length; var len = buff.Length;
@ -829,18 +830,18 @@ namespace WebSocketSharp
// As server // As server
private HttpResponse createHandshakeCloseResponse (HttpStatusCode code) private HttpResponse createHandshakeCloseResponse (HttpStatusCode code)
{ {
var res = HttpResponse.CreateCloseResponse (code); var ret = HttpResponse.CreateCloseResponse (code);
res.Headers["Sec-WebSocket-Version"] = _version; ret.Headers["Sec-WebSocket-Version"] = _version;
return res; return ret;
} }
// As client // As client
private HttpRequest createHandshakeRequest () private HttpRequest createHandshakeRequest ()
{ {
var req = HttpRequest.CreateWebSocketRequest (_uri); var ret = HttpRequest.CreateWebSocketRequest (_uri);
var headers = req.Headers; var headers = ret.Headers;
if (!_origin.IsNullOrEmpty ()) if (!_origin.IsNullOrEmpty ())
headers["Origin"] = _origin; headers["Origin"] = _origin;
@ -849,9 +850,9 @@ namespace WebSocketSharp
if (_protocols != null) if (_protocols != null)
headers["Sec-WebSocket-Protocol"] = _protocols.ToString (", "); headers["Sec-WebSocket-Protocol"] = _protocols.ToString (", ");
var extensions = createExtensions (); var exts = createExtensions ();
if (extensions != null) if (exts != null)
headers["Sec-WebSocket-Extensions"] = extensions; headers["Sec-WebSocket-Extensions"] = exts;
headers["Sec-WebSocket-Version"] = _version; headers["Sec-WebSocket-Version"] = _version;
@ -868,17 +869,17 @@ namespace WebSocketSharp
headers["Authorization"] = authRes.ToString (); headers["Authorization"] = authRes.ToString ();
if (_cookies.Count > 0) if (_cookies.Count > 0)
req.SetCookies (_cookies); ret.SetCookies (_cookies);
return req; return ret;
} }
// As server // As server
private HttpResponse createHandshakeResponse () private HttpResponse createHandshakeResponse ()
{ {
var res = HttpResponse.CreateWebSocketResponse (); var ret = HttpResponse.CreateWebSocketResponse ();
var headers = res.Headers; var headers = ret.Headers;
headers["Sec-WebSocket-Accept"] = CreateResponseKey (_base64Key); headers["Sec-WebSocket-Accept"] = CreateResponseKey (_base64Key);
if (_protocol != null) if (_protocol != null)
@ -888,9 +889,9 @@ namespace WebSocketSharp
headers["Sec-WebSocket-Extensions"] = _extensions; headers["Sec-WebSocket-Extensions"] = _extensions;
if (_cookies.Count > 0) if (_cookies.Count > 0)
res.SetCookies (_cookies); ret.SetCookies (_cookies);
return res; return ret;
} }
private MessageEventArgs dequeueFromMessageEventQueue () private MessageEventArgs dequeueFromMessageEventQueue ()
@ -1087,10 +1088,10 @@ namespace WebSocketSharp
var ext = e.Trim (); var ext = e.Trim ();
if (!comp && ext.IsCompressionExtension (CompressionMethod.Deflate)) { if (!comp && ext.IsCompressionExtension (CompressionMethod.Deflate)) {
_compression = CompressionMethod.Deflate; _compression = CompressionMethod.Deflate;
var c = _compression.ToExtensionString ( var str = _compression.ToExtensionString (
"client_no_context_takeover", "server_no_context_takeover"); "client_no_context_takeover", "server_no_context_takeover");
buff.AppendFormat ("{0}, ", c); buff.AppendFormat ("{0}, ", str);
comp = true; comp = true;
} }
} }
@ -1728,12 +1729,12 @@ namespace WebSocketSharp
/// and releases all associated resources. /// and releases all associated resources.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method emits a <see cref="OnError"/> event if <paramref name="code"/> /// This method emits a <see cref="OnError"/> event if <paramref name="code"/> isn't in
/// isn't in the allowable range of the close status code. /// the allowable range of the close status code.
/// </remarks> /// </remarks>
/// <param name="code"> /// <param name="code">
/// A <see cref="ushort"/> that represents the status code indicating the reason /// A <see cref="ushort"/> that represents the status code indicating the reason for
/// for the close. /// the close.
/// </param> /// </param>
public void Close (ushort code) public void Close (ushort code)
{ {
@ -1759,8 +1760,8 @@ namespace WebSocketSharp
/// and releases all associated resources. /// and releases all associated resources.
/// </summary> /// </summary>
/// <param name="code"> /// <param name="code">
/// One of the <see cref="CloseStatusCode"/> enum values, represents the status code /// One of the <see cref="CloseStatusCode"/> enum values, represents the status code indicating
/// indicating the reason for the close. /// the reason for the close.
/// </param> /// </param>
public void Close (CloseStatusCode code) public void Close (CloseStatusCode code)
{ {
@ -1782,17 +1783,16 @@ namespace WebSocketSharp
} }
/// <summary> /// <summary>
/// Closes the WebSocket connection with the specified <see cref="ushort"/> /// Closes the WebSocket connection with the specified <see cref="ushort"/> and
/// and <see cref="string"/>, and releases all associated resources. /// <see cref="string"/>, and releases all associated resources.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method emits a <see cref="OnError"/> event if <paramref name="code"/> /// This method emits a <see cref="OnError"/> event if <paramref name="code"/> isn't in
/// isn't in the allowable range of the close status code or the size of /// the allowable range of the close status code or the size of <paramref name="reason"/> is
/// <paramref name="reason"/> is greater than 123 bytes. /// greater than 123 bytes.
/// </remarks> /// </remarks>
/// <param name="code"> /// <param name="code">
/// A <see cref="ushort"/> that represents the status code indicating the reason /// A <see cref="ushort"/> that represents the status code indicating the reason for the close.
/// for the close.
/// </param> /// </param>
/// <param name="reason"> /// <param name="reason">
/// A <see cref="string"/> that represents the reason for the close. /// A <see cref="string"/> that represents the reason for the close.
@ -1817,16 +1817,16 @@ namespace WebSocketSharp
} }
/// <summary> /// <summary>
/// Closes the WebSocket connection with the specified <see cref="CloseStatusCode"/> /// Closes the WebSocket connection with the specified <see cref="CloseStatusCode"/> and
/// and <see cref="string"/>, and releases all associated resources. /// <see cref="string"/>, and releases all associated resources.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method emits a <see cref="OnError"/> event if the size of <paramref name="reason"/> /// This method emits a <see cref="OnError"/> event if the size of <paramref name="reason"/> is
/// is greater than 123 bytes. /// greater than 123 bytes.
/// </remarks> /// </remarks>
/// <param name="code"> /// <param name="code">
/// One of the <see cref="CloseStatusCode"/> enum values, represents the status code /// One of the <see cref="CloseStatusCode"/> enum values, represents the status code indicating
/// indicating the reason for the close. /// the reason for the close.
/// </param> /// </param>
/// <param name="reason"> /// <param name="reason">
/// A <see cref="string"/> that represents the reason for the close. /// A <see cref="string"/> that represents the reason for the close.
@ -1912,8 +1912,8 @@ namespace WebSocketSharp
/// This method doesn't wait for the close to be complete. /// This method doesn't wait for the close to be complete.
/// </remarks> /// </remarks>
/// <param name="code"> /// <param name="code">
/// One of the <see cref="CloseStatusCode"/> enum values, represents the status code /// One of the <see cref="CloseStatusCode"/> enum values, represents
/// indicating the reason for the close. /// the status code indicating the reason for the close.
/// </param> /// </param>
public void CloseAsync (CloseStatusCode code) public void CloseAsync (CloseStatusCode code)
{ {
@ -1935,8 +1935,8 @@ namespace WebSocketSharp
} }
/// <summary> /// <summary>
/// Closes the WebSocket connection asynchronously with the specified <see cref="ushort"/> /// Closes the WebSocket connection asynchronously with the specified <see cref="ushort"/> and
/// and <see cref="string"/>, and releases all associated resources. /// <see cref="string"/>, and releases all associated resources.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// <para> /// <para>
@ -1944,8 +1944,8 @@ namespace WebSocketSharp
/// </para> /// </para>
/// <para> /// <para>
/// This method emits a <see cref="OnError"/> event if <paramref name="code"/> isn't in /// This method emits a <see cref="OnError"/> event if <paramref name="code"/> isn't in
/// the allowable range of the close status code or the size of <paramref name="reason"/> /// the allowable range of the close status code or the size of <paramref name="reason"/> is
/// is greater than 123 bytes. /// greater than 123 bytes.
/// </para> /// </para>
/// </remarks> /// </remarks>
/// <param name="code"> /// <param name="code">
@ -1983,13 +1983,13 @@ namespace WebSocketSharp
/// This method doesn't wait for the close to be complete. /// This method doesn't wait for the close to be complete.
/// </para> /// </para>
/// <para> /// <para>
/// This method emits a <see cref="OnError"/> event if the size of <paramref name="reason"/> /// This method emits a <see cref="OnError"/> event if the size of
/// is greater than 123 bytes. /// <paramref name="reason"/> is greater than 123 bytes.
/// </para> /// </para>
/// </remarks> /// </remarks>
/// <param name="code"> /// <param name="code">
/// One of the <see cref="CloseStatusCode"/> enum values, represents the status code /// One of the <see cref="CloseStatusCode"/> enum values, represents
/// indicating the reason for the close. /// the status code indicating the reason for the close.
/// </param> /// </param>
/// <param name="reason"> /// <param name="reason">
/// A <see cref="string"/> that represents the reason for the close. /// A <see cref="string"/> that represents the reason for the close.
@ -2099,7 +2099,7 @@ namespace WebSocketSharp
} }
/// <summary> /// <summary>
/// Sends a binary <paramref name="data"/> using the WebSocket connection. /// Sends binary <paramref name="data"/> using the WebSocket connection.
/// </summary> /// </summary>
/// <param name="data"> /// <param name="data">
/// An array of <see cref="byte"/> that represents the binary data to send. /// An array of <see cref="byte"/> that represents the binary data to send.
@ -2118,8 +2118,7 @@ namespace WebSocketSharp
} }
/// <summary> /// <summary>
/// Sends the specified <paramref name="file"/> as a binary data /// Sends the specified <paramref name="file"/> as binary data using the WebSocket connection.
/// using the WebSocket connection.
/// </summary> /// </summary>
/// <param name="file"> /// <param name="file">
/// A <see cref="FileInfo"/> that represents the file to send. /// A <see cref="FileInfo"/> that represents the file to send.
@ -2138,7 +2137,7 @@ namespace WebSocketSharp
} }
/// <summary> /// <summary>
/// Sends a text <paramref name="data"/> using the WebSocket connection. /// Sends text <paramref name="data"/> using the WebSocket connection.
/// </summary> /// </summary>
/// <param name="data"> /// <param name="data">
/// A <see cref="string"/> that represents the text data to send. /// A <see cref="string"/> that represents the text data to send.
@ -2157,7 +2156,7 @@ namespace WebSocketSharp
} }
/// <summary> /// <summary>
/// Sends a binary <paramref name="data"/> asynchronously using the WebSocket connection. /// Sends binary <paramref name="data"/> asynchronously using the WebSocket connection.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method doesn't wait for the send to be complete. /// This method doesn't wait for the send to be complete.
@ -2184,8 +2183,8 @@ namespace WebSocketSharp
} }
/// <summary> /// <summary>
/// Sends the specified <paramref name="file"/> as a binary data asynchronously /// Sends the specified <paramref name="file"/> as binary data asynchronously using
/// using the WebSocket connection. /// the WebSocket connection.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method doesn't wait for the send to be complete. /// This method doesn't wait for the send to be complete.
@ -2212,7 +2211,7 @@ namespace WebSocketSharp
} }
/// <summary> /// <summary>
/// Sends a text <paramref name="data"/> asynchronously using the WebSocket connection. /// Sends text <paramref name="data"/> asynchronously using the WebSocket connection.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method doesn't wait for the send to be complete. /// This method doesn't wait for the send to be complete.
@ -2239,8 +2238,8 @@ namespace WebSocketSharp
} }
/// <summary> /// <summary>
/// Sends a binary data from the specified <see cref="Stream"/> asynchronously /// Sends binary data from the specified <see cref="Stream"/> asynchronously using
/// using the WebSocket connection. /// the WebSocket connection.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method doesn't wait for the send to be complete. /// This method doesn't wait for the send to be complete.
@ -2283,7 +2282,7 @@ namespace WebSocketSharp
if (len < length) if (len < length)
_logger.Warn ( _logger.Warn (
String.Format ( String.Format (
"The data with 'length' cannot be read from 'stream'.\nexpected: {0} actual: {1}", "The data with 'length' cannot be read from 'stream':\n expected: {0}\n actual: {1}",
length, length,
len)); len));
@ -2298,8 +2297,8 @@ namespace WebSocketSharp
} }
/// <summary> /// <summary>
/// Sets an HTTP <paramref name="cookie"/> to send with the WebSocket connection request /// Sets an HTTP <paramref name="cookie"/> to send with the WebSocket connection request to
/// to the server. /// the server.
/// </summary> /// </summary>
/// <param name="cookie"> /// <param name="cookie">
/// A <see cref="Cookie"/> that represents the cookie to send. /// A <see cref="Cookie"/> that represents the cookie to send.
@ -2307,9 +2306,7 @@ namespace WebSocketSharp
public void SetCookie (Cookie cookie) public void SetCookie (Cookie cookie)
{ {
lock (_forConn) { lock (_forConn) {
var msg = checkIfAvailable (false, false) ?? var msg = checkIfAvailable (false, false) ?? (cookie == null ? "'cookie' is null." : null);
(cookie == null ? "'cookie' is null." : null);
if (msg != null) { if (msg != null) {
_logger.Error (msg); _logger.Error (msg);
error ("An error has occurred in setting the cookie.", null); error ("An error has occurred in setting the cookie.", null);