private void RunSession(SSUSession sess, RunBatchWait sync) { if (sess.Terminated) { return; } try { lock ( sess ) { #if DEBUG Stopwatch Stopwatch1 = new Stopwatch(); Stopwatch1.Start(); #endif var running = sess.Run(); if (!running) { Logging.LogTransport("SSUHost: Terminated Session " + sess.DebugId + " removed."); RemoveSession(sess); } #if DEBUG Stopwatch1.Stop(); if (Stopwatch1.ElapsedMilliseconds > SessionCallWarningLevelMilliseconds) { Logging.LogTransport( string.Format("SSUHost Run: WARNING Session {0} used {1}ms cpu.", sess, Stopwatch1.ElapsedMilliseconds)); } #endif } } catch (ThreadAbortException taex) { AddFailedSession(sess); Logging.Log(taex); } catch (ThreadInterruptedException tiex) { AddFailedSession(sess); Logging.Log(tiex); } catch (ChecksumFailureException cfex) { AddFailedSession(sess); Logging.Log(cfex); } catch (SignatureCheckFailureException scex) { AddFailedSession(sess); Logging.Log(scex); } catch (EndOfStreamEncounteredException eosex) { AddFailedSession(sess); Logging.Log(eosex); } catch (FailedToConnectException fcex) { AddFailedSession(sess); Logging.LogTransport( string.Format("SSUHost Run: Session failed to connect: {0}", fcex.Message)); if (sess != null && sess.RemoteRouterIdentity != null) { NetDb.Inst.Statistics.FailedToConnect(sess.RemoteRouterIdentity.IdentHash); } // Reserve the execption list for serious errors // sess.RaiseException( fcex ); } catch (Exception ex) { AddFailedSession(sess); Logging.Log(ex); sess.RaiseException(ex); } finally { sync.Set(); } }
void Run() { try { CreateSocket(); while (!Terminated) { try { MySocket.BeginReceiveFrom(ReceiveBuf, 0, ReceiveBuf.Length, SocketFlags.None, ref RemoteEP, new AsyncCallback(ReceiveCallback), MySocket); while (!Terminated) { Thread.Sleep(50); SSUSession[] sessions; lock ( NeedsCpu ) { sessions = NeedsCpu.ToArray(); } if (sessions.Length > 0) { RunBatchWait batchsync = new RunBatchWait(sessions.Length); foreach (var sess in sessions) { ThreadPool.QueueUserWorkItem(cb => RunSession(sess, batchsync)); } if (!batchsync.WaitOne(5000)) { Logging.LogTransport("SSUHost: Run tasks counting error."); } } if (FailedSessions.Count > 0) { SSUSession sess; while ((sess = PopFailedSession()) != null) { Logging.LogTransport("SSUHost: Failed Session " + sess.DebugId + " removed."); if (sess.RemoteEP != null) { ReportEPProblem(sess.RemoteEP); } RemoveSession(sess); sess.Terminate(); } } } } catch (ThreadAbortException ex) { Logging.Log(ex); } catch (Exception ex) { Logging.Log(ex); } } } finally { Terminated = true; Worker = null; } }