[Modify] Polish it
This commit is contained in:
parent
f902958aad
commit
3521004c43
@ -495,12 +495,12 @@ Extended Payload Length: {7}
|
|||||||
|
|
||||||
private static void readExtendedPayloadLengthAsync (
|
private static void readExtendedPayloadLengthAsync (
|
||||||
Stream stream,
|
Stream stream,
|
||||||
int length,
|
|
||||||
WebSocketFrame frame,
|
WebSocketFrame frame,
|
||||||
Action<WebSocketFrame> completed,
|
Action<WebSocketFrame> completed,
|
||||||
Action<Exception> error)
|
Action<Exception> error)
|
||||||
{
|
{
|
||||||
if (length == 0) {
|
var len = frame._payloadLength < 126 ? 0 : (frame._payloadLength == 126 ? 2 : 8);
|
||||||
|
if (len == 0) {
|
||||||
frame._extPayloadLength = WebSocket.EmptyBytes;
|
frame._extPayloadLength = WebSocket.EmptyBytes;
|
||||||
completed (frame);
|
completed (frame);
|
||||||
|
|
||||||
@ -508,13 +508,13 @@ Extended Payload Length: {7}
|
|||||||
}
|
}
|
||||||
|
|
||||||
stream.ReadBytesAsync (
|
stream.ReadBytesAsync (
|
||||||
length,
|
len,
|
||||||
len => {
|
bytes => {
|
||||||
if (len.Length != length)
|
if (bytes.Length != len)
|
||||||
throw new WebSocketException (
|
throw new WebSocketException (
|
||||||
"The 'Extended Payload Length' of a frame cannot be read from the data source.");
|
"The 'Extended Payload Length' of a frame cannot be read from the data source.");
|
||||||
|
|
||||||
frame._extPayloadLength = len;
|
frame._extPayloadLength = bytes;
|
||||||
completed (frame);
|
completed (frame);
|
||||||
},
|
},
|
||||||
error);
|
error);
|
||||||
@ -528,7 +528,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 (2, header => completed (processHeader (header)), error);
|
stream.ReadBytesAsync (2, bytes => completed (processHeader (bytes)), error);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static WebSocketFrame readMaskingKey (Stream stream, WebSocketFrame frame)
|
private static WebSocketFrame readMaskingKey (Stream stream, WebSocketFrame frame)
|
||||||
@ -550,12 +550,12 @@ Extended Payload Length: {7}
|
|||||||
|
|
||||||
private static void readMaskingKeyAsync (
|
private static void readMaskingKeyAsync (
|
||||||
Stream stream,
|
Stream stream,
|
||||||
int length,
|
|
||||||
WebSocketFrame frame,
|
WebSocketFrame frame,
|
||||||
Action<WebSocketFrame> completed,
|
Action<WebSocketFrame> completed,
|
||||||
Action<Exception> error)
|
Action<Exception> error)
|
||||||
{
|
{
|
||||||
if (length == 0) {
|
var len = frame.IsMasked ? 4 : 0;
|
||||||
|
if (len == 0) {
|
||||||
frame._maskingKey = WebSocket.EmptyBytes;
|
frame._maskingKey = WebSocket.EmptyBytes;
|
||||||
completed (frame);
|
completed (frame);
|
||||||
|
|
||||||
@ -563,13 +563,13 @@ Extended Payload Length: {7}
|
|||||||
}
|
}
|
||||||
|
|
||||||
stream.ReadBytesAsync (
|
stream.ReadBytesAsync (
|
||||||
length,
|
len,
|
||||||
key => {
|
bytes => {
|
||||||
if (key.Length != length)
|
if (bytes.Length != len)
|
||||||
throw new WebSocketException (
|
throw new WebSocketException (
|
||||||
"The 'Masking Key' of a frame cannot be read from the data source.");
|
"The 'Masking Key' of a frame cannot be read from the data source.");
|
||||||
|
|
||||||
frame._maskingKey = key;
|
frame._maskingKey = bytes;
|
||||||
completed (frame);
|
completed (frame);
|
||||||
},
|
},
|
||||||
error);
|
error);
|
||||||
@ -608,34 +608,39 @@ Extended Payload Length: {7}
|
|||||||
|
|
||||||
private static void readPayloadDataAsync (
|
private static void readPayloadDataAsync (
|
||||||
Stream stream,
|
Stream stream,
|
||||||
ulong length,
|
|
||||||
WebSocketFrame frame,
|
WebSocketFrame frame,
|
||||||
Action<WebSocketFrame> completed,
|
Action<WebSocketFrame> completed,
|
||||||
Action<Exception> error)
|
Action<Exception> error)
|
||||||
{
|
{
|
||||||
if (length > PayloadData.MaxLength)
|
ulong len = frame._payloadLength < 126
|
||||||
throw new WebSocketException (
|
? frame._payloadLength
|
||||||
CloseStatusCode.TooBig,
|
: frame._payloadLength == 126
|
||||||
"The length of 'Payload Data' of a frame is greater than the allowable max length.");
|
? frame._extPayloadLength.ToUInt16 (ByteOrder.Big)
|
||||||
|
: frame._extPayloadLength.ToUInt64 (ByteOrder.Big);
|
||||||
|
|
||||||
var masked = frame._mask == Mask.Mask;
|
if (len == 0) {
|
||||||
if (length == 0) {
|
frame._payloadData = new PayloadData (WebSocket.EmptyBytes, frame.IsMasked);
|
||||||
frame._payloadData = new PayloadData (WebSocket.EmptyBytes, masked);
|
|
||||||
completed (frame);
|
completed (frame);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if allowable length.
|
||||||
|
if (len > PayloadData.MaxLength)
|
||||||
|
throw new WebSocketException (
|
||||||
|
CloseStatusCode.TooBig,
|
||||||
|
"The length of 'Payload Data' of a frame is greater than the allowable max length.");
|
||||||
|
|
||||||
if (frame._payloadLength < 127) {
|
if (frame._payloadLength < 127) {
|
||||||
var len = (int) length;
|
var ilen = (int) len;
|
||||||
stream.ReadBytesAsync (
|
stream.ReadBytesAsync (
|
||||||
len,
|
ilen,
|
||||||
data => {
|
bytes => {
|
||||||
if (data.Length != len)
|
if (bytes.Length != ilen)
|
||||||
throw new WebSocketException (
|
throw new WebSocketException (
|
||||||
"The 'Payload Data' of a frame cannot be read from the data source.");
|
"The 'Payload Data' of a frame cannot be read from the data source.");
|
||||||
|
|
||||||
frame._payloadData = new PayloadData (data, masked);
|
frame._payloadData = new PayloadData (bytes, frame.IsMasked);
|
||||||
completed (frame);
|
completed (frame);
|
||||||
},
|
},
|
||||||
error);
|
error);
|
||||||
@ -643,16 +648,16 @@ Extended Payload Length: {7}
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var llen = (long) length;
|
var llen = (long) len;
|
||||||
stream.ReadBytesAsync (
|
stream.ReadBytesAsync (
|
||||||
llen,
|
llen,
|
||||||
1024,
|
1024,
|
||||||
data => {
|
bytes => {
|
||||||
if (data.LongLength != llen)
|
if (bytes.LongLength != llen)
|
||||||
throw new WebSocketException (
|
throw new WebSocketException (
|
||||||
"The 'Payload Data' of a frame cannot be read from the data source.");
|
"The 'Payload Data' of a frame cannot be read from the data source.");
|
||||||
|
|
||||||
frame._payloadData = new PayloadData (data, masked);
|
frame._payloadData = new PayloadData (bytes, frame.IsMasked);
|
||||||
completed (frame);
|
completed (frame);
|
||||||
},
|
},
|
||||||
error);
|
error);
|
||||||
@ -724,41 +729,27 @@ Extended Payload Length: {7}
|
|||||||
{
|
{
|
||||||
readHeaderAsync (
|
readHeaderAsync (
|
||||||
stream,
|
stream,
|
||||||
frame => {
|
frame =>
|
||||||
var payloadLen = frame._payloadLength;
|
|
||||||
readExtendedPayloadLengthAsync (
|
readExtendedPayloadLengthAsync (
|
||||||
stream,
|
stream,
|
||||||
payloadLen < 126 ? 0 : (payloadLen == 126 ? 2 : 8),
|
|
||||||
frame,
|
frame,
|
||||||
frame1 => {
|
frame1 =>
|
||||||
var masked = frame1._mask == Mask.Mask;
|
|
||||||
readMaskingKeyAsync (
|
readMaskingKeyAsync (
|
||||||
stream,
|
stream,
|
||||||
masked ? 4 : 0,
|
|
||||||
frame1,
|
frame1,
|
||||||
frame2 => {
|
frame2 =>
|
||||||
ulong len = payloadLen < 126
|
|
||||||
? payloadLen
|
|
||||||
: payloadLen == 126
|
|
||||||
? frame2._extPayloadLength.ToUInt16 (ByteOrder.Big)
|
|
||||||
: frame2._extPayloadLength.ToUInt64 (ByteOrder.Big);
|
|
||||||
|
|
||||||
readPayloadDataAsync (
|
readPayloadDataAsync (
|
||||||
stream,
|
stream,
|
||||||
len,
|
|
||||||
frame2,
|
frame2,
|
||||||
frame3 => {
|
frame3 => {
|
||||||
if (unmask && masked)
|
if (unmask && frame3.IsMasked)
|
||||||
frame3.Unmask ();
|
frame3.Unmask ();
|
||||||
|
|
||||||
completed (frame3);
|
completed (frame3);
|
||||||
},
|
},
|
||||||
error);
|
error),
|
||||||
},
|
error),
|
||||||
error);
|
error),
|
||||||
},
|
|
||||||
error);
|
|
||||||
},
|
|
||||||
error);
|
error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user