Modified WebSocket.cs

This commit is contained in:
sta 2013-05-07 17:04:51 +09:00
parent 9a3c6632de
commit 3432fd1727
38 changed files with 46 additions and 61 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -143,7 +143,7 @@ namespace WebSocketSharp {
public WebSocket(string url, params string[] protocols) public WebSocket(string url, params string[] protocols)
: this() : this()
{ {
if (url.IsNull()) if (url == null)
throw new ArgumentNullException("url"); throw new ArgumentNullException("url");
Uri uri; Uri uri;
@ -332,7 +332,7 @@ namespace WebSocketSharp {
var origin = new Uri(value); var origin = new Uri(value);
if (!origin.IsAbsoluteUri || origin.Segments.Length > 1) if (!origin.IsAbsoluteUri || origin.Segments.Length > 1)
{ {
onError("The syntax of value must be '<scheme>://<host>[:<port>]'."); onError("The syntax of value of Origin must be '<scheme>://<host>[:<port>]'.");
return; return;
} }
@ -524,13 +524,13 @@ namespace WebSocketSharp {
// As client // As client
private void closeResourcesAsClient() private void closeResourcesAsClient()
{ {
if (!_wsStream.IsNull()) if (_wsStream != null)
{ {
_wsStream.Dispose(); _wsStream.Dispose();
_wsStream = null; _wsStream = null;
} }
if (!_tcpClient.IsNull()) if (_tcpClient != null)
{ {
_tcpClient.Close(); _tcpClient.Close();
_tcpClient = null; _tcpClient = null;
@ -540,11 +540,11 @@ namespace WebSocketSharp {
// As server // As server
private void closeResourcesAsServer() private void closeResourcesAsServer()
{ {
if (!_context.IsNull() && !_closeContext.IsNull()) if (_context != null && _closeContext != null)
{ {
_closeContext(); _closeContext();
_wsStream = null; _wsStream = null;
_context = null; _context = null;
} }
} }
@ -561,7 +561,7 @@ namespace WebSocketSharp {
while (true) while (true)
{ {
var frame = readFrame(); var frame = _wsStream.ReadFrame();
if (processAbnormal(frame)) if (processAbnormal(frame))
return false; return false;
@ -622,7 +622,10 @@ namespace WebSocketSharp {
private static WsFrame createControlFrame(Opcode opcode, PayloadData payloadData, bool client) private static WsFrame createControlFrame(Opcode opcode, PayloadData payloadData, bool client)
{ {
return createFrame(Fin.FINAL, opcode, payloadData, false, client); var mask = client ? Mask.MASK : Mask.UNMASK;
var frame = new WsFrame(Fin.FINAL, opcode, mask, payloadData);
return frame;
} }
private static string createCurrentCompressionExtension(CompressionMethod method) private static string createCurrentCompressionExtension(CompressionMethod method)
@ -653,7 +656,7 @@ namespace WebSocketSharp {
{ {
var extensions = new StringBuilder(64); var extensions = new StringBuilder(64);
var comp = createCompressionExtension(_compression); var comp = createCompressionExtension(_compression);
if (!comp.IsEmpty()) if (comp.Length > 0)
extensions.Append(comp); extensions.Append(comp);
return extensions.Length > 0 return extensions.Length > 0
@ -672,7 +675,7 @@ namespace WebSocketSharp {
var req = new RequestHandshake(path); var req = new RequestHandshake(path);
req.AddHeader("Host", host); req.AddHeader("Host", host);
if (!_origin.IsEmpty()) if (_origin.Length > 0)
req.AddHeader("Origin", _origin); req.AddHeader("Origin", _origin);
req.AddHeader("Sec-WebSocket-Key", _base64key); req.AddHeader("Sec-WebSocket-Key", _base64key);
@ -681,7 +684,7 @@ namespace WebSocketSharp {
req.AddHeader("Sec-WebSocket-Protocol", _protocols); req.AddHeader("Sec-WebSocket-Protocol", _protocols);
var extensions = createRequestExtensions(); var extensions = createRequestExtensions();
if (!extensions.IsEmpty()) if (extensions.Length > 0)
req.AddHeader("Sec-WebSocket-Extensions", extensions); req.AddHeader("Sec-WebSocket-Extensions", extensions);
req.AddHeader("Sec-WebSocket-Version", _version); req.AddHeader("Sec-WebSocket-Version", _version);
@ -697,7 +700,7 @@ namespace WebSocketSharp {
{ {
var res = new ResponseHandshake(); var res = new ResponseHandshake();
res.AddHeader("Sec-WebSocket-Accept", createResponseKey()); res.AddHeader("Sec-WebSocket-Accept", createResponseKey());
if (!_extensions.IsEmpty()) if (_extensions.Length > 0)
res.AddHeader("Sec-WebSocket-Extensions", _extensions); res.AddHeader("Sec-WebSocket-Extensions", _extensions);
if (_cookies.Count > 0) if (_cookies.Count > 0)
@ -774,7 +777,7 @@ namespace WebSocketSharp {
private static bool isCompressionExtension(string value, CompressionMethod method) private static bool isCompressionExtension(string value, CompressionMethod method)
{ {
var expected = createCompressionExtension(method); var expected = createCompressionExtension(method);
return !expected.IsEmpty() return expected.Length > 0
? value.Equals(expected) ? value.Equals(expected)
: false; : false;
} }
@ -834,7 +837,7 @@ namespace WebSocketSharp {
private void onClose(CloseEventArgs eventArgs) private void onClose(CloseEventArgs eventArgs)
{ {
if (!Thread.CurrentThread.IsBackground) if (!Thread.CurrentThread.IsBackground)
if (!_exitReceiving.IsNull()) if (_exitReceiving != null)
_exitReceiving.WaitOne(5 * 1000); _exitReceiving.WaitOne(5 * 1000);
if (!closeResources()) if (!closeResources())
@ -855,7 +858,7 @@ namespace WebSocketSharp {
private void onMessage(MessageEventArgs eventArgs) private void onMessage(MessageEventArgs eventArgs)
{ {
if (!eventArgs.IsNull()) if (eventArgs != null)
OnMessage.Emit(this, eventArgs); OnMessage.Emit(this, eventArgs);
} }
@ -897,7 +900,7 @@ namespace WebSocketSharp {
private bool processAbnormal(WsFrame frame) private bool processAbnormal(WsFrame frame)
{ {
if (!frame.IsNull()) if (frame != null)
return false; return false;
#if DEBUG #if DEBUG
@ -950,7 +953,7 @@ namespace WebSocketSharp {
return false; return false;
bool incorrect = !frame.IsData || bool incorrect = !frame.IsData ||
frame.IsCompressed && _compression == CompressionMethod.NONE; (frame.IsCompressed && _compression == CompressionMethod.NONE);
if (!incorrect) if (!incorrect)
processFragments(frame); processFragments(frame);
@ -1055,7 +1058,7 @@ namespace WebSocketSharp {
} }
if (buffer.Count > 0) if (buffer.Count > 0)
_extensions = buffer.ToArray().ToString(","); _extensions = buffer.ToArray().ToString(", ");
} }
// As server // As server
@ -1147,20 +1150,10 @@ namespace WebSocketSharp {
_protocol = protocol; _protocol = protocol;
} }
private WsFrame readFrame()
{
return _wsStream.ReadFrame();
}
private string[] readHandshake()
{
return _wsStream.ReadHandshake();
}
// As client // As client
private ResponseHandshake receiveResponseHandshake() private ResponseHandshake receiveResponseHandshake()
{ {
var res = ResponseHandshake.Parse(readHandshake()); var res = ResponseHandshake.Parse(_wsStream.ReadHandshake());
#if DEBUG #if DEBUG
Console.WriteLine("WS: Info@receiveResponseHandshake: Response handshake from server:\n"); Console.WriteLine("WS: Info@receiveResponseHandshake: Response handshake from server:\n");
Console.WriteLine(res.ToString()); Console.WriteLine(res.ToString());
@ -1198,7 +1191,7 @@ namespace WebSocketSharp {
try try
{ {
if (_wsStream.IsNull()) if (_wsStream == null)
return false; return false;
_wsStream.Write(frame); _wsStream.Write(frame);
@ -1211,14 +1204,6 @@ namespace WebSocketSharp {
} }
} }
private void send(Opcode opcode, byte[] data)
{
using (MemoryStream ms = new MemoryStream(data))
{
send(opcode, ms);
}
}
private void send(Opcode opcode, Stream stream) private void send(Opcode opcode, Stream stream)
{ {
if (_compression == CompressionMethod.NONE) if (_compression == CompressionMethod.NONE)
@ -1264,21 +1249,15 @@ namespace WebSocketSharp {
return send(frame); return send(frame);
} }
private void sendAsync(Opcode opcode, byte[] data, Action completed)
{
sendAsync(opcode, new MemoryStream(data), completed);
}
private void sendAsync(Opcode opcode, Stream stream, Action completed) private void sendAsync(Opcode opcode, Stream stream, Action completed)
{ {
Action<Opcode, Stream> action = send; Action<Opcode, Stream> action = send;
AsyncCallback callback = (ar) => AsyncCallback callback = (ar) =>
{ {
try try
{ {
action.EndInvoke(ar); action.EndInvoke(ar);
if (!completed.IsNull()) if (completed != null)
completed(); completed();
} }
catch (Exception ex) catch (Exception ex)
@ -1527,7 +1506,7 @@ namespace WebSocketSharp {
/// </returns> /// </returns>
public bool Ping(string message) public bool Ping(string message)
{ {
if (message.IsNull()) if (message == null)
message = String.Empty; message = String.Empty;
return _client return _client
@ -1543,13 +1522,16 @@ namespace WebSocketSharp {
/// </param> /// </param>
public void Send(byte[] data) public void Send(byte[] data)
{ {
if (data.IsNull()) if (data == null)
{ {
onError("'data' must not be null."); onError("'data' must not be null.");
return; return;
} }
send(Opcode.BINARY, data); using (var ms = new MemoryStream(data))
{
send(Opcode.BINARY, ms);
}
} }
/// <summary> /// <summary>
@ -1560,14 +1542,16 @@ namespace WebSocketSharp {
/// </param> /// </param>
public void Send(string data) public void Send(string data)
{ {
if (data.IsNull()) if (data == null)
{ {
onError("'data' must not be null."); onError("'data' must not be null.");
return; return;
} }
var buffer = Encoding.UTF8.GetBytes(data); using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(data)))
send(Opcode.TEXT, buffer); {
send(Opcode.TEXT, ms);
}
} }
/// <summary> /// <summary>
@ -1578,13 +1562,13 @@ namespace WebSocketSharp {
/// </param> /// </param>
public void Send(FileInfo file) public void Send(FileInfo file)
{ {
if (file.IsNull()) if (file == null)
{ {
onError("'file' must not be null."); onError("'file' must not be null.");
return; return;
} }
using (FileStream fs = file.OpenRead()) using (var fs = file.OpenRead())
{ {
send(Opcode.BINARY, fs); send(Opcode.BINARY, fs);
} }
@ -1602,13 +1586,14 @@ namespace WebSocketSharp {
/// </param> /// </param>
public void SendAsync(byte[] data, Action completed) public void SendAsync(byte[] data, Action completed)
{ {
if (data.IsNull()) if (data == null)
{ {
onError("'data' must not be null."); onError("'data' must not be null.");
return; return;
} }
sendAsync(Opcode.BINARY, data, completed); var ms = new MemoryStream(data);
sendAsync(Opcode.BINARY, ms, completed);
} }
/// <summary> /// <summary>
@ -1623,14 +1608,14 @@ namespace WebSocketSharp {
/// </param> /// </param>
public void SendAsync(string data, Action completed) public void SendAsync(string data, Action completed)
{ {
if (data.IsNull()) if (data == null)
{ {
onError("'data' must not be null."); onError("'data' must not be null.");
return; return;
} }
var buffer = Encoding.UTF8.GetBytes(data); var ms = new MemoryStream(Encoding.UTF8.GetBytes(data));
sendAsync(Opcode.TEXT, buffer, completed); sendAsync(Opcode.TEXT, ms, completed);
} }
/// <summary> /// <summary>
@ -1645,7 +1630,7 @@ namespace WebSocketSharp {
/// </param> /// </param>
public void SendAsync(FileInfo file, Action completed) public void SendAsync(FileInfo file, Action completed)
{ {
if (file.IsNull()) if (file == null)
{ {
onError("'file' must not be null."); onError("'file' must not be null.");
return; return;
@ -1665,7 +1650,7 @@ namespace WebSocketSharp {
if (isOpened(true)) if (isOpened(true))
return; return;
if (cookie.IsNull()) if (cookie == null)
{ {
onError("'cookie' must not be null."); onError("'cookie' must not be null.");
return; return;

View File

@ -1,6 +1,6 @@
<Overview> <Overview>
<Assemblies> <Assemblies>
<Assembly Name="websocket-sharp" Version="1.0.2.27098"> <Assembly Name="websocket-sharp" Version="1.0.2.30668">
<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> <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> <Attributes>
<Attribute> <Attribute>