Пример #1
0
 /// <SUMMARY>
 /// Callback function: A new connection is waiting.
 /// </SUMMARY>
 private void ConnectionReady_Handler(IAsyncResult ar)
 {
     lock (this)
     {
         if (m_listener == null)
         {
             return;
         }
         Socket conn = m_listener.EndAccept(ar);
         if (m_connections.Count >= _maxConnections)
         {
             //Max number of connections reached.
             conn.Shutdown(SocketShutdown.Both);
             conn.Dispose();
             conn = null;
         }
         else
         {
             //Start servicing a new connection
             ConnectionState st = new ConnectionState();
             st.m_conn     = conn;
             st.m_server   = this;
             st.m_provider = (TcpServiceProvider)m_provider.Clone();
             st.m_buffer   = new byte[4];
             m_connections.Add(st);
             //Queue the rest of the job to be executed latter
             ThreadPool.QueueUserWorkItem(AcceptConnection, st);
         }
         //Resume the listening callback loop
         m_listener.BeginAccept(ConnectionReady, null);
     }
 }
Пример #2
0
 /// <SUMMARY>
 /// Callback function: A new connection is waiting.
 /// </SUMMARY>
 private void ConnectionReady_Handler(IAsyncResult ar)
 {
     lock (this)
     {
         if (_listener == null)
         {
             return;
         }
         Socket conn = _listener.EndAccept(ar);
         if (_connections.Count >= _maxConnections)
         {
             //Max number of connections reached.
             string msg = "TLDCS_ERROR: 001 Server busy";
             conn.Send(Encoding.UTF8.GetBytes(msg), 0, msg.Length, SocketFlags.None);
             conn.Shutdown(SocketShutdown.Both);
             conn.Close();
         }
         else
         {
             //Start servicing a new connection
             ConnectionState st = new ConnectionState();
             st._conn     = conn;
             st._server   = this;
             st._provider = (TcpServiceProvider)_provider.Clone();
             st._buffer   = new byte[4];
             _connections.Add(st);
             //Queue the rest of the job to be executed latter
             ThreadPool.QueueUserWorkItem(AcceptConnection, st);
         }
         //Resume the listening callback loop
         _listener.BeginAccept(ConnectionReady, null);
     }
 }
Пример #3
0
        private void Server_ClientConnected(object sender, System.Net.Sockets.TcpClient e)
        {
            try
            {
                // ConnectionReady_Handler
                var st = new ConnectionState();
                st.m_conn     = e;
                st.m_server   = this;
                st.m_provider = (TcpServiceProvider)serviceProvider.Clone();
                st.m_buffer   = new byte[4];

                //AcceptConnection_Handler
                st.m_provider.OnAcceptConnection(st);
                connectionState = st;

                LogExt.log.Debug($"SimpleTCP, Connected, Client:{e.Client.AddressFamily}");
            }
            catch (Exception ex)
            {
                LogExt.log.Error(nameof(Server_ClientConnected), ex);
                //report error in provider... Probably to the EventLog
            }
        }