Skip to content

Commit

Permalink
Added check in channel loop to see if client had disconnected
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Proud committed Jul 15, 2023
1 parent cbaa346 commit a9d391f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
32 changes: 22 additions & 10 deletions Channel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
}

}
}
}
Expand All @@ -62,6 +68,7 @@ public void Send(string message)
stream.Write(data, 0, data.Length);
}


public void Close()
{
Dispose(false);
Expand All @@ -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));
}
}
}
2 changes: 1 addition & 1 deletion EasyTCP.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

</Project>

0 comments on commit a9d391f

Please sign in to comment.