[Modify] Move them to the extension methods

This commit is contained in:
sta 2015-12-15 17:01:43 +09:00
parent a56f0c87dc
commit 514cccac12
2 changed files with 19 additions and 21 deletions

View File

@ -508,6 +508,16 @@ namespace WebSocketSharp
return value.StartsWith (method.ToExtensionString ()); return value.StartsWith (method.ToExtensionString ());
} }
internal static bool IsControl (this byte opcode)
{
return opcode > 0x7 && opcode < 0x10;
}
internal static bool IsData (this byte opcode)
{
return opcode == 0x1 || opcode == 0x2;
}
internal static bool IsPortNumber (this int value) internal static bool IsPortNumber (this int value)
{ {
return value > 0 && value < 65536; return value > 0 && value < 65536;
@ -529,6 +539,11 @@ namespace WebSocketSharp
code == CloseStatusCode.TlsHandshakeFailure; code == CloseStatusCode.TlsHandshakeFailure;
} }
internal static bool IsSupported (this byte opcode)
{
return Enum.IsDefined (typeof (Opcode), opcode);
}
internal static bool IsText (this string value) internal static bool IsText (this string value)
{ {
var len = value.Length; var len = value.Length;

View File

@ -377,33 +377,16 @@ namespace WebSocketSharp
return output.ToString (); return output.ToString ();
} }
private static bool isControl (byte opcode)
{
return opcode == (byte) Opcode.Close ||
opcode == (byte) Opcode.Ping ||
opcode == (byte) Opcode.Pong;
}
private static bool isControl (Opcode opcode) private static bool isControl (Opcode opcode)
{ {
return opcode == Opcode.Close || opcode == Opcode.Ping || opcode == Opcode.Pong; return opcode == Opcode.Close || opcode == Opcode.Ping || opcode == Opcode.Pong;
} }
private static bool isData (byte opcode)
{
return opcode == (byte) Opcode.Text || opcode == (byte) Opcode.Binary;
}
private static bool isData (Opcode opcode) private static bool isData (Opcode opcode)
{ {
return opcode == Opcode.Text || opcode == Opcode.Binary; return opcode == Opcode.Text || opcode == Opcode.Binary;
} }
private static bool isSupported (byte opcode)
{
return Enum.IsDefined (typeof (Opcode), opcode);
}
private static string print (WebSocketFrame frame) private static string print (WebSocketFrame frame)
{ {
// Payload Length // Payload Length
@ -478,13 +461,13 @@ Extended Payload Length: {7}
var payloadLen = (byte) (header[1] & 0x7f); var payloadLen = (byte) (header[1] & 0x7f);
// Check if valid header. // Check if valid header.
var err = !isSupported (opcode) var err = !opcode.IsSupported ()
? "An unsupported opcode." ? "An unsupported opcode."
: isControl (opcode) && fin == Fin.More : opcode.IsControl () && fin == Fin.More
? "A control frame is fragmented." ? "A control frame is fragmented."
: isControl (opcode) && payloadLen > 125 : opcode.IsControl () && payloadLen > 125
? "A control frame has a long payload length." ? "A control frame has a long payload length."
: !isData (opcode) && rsv1 == Rsv.On : !opcode.IsData () && rsv1 == Rsv.On
? "A non data frame is compressed." ? "A non data frame is compressed."
: null; : null;