Example #1
0
 private void AddThread(SocketInitiatorThread thread)
 {
     lock (sync_)
     {
         threads_[thread.Session.SessionID] = thread;
     }
 }
Example #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);
            }
        }
 private void RemoveThread(SessionID sessionID)
 {
     // We can come in here on the thread being removed, and on another thread too in the case
     // of dynamic session removal, so make sure we won't deadlock...
     if (Monitor.TryEnter(sync_))
     {
         SocketInitiatorThread thread = null;
         if (threads_.TryGetValue(sessionID, out thread))
         {
             try
             {
                 thread.Join();
             }
             catch { }
             threads_.Remove(sessionID);
         }
         Monitor.Exit(sync_);
     }
 }
 private void RemoveThread(SessionID sessionID)
 {
     // We can come in here on the thread being removed, and on another thread too in the case
     // of dynamic session removal, so make sure we won't deadlock...
     // yg??
     //if (Monitor.TryEnter(sync_))
     if (Monitor.TryEnter(GdaxPrototyping.Common.Core.Threading.TThreadingHelpers.MainLockable, 3000))
     {
         SocketInitiatorThread thread = null;
         if (threads_.TryGetValue(sessionID, out thread))
         {
             try
             {
                 thread.Join();
             }
             catch { }
             threads_.Remove(sessionID);
         }
         Monitor.Exit(GdaxPrototyping.Common.Core.Threading.TThreadingHelpers.MainLockable);
     }
 }
        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);
            }
            catch (SocketException e)
            {
                t.Session.Log.OnEvent("Connection failed: " + e.Message);
            }
            catch (Exception)
            {
                // It might be the logger ObjectDisposedException, so don't try to log!
            }
            finally
            {
                t.Initiator.RemoveThread(t);
                t.Initiator.SetDisconnected(t.Session.SessionID);
            }
        }
Example #6
0
        protected override void DoConnect(SessionID sessionID, Dictionary settings)
        {
            Session session = null;

            try
            {
                session = Session.LookupSession(sessionID);
                if (!session.IsSessionTime)
                    return;

                IPEndPoint socketEndPoint = GetNextSocketEndPoint(sessionID, settings);
                SetPending(sessionID);
                session.Log.OnEvent("Connecting to " + socketEndPoint.Address + " on port " + socketEndPoint.Port);

                SocketInitiatorThread t = new SocketInitiatorThread(this, session, socketEndPoint, socketSettings_);
                t.Start();
                AddThread(t);

            }
            catch (System.Exception e)
            {
                if (null != session)
                    session.Log.OnEvent(e.Message);
            }
        }
Example #7
0
 private void RemoveThread(SocketInitiatorThread thread)
 {
     lock (sync_)
     {
         thread.Join();
         threads_.Remove(thread.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);
            }
        }
 private void RemoveThread(SocketInitiatorThread thread)
 {
     RemoveThread(thread.Session.SessionID);
 }
Example #10
0
        protected override void DoConnect(SessionID sessionID, Dictionary settings)
        {
            Session session = null;

            try
            {
                session = Session.LookupSession(sessionID);
                if (!session.IsSessionTime)
                    return;

                IPEndPoint socketEndPoint = GetNextSocketEndPoint(sessionID, settings);
                SetPending(sessionID);
                session.Log.OnEvent("Connecting to " + socketEndPoint.Address + " on port " + socketEndPoint.Port);

                //Setup socket settings based on current section
                var socketSettings = socketSettings_.Clone();
                socketSettings.Configure(settings);

                // Create a Ssl-SocketInitiatorThread if a certificate is given
                SocketInitiatorThread t = new SocketInitiatorThread(this, session, socketEndPoint, socketSettings);                
                t.Start();
                AddThread(t);

            }
            catch (System.Exception e)
            {
                if (null != session)
                    session.Log.OnEvent(e.Message);
            }
        }
Example #11
0
 private void RemoveThread(SocketInitiatorThread thread)
 {
     RemoveThread(thread.Session.SessionID);
 }
Example #12
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);
            }
        }