コード例 #1
0
        private void WatchThread()
        {
            Stopwatch checkSamStopwatch = new Stopwatch();
            checkSamStopwatch.Start();

            for (; ; )
            {
                Thread.Sleep(1000);
                if (this.State == ManagerState.Stop) return;

                if (!checkSamStopwatch.IsRunning || checkSamStopwatch.Elapsed.TotalSeconds >= 30)
                {
                    checkSamStopwatch.Restart();

                    if (_oldSamBridgeUri != this.SamBridgeUri)
                    {
                        string i2pUri = null;

                        lock (_samServerLock)
                        {
                            if (_samListener != null)
                            {
                                _samListener.Dispose();
                            }

                            try
                            {
                                var match = _regex.Match(this.SamBridgeUri);
                                if (!match.Success) throw new Exception();

                                if (match.Groups[1].Value == "tcp")
                                {
                                    SamListener listener = null;

                                    try
                                    {
                                        string caption = "Outopos_Server";
                                        string[] options = new string[]
                                        {
                                            "inbound.nickname=" + caption,
                                            "outbound.nickname=" + caption
                                        };

                                        string optionsString = string.Join(" ", options);

                                        listener = new SamListener(match.Groups[2].Value, int.Parse(match.Groups[3].Value), optionsString);

                                        string base64Address = listener.Session.DestinationBase64;
                                        string base32Address = I2PEncoding.Base32Address.FromDestinationBase64(base64Address);

                                        Debug.WriteLine("New I2P BaseNode generated." + "\n" +
                                            "i2p:" + base64Address + "\n" +
                                            "i2p:" + base32Address);

                                        i2pUri = string.Format("i2p:{0}", base32Address);

                                        _samListener = listener;
                                    }
                                    catch (SamException ex)
                                    {
                                        Debug.WriteLine(ex);

                                        if (listener != null) listener.Dispose();
                                    }
                                }
                            }
                            catch (Exception)
                            {

                            }
                        }

                        lock (this.ThisLock)
                        {
                            if (i2pUri != _settings.I2pUri)
                            {
                                if (this.RemoveUri(_settings.I2pUri))
                                    Log.Information(string.Format("Remove Node uri: {0}", _settings.I2pUri));
                            }

                            _settings.I2pUri = i2pUri;

                            if (_settings.I2pUri != null)
                            {
                                if (this.AddUri(_settings.I2pUri))
                                    Log.Information(string.Format("Add Node uri: {0}", _settings.I2pUri));
                            }

                            _oldSamBridgeUri = this.SamBridgeUri;
                        }
                    }
                }
            }
        }
コード例 #2
0
        public override void Stop()
        {
            lock (_stateLock)
            {
                lock (this.ThisLock)
                {
                    if (this.State == ManagerState.Stop) return;
                    _state = ManagerState.Stop;
                }

                _watchThread.Join();
                _watchThread = null;

                lock (_samClientLock)
                {
                    if (_samSession != null)
                        _samSession.Dispose();

                    _samSession = null;
                }

                lock (_samServerLock)
                {
                    if (_samListener != null)
                        _samListener.Dispose();

                    _samListener = null;

                    _oldSamBridgeUri = null;
                }

                lock (this.ThisLock)
                {
                    if (_settings.I2pUri != null)
                    {
                        if (this.RemoveUri(_settings.I2pUri))
                            Log.Information(string.Format("Remove Node uri: {0}", _settings.I2pUri));
                    }
                    _settings.I2pUri = null;
                }
            }
        }