Esempio n. 1
0
        private void Run()
        {
            try
            {
                var ntcphost = new NTCPHost();

                ntcphost.ConnectionCreated += NTCPhost_ConnectionCreated;
                SsuHost.ConnectionCreated  += SSUHost_ConnectionCreated;

                while (!Terminated)
                {
                    try
                    {
                        Thread.Sleep(1000);

                        var known = CurrentlyUnknownRouters.FindKnown();
                        foreach (var found in known)
                        {
                            foreach (var msg in found.Messages)
                            {
                                Logging.LogTransport($"TransportProvider: Destination {found.Destination.Id32Short} found. Sending data.");
                                TransportProvider.Send(found.Destination, msg);
                            }
                        }

#if DEBUG
                        ActiveConnectionLog.Do(() =>
                        {
                            var ntcprunning     = RunningTransports.Where(t => t.Protocol == "NTCP").ToArray();
                            var ssurunning      = RunningTransports.Where(t => t.Protocol == "SSU").ToArray();
                            var ntcpestablished = EstablishedTransports.SelectMany(t => t.Value.Where(t2 => t2.Key.Protocol == "NTCP")).ToArray();
                            var ssuestablished  = EstablishedTransports.SelectMany(t => t.Value.Where(t2 => t2.Key.Protocol == "SSU")).ToArray();

                            Logging.LogDebug(
                                $"TransportProvider: Established out/in " +
                                $"({ssuestablished.Count( t => t.Key.Outgoing )}/{ssurunning.Count( t => t.Outgoing )})/" +
                                $"({ssuestablished.Count( t => !t.Key.Outgoing )}/{ssurunning.Count( t => !t.Outgoing )}) SSU, " +
                                $"({ntcpestablished.Count( t => t.Key.Outgoing )}/{ntcprunning.Count( t => t.Outgoing )})/" +
                                $"({ntcpestablished.Count( t => !t.Key.Outgoing )}/{ntcprunning.Count( t => !t.Outgoing )}) NTCP.");

                            var ntcpsent = ntcprunning.Sum(t => t.BytesSent);
                            var ntcprecv = ntcprunning.Sum(t => t.BytesReceived);
                            var ssusent  = ssurunning.Sum(t => t.BytesSent);
                            var ssurecv  = ssurunning.Sum(t => t.BytesReceived);

                            Logging.LogDebug(
                                $"TransportProvider: send / recv  " +
                                $"SSU: {BytesToReadable( ssusent ),12} / {BytesToReadable( ssurecv ),12}    " +
                                $"NTCP: {BytesToReadable( ntcpsent ),12} / {BytesToReadable( ntcprecv ),12}");
                        });
#endif

                        DropOldExceptions.Do(delegate
                        {
                            lock ( AddressesWithExceptions )
                            {
                                var remove = AddressesWithExceptions.Where(eh =>
                                                                           eh.Value.Generated.DeltaToNow.ToMinutes >= ExeptionHistoryLifetimeMinutes).Select(eh => eh.Key).ToArray();
                                foreach (var one in remove)
                                {
                                    AddressesWithExceptions.Remove(one);
                                }
                            }
                        });
                    }
                    catch (ThreadAbortException ex)
                    {
                        Logging.Log(ex);
                    }
                    catch (Exception ex)
                    {
                        Logging.Log(ex);
                    }
                }
            }
            finally
            {
                Terminated = true;
            }
        }
Esempio n. 2
0
        private void Run()
        {
            try
            {
                foreach (var tp in TransportProtocols)
                {
                    tp.ConnectionCreated += TransportProtocol_ConnectionCreated;
                }

                while (!Terminated)
                {
                    try
                    {
                        Thread.Sleep(1000);

                        var known = CurrentlyUnknownRouters.FindKnown();
                        foreach (var found in known)
                        {
                            foreach (var msg in found.Messages)
                            {
                                Logging.LogTransport($"TransportProvider: Destination {found.Destination.Id32Short} found. Sending data.");
                                TransportProvider.Send(found.Destination, msg);
                            }
                        }

                        ActiveConnectionLog.Do(() =>
                        {
                            if (Logging.LogLevel > Logging.LogLevels.Information)
                            {
                                return;
                            }

                            ITransport[] rt;
                            lock ( RunningTransports )
                            {
                                rt = RunningTransports.ToArray();
                            }

                            var et = EstablishedTransports.ToArray();

                            var ntcprunning     = rt.Where(t => t.Protocol == "NTCP").ToArray();
                            var ssurunning      = rt.Where(t => t.Protocol == "SSU").ToArray();
                            var ntcpestablished = et.SelectMany(t => t.Value.Where(t2 => t2.Key.Protocol == "NTCP")).ToArray();
                            var ssuestablished  = et.SelectMany(t => t.Value.Where(t2 => t2.Key.Protocol == "SSU")).ToArray();

                            Logging.LogInformation(
                                $"TransportProvider: (Established out/Running out)/(Established in/Running in) " +
                                $"({ssuestablished.Count( t => t.Key.Outgoing )}/{ssurunning.Count( t => t.Outgoing )})/" +
                                $"({ssuestablished.Count( t => !t.Key.Outgoing )}/{ssurunning.Count( t => !t.Outgoing )}) SSU, " +
                                $"({ntcpestablished.Count( t => t.Key.Outgoing )}/{ntcprunning.Count( t => t.Outgoing )})/" +
                                $"({ntcpestablished.Count( t => !t.Key.Outgoing )}/{ntcprunning.Count( t => !t.Outgoing )}) NTCP.");

#if DEBUG
                            var ntcpsent = ntcprunning.Sum(t => t.BytesSent);
                            var ntcprecv = ntcprunning.Sum(t => t.BytesReceived);
                            var ssusent  = ssurunning.Sum(t => t.BytesSent);
                            var ssurecv  = ssurunning.Sum(t => t.BytesReceived);

                            Logging.LogDebug(
                                $"TransportProvider: send / recv  " +
                                $"SSU: {BytesToReadable( ssusent ),12} / {BytesToReadable( ssurecv ),12}    " +
                                $"NTCP: {BytesToReadable( ntcpsent ),12} / {BytesToReadable( ntcprecv ),12}");
#endif
                        });

                        DropOldExceptions.Do(delegate
                        {
                            lock ( AddressesWithExceptions )
                            {
                                var remove = AddressesWithExceptions.Where(eh =>
                                                                           eh.Value.Generated.DeltaToNow > ExeptionHistoryLifetime)
                                             .Select(eh => eh.Key)
                                             .ToArray();

                                foreach (var one in remove)
                                {
                                    AddressesWithExceptions.Remove(one);
                                }
                            }
                        });
                    }
                    catch (ThreadAbortException ex)
                    {
                        Logging.Log(ex);
                    }
                    catch (Exception ex)
                    {
                        Logging.Log(ex);
                    }
                }
            }
            finally
            {
                Terminated = true;
            }
        }