Modified MessageEventArgs.cs, WsFrame.cs

This commit is contained in:
sta
2013-05-19 01:16:27 +09:00
parent 6f13615945
commit db9be84e85
39 changed files with 56 additions and 60 deletions

View File

@@ -78,9 +78,8 @@ namespace WebSocketSharp {
/// </value>
public string Data {
get {
_data = _data != null
? _data
: _rawData.LongLength == 0
if (_data == null)
_data = _rawData.LongLength == 0
? String.Empty
: _opcode == Opcode.TEXT
? Encoding.UTF8.GetString(_rawData)

View File

@@ -113,13 +113,13 @@ namespace WebSocketSharp {
internal bool IsBinary {
get {
return isBinary(Opcode);
return Opcode == Opcode.BINARY;
}
}
internal bool IsClose {
get {
return isClose(Opcode);
return Opcode == Opcode.CLOSE;
}
}
@@ -131,55 +131,64 @@ namespace WebSocketSharp {
internal bool IsContinuation {
get {
return isContinuation(Opcode);
return Opcode == Opcode.CONT;
}
}
internal bool IsControl {
get {
return isControl(Opcode);
var opcode = Opcode;
return opcode == Opcode.CLOSE || opcode == Opcode.PING || opcode == Opcode.PONG;
}
}
internal bool IsData {
get {
return isData(Opcode);
var opcode = Opcode;
return opcode == Opcode.BINARY || opcode == Opcode.TEXT;
}
}
internal bool IsFinal {
get {
return isFinal(Fin);
return Fin == Fin.FINAL;
}
}
internal bool IsFragmented {
get {
return !IsFinal || IsContinuation;
return Fin == Fin.MORE || Opcode == Opcode.CONT;
}
}
internal bool IsMasked {
get {
return isMasked(Mask);
return Mask == Mask.MASK;
}
}
internal bool IsPerMessageCompressed {
get {
var opcode = Opcode;
return (opcode == Opcode.BINARY || opcode == Opcode.TEXT) && Rsv1 == Rsv.ON;
}
}
internal bool IsPing {
get {
return isPing(Opcode);
return Opcode == Opcode.PING;
}
}
internal bool IsPong {
get {
return isPong(Opcode);
return Opcode == Opcode.PONG;
}
}
internal bool IsText {
get {
return isText(Opcode);
return Opcode == Opcode.TEXT;
}
}
@@ -189,19 +198,6 @@ namespace WebSocketSharp {
}
}
internal bool PerMessageCompressed {
get {
return IsData && IsCompressed;
}
set {
if (value && !IsData)
return;
Rsv1 = value ? Rsv.ON : Rsv.OFF;
}
}
#endregion
#region Public Properties
@@ -230,24 +226,20 @@ namespace WebSocketSharp {
#region Private Methods
private static WsFrame createCloseFrame(WebSocketException exception)
private static WsFrame createCloseFrame(CloseStatusCode code, string message)
{
using (var buffer = new MemoryStream())
{
var code = (ushort)exception.Code;
var msg = exception.Message;
buffer.Write(code.ToByteArray(ByteOrder.BIG), 0, 2);
if (msg.Length != 0)
var tmp = ((ushort)code).ToByteArray(ByteOrder.BIG);
buffer.Write(tmp, 0, 2);
if (message.Length != 0)
{
var tmp = Encoding.UTF8.GetBytes(msg);
tmp = Encoding.UTF8.GetBytes(message);
buffer.Write(tmp, 0, tmp.Length);
}
buffer.Close();
var payload = new PayloadData(buffer.ToArray());
var frame = new WsFrame(Fin.FINAL, Opcode.CLOSE, Mask.UNMASK, payload);
return frame;
return new WsFrame(Fin.FINAL, Opcode.CLOSE, Mask.UNMASK, new PayloadData(buffer.ToArray()));
}
}
@@ -382,9 +374,6 @@ namespace WebSocketSharp {
private static WsFrame parse(byte[] header, Stream stream, bool unmask)
{
if (header == null || header.Length != 2)
return null;
/* Header */
// FIN
@@ -403,7 +392,7 @@ namespace WebSocketSharp {
var payloadLen = (byte)(header[1] & 0x7f);
if (isControl(opcode) && payloadLen > 125)
throw new WebSocketException(CloseStatusCode.INCONSISTENT_DATA,
return createCloseFrame(CloseStatusCode.INCONSISTENT_DATA,
"The payload length of a control frame must be 125 bytes or less.");
var frame = new WsFrame {
@@ -429,7 +418,8 @@ namespace WebSocketSharp {
: new byte[]{};
if (extLen > 0 && extPayloadLen.Length != extLen)
throw new IOException();
return createCloseFrame(CloseStatusCode.ABNORMAL,
"'Extended Payload Length' of a frame cannot be read from the data stream.");
frame.ExtPayloadLen = extPayloadLen;
@@ -441,7 +431,8 @@ namespace WebSocketSharp {
: new byte[]{};
if (masked && maskingKey.Length != 4)
throw new IOException();
return createCloseFrame(CloseStatusCode.ABNORMAL,
"'Masking Key' of a frame cannot be read from the data stream.");
frame.MaskingKey = maskingKey;
@@ -457,14 +448,18 @@ namespace WebSocketSharp {
if (dataLen > 0)
{
if (payloadLen > 126 && dataLen > PayloadData.MaxLength)
throw new WebSocketException(CloseStatusCode.TOO_BIG);
{
var code = CloseStatusCode.TOO_BIG;
return createCloseFrame(code, code.GetMessage());
}
data = dataLen > 1024
? stream.ReadBytesInternal((long)dataLen, 1024)
: stream.ReadBytesInternal((int)dataLen);
if (data.LongLength != (long)dataLen)
throw new IOException();
return createCloseFrame(CloseStatusCode.ABNORMAL,
"'Payload Data' of a frame cannot be read from the data stream.");
}
else
{
@@ -559,11 +554,10 @@ namespace WebSocketSharp {
try
{
var header = stream.ReadBytesInternal(2);
frame = parse(header, stream, unmask);
}
catch (WebSocketException ex)
{
frame = createCloseFrame(ex);
frame = header.Length == 2
? parse(header, stream, unmask)
: createCloseFrame(CloseStatusCode.ABNORMAL,
"'Header' of a frame cannot be read from the data stream.");
}
catch (Exception ex)
{
@@ -594,17 +588,20 @@ namespace WebSocketSharp {
try
{
var readLen = stream.EndRead(ar);
if (readLen > 0)
if (readLen == 1)
{
if (readLen == 1)
header[1] = (byte)stream.ReadByte();
frame = parse(header, stream, unmask);
var tmp = stream.ReadByte();
if (tmp > -1)
{
header[1] = (byte)tmp;
readLen++;
}
}
}
catch (WebSocketException ex)
{
frame = createCloseFrame(ex);
frame = readLen == 2
? parse(header, stream, unmask)
: createCloseFrame(CloseStatusCode.ABNORMAL,
"'Header' of a frame cannot be read from the data stream.");
}
catch (Exception ex)
{

View File

@@ -1,6 +1,6 @@
<Overview>
<Assemblies>
<Assembly Name="websocket-sharp" Version="1.0.2.41351">
<Assembly Name="websocket-sharp" Version="1.0.2.2226">
<AssemblyPublicKey>[00 24 00 00 04 80 00 00 94 00 00 00 06 02 00 00 00 24 00 00 52 53 41 31 00 04 00 00 11 00 00 00 29 17 fb 89 fe c3 91 f7 2b cb 8b e2 61 d2 3f 05 93 6d 65 a8 9e 63 72 a6 f5 d5 2c f2 9d 20 fa 0b c0 70 6a f6 88 7e 8b 90 3f 39 f5 76 c8 48 e0 bb 7b b2 7b ed d3 10 a7 1a 0f 70 98 0f 7f f4 4b 53 09 d2 a5 ef 36 c3 56 b4 aa f0 91 72 63 25 07 89 e0 93 3e 3f 2e f2 b9 73 0e 12 15 5d 43 56 c3 f4 70 a5 89 fe f7 f6 ac 3e 77 c2 d8 d0 84 91 f4 0c d1 f3 8e dc c3 c3 b8 38 3d 0c bf 17 de 20 78 c1 ]</AssemblyPublicKey>
<Attributes>
<Attribute>