public static void SocketInitiatorThreadStart(object socketInitiatorThread)
        {
            SocketInitiatorThread t = socketInitiatorThread as SocketInitiatorThread;

            try
            {
                t.Connect();
                t.Initiator.SetConnected(t.Session.SessionID);
                t.Session.Log.OnEvent("Connection succeeded");
                t.Session.Next();
                while (t.Read())
                {
                }
                if (t.Initiator.IsStopped)
                {
                    t.Initiator.RemoveThread(t);
                }
                t.Initiator.SetDisconnected(t.Session.SessionID);
            }
            catch (IOException ex) // Can be exception when connecting, during ssl authentication or when reading
            {
                t.Session.Log.OnEvent("Connection failed: " + ex.Message);
                t.Initiator.RemoveThread(t);
                t.Initiator.SetDisconnected(t.Session.SessionID);
            }
            catch (SocketException e)
            {
                t.Session.Log.OnEvent("Connection failed: " + e.Message);
                t.Initiator.RemoveThread(t);
                t.Initiator.SetDisconnected(t.Session.SessionID);
            }
        }
Beispiel #2
0
        public static void SocketInitiatorThreadStart(object socketInitiatorThread)
        {
            SocketInitiatorThread t = socketInitiatorThread as SocketInitiatorThread;

            if (t == null)
            {
                return;
            }
            try
            {
                t.Connect();
                t.Initiator.SetConnected(t.Session.SessionID);
                t.Session.Log.OnEvent("Connection succeeded");
                t.Session.Next();
                while (t.Read())
                {
                }
                if (t.Initiator.IsStopped)
                {
                    t.Initiator.RemoveThread(t);
                }
                t.Initiator.SetDisconnected(t.Session.SessionID);
            }
            catch (IOException ex) // Can be exception when connecting, during ssl authentication or when reading
            {
                t.Session.Log.OnEvent("Connection failed: " + ex.Message);
            }
            catch (SocketException e)
            {
                t.Session.Log.OnEvent("Connection failed: " + e.Message);
            }
            catch (System.Security.Authentication.AuthenticationException ex) // some certificate problems
            {
                t.Session.Log.OnEvent("Connection failed (AuthenticationException): " + ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                // It might be the logger ObjectDisposedException, so don't try to log!
            }
            finally
            {
                t.Initiator.RemoveThread(t);
                t.Initiator.SetDisconnected(t.Session.SessionID);
            }
        }
        public static void SocketInitiatorThreadStart(object socketInitiatorThread)
        {
            SocketInitiatorThread t = socketInitiatorThread as SocketInitiatorThread;

            if (t == null)
            {
                return;
            }

            string exceptionEvent = null;

            try
            {
                try
                {
                    t.Connect();
                    t.Initiator.SetConnected(t.Session.SessionID);
                    t.Session.Log.OnEvent("Connection succeeded");
                    t.Session.Next();
                    while (t.Read())
                    {
                    }

                    if (t.Initiator.IsStopped)
                    {
                        t.Initiator.RemoveThread(t);
                    }
                    t.Initiator.SetDisconnected(t.Session.SessionID);
                }
                catch (IOException ex) // Can be exception when connecting, during ssl authentication or when reading
                {
                    exceptionEvent = $"Connection failed: {ex.Message}";
                }
                catch (SocketException e)
                {
                    exceptionEvent = $"Connection failed: {e.Message}";
                }
                catch (System.Security.Authentication.AuthenticationException ex) // some certificate problems
                {
                    exceptionEvent = $"Connection failed (AuthenticationException): {ex.Message}";
                }
                catch (Exception ex)
                {
                    exceptionEvent = $"Unexpected exception: {ex}";
                }

                if (exceptionEvent != null)
                {
                    if (t.Session.Disposed)
                    {
                        // The session is disposed, and so is its log. We cannot use it to log the event,
                        // so we resort to storing it in a local file.
                        try
                        {
                            File.AppendAllText("DisposedSessionEvents.log", $"{System.DateTime.Now:G}: {exceptionEvent}{Environment.NewLine}");
                        }
                        catch (IOException)
                        {
                            // Prevent IO exceptions from crashing the application
                        }
                    }
                    else
                    {
                        t.Session.Log.OnEvent(exceptionEvent);
                    }
                }
            }
            finally
            {
                t.Initiator.RemoveThread(t);
                t.Initiator.SetDisconnected(t.Session.SessionID);
            }
        }
Beispiel #4
0
        public static void SocketInitiatorThreadStart(object socketInitiatorThread)
        {
            SocketInitiatorThread t = socketInitiatorThread as SocketInitiatorThread;

            if (t == null)
            {
                return;
            }
            try
            {
                t.Connect();
                t.Initiator.SetConnected(t.Session.SessionID);
                t.Session.Log.OnEvent("Connection succeeded");
                t.Session.Next();
                while (t.Read())
                {
                }

                if (t.Initiator.IsStopped)
                {
                    t.Initiator.RemoveThread(t);
                }
                t.Initiator.SetDisconnected(t.Session.SessionID);
            }
            catch (IOException ex) // Can be exception when connecting, during ssl authentication or when reading
            {
                t.Session.Log.OnEvent("Connection failed: " + ex.Message);
            }
            catch (SocketException e)
            {
                t.Session.Log.OnEvent("Connection failed: " + e.Message);
            }
            catch (System.Security.Authentication.AuthenticationException ex) // some certificate problems
            {
                t.Session.Log.OnEvent("Connection failed (AuthenticationException): " + ex.Message);
            }
            catch (ObjectDisposedException ex)
            {
                // There's a chance that the logger is the thing that got disposed,
                // so let's make sure we're not going to throw a repeat exception
                // by hitting that logger again.

                try
                {
                    t.Session.Log.OnEvent(ex.ToString());
                }
                catch (ObjectDisposedException ode)
                {
                    // Can't use the logger, so write a different file.
                    System.IO.StreamWriter sw = System.IO.File.AppendText("ObjectDisposedException.log");
                    try
                    {
                        string logLine = $"{System.DateTime.Now:G}: {ode}";
                        sw.WriteLine(logLine);
                    }
                    finally
                    {
                        sw.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                t.Session.Log.OnEvent("Unexpected exception: " + ex);
            }
            finally
            {
                t.Initiator.RemoveThread(t);
                t.Initiator.SetDisconnected(t.Session.SessionID);
            }
        }