From 9f8caf4de0e7a4ae86fa7b214cc34e743aa3180d Mon Sep 17 00:00:00 2001 From: mark Date: Sat, 18 Dec 2010 23:35:42 +0000 Subject: [PATCH] *Async Socket Methods --- Server/Network/Listener.cs | 115 ++++++++++++++++++++++++++----------- 1 file changed, 80 insertions(+), 35 deletions(-) diff --git a/Server/Network/Listener.cs b/Server/Network/Listener.cs index 9c4da7a75..e2c3ec4e3 100644 --- a/Server/Network/Listener.cs +++ b/Server/Network/Listener.cs @@ -36,7 +36,11 @@ namespace Server.Network private Queue m_Accepted; private object m_AcceptedSyncRoot; +#if Framework_4_0 + private SocketAsyncEventArgs m_EventArgs; +#else private AsyncCallback m_OnAccept; +#endif private static Socket[] m_EmptySockets = new Socket[0]; @@ -51,55 +55,36 @@ namespace Server.Network { m_Accepted = new Queue(); m_AcceptedSyncRoot = ((ICollection)m_Accepted).SyncRoot; - m_OnAccept = new AsyncCallback( OnAccept ); + m_Listener = Bind( ipep ); + + if ( m_Listener == null ) + return; + + DisplayListener(); + +#if Framework_4_0 + SocketAsyncEventArgs m_EventArgs = new SocketAsyncEventArgs(); + m_EventArgs.Completed += new EventHandler( Accept_Completion ); + ThreadPool.QueueUserWorkItem( new WaitCallback( Accept_Start ); +#else + m_OnAccept = new AsyncCallback( OnAccept ); + IAsyncResult res = m_Listener.BeginAccept( m_OnAccept, m_Listener ); +#endif } private Socket Bind( IPEndPoint ipep ) { - Socket s = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp ); + Socket s = new Socket( ipep.AddressFamily, SocketType.Stream, ProtocolType.Tcp ); try { s.LingerState.Enabled = false; -#if !MONO s.ExclusiveAddressUse = false; -#endif s.Bind( ipep ); s.Listen( 8 ); - if ( ipep.Address.Equals( IPAddress.Any ) ) { - NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces(); - - foreach ( NetworkInterface adapter in adapters ) { - IPInterfaceProperties properties = adapter.GetIPProperties(); - - foreach ( IPAddressInformation unicast in properties.UnicastAddresses ) { - Console.WriteLine( "Listening: {0}:{1}", unicast.Address, ipep.Port ); - } - } - - /* - try { - Console.WriteLine( "Listening: {0}:{1}", IPAddress.Loopback, ipep.Port ); - - IPHostEntry iphe = Dns.GetHostEntry( Dns.GetHostName() ); - - IPAddress[] ip = iphe.AddressList; - - for ( int i = 0; i < ip.Length; ++i ) - Console.WriteLine( "Listening: {0}:{1}", ip[i], ipep.Port ); - } - catch { } - */ - } - else { - Console.WriteLine( "Listening: {0}:{1}", ipep.Address, ipep.Port ); - } - - IAsyncResult res = s.BeginAccept( m_OnAccept, s ); - return s; } catch ( Exception e ) @@ -123,6 +108,65 @@ namespace Server.Network } } + private void DisplayListener() + { + IPEndPoint ipep = m_Listener.EndPoint as IPEndPoint; + + if ( ipep == null ) + return; + + if ( ipep.Address.Equals( IPAddress.Any ) || ipep.Address.Equals( IPAddress.IPv6Any ) ) { + NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces(); + foreach ( NetworkInterface adapter in adapters ) { + IPInterfaceProperties properties = adapter.GetIPProperties(); + foreach ( IPAddressInformation unicast in properties.UnicastAddresses ) { + if ( ipep.AddressFamily == unicast.Address.AddressFamily ) + Console.WriteLine( "Listening: {0}:{1}", unicast.Address, ipep.Port ); + } + } + /* + try { + Console.WriteLine( "Listening: {0}:{1}", IPAddress.Loopback, ipep.Port ); + IPHostEntry iphe = Dns.GetHostEntry( Dns.GetHostName() ); + IPAddress[] ip = iphe.AddressList; + for ( int i = 0; i < ip.Length; ++i ) + Console.WriteLine( "Listening: {0}:{1}", ip[i], ipep.Port ); + } + catch { } + */ + } + else { + Console.WriteLine( "Listening: {0}:{1}", ipep.Address, ipep.Port ); + } + } + +#if Framework_4_0 + private void Accept_Start() + { + while ( !m_Listener.AcceptAsync( m_EventArgs ) ) { + Accept_Process( m_EventArgs ); + } + } + + private void Accept_Completion( object sender, SocketAsyncEventArgs e ) + { + Accept_Process( e ); + + Accept_Start(); + } + + private void Accept_Process( SocketAsyncEventArgs e ) + { + if ( e.SocketError == SocketError.Success && VerifySocket( e.AcceptSocket ) ) { + Enqueue( e.AcceptSocket ); + } else { + Release( e.AcceptSocket ); + } + + e.AcceptSocket = null; + } +#else + private void OnAccept( IAsyncResult asyncResult ) { Socket listener = (Socket) asyncResult.AsyncState; @@ -151,6 +195,7 @@ namespace Server.Network } catch ( ObjectDisposedException ) { } } +#endif private bool VerifySocket( Socket socket ) { try {