[Modify] Polish it

This commit is contained in:
sta 2015-09-19 15:35:15 +09:00
parent 6d3960ed7d
commit 48b55173fa

View File

@ -425,6 +425,57 @@ Extended Payload Length: {7}
payload); payload);
} }
private static WebSocketFrame processHeader (byte[] header)
{
if (header.Length != 2)
throw new WebSocketException (
"The header part of a frame cannot be read from the data source.");
// FIN
var fin = (header[0] & 0x80) == 0x80 ? Fin.Final : Fin.More;
// RSV1
var rsv1 = (header[0] & 0x40) == 0x40 ? Rsv.On : Rsv.Off;
// RSV2
var rsv2 = (header[0] & 0x20) == 0x20 ? Rsv.On : Rsv.Off;
// RSV3
var rsv3 = (header[0] & 0x10) == 0x10 ? Rsv.On : Rsv.Off;
// Opcode
var opcode = (Opcode) (header[0] & 0x0f);
// MASK
var mask = (header[1] & 0x80) == 0x80 ? Mask.Mask : Mask.Unmask;
// Payload Length
var payloadLen = (byte) (header[1] & 0x7f);
// Check if valid header.
var err = isControl (opcode) && payloadLen > 125
? "A control frame has payload data which is greater than the allowable max length."
: isControl (opcode) && fin == Fin.More
? "A control frame is fragmented."
: !isData (opcode) && rsv1 == Rsv.On
? "A non data frame is compressed."
: null;
if (err != null)
throw new WebSocketException (CloseStatusCode.ProtocolError, err);
var frame = new WebSocketFrame ();
frame._fin = fin;
frame._rsv1 = rsv1;
frame._rsv2 = rsv2;
frame._rsv3 = rsv3;
frame._opcode = opcode;
frame._mask = mask;
frame._payloadLength = payloadLen;
return frame;
}
private static WebSocketFrame read (byte[] header, Stream stream, bool unmask) private static WebSocketFrame read (byte[] header, Stream stream, bool unmask)
{ {
/* Header */ /* Header */
@ -550,58 +601,7 @@ Extended Payload Length: {7}
private static void readHeaderAsync ( private static void readHeaderAsync (
Stream stream, Action<WebSocketFrame> completed, Action<Exception> error) Stream stream, Action<WebSocketFrame> completed, Action<Exception> error)
{ {
stream.ReadBytesAsync ( stream.ReadBytesAsync (2, header => completed (processHeader (header)), error);
2,
header => {
if (header.Length != 2)
throw new WebSocketException (
"The header part of a frame cannot be read from the data source.");
// FIN
var fin = (header[0] & 0x80) == 0x80 ? Fin.Final : Fin.More;
// RSV1
var rsv1 = (header[0] & 0x40) == 0x40 ? Rsv.On : Rsv.Off;
// RSV2
var rsv2 = (header[0] & 0x20) == 0x20 ? Rsv.On : Rsv.Off;
// RSV3
var rsv3 = (header[0] & 0x10) == 0x10 ? Rsv.On : Rsv.Off;
// Opcode
var opcode = (Opcode) (header[0] & 0x0f);
// MASK
var mask = (header[1] & 0x80) == 0x80 ? Mask.Mask : Mask.Unmask;
// Payload Length
var payloadLen = (byte) (header[1] & 0x7f);
// Check if valid header.
var err = isControl (opcode) && payloadLen > 125
? "A control frame has payload data which is greater than the allowable max length."
: isControl (opcode) && fin == Fin.More
? "A control frame is fragmented."
: !isData (opcode) && rsv1 == Rsv.On
? "A non data frame is compressed."
: null;
if (err != null)
throw new WebSocketException (CloseStatusCode.ProtocolError, err);
var frame = new WebSocketFrame ();
frame._fin = fin;
frame._rsv1 = rsv1;
frame._rsv2 = rsv2;
frame._rsv3 = rsv3;
frame._opcode = opcode;
frame._mask = mask;
frame._payloadLength = payloadLen;
completed (frame);
},
error);
} }
private static void readMaskingKeyAsync ( private static void readMaskingKeyAsync (