[Mono-bugs] [Bug 52601][Nor] New - Asyncronous Tcp failes to complete EndInvoke (test case included)

bugzilla-daemon@bugzilla.ximian.com bugzilla-daemon@bugzilla.ximian.com
Mon, 5 Jan 2004 07:08:43 -0500 (EST)


Please do not reply to this email- if you want to comment on the bug, go to the
URL shown below and enter your comments there.

Changed by oyvind@hvamstad.net.

http://bugzilla.ximian.com/show_bug.cgi?id=52601

--- shadow/52601	2004-01-05 07:08:43.000000000 -0500
+++ shadow/52601.tmp.1636	2004-01-05 07:08:43.000000000 -0500
@@ -0,0 +1,179 @@
+Bug#: 52601
+Product: Mono/Class Libraries
+Version: unspecified
+OS: 
+OS Details: 
+Status: NEW   
+Resolution: 
+Severity: 
+Priority: Normal
+Component: System
+AssignedTo: mono-bugs@ximian.com                            
+ReportedBy: oyvind@hvamstad.net               
+QAContact: mono-bugs@ximian.com
+TargetMilestone: ---
+URL: 
+Summary: Asyncronous Tcp failes to complete EndInvoke (test case included)
+
+Description of Problem:
+Asyncronous Tcp failes to complete EndInvoke
+(My first bug report ever...)
+
+Steps to reproduce the problem:
+1. Compile the code under Additional Information (How do I attach a file?)
+2. Run it
+
+Actual Results:
+The test case hangs on EndInvoke()..
+
+Expected Results:
+In the 0.26 distribution the test case finishes the invokation and enables
+new requests.
+
+
+How often does this happen? 
+Every time
+
+Additional Information:
+Test case: (Don't know how to attach file)
+using System;
+using System.Diagnostics;
+using System.Net;
+using System.Net.Sockets;
+
+namespace TcpTezt
+{        
+
+public delegate void TcpAcceptEventHandler(Object sender, Socket socket); 
+      
+
+public class TcpServer
+{
+  public event TcpAcceptEventHandler Accept;
+  protected TcpAsyncListener listener;
+  private IPAddress address;
+  private IPEndPoint endpoint;
+  private bool stop = false;
+
+  public TcpServer(int port)
+  {
+    this.endpoint = new IPEndPoint(IPAddress.Any,port);                   
+                          
+  }
+
+  public static int Main()
+  {
+    TcpServer ts = new TcpServer(8080);
+    ts.Start();
+    Console.ReadLine();
+    return 0;
+  }
+
+  public void Start()
+  {
+    listener = new TcpAsyncListener(endpoint);
+    listener.Start();
+    listener.BeginAcceptSocket(new AsyncCallback(this.OnAcceptSocket),
+                               listener);                                 
+             
+    endpoint.Port = listener.LocalEndPoint.Port;                          
+            
+  }
+
+  protected virtual void OnAcceptSocket(IAsyncResult result)
+  {
+    try {
+      if ( stop == false ) {
+        TcpAsyncListener listener = (TcpAsyncListener)result.AsyncState;  
+                                                                          
+          
+        Socket socket = listener.EndAcceptSocket(result);                 
+                        
+        listener.BeginAcceptSocket(new AsyncCallback(this.OnAcceptSocket),
+      
+                                   listener);                             
+         
+        if (socket != null) {                                                 
+          if (null != Accept) {                                           
+                  
+            Accept(this, socket);
+          }
+          else {                                                          
+        
+            socket.Shutdown(SocketShutdown.Both);                         
+                               
+            socket.Close();
+          }
+        }
+      }
+    } catch (Exception e) {
+        Console.WriteLine(e.ToString());
+    }
+  }
+
+  public virtual void Stop()
+  {
+    stop = true;
+    this.listener.Stop();
+  }
+}
+
+public class TcpAsyncListener : TcpListener
+{
+  private delegate Socket AcceptSocketDelegate();
+  private delegate TcpClient AcceptTcpClientDelegate();                   
+  private AcceptSocketDelegate acceptSocket;
+  private AcceptTcpClientDelegate acceptTcpClient;
+  public TcpAsyncListener(IPEndPoint endpoint) : base(endpoint)           
+      
+  {
+  }
+
+  public TcpAsyncListener(IPAddress address, int port) : base(address,port)
+               
+  {
+  }                    
+
+  public IAsyncResult BeginAcceptSocket(AsyncCallback callback,Object state)
+  {
+    if (acceptSocket == null)
+      acceptSocket = new AcceptSocketDelegate(this.AcceptSocket);         
+                  
+      return acceptSocket.BeginInvoke(callback, state);
+  }
+
+  public IAsyncResult BeginAcceptTcpClient(AsyncCallback callback,Object state)
+  {
+    if (null == acceptTcpClient)                                
+      acceptTcpClient = new AcceptTcpClientDelegate(this.AcceptTcpClient);
+                             
+      return acceptTcpClient.BeginInvoke(callback,state);
+  }
+
+  // The code seems to hang right here.. But it works with 0.26           
+     
+  //
+  //
+  public Socket EndAcceptSocket(IAsyncResult result)
+  {
+    Console.WriteLine("{0}: Calling EndInvoke", "EndAcceptSocket");       
+                 
+    Socket s = acceptSocket.EndInvoke(result);                        
+    Console.WriteLine("{0}: EndInvoke finished", "EndAcceptSocket");      
+                 
+    return s;
+  }
+
+  public TcpClient EndAcceptTcpClient(IAsyncResult result)
+  {
+    return acceptTcpClient.EndInvoke(result);
+  }                
+
+  public IPEndPoint LocalEndPoint
+  {
+    get {
+      return (IPEndPoint)this.Server.LocalEndPoint;
+    }
+  }
+}
+}