コード例 #1
0
        private void Remove(ITransport instance)
        {
            if (instance == null)
            {
                return;
            }

            lock ( EstablishedTransports )
            {
                var matches = EstablishedTransports.Where(t => object.ReferenceEquals(t.Value, instance)).Select(t => t.Key).ToArray();
                foreach (var key in matches)
                {
                    EstablishedTransports.Remove(key);
                }
            }
            lock ( RunningTransports )
            {
                var matches = RunningTransports.Where(t => object.ReferenceEquals(t.Value, instance)).Select(t => t.Key).ToArray();
                foreach (var key in matches)
                {
                    RunningTransports.Remove(key);
                }
            }

            instance.Terminate();
        }
コード例 #2
0
        private void Remove(ITransport instance)
        {
            if (instance == null)
            {
                return;
            }

            foreach (var one in EstablishedTransports.ToArray())
            {
                one.Value.TryRemove(instance, out _);
                if (one.Value.IsEmpty)
                {
                    EstablishedTransports.TryRemove(one.Key, out _);
                }
            }

            lock ( RunningTransports )
            {
                RunningTransports.Remove(instance);
            }

            instance.Terminate();
        }
コード例 #3
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;
            }
        }
コード例 #4
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;
            }
        }