// http://stackoverflow.com/questions/7690520/c-sharp-networking-tcpclient
 async void TcpListenerLoop()
 {
     try
     {
         //Console.WriteLine($"*TcpListener is starting on port {m_privateIP}:{m_port}.");
         Utils.Logger.Info($"*TcpListener is starting on port {m_privateIP}:{m_port}.");
         m_tcpListener = new System.Net.Sockets.TcpListener(IPAddress.Parse(m_privateIP), m_port);
         m_tcpListener.Start();
         while (true)
         {
             var tcpListenerCurrentClientTask = m_tcpListener.AcceptTcpClientAsync();
             var tcpClient = await tcpListenerCurrentClientTask;        // await Task is blocking. OK. When App is exiting gracefully, there is an Exception: no problem. Let VS swallow it.
             Console.WriteLine($"TcpListenerLoop.NextClientAccepted.");
             Utils.Logger.Info($"TcpListenerLoop.NextClientAccepted.");
             if (Utils.MainThreadIsExiting?.IsSet ?? false)
             {
                 return;                      // if App is exiting gracefully, don't start new thread
             }
             m_tcpClientQueue.Add(tcpClient); // If it is a long processing, e.g. reading the TcpClient, do it in a separate thread. If it is just added to the queue, don't start a new thread
             //(new Thread((x) => ReadTcpClientStream(x)) { IsBackground = true }).Start(tcpClient);    // read the BinaryReader() and deserialize in separate thread, so not block the TcpListener loop
         }
     }
     catch (Exception e) // Background thread can crash application. A background thread does not keep the managed execution environment running.
     {
         if (Utils.MainThreadIsExiting?.IsSet ?? false)
         {
             return; // when App is exiting gracefully, this Exception is not a problem
         }
         Utils.Logger.Error("TcpListenerLoop(): Not expected Exception. We send email by StrongAssert and rethrow exception, which will crash App. TcpListenerLoop. " + e.Message + " ,InnerException: " + ((e.InnerException != null) ? e.InnerException.Message : ""));
         StrongAssert.Fail(Severity.ThrowException, "TcpListenerLoop(): Not expected Exception. We send email by StrongAssert and rethrow exception, which will crash App. TcpListenerLoop. VirtualBroker: manual restart is needed.");
         throw;  // if we don't listen to TcpListener any more, there is no point to continue. Crash the App.
     }
 }
Exemple #2
0
        public void ScheduleTrigger(SqTrigger p_trigger, bool p_isMarketHoursValid, bool p_isMarketTradingDay, DateTime p_marketOpenTimeUtc, DateTime p_marketCloseTimeUtc)
        {
            DateTime?proposedTime = CalcNextTriggerTime(p_trigger, p_isMarketHoursValid, p_isMarketTradingDay, p_marketOpenTimeUtc, p_marketCloseTimeUtc);

            if (proposedTime != null)
            {
                bool doSetTimer = true;
                if (p_trigger.NextScheduleTimeUtc != null)
                {
                    TimeSpan timeSpan = ((DateTime)p_trigger.NextScheduleTimeUtc > (DateTime)proposedTime) ? (DateTime)p_trigger.NextScheduleTimeUtc - (DateTime)proposedTime : (DateTime)proposedTime - (DateTime)p_trigger.NextScheduleTimeUtc;
                    if (timeSpan.TotalMilliseconds < 1000.0)    // if the proposedTime is not significantly different that the scheduledTime
                    {
                        doSetTimer = false;
                    }
                }
                if (doSetTimer)
                {
                    p_trigger.NextScheduleTimeUtc = proposedTime;

                    StrongAssert.True((DateTime)p_trigger.NextScheduleTimeUtc > DateTime.UtcNow, Severity.ThrowException, "nextScheduleTimeUtc > DateTime.UtcNow");
                    p_trigger.Timer.Change((DateTime)p_trigger.NextScheduleTimeUtc - DateTime.UtcNow, TimeSpan.FromMilliseconds(-1.0));
                }
            }
            // Warn() temporarily to show it on Console
            //Console.WriteLine($"{DateTime.UtcNow.ToString("dd'T'HH':'mm':'ss")}: Task '" + p_trigger.TriggeredTask.Name + "' next time: " + ((p_trigger.NextScheduleTimeUtc != null) ? ((DateTime)p_trigger.NextScheduleTimeUtc).ToString("dd'T'HH':'mm':'ss") : "null"));
            Utils.Logger.Info("Trigger '" + String.Concat(p_trigger.SqTask !.Name, ".", p_trigger.Name) + "' next time: " + ((p_trigger.NextScheduleTimeUtc != null) ? ((DateTime)p_trigger.NextScheduleTimeUtc).ToString("dd'T'HH':'mm':'ss") : "null"));
        }