예제 #1
0
        internal void Dispose()
        {
            // This function literally kills the socket, no matter if the message queue is not empty.
            try
            {
                if (!Disposed)
                {
                    ListenerClientsInfo.DecrementCount(localPort);
                    Disposed = true;
                    worker.Close();
                    if (OnConnectionClosed != null)
                    {
                        OnConnectionClosed(this);
                    }

                    string channelLog = Utils.HelperComDriverLogger.GetLoggerChannel(this);
                    try
                    {
                        ComDriverLogger.LogConnectionState(DateTime.Now, channelLog,
                                                           Unitronics.ComDriver.ConnectionStatus.ConnectionClosed.ToString() +
                                                           ", Remote IP (Client): " + RemoteIP + ", Client GUID: " + guid.ToString());
                    }
                    catch
                    {
                    }

                    AbortAll();
                }
            }
            catch (Exception ex)
            {
                string exceptionText = "InnerListenerClientEcxeption (Dispose)" + " - " + ex.Message;
                ComDriverLogger.LogExceptions(DateTime.Now, exceptionText);
            }
        }
예제 #2
0
        private void initSocket(Socket socket, int port)
        {
            try
            {
                worker    = socket;
                localPort = port;
                IPEndPoint ep = worker.RemoteEndPoint as IPEndPoint;
                RemoteIP = ep.Address.ToString();

                string channelLog = Utils.HelperComDriverLogger.GetLoggerChannel(this);
                try
                {
                    ComDriverLogger.LogConnectionState(DateTime.Now, channelLog,
                                                       Unitronics.ComDriver.ConnectionStatus.ConnectionOpened.ToString() + ", Remote IP (Client): " +
                                                       RemoteIP + ", Client GUID: " + guid.ToString());
                }
                catch
                {
                }

                ListenerClientsInfo.IncrementCount(localPort);
                waitForData();
            }
            catch (Exception ex)
            {
                if (!Disposed)
                {
                    ListenerClientsInfo.DecrementCount(localPort);
                }
                try
                {
                    worker.Close();
                }
                catch
                {
                }

                if (!Disposed)
                {
                    string channelLog = Utils.HelperComDriverLogger.GetLoggerChannel(this);
                    try
                    {
                        ComDriverLogger.LogConnectionState(DateTime.Now, channelLog,
                                                           Unitronics.ComDriver.ConnectionStatus.ConnectionClosed.ToString() +
                                                           ", Remote IP (Client): " + RemoteIP + ", Client GUID: " + guid.ToString());
                    }
                    catch
                    {
                    }
                }

                Disposed = true;

                string exceptionText = "InnerListenerClientEcxeption (initSocket)" + " - " + ex.Message;
                ComDriverLogger.LogExceptions(DateTime.Now, exceptionText);

                AbortAll();
            }
        }
예제 #3
0
        private void onDataReceived(IAsyncResult ar)
        {
            SocketState socketState = null;

            try
            {
                socketState = (SocketState)ar.AsyncState;
                int cbRead = 0;
                cbRead = worker.EndReceive(ar);

                if (cbRead > 0)
                {
                    byte[] temp = new byte[cbRead];
                    Array.Copy(socketState.ReceivedData, 0, temp, 0, cbRead);
                    OnDataReceived(temp);
                    waitForData();
                }
                else
                {
                    try
                    {
                        if (!Disposed)
                        {
                            ListenerClientsInfo.DecrementCount(localPort);
                        }
                        worker.Close();
                    }
                    catch
                    {
                    }

                    if (!Disposed)
                    {
                        string channelLog = Utils.HelperComDriverLogger.GetLoggerChannel(this);
                        try
                        {
                            ComDriverLogger.LogConnectionState(DateTime.Now, channelLog,
                                                               Unitronics.ComDriver.ConnectionStatus.ConnectionClosed.ToString() +
                                                               ", Remote IP (Client): " + RemoteIP + ", Client GUID: " + guid.ToString());
                        }
                        catch
                        {
                        }

                        Disposed = true;

                        AbortAll();
                        if (OnConnectionClosed != null)
                        {
                            OnConnectionClosed(this);
                        }
                    }
                }
            }
            catch (ObjectDisposedException)
            {
                try
                {
                    if (!Disposed)
                    {
                        ListenerClientsInfo.DecrementCount(localPort);
                    }
                    worker.Close();
                }
                catch
                {
                }

                if (!Disposed)
                {
                    string channelLog = Utils.HelperComDriverLogger.GetLoggerChannel(this);
                    try
                    {
                        ComDriverLogger.LogConnectionState(DateTime.Now, channelLog,
                                                           Unitronics.ComDriver.ConnectionStatus.ConnectionClosed.ToString() +
                                                           ", Remote IP (Client): " + RemoteIP + ", Client GUID: " + guid.ToString());
                    }
                    catch
                    {
                    }

                    Disposed = true;

                    AbortAll();
                    if (OnConnectionClosed != null)
                    {
                        OnConnectionClosed(this);
                    }
                }
            }
            catch (SocketException se)
            {
                try
                {
                    if (!Disposed)
                    {
                        ListenerClientsInfo.DecrementCount(localPort);
                    }
                    worker.Close();
                }
                catch
                {
                }

                if (!Disposed)
                {
                    string channelLog = Utils.HelperComDriverLogger.GetLoggerChannel(this);
                    try
                    {
                        ComDriverLogger.LogConnectionState(DateTime.Now, channelLog,
                                                           Unitronics.ComDriver.ConnectionStatus.ConnectionClosed.ToString() +
                                                           ", Remote IP (Client): " + RemoteIP + ", Client GUID: " + guid.ToString());
                    }
                    catch
                    {
                    }

                    Disposed = true;

                    AbortAll();

                    if (OnConnectionClosed != null)
                    {
                        OnConnectionClosed(this);
                    }
                }
            }
            catch
            {
                try
                {
                    if (!Disposed)
                    {
                        ListenerClientsInfo.DecrementCount(localPort);
                    }
                    worker.Close();
                }
                catch
                {
                }

                if (!Disposed)
                {
                    string channelLog = Utils.HelperComDriverLogger.GetLoggerChannel(this);
                    try
                    {
                        ComDriverLogger.LogConnectionState(DateTime.Now, channelLog,
                                                           Unitronics.ComDriver.ConnectionStatus.ConnectionClosed.ToString() +
                                                           ", Remote IP (Client): " + RemoteIP);
                    }
                    catch
                    {
                    }

                    Disposed = true;

                    AbortAll();

                    if (OnConnectionClosed != null)
                    {
                        OnConnectionClosed(this);
                    }
                }
            }
        }