Skip to content

Commit 7b73bb8

Browse files
committed
"An asynchronous socket operation is already in progress using this socketasynceventargs instance" Exception #41
1 parent e61e019 commit 7b73bb8

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

.appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Specify version format
2-
version: "3.0.5.{build}"
2+
version: "3.0.6.{build}"
33

44
# Image to use
55
image: Visual Studio 2019

source/NetCoreServer/NetCoreServer.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.1</TargetFramework>
5-
<Version>3.0.5</Version>
5+
<Version>3.0.6</Version>
66
<Authors>Ivan Shynkarenka</Authors>
77
<Copyright>Copyright (c) 2019-2020 Ivan Shynkarenka</Copyright>
88
<RepositoryUrl>https://github.com/chronoxor/NetCoreServer</RepositoryUrl>

source/NetCoreServer/TcpClient.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,12 @@ private void ProcessReceive(SocketAsyncEventArgs e)
718718
{
719719
// If zero is returned from a read operation, the remote end has closed the connection
720720
if (size > 0)
721-
TryReceive();
721+
{
722+
if (Thread.CurrentThread.ManagedThreadId == _sendThreadId)
723+
ThreadPool.QueueUserWorkItem(_ => TryReceive());
724+
else
725+
TryReceive();
726+
}
722727
else
723728
DisconnectAsync();
724729
}
@@ -768,7 +773,12 @@ private void ProcessSend(SocketAsyncEventArgs e)
768773

769774
// Try to send again if the client is valid
770775
if (e.SocketError == SocketError.Success)
771-
TrySend();
776+
{
777+
if (Thread.CurrentThread.ManagedThreadId == _sendThreadId)
778+
ThreadPool.QueueUserWorkItem(_ => TrySend());
779+
else
780+
TrySend();
781+
}
772782
else
773783
{
774784
SendError(e.SocketError);

source/NetCoreServer/TcpSession.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,12 @@ private void ProcessReceive(SocketAsyncEventArgs e)
551551
{
552552
// If zero is returned from a read operation, the remote end has closed the connection
553553
if (size > 0)
554-
TryReceive();
554+
{
555+
if (Thread.CurrentThread.ManagedThreadId == _sendThreadId)
556+
ThreadPool.QueueUserWorkItem(_ => TryReceive());
557+
else
558+
TryReceive();
559+
}
555560
else
556561
Disconnect();
557562
}
@@ -602,7 +607,12 @@ private void ProcessSend(SocketAsyncEventArgs e)
602607

603608
// Try to send again if the session is valid
604609
if (e.SocketError == SocketError.Success)
605-
TrySend();
610+
{
611+
if (Thread.CurrentThread.ManagedThreadId == _sendThreadId)
612+
ThreadPool.QueueUserWorkItem(_ => TrySend());
613+
else
614+
TrySend();
615+
}
606616
else
607617
{
608618
SendError(e.SocketError);

0 commit comments

Comments
 (0)