Refactored Ext.cs

This commit is contained in:
sta 2014-08-19 14:14:59 +09:00
parent 2355d88a21
commit e7aa1a469d
5 changed files with 142 additions and 143 deletions

View File

@ -114,7 +114,9 @@ namespace WebSocketSharp
stream.Position = 0; stream.Position = 0;
using (var ds = new DeflateStream (stream, CompressionMode.Decompress, true)) { using (var ds = new DeflateStream (stream, CompressionMode.Decompress, true)) {
ds.CopyTo (output, true); ds.CopyTo (output);
output.Position = 0;
return output; return output;
} }
} }
@ -133,13 +135,12 @@ namespace WebSocketSharp
if (len < 1) if (len < 1)
return buffer.SubArray (0, offset); return buffer.SubArray (0, offset);
var tmp = 0;
while (len < length) { while (len < length) {
tmp = stream.Read (buffer, offset + len, length - len); var readLen = stream.Read (buffer, offset + len, length - len);
if (tmp < 1) if (readLen < 1)
break; break;
len += tmp; len += readLen;
} }
return len < length return len < length
@ -148,19 +149,19 @@ namespace WebSocketSharp
} }
private static bool readBytes ( private static bool readBytes (
this Stream stream, byte[] buffer, int offset, int length, Stream dest) this Stream stream, byte[] buffer, int offset, int length, Stream destination)
{ {
var bytes = stream.readBytes (buffer, offset, length); var bytes = stream.readBytes (buffer, offset, length);
var len = bytes.Length; var len = bytes.Length;
dest.Write (bytes, 0, len); destination.Write (bytes, 0, len);
return len == offset + length; return len == offset + length;
} }
private static void times (this ulong n, Action act) private static void times (this ulong n, Action action)
{ {
for (ulong i = 0; i < n; i++) for (ulong i = 0; i < n; i++)
act (); action ();
} }
#endregion #endregion
@ -170,11 +171,11 @@ namespace WebSocketSharp
internal static byte[] Append (this ushort code, string reason) internal static byte[] Append (this ushort code, string reason)
{ {
using (var buff = new MemoryStream ()) { using (var buff = new MemoryStream ()) {
var tmp = code.ToByteArrayInternally (ByteOrder.Big); var bytes = code.InternalToByteArray (ByteOrder.Big);
buff.Write (tmp, 0, 2); buff.Write (bytes, 0, 2);
if (reason != null && reason.Length > 0) { if (reason != null && reason.Length > 0) {
tmp = Encoding.UTF8.GetBytes (reason); bytes = Encoding.UTF8.GetBytes (reason);
buff.Write (tmp, 0, tmp.Length); buff.Write (bytes, 0, bytes.Length);
} }
buff.Close (); buff.Close ();
@ -283,14 +284,14 @@ namespace WebSocketSharp
: null; : null;
} }
internal static string CheckIfValidServicePath (this string servicePath) internal static string CheckIfValidServicePath (this string path)
{ {
return servicePath == null || servicePath.Length == 0 return path == null || path.Length == 0
? "'servicePath' is null or empty." ? "'path' is null or empty."
: servicePath[0] != '/' : path[0] != '/'
? "'servicePath' isn't absolute path." ? "'path' isn't absolute path."
: servicePath.IndexOfAny (new[] {'?', '#'}) != -1 : path.IndexOfAny (new[] { '?', '#' }) > -1
? "'servicePath' contains either or both query and fragment components." ? "'path' contains either or both query and fragment components."
: null; : null;
} }
@ -364,29 +365,21 @@ namespace WebSocketSharp
return contains (0); return contains (0);
} }
internal static T[] Copy<T> (this T[] src, long length) internal static T[] Copy<T> (this T[] source, long length)
{ {
var dest = new T[length]; var dest = new T[length];
Array.Copy (src, 0, dest, 0, length); Array.Copy (source, 0, dest, 0, length);
return dest; return dest;
} }
internal static void CopyTo (this Stream src, Stream dest) internal static void CopyTo (this Stream source, Stream destination)
{ {
src.CopyTo (dest, false);
}
internal static void CopyTo (this Stream src, Stream dest, bool setDefaultPosition)
{
var readLen = 0;
var buffLen = 256; var buffLen = 256;
var buff = new byte[buffLen]; var buff = new byte[buffLen];
while ((readLen = src.Read (buff, 0, buffLen)) > 0) var readLen = 0;
dest.Write (buff, 0, readLen); while ((readLen = source.Read (buff, 0, buffLen)) > 0)
destination.Write (buff, 0, readLen);
if (setDefaultPosition)
dest.Position = 0;
} }
internal static byte[] Decompress (this byte[] data, CompressionMethod method) internal static byte[] Decompress (this byte[] data, CompressionMethod method)
@ -460,7 +453,7 @@ namespace WebSocketSharp
if (original[0] != '/') if (original[0] != '/')
return null; return null;
var i = original.IndexOfAny (new[] {'?', '#'}); var i = original.IndexOfAny (new[] { '?', '#' });
return i > 0 return i > 0
? original.Substring (0, i) ? original.Substring (0, i)
: original; : original;
@ -540,15 +533,37 @@ namespace WebSocketSharp
return null; return null;
var val = nameAndValue.Substring (i + 1).Trim (); var val = nameAndValue.Substring (i + 1).Trim ();
return unquote && val.Length > 1 return unquote
? val.Unquote () ? val.Unquote ()
: val; : val;
} }
internal static TcpListenerWebSocketContext GetWebSocketContext ( internal static TcpListenerWebSocketContext GetWebSocketContext (
this TcpClient client, string protocol, bool secure, X509Certificate cert, Logger logger) this TcpClient tcpClient,
string protocol,
bool secure,
X509Certificate certificate,
Logger logger)
{ {
return new TcpListenerWebSocketContext (client, protocol, secure, cert, logger); return new TcpListenerWebSocketContext (tcpClient, protocol, secure, certificate, logger);
}
internal static byte[] InternalToByteArray (this ushort value, ByteOrder order)
{
var bytes = BitConverter.GetBytes (value);
if (!order.IsHostOrder ())
Array.Reverse (bytes);
return bytes;
}
internal static byte[] InternalToByteArray (this ulong value, ByteOrder order)
{
var bytes = BitConverter.GetBytes (value);
if (!order.IsHostOrder ())
Array.Reverse (bytes);
return bytes;
} }
internal static bool IsCompressionExtension (this string value) internal static bool IsCompressionExtension (this string value)
@ -581,7 +596,7 @@ namespace WebSocketSharp
{ {
var len = value.Length; var len = value.Length;
for (var i = 0; i < len; i++) { for (var i = 0; i < len; i++) {
char c = value[i]; var c = value[i];
if (c < 0x20 && !"\r\n\t".Contains (c)) if (c < 0x20 && !"\r\n\t".Contains (c))
return false; return false;
@ -600,7 +615,7 @@ namespace WebSocketSharp
internal static bool IsToken (this string value) internal static bool IsToken (this string value)
{ {
foreach (char c in value) foreach (var c in value)
if (c < 0x20 || c >= 0x7f || _tspecials.Contains (c)) if (c < 0x20 || c >= 0x7f || _tspecials.Contains (c))
return false; return false;
@ -696,18 +711,17 @@ namespace WebSocketSharp
} }
internal static IEnumerable<string> SplitHeaderValue ( internal static IEnumerable<string> SplitHeaderValue (
this string value, params char[] separator) this string value, params char[] separators)
{ {
var len = value.Length; var len = value.Length;
var separators = new string (separator); var seps = new string (separators);
var buff = new StringBuilder (32); var buff = new StringBuilder (32);
var quoted = false;
var escaped = false; var escaped = false;
var quoted = false;
char c;
for (var i = 0; i < len; i++) { for (var i = 0; i < len; i++) {
c = value[i]; var c = value[i];
if (c == '"') { if (c == '"') {
if (escaped) if (escaped)
escaped = !escaped; escaped = !escaped;
@ -718,7 +732,7 @@ namespace WebSocketSharp
if (i < len - 1 && value[i + 1] == '"') if (i < len - 1 && value[i + 1] == '"')
escaped = true; escaped = true;
} }
else if (separators.Contains (c)) { else if (seps.Contains (c)) {
if (!quoted) { if (!quoted) {
yield return buff.ToString (); yield return buff.ToString ();
buff.Length = 0; buff.Length = 0;
@ -747,24 +761,6 @@ namespace WebSocketSharp
} }
} }
internal static byte[] ToByteArrayInternally (this ushort value, ByteOrder order)
{
var bytes = BitConverter.GetBytes (value);
if (!order.IsHostOrder ())
Array.Reverse (bytes);
return bytes;
}
internal static byte[] ToByteArrayInternally (this ulong value, ByteOrder order)
{
var bytes = BitConverter.GetBytes (value);
if (!order.IsHostOrder ())
Array.Reverse (bytes);
return bytes;
}
internal static CompressionMethod ToCompressionMethod (this string value) internal static CompressionMethod ToCompressionMethod (this string value)
{ {
foreach (CompressionMethod method in Enum.GetValues (typeof (CompressionMethod))) foreach (CompressionMethod method in Enum.GetValues (typeof (CompressionMethod)))
@ -797,14 +793,14 @@ namespace WebSocketSharp
return new List<TSource> (source); return new List<TSource> (source);
} }
internal static ushort ToUInt16 (this byte[] src, ByteOrder srcOrder) internal static ushort ToUInt16 (this byte[] source, ByteOrder sourceOrder)
{ {
return BitConverter.ToUInt16 (src.ToHostOrder (srcOrder), 0); return BitConverter.ToUInt16 (source.ToHostOrder (sourceOrder), 0);
} }
internal static ulong ToUInt64 (this byte[] src, ByteOrder srcOrder) internal static ulong ToUInt64 (this byte[] source, ByteOrder sourceOrder)
{ {
return BitConverter.ToUInt64 (src.ToHostOrder (srcOrder), 0); return BitConverter.ToUInt64 (source.ToHostOrder (sourceOrder), 0);
} }
internal static string TrimEndSlash (this string value) internal static string TrimEndSlash (this string value)
@ -886,6 +882,9 @@ namespace WebSocketSharp
internal static string Unquote (this string value) internal static string Unquote (this string value)
{ {
var start = value.IndexOf ('"'); var start = value.IndexOf ('"');
if (start < 0)
return value;
var end = value.LastIndexOf ('"'); var end = value.LastIndexOf ('"');
var len = end - start - 1; var len = end - start - 1;
@ -896,9 +895,9 @@ namespace WebSocketSharp
: value.Substring (start + 1, len).Replace ("\\\"", "\""); : value.Substring (start + 1, len).Replace ("\\\"", "\"");
} }
internal static void WriteBytes (this Stream stream, byte[] data) internal static void WriteBytes (this Stream stream, byte[] bytes)
{ {
using (var input = new MemoryStream (data)) using (var input = new MemoryStream (bytes))
input.CopyTo (stream); input.CopyTo (stream);
} }
@ -926,7 +925,7 @@ namespace WebSocketSharp
? true ? true
: value == null || value.Length == 0 : value == null || value.Length == 0
? false ? false
: value.IndexOfAny (chars) != -1; : value.IndexOfAny (chars) > -1;
} }
/// <summary> /// <summary>
@ -945,9 +944,9 @@ namespace WebSocketSharp
/// </param> /// </param>
public static bool Contains (this NameValueCollection collection, string name) public static bool Contains (this NameValueCollection collection, string name)
{ {
return collection == null || collection.Count == 0 return collection != null && collection.Count > 0
? false ? collection[name] != null
: collection[name] != null; : false;
} }
/// <summary> /// <summary>
@ -1041,9 +1040,9 @@ namespace WebSocketSharp
public static CookieCollection GetCookies (this NameValueCollection headers, bool response) public static CookieCollection GetCookies (this NameValueCollection headers, bool response)
{ {
var name = response ? "Set-Cookie" : "Cookie"; var name = response ? "Set-Cookie" : "Cookie";
return headers == null || !headers.Contains (name) return headers != null && headers.Contains (name)
? new CookieCollection () ? CookieCollection.Parse (headers[name], response)
: CookieCollection.Parse (headers[name], response); : new CookieCollection ();
} }
/// <summary> /// <summary>
@ -1387,13 +1386,13 @@ namespace WebSocketSharp
/// <param name="n"> /// <param name="n">
/// An <see cref="int"/> is the number of times to execute. /// An <see cref="int"/> is the number of times to execute.
/// </param> /// </param>
/// <param name="act"> /// <param name="action">
/// An <see cref="Action"/> delegate that references the method(s) to execute. /// An <see cref="Action"/> delegate that references the method(s) to execute.
/// </param> /// </param>
public static void Times (this int n, Action act) public static void Times (this int n, Action action)
{ {
if (n > 0 && act != null) if (n > 0 && action != null)
((ulong) n).times (act); ((ulong) n).times (action);
} }
/// <summary> /// <summary>
@ -1402,13 +1401,13 @@ namespace WebSocketSharp
/// <param name="n"> /// <param name="n">
/// A <see cref="long"/> is the number of times to execute. /// A <see cref="long"/> is the number of times to execute.
/// </param> /// </param>
/// <param name="act"> /// <param name="action">
/// An <see cref="Action"/> delegate that references the method(s) to execute. /// An <see cref="Action"/> delegate that references the method(s) to execute.
/// </param> /// </param>
public static void Times (this long n, Action act) public static void Times (this long n, Action action)
{ {
if (n > 0 && act != null) if (n > 0 && action != null)
((ulong) n).times (act); ((ulong) n).times (action);
} }
/// <summary> /// <summary>
@ -1417,13 +1416,13 @@ namespace WebSocketSharp
/// <param name="n"> /// <param name="n">
/// A <see cref="uint"/> is the number of times to execute. /// A <see cref="uint"/> is the number of times to execute.
/// </param> /// </param>
/// <param name="act"> /// <param name="action">
/// An <see cref="Action"/> delegate that references the method(s) to execute. /// An <see cref="Action"/> delegate that references the method(s) to execute.
/// </param> /// </param>
public static void Times (this uint n, Action act) public static void Times (this uint n, Action action)
{ {
if (n > 0 && act != null) if (n > 0 && action != null)
((ulong) n).times (act); ((ulong) n).times (action);
} }
/// <summary> /// <summary>
@ -1432,13 +1431,13 @@ namespace WebSocketSharp
/// <param name="n"> /// <param name="n">
/// A <see cref="ulong"/> is the number of times to execute. /// A <see cref="ulong"/> is the number of times to execute.
/// </param> /// </param>
/// <param name="act"> /// <param name="action">
/// An <see cref="Action"/> delegate that references the method(s) to execute. /// An <see cref="Action"/> delegate that references the method(s) to execute.
/// </param> /// </param>
public static void Times (this ulong n, Action act) public static void Times (this ulong n, Action action)
{ {
if (n > 0 && act != null) if (n > 0 && action != null)
n.times (act); n.times (action);
} }
/// <summary> /// <summary>
@ -1447,16 +1446,16 @@ namespace WebSocketSharp
/// <param name="n"> /// <param name="n">
/// An <see cref="int"/> is the number of times to execute. /// An <see cref="int"/> is the number of times to execute.
/// </param> /// </param>
/// <param name="act"> /// <param name="action">
/// An <c>Action&lt;int&gt;</c> delegate that references the method(s) to execute. /// An <c>Action&lt;int&gt;</c> delegate that references the method(s) to execute.
/// An <see cref="int"/> parameter to pass to the method(s) is the zero-based count /// An <see cref="int"/> parameter to pass to the method(s) is the zero-based count
/// of iteration. /// of iteration.
/// </param> /// </param>
public static void Times (this int n, Action<int> act) public static void Times (this int n, Action<int> action)
{ {
if (n > 0 && act != null) if (n > 0 && action != null)
for (int i = 0; i < n; i++) for (int i = 0; i < n; i++)
act (i); action (i);
} }
/// <summary> /// <summary>
@ -1465,16 +1464,16 @@ namespace WebSocketSharp
/// <param name="n"> /// <param name="n">
/// A <see cref="long"/> is the number of times to execute. /// A <see cref="long"/> is the number of times to execute.
/// </param> /// </param>
/// <param name="act"> /// <param name="action">
/// An <c>Action&lt;long&gt;</c> delegate that references the method(s) to execute. /// An <c>Action&lt;long&gt;</c> delegate that references the method(s) to execute.
/// A <see cref="long"/> parameter to pass to the method(s) is the zero-based count /// A <see cref="long"/> parameter to pass to the method(s) is the zero-based count
/// of iteration. /// of iteration.
/// </param> /// </param>
public static void Times (this long n, Action<long> act) public static void Times (this long n, Action<long> action)
{ {
if (n > 0 && act != null) if (n > 0 && action != null)
for (long i = 0; i < n; i++) for (long i = 0; i < n; i++)
act (i); action (i);
} }
/// <summary> /// <summary>
@ -1483,16 +1482,16 @@ namespace WebSocketSharp
/// <param name="n"> /// <param name="n">
/// A <see cref="uint"/> is the number of times to execute. /// A <see cref="uint"/> is the number of times to execute.
/// </param> /// </param>
/// <param name="act"> /// <param name="action">
/// An <c>Action&lt;uint&gt;</c> delegate that references the method(s) to execute. /// An <c>Action&lt;uint&gt;</c> delegate that references the method(s) to execute.
/// A <see cref="uint"/> parameter to pass to the method(s) is the zero-based count /// A <see cref="uint"/> parameter to pass to the method(s) is the zero-based count
/// of iteration. /// of iteration.
/// </param> /// </param>
public static void Times (this uint n, Action<uint> act) public static void Times (this uint n, Action<uint> action)
{ {
if (n > 0 && act != null) if (n > 0 && action != null)
for (uint i = 0; i < n; i++) for (uint i = 0; i < n; i++)
act (i); action (i);
} }
/// <summary> /// <summary>
@ -1501,52 +1500,52 @@ namespace WebSocketSharp
/// <param name="n"> /// <param name="n">
/// A <see cref="ulong"/> is the number of times to execute. /// A <see cref="ulong"/> is the number of times to execute.
/// </param> /// </param>
/// <param name="act"> /// <param name="action">
/// An <c>Action&lt;ulong&gt;</c> delegate that references the method(s) to execute. /// An <c>Action&lt;ulong&gt;</c> delegate that references the method(s) to execute.
/// A <see cref="ulong"/> parameter to pass to this method(s) is the zero-based count /// A <see cref="ulong"/> parameter to pass to this method(s) is the zero-based count
/// of iteration. /// of iteration.
/// </param> /// </param>
public static void Times (this ulong n, Action<ulong> act) public static void Times (this ulong n, Action<ulong> action)
{ {
if (n > 0 && act != null) if (n > 0 && action != null)
for (ulong i = 0; i < n; i++) for (ulong i = 0; i < n; i++)
act (i); action (i);
} }
/// <summary> /// <summary>
/// Converts the specified array of <see cref="byte"/> to the specified type data. /// Converts the specified array of <see cref="byte"/> to the specified type data.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// A T converted from <paramref name="src"/>, or a default value of T if /// A T converted from <paramref name="source"/>, or a default value of T if
/// <paramref name="src"/> is an empty array of <see cref="byte"/> or if the /// <paramref name="source"/> is an empty array of <see cref="byte"/> or if the
/// type of T isn't <see cref="bool"/>, <see cref="char"/>, <see cref="double"/>, /// type of T isn't <see cref="bool"/>, <see cref="char"/>, <see cref="double"/>,
/// <see cref="float"/>, <see cref="int"/>, <see cref="long"/>, <see cref="short"/>, /// <see cref="float"/>, <see cref="int"/>, <see cref="long"/>, <see cref="short"/>,
/// <see cref="uint"/>, <see cref="ulong"/>, or <see cref="ushort"/>. /// <see cref="uint"/>, <see cref="ulong"/>, or <see cref="ushort"/>.
/// </returns> /// </returns>
/// <param name="src"> /// <param name="source">
/// An array of <see cref="byte"/> to convert. /// An array of <see cref="byte"/> to convert.
/// </param> /// </param>
/// <param name="srcOrder"> /// <param name="sourceOrder">
/// One of the <see cref="ByteOrder"/> enum values, indicates the byte order of /// One of the <see cref="ByteOrder"/> enum values, indicates the byte order of
/// <paramref name="src"/>. /// <paramref name="source"/>.
/// </param> /// </param>
/// <typeparam name="T"> /// <typeparam name="T">
/// The type of the return. The T must be a value type. /// The type of the return. The T must be a value type.
/// </typeparam> /// </typeparam>
/// <exception cref="ArgumentNullException"> /// <exception cref="ArgumentNullException">
/// <paramref name="src"/> is <see langword="null"/>. /// <paramref name="source"/> is <see langword="null"/>.
/// </exception> /// </exception>
public static T To<T> (this byte[] src, ByteOrder srcOrder) public static T To<T> (this byte[] source, ByteOrder sourceOrder)
where T : struct where T : struct
{ {
if (src == null) if (source == null)
throw new ArgumentNullException ("src"); throw new ArgumentNullException ("source");
if (src.Length == 0) if (source.Length == 0)
return default (T); return default (T);
var type = typeof (T); var type = typeof (T);
var buff = src.ToHostOrder (srcOrder); var buff = source.ToHostOrder (sourceOrder);
return type == typeof (Boolean) return type == typeof (Boolean)
? (T)(object) BitConverter.ToBoolean (buff, 0) ? (T)(object) BitConverter.ToBoolean (buff, 0)
@ -1624,26 +1623,26 @@ namespace WebSocketSharp
/// Converts the order of the specified array of <see cref="byte"/> to the host byte order. /// Converts the order of the specified array of <see cref="byte"/> to the host byte order.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// An array of <see cref="byte"/> converted from <paramref name="src"/>. /// An array of <see cref="byte"/> converted from <paramref name="source"/>.
/// </returns> /// </returns>
/// <param name="src"> /// <param name="source">
/// An array of <see cref="byte"/> to convert. /// An array of <see cref="byte"/> to convert.
/// </param> /// </param>
/// <param name="srcOrder"> /// <param name="sourceOrder">
/// One of the <see cref="ByteOrder"/> enum values, indicates the byte order of /// One of the <see cref="ByteOrder"/> enum values, indicates the byte order of
/// <paramref name="src"/>. /// <paramref name="source"/>.
/// </param> /// </param>
/// <exception cref="ArgumentNullException"> /// <exception cref="ArgumentNullException">
/// <paramref name="src"/> is <see langword="null"/>. /// <paramref name="source"/> is <see langword="null"/>.
/// </exception> /// </exception>
public static byte[] ToHostOrder (this byte[] src, ByteOrder srcOrder) public static byte[] ToHostOrder (this byte[] source, ByteOrder sourceOrder)
{ {
if (src == null) if (source == null)
throw new ArgumentNullException ("src"); throw new ArgumentNullException ("source");
return src.Length > 1 && !srcOrder.IsHostOrder () return source.Length > 1 && !sourceOrder.IsHostOrder ()
? src.Reverse () ? source.Reverse ()
: src; : source;
} }
/// <summary> /// <summary>
@ -1717,9 +1716,9 @@ namespace WebSocketSharp
/// </param> /// </param>
public static string UrlDecode (this string value) public static string UrlDecode (this string value)
{ {
return value == null || value.Length == 0 return value != null && value.Length > 0
? value ? HttpUtility.UrlDecode (value)
: HttpUtility.UrlDecode (value); : value;
} }
/// <summary> /// <summary>
@ -1734,9 +1733,9 @@ namespace WebSocketSharp
/// </param> /// </param>
public static string UrlEncode (this string value) public static string UrlEncode (this string value)
{ {
return value == null || value.Length == 0 return value != null && value.Length > 0
? value ? HttpUtility.UrlEncode (value)
: HttpUtility.UrlEncode (value); : value;
} }
/// <summary> /// <summary>

View File

@ -410,7 +410,7 @@ namespace WebSocketSharp.Server
} }
_services.Stop ( _services.Stop (
((ushort) CloseStatusCode.ServerError).ToByteArrayInternally (ByteOrder.Big), true); ((ushort) CloseStatusCode.ServerError).InternalToByteArray (ByteOrder.Big), true);
_listener.Abort (); _listener.Abort ();
_state = ServerState.Stop; _state = ServerState.Stop;

View File

@ -466,7 +466,7 @@ namespace WebSocketSharp.Server
_listener.Stop (); _listener.Stop ();
_services.Stop ( _services.Stop (
((ushort) CloseStatusCode.ServerError).ToByteArrayInternally (ByteOrder.Big), true); ((ushort) CloseStatusCode.ServerError).InternalToByteArray (ByteOrder.Big), true);
_state = ServerState.Stop; _state = ServerState.Stop;
} }

View File

@ -295,7 +295,7 @@ namespace WebSocketSharp.Server
if (host.Sessions.State == ServerState.Start) if (host.Sessions.State == ServerState.Start)
host.Sessions.Stop ( host.Sessions.Stop (
((ushort) CloseStatusCode.Away).ToByteArrayInternally (ByteOrder.Big), true); ((ushort) CloseStatusCode.Away).InternalToByteArray (ByteOrder.Big), true);
return true; return true;
} }

View File

@ -108,11 +108,11 @@ namespace WebSocketSharp
} }
else if (len < 0x010000) { else if (len < 0x010000) {
_payloadLength = (byte) 126; _payloadLength = (byte) 126;
_extPayloadLength = ((ushort) len).ToByteArrayInternally (ByteOrder.Big); _extPayloadLength = ((ushort) len).InternalToByteArray (ByteOrder.Big);
} }
else { else {
_payloadLength = (byte) 127; _payloadLength = (byte) 127;
_extPayloadLength = len.ToByteArrayInternally (ByteOrder.Big); _extPayloadLength = len.InternalToByteArray (ByteOrder.Big);
} }
if (mask == Mask.Mask) { if (mask == Mask.Mask) {
@ -640,7 +640,7 @@ Extended Payload Length: {7}
header = (header << 4) + (int) _opcode; header = (header << 4) + (int) _opcode;
header = (header << 1) + (int) _mask; header = (header << 1) + (int) _mask;
header = (header << 7) + (int) _payloadLength; header = (header << 7) + (int) _payloadLength;
buff.Write (((ushort) header).ToByteArrayInternally (ByteOrder.Big), 0, 2); buff.Write (((ushort) header).InternalToByteArray (ByteOrder.Big), 0, 2);
if (_payloadLength > 125) if (_payloadLength > 125)
buff.Write (_extPayloadLength, 0, _extPayloadLength.Length); buff.Write (_extPayloadLength, 0, _extPayloadLength.Length);