Skip to content

Commit

Permalink
"An asynchronous socket operation is already in progress using this s…
Browse files Browse the repository at this point in the history
…ocketasynceventargs instance" Exception #99
  • Loading branch information
chronoxor committed Nov 27, 2020
1 parent 2fa0e67 commit 98155da
Show file tree
Hide file tree
Showing 18 changed files with 91 additions and 123 deletions.
2 changes: 1 addition & 1 deletion .appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Specify version format
version: "5.0.1.{build}"
version: "5.0.2.{build}"

# Image to use
image: Visual Studio 2019
Expand Down
2 changes: 0 additions & 2 deletions performance/HttpTraceClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,10 @@ static void Main(string[] args)
foreach (var client in httpClients)
client.ConnectAsync();
Console.WriteLine("Done!");
/*
foreach (var client in httpClients)
while (!client.IsConnected)
Thread.Yield();
Console.WriteLine("All clients connected!");
*/

// Wait for benchmarking
Console.Write("Benchmarking...");
Expand Down
2 changes: 0 additions & 2 deletions performance/HttpsTraceClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,10 @@ static void Main(string[] args)
foreach (var client in httpsClients)
client.ConnectAsync();
Console.WriteLine("Done!");
/*
foreach (var client in httpsClients)
while (!client.IsHandshaked)
Thread.Yield();
Console.WriteLine("All clients connected!");
*/

// Wait for benchmarking
Console.Write("Benchmarking...");
Expand Down
4 changes: 1 addition & 3 deletions performance/SslEchoClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,14 @@ static void Main(string[] args)
foreach (var client in echoClients)
client.ConnectAsync();
Console.WriteLine("Done!");
/*
foreach (var client in echoClients)
while (!client.IsHandshaked)
Thread.Yield();
Console.WriteLine("All clients connected!");
*/

// Wait for benchmarking
Console.Write("Benchmarking...");
Thread.Sleep(seconds * 1000);
Thread.Sleep(seconds * 6000 * 5);
Console.WriteLine("Done!");

// Disconnect clients
Expand Down
2 changes: 0 additions & 2 deletions performance/SslMulticastClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,10 @@ static void Main(string[] args)
foreach (var client in multicastClients)
client.ConnectAsync();
Console.WriteLine("Done!");
/*
foreach (var client in multicastClients)
while (!client.IsHandshaked)
Thread.Yield();
Console.WriteLine("All clients connected!");
*/

// Wait for benchmarking
Console.Write("Benchmarking...");
Expand Down
2 changes: 0 additions & 2 deletions performance/TcpEchoClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,10 @@ static void Main(string[] args)
foreach (var client in echoClients)
client.ConnectAsync();
Console.WriteLine("Done!");
/*
foreach (var client in echoClients)
while (!client.IsConnected)
Thread.Yield();
Console.WriteLine("All clients connected!");
*/

// Wait for benchmarking
Console.Write("Benchmarking...");
Expand Down
2 changes: 0 additions & 2 deletions performance/TcpMulticastClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,10 @@ static void Main(string[] args)
foreach (var client in multicastClients)
client.ConnectAsync();
Console.WriteLine("Done!");
/*
foreach (var client in multicastClients)
while (!client.IsConnected)
Thread.Yield();
Console.WriteLine("All clients connected!");
*/

// Wait for benchmarking
Console.Write("Benchmarking...");
Expand Down
2 changes: 0 additions & 2 deletions performance/UdpEchoClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,10 @@ static void Main(string[] args)
foreach (var client in echoClients)
client.Connect();
Console.WriteLine("Done!");
/*
foreach (var client in echoClients)
while (!client.IsConnected)
Thread.Yield();
Console.WriteLine("All clients connected!");
*/

// Wait for benchmarking
Console.Write("Benchmarking...");
Expand Down
2 changes: 0 additions & 2 deletions performance/UdpMulticastClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,10 @@ static void Main(string[] args)
foreach (var client in multicastClients)
client.Connect();
Console.WriteLine("Done!");
/*
foreach (var client in multicastClients)
while (!client.IsConnected)
Thread.Yield();
Console.WriteLine("All clients connected!");
*/

// Wait for benchmarking
Console.Write("Benchmarking...");
Expand Down
2 changes: 0 additions & 2 deletions performance/WsEchoClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,10 @@ static void Main(string[] args)
foreach (var client in echoClients)
client.ConnectAsync();
Console.WriteLine("Done!");
/*
foreach (var client in echoClients)
while (!client.IsConnected)
Thread.Yield();
Console.WriteLine("All clients connected!");
*/

// Wait for benchmarking
Console.Write("Benchmarking...");
Expand Down
2 changes: 0 additions & 2 deletions performance/WsMulticastClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,10 @@ static void Main(string[] args)
foreach (var client in multicastClients)
client.ConnectAsync();
Console.WriteLine("Done!");
/*
foreach (var client in multicastClients)
while (!client.IsConnected)
Thread.Yield();
Console.WriteLine("All clients connected!");
*/

// Wait for benchmarking
Console.Write("Benchmarking...");
Expand Down
2 changes: 0 additions & 2 deletions performance/WssEchoClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,10 @@ static void Main(string[] args)
foreach (var client in echoClients)
client.ConnectAsync();
Console.WriteLine("Done!");
/*
foreach (var client in echoClients)
while (!client.IsConnected)
Thread.Yield();
Console.WriteLine("All clients connected!");
*/

// Wait for benchmarking
Console.Write("Benchmarking...");
Expand Down
2 changes: 0 additions & 2 deletions performance/WssMulticastClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,10 @@ static void Main(string[] args)
foreach (var client in multicastClients)
client.ConnectAsync();
Console.WriteLine("Done!");
/*
foreach (var client in multicastClients)
while (!client.IsConnected)
Thread.Yield();
Console.WriteLine("All clients connected!");
*/

// Wait for benchmarking
Console.Write("Benchmarking...");
Expand Down
2 changes: 1 addition & 1 deletion source/NetCoreServer/NetCoreServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<Version>5.0.1.0</Version>
<Version>5.0.2.0</Version>
<Authors>Ivan Shynkarenka</Authors>
<Copyright>Copyright (c) 2019-2020 Ivan Shynkarenka</Copyright>
<RepositoryUrl>https://github.com/chronoxor/NetCoreServer</RepositoryUrl>
Expand Down
53 changes: 25 additions & 28 deletions source/NetCoreServer/SslClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -491,22 +491,21 @@ public virtual bool SendAsync(byte[] buffer, long offset, long size)

lock (_sendLock)
{
// Detect multiple send handlers
bool sendRequired = _sendBufferMain.IsEmpty || _sendBufferFlush.IsEmpty;

// Fill the main send buffer
_sendBufferMain.Append(buffer, offset, size);

// Update statistic
BytesPending = _sendBufferMain.Size;

// Avoid multiple send handlers
if (!sendRequired)
if (_sending)
return true;
}
else
_sending = true;

// Try to send the main buffer
Task.Factory.StartNew(TrySend);
// Try to send the main buffer
Task.Factory.StartNew(TrySend);
}

return true;
}
Expand Down Expand Up @@ -617,41 +616,41 @@ private void TryReceive()
/// </summary>
private void TrySend()
{
if (_sending)
return;

if (!IsHandshaked)
return;

bool empty = false;

lock (_sendLock)
{
if (_sending)
return;

// Swap send buffers
// Is previous socket send in progress?
if (_sendBufferFlush.IsEmpty)
{
lock (_sendLock)
{
// Swap flush and main buffers
_sendBufferFlush = Interlocked.Exchange(ref _sendBufferMain, _sendBufferFlush);
_sendBufferFlushOffset = 0;
// Swap flush and main buffers
_sendBufferFlush = Interlocked.Exchange(ref _sendBufferMain, _sendBufferFlush);
_sendBufferFlushOffset = 0;

// Update statistic
BytesPending = 0;
BytesSending += _sendBufferFlush.Size;
// Update statistic
BytesPending = 0;
BytesSending += _sendBufferFlush.Size;

_sending = !_sendBufferFlush.IsEmpty;
// Check if the flush buffer is empty
if (_sendBufferFlush.IsEmpty)
{
// Need to call empty send buffer handler
empty = true;

// End sending process
_sending = false;
}
}
else
return;
}

// Check if the flush buffer is empty
if (_sendBufferFlush.IsEmpty)
// Call the empty send buffer handler
if (empty)
{
// Call the empty send buffer handler
OnEmpty();
return;
}
Expand Down Expand Up @@ -892,8 +891,6 @@ private void ProcessSend(IAsyncResult result)
OnSent(size, BytesPending + BytesSending);
}

_sending = false;

// Try to send again if the client is valid
TrySend();
}
Expand Down
53 changes: 25 additions & 28 deletions source/NetCoreServer/SslSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,22 +302,21 @@ public virtual bool SendAsync(byte[] buffer, long offset, long size)

lock (_sendLock)
{
// Detect multiple send handlers
bool sendRequired = _sendBufferMain.IsEmpty || _sendBufferFlush.IsEmpty;

// Fill the main send buffer
_sendBufferMain.Append(buffer, offset, size);

// Update statistic
BytesPending = _sendBufferMain.Size;

// Avoid multiple send handlers
if (!sendRequired)
if (_sending)
return true;
}
else
_sending = true;

// Try to send the main buffer
Task.Factory.StartNew(TrySend);
// Try to send the main buffer
Task.Factory.StartNew(TrySend);
}

return true;
}
Expand Down Expand Up @@ -428,41 +427,41 @@ private void TryReceive()
/// </summary>
private void TrySend()
{
if (_sending)
return;

if (!IsHandshaked)
return;

bool empty = false;

lock (_sendLock)
{
if (_sending)
return;

// Swap send buffers
// Is previous socket send in progress?
if (_sendBufferFlush.IsEmpty)
{
lock (_sendLock)
{
// Swap flush and main buffers
_sendBufferFlush = Interlocked.Exchange(ref _sendBufferMain, _sendBufferFlush);
_sendBufferFlushOffset = 0;
// Swap flush and main buffers
_sendBufferFlush = Interlocked.Exchange(ref _sendBufferMain, _sendBufferFlush);
_sendBufferFlushOffset = 0;

// Update statistic
BytesPending = 0;
BytesSending += _sendBufferFlush.Size;
// Update statistic
BytesPending = 0;
BytesSending += _sendBufferFlush.Size;

_sending = !_sendBufferFlush.IsEmpty;
// Check if the flush buffer is empty
if (_sendBufferFlush.IsEmpty)
{
// Need to call empty send buffer handler
empty = true;

// End sending process
_sending = false;
}
}
else
return;
}

// Check if the flush buffer is empty
if (_sendBufferFlush.IsEmpty)
// Call the empty send buffer handler
if (empty)
{
// Call the empty send buffer handler
OnEmpty();
return;
}
Expand Down Expand Up @@ -632,8 +631,6 @@ private void ProcessSend(IAsyncResult result)
OnSent(size, BytesPending + BytesSending);
}

_sending = false;

// Try to send again if the session is valid
TrySend();
}
Expand Down
Loading

0 comments on commit 98155da

Please sign in to comment.