Ejemplo n.º 1
0
 private void ThreadTask()
 {
     try
     {
         while (!m_Terminate)
         {
             Socket sckt = null;
             try
             {
                 sckt = m_IncomingSocket.Accept();
             }
             catch (SocketException e)
             {
                 Trace(e);
                 sckt = null;
             }
             if (sckt != null)
             {
                 if (MaxConn == 0 ||
                     m_ConnList.Count < MaxConn)
                 {
                     var scktConn = new SocketConn(sckt, false);
                     scktConn.OnEventTrace += ScktConn_OnEventTrace;
                     scktConn.OnDisconnect += new SocketConn.DlgDisconnect(SocketConn_Disconnect);
                     scktConn.OnSendError  += new SocketConn.DlgSendError(SocketConn_SendError);
                     if (!m_ConnList.TryAdd(scktConn, scktConn))
                     {
                         Trace(EventType.Exception,
                               "Couldn't add connection to socket server connection list. Refusing connection. ConnID: {0}",
                               scktConn);
                         sckt.Close();
                     }
                     OnConnect?.Invoke(scktConn);
                     if (m_StartScktConnReadThread)
                     {
                         scktConn.OnReceiveData  += new SocketConn.DlgReceiveData(SocketConn_ReceiveData);
                         scktConn.OnReceiveError += new SocketConn.DlgReceiveError(SocketConn_ReceiveError);
                         scktConn.StartReadThread();
                     }
                     if (m_AcceptDelay)
                     {
                         m_AcceptDelayWaitHandle.WaitOne(ACCEPT_DELAY, false);
                     }
                 }
                 else
                 {
                     sckt.Close();
                     Trace(
                         EventType.Warning,
                         "Socket connection refused. Reached connection limit of {0} connections. {1}/{2}",
                         MaxConn,
                         sckt.LocalEndPoint,
                         sckt.RemoteEndPoint);
                 }
             }
         }
     }
     catch (Exception exc) { Trace(exc); }
 }
Ejemplo n.º 2
0
 private void ThreadTask()
 {
     try
     {
         while (!m_Terminate)
         {
             if (m_SocketConn == null || !m_SocketConn.Connected)
             {
                 var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                 try
                 {
                     socket.Connect(m_RemoteAddress, m_RemotePort);
                     m_SocketConn = new SocketConn(socket, false);
                     m_SocketConn.OnEventTrace += ScktConn_OnEventTrace;
                     m_SocketConn.OnDisconnect += new SocketConn.DlgDisconnect(SocketConn_Disconnect);
                     m_SocketConn.OnSendError  += new SocketConn.DlgSendError(SocketConn_SendError);
                     if (m_StartScktConnReadThread)
                     {
                         m_SocketConn.OnReceiveData  += new SocketConn.DlgReceiveData(SocketConn_ReceiveData);
                         m_SocketConn.OnReceiveError += new SocketConn.DlgReceiveError(SocketConn_ReceiveError);
                         m_SocketConn.StartReadThread();
                     }
                     OnConnect?.Invoke(m_SocketConn);
                 }
                 catch (Exception e)
                 {
                     Trace(EventType.Error,
                           "Exception connecting the socket. Remote endpoint: {0}",
                           RemoteEndPoint);
                     OnConnectError?.Invoke(e);
                     TryToReconnect();
                 }
             }
             m_ReconnectSignal.WaitOne(RETRY_CONNECT);
         }
     }
     catch (Exception exc)
     {
         Trace(exc);
     }
 }