From 3480a13d784b6971246b6b6bda4488c35412514a Mon Sep 17 00:00:00 2001 From: Radek Zikmund <32671551+rzikm@users.noreply.github.com> Date: Fri, 26 Aug 2022 13:14:07 +0200 Subject: [PATCH] Update AsyncSockerServer to fix possible StackOverflow This reflects fixes from https://github.com/dotnet/runtime/pull/74644 --- .../Overview/AsyncSocketServer.cs | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/snippets/csharp/System.Net.Sockets/SocketAsyncEventArgs/Overview/AsyncSocketServer.cs b/snippets/csharp/System.Net.Sockets/SocketAsyncEventArgs/Overview/AsyncSocketServer.cs index 0b5397217ff..aa92df149b3 100644 --- a/snippets/csharp/System.Net.Sockets/SocketAsyncEventArgs/Overview/AsyncSocketServer.cs +++ b/snippets/csharp/System.Net.Sockets/SocketAsyncEventArgs/Overview/AsyncSocketServer.cs @@ -231,6 +231,8 @@ public void Start(IPEndPoint localEndPoint) listenSocket.Listen(100); // post accepts on the listening socket + SocketAsyncEventArgs acceptEventArg = new SocketAsyncEventArgs(); + acceptEventArg.Completed += new EventHandler(AcceptEventArg_Completed); StartAccept(null); //Console.WriteLine("{0} connected sockets with one outstanding receive posted to each....press any key", m_outstandingReadCount); @@ -244,22 +246,19 @@ public void Start(IPEndPoint localEndPoint) // the accept operation on the server's listening socket public void StartAccept(SocketAsyncEventArgs acceptEventArg) { - if (acceptEventArg == null) + // socket must be cleared since the context object is being reused + acceptEventArg.AcceptSocket = null; + + // loop while the method completes synchronously + bool willRaiseEvent = false; + while (!willRaiseEvent) { - acceptEventArg = new SocketAsyncEventArgs(); - acceptEventArg.Completed += new EventHandler(AcceptEventArg_Completed); - } - else - { - // socket must be cleared since the context object is being reused - acceptEventArg.AcceptSocket = null; - } - - m_maxNumberAcceptedClients.WaitOne(); - bool willRaiseEvent = listenSocket.AcceptAsync(acceptEventArg); - if (!willRaiseEvent) - { - ProcessAccept(acceptEventArg); + m_maxNumberAcceptedClients.WaitOne(); + bool willRaiseEvent = listenSocket.AcceptAsync(acceptEventArg); + if (!willRaiseEvent) + { + ProcessAccept(acceptEventArg); + } } } @@ -269,6 +268,9 @@ public void StartAccept(SocketAsyncEventArgs acceptEventArg) void AcceptEventArg_Completed(object sender, SocketAsyncEventArgs e) { ProcessAccept(e); + + // Accept the next connection request + StartAccept(e); } private void ProcessAccept(SocketAsyncEventArgs e) @@ -284,12 +286,10 @@ private void ProcessAccept(SocketAsyncEventArgs e) // As soon as the client is connected, post a receive to the connection bool willRaiseEvent = e.AcceptSocket.ReceiveAsync(readEventArgs); - if(!willRaiseEvent){ + if(!willRaiseEvent) + { ProcessReceive(readEventArgs); } - - // Accept the next connection request - StartAccept(e); } // This method is called whenever a receive or send operation is completed on a socket