diff --git a/Channel.cs b/Channel.cs index 22ef955..7090350 100644 --- a/Channel.cs +++ b/Channel.cs @@ -38,20 +38,26 @@ public void Open(TcpClient client) while(isOpen) { - while ((position = stream.Read(buffer, 0, buffer.Length)) != 0 && isOpen) + if (clientDisconnected()) { - data = Encoding.UTF8.GetString(buffer, 0, position); - var args = new DataReceivedArgs() + Close(); + } + else + { + while ((position = stream.Read(buffer, 0, buffer.Length)) != 0 && isOpen) { - Message = data, - ConnectionId = Id, - ThisChannel = this - }; + data = Encoding.UTF8.GetString(buffer, 0, position); + var args = new DataReceivedArgs() + { + Message = data, + ConnectionId = Id, + ThisChannel = this + }; - thisServer.OnDataIn(args); - if(!isOpen) { break; } + thisServer.OnDataIn(args); + if(!isOpen) { break; } + } } - } } } @@ -62,6 +68,7 @@ public void Send(string message) stream.Write(data, 0, data.Length); } + public void Close() { Dispose(false); @@ -88,5 +95,10 @@ public void Dispose() Dispose(disposing: true); GC.SuppressFinalize(this); } + + private bool clientDisconnected() + { + return (thisClient.Client.Available == 0 && thisClient.Client.Poll(1, SelectMode.SelectRead)); + } } }