Fix for issue #40

This commit is contained in:
sta 2014-04-28 17:47:32 +09:00
parent 16f5c03b95
commit 6d987ed025

View File

@ -178,6 +178,24 @@ namespace WebSocketSharp.Net
removeConnection (); removeConnection ();
} }
private void disposeTimer ()
{
if (_timer == null)
return;
var timer = _timer;
_timer = null;
try {
timer.Change (Timeout.Infinite, Timeout.Infinite);
}
catch {
}
if (timer != null)
timer.Dispose ();
}
private void init () private void init ()
{ {
_chunked = false; _chunked = false;
@ -194,10 +212,10 @@ namespace WebSocketSharp.Net
private static void onRead (IAsyncResult asyncResult) private static void onRead (IAsyncResult asyncResult)
{ {
var conn = (HttpConnection) asyncResult.AsyncState; var conn = (HttpConnection) asyncResult.AsyncState;
conn._timer.Change (Timeout.Infinite, Timeout.Infinite);
var read = -1; var read = -1;
try { try {
conn._timer.Change (Timeout.Infinite, Timeout.Infinite);
read = conn._stream.EndRead (asyncResult); read = conn._stream.EndRead (asyncResult);
conn._requestBuffer.Write (conn._buffer, 0, read); conn._requestBuffer.Write (conn._buffer, 0, read);
if (conn._requestBuffer.Length > 32768) { if (conn._requestBuffer.Length > 32768) {
@ -212,6 +230,7 @@ namespace WebSocketSharp.Net
conn.SendError (); conn.SendError ();
if (conn._socket != null) { if (conn._socket != null) {
conn.disposeTimer ();
conn.closeSocket (); conn.closeSocket ();
conn.unbind (); conn.unbind ();
} }
@ -220,6 +239,7 @@ namespace WebSocketSharp.Net
} }
if (read <= 0) { if (read <= 0) {
conn.disposeTimer ();
conn.closeSocket (); conn.closeSocket ();
conn.unbind (); conn.unbind ();
@ -263,6 +283,7 @@ namespace WebSocketSharp.Net
private static void onTimeout (object state) private static void onTimeout (object state)
{ {
var conn = (HttpConnection) state; var conn = (HttpConnection) state;
conn.disposeTimer ();
conn.closeSocket (); conn.closeSocket ();
conn.unbind (); conn.unbind ();
} }
@ -369,6 +390,7 @@ namespace WebSocketSharp.Net
var req = _context.Request; var req = _context.Request;
var res = _context.Response; var res = _context.Response;
force |= !req.KeepAlive; force |= !req.KeepAlive;
if (!force) if (!force)
force = res.Headers ["Connection"] == "close"; force = res.Headers ["Connection"] == "close";
@ -387,15 +409,17 @@ namespace WebSocketSharp.Net
var socket = _socket; var socket = _socket;
_socket = null; _socket = null;
disposeTimer ();
try { try {
socket.Shutdown (SocketShutdown.Both); socket.Shutdown (SocketShutdown.Both);
} }
catch { catch {
} }
finally {
if (socket != null) if (socket != null)
socket.Close (); socket.Close ();
}
unbind (); unbind ();
removeConnection (); removeConnection ();
@ -421,7 +445,7 @@ namespace WebSocketSharp.Net
_stream.BeginRead (_buffer, 0, _bufferSize, onRead, this); _stream.BeginRead (_buffer, 0, _bufferSize, onRead, this);
} }
catch { catch {
_timer.Change (Timeout.Infinite, Timeout.Infinite); disposeTimer ();
closeSocket (); closeSocket ();
unbind (); unbind ();
} }