public PipePoint Subscribe(int?bufferSize = null, CancellationToken cancelForNewPipe = default)
        {
            bufferSize = Math.Max(bufferSize ?? this.BufferSize, 1);

            PipePoint mySide = PipePoint.NewDuplexPipeAndGetOneSide(PipePointSide.A_LowerSide, cancelForNewPipe, bufferSize.Value);

            mySide.CounterPart._MarkNotNull();

            mySide.AddOnDisconnected(() => Unsubscribe(mySide.CounterPart));

            lock (this.MasterBuffer.LockObj)
            {
                ReadOnlySpan <ReadOnlyMemory <byte> > currentAllData = this.MasterBuffer.GetAllFast();

                lock (mySide.StreamWriter.LockObj)
                {
                    mySide.StreamWriter.NonStopWriteWithLock(currentAllData, true, FastStreamNonStopWriteMode.DiscardExistingData);
                }
            }

            lock (LockObj)
            {
                this.SubscribersList.Add(mySide.CounterPart);
            }

            return(mySide.CounterPart);
        }
Esempio n. 2
0
    protected override async Task <PipePoint> ConnectImplAsync(CancellationToken cancel = default)
    {
        await Task.CompletedTask;

        cancel.ThrowIfCancellationRequested();

        PipePoint?           pipePointMySide   = null;
        PipePoint?           pipePointUserSide = null;
        ComPortStreamWrapper?pipePointWrapper  = null;

        SerialPort port = new SerialPort(this.Settings.TargetName, this.Settings.BaudRate, this.Settings.Parity, this.Settings.DataBits, this.Settings.StopBits);

        try
        {
            port.Handshake = this.Settings.Handshake;

            if (Settings.CommTimeoutMsecs >= 1)
            {
                port.ReadTimeout = Settings.CommTimeoutMsecs;
            }

            port.Open();

            this.SerialPort = port;

            pipePointMySide   = PipePoint.NewDuplexPipeAndGetOneSide(PipePointSide.A_LowerSide);
            pipePointUserSide = pipePointMySide.CounterPart._NullCheck();
            pipePointWrapper  = new ComPortStreamWrapper(pipePointMySide, this.SerialPort);

            this.PipePointMySide   = pipePointMySide;
            this.PipePointUserSide = pipePointUserSide;
            this.PipePointWrapper  = pipePointWrapper;

            return(this.PipePointUserSide);
        }
        catch
        {
            pipePointMySide._DisposeSafe();
            pipePointUserSide._DisposeSafe();
            pipePointWrapper._DisposeSafe();

            port._DisposeSafe();
            throw;
        }
    }
    public LazyBuffer(LazyBufferEmitterBase?initialEmitter = null, LazyBufferOptions?options = null, CancellationToken cancel = default) : base(cancel)
    {
        if (options == null)
        {
            options = new LazyBufferOptions();
        }

        this.Options = options;

        this.Reader = PipePoint.NewDuplexPipeAndGetOneSide(PipePointSide.A_LowerSide, this.GrandCancel, Options.BufferSize);

        this.Writer = this.Reader.CounterPart._NullCheck();

        if (initialEmitter != null)
        {
            RegisterEmitter(initialEmitter);
        }
    }
Esempio n. 4
0
        public ExecInstance(ExecOptions options)
        {
            try
            {
                this.InputEncoding  = Console.OutputEncoding;
                this.OutputEncoding = this.ErrorEncoding = Console.InputEncoding;

                this.Options = options._NullCheck();

                ProcessStartInfo info = new ProcessStartInfo()
                {
                    FileName               = options.FileName,
                    UseShellExecute        = false,
                    RedirectStandardOutput = true,
                    RedirectStandardError  = true,
                    RedirectStandardInput  = true,
                    CreateNoWindow         = true,
                    WorkingDirectory       = options.CurrentDirectory._NullIfEmpty() !,
                };

                if (this.Options.AdditionalEnvVars != null)
                {
                    foreach (var kv in this.Options.AdditionalEnvVars)
                    {
                        if (kv.Key._IsFilled() && kv.Value._IsFilled())
                        {
                            info.EnvironmentVariables.Add(kv.Key, kv.Value);
                        }
                    }
                }

                if (options.ArgumentsList != null)
                {
                    options.ArgumentsList._DoForEach(a => info.ArgumentList.Add(a._NonNull()));
                }
                else
                {
                    info.Arguments = options.Arguments._NullIfZeroLen() !;
                }

                StartTick = Time.HighResTick64;

                Proc = Process.Start(info) !;

                Proc._FixProcessObjectHandleLeak(); // メモリリーク解消

                this.ProcessId = Proc.Id;

                // 標準入出力を接続する
                this.StandardPipePoint_MySide = PipePoint.NewDuplexPipeAndGetOneSide(PipePointSide.A_LowerSide);
                this._InputOutputPipePoint    = this.StandardPipePoint_MySide.CounterPart._NullCheck();
                this.StandardPipeWrapper      = new PipePointStreamWrapper(StandardPipePoint_MySide, Proc.StandardOutput.BaseStream, Proc.StandardInput.BaseStream);

                // 標準エラー出力を接続する
                this.ErrorPipePoint_MySide = PipePoint.NewDuplexPipeAndGetOneSide(PipePointSide.A_LowerSide);
                this._ErrorPipePoint       = this.ErrorPipePoint_MySide.CounterPart._NullCheck();
                this.ErrorPipeWrapper      = new PipePointStreamWrapper(ErrorPipePoint_MySide, Proc.StandardError.BaseStream, Stream.Null);

                if (options.Flags.Bit(ExecFlags.EasyInputOutputMode))
                {
                    this.EasyInputOutputStub   = this._InputOutputPipePoint.GetNetAppProtocolStub(noCheckDisconnected: true);
                    this.EasyInputOutputStream = this.EasyInputOutputStub.GetStream();

                    this.EasyErrorStub   = this._ErrorPipePoint.GetNetAppProtocolStub(noCheckDisconnected: true);
                    this.EasyErrorStream = this.EasyErrorStub.GetStream();

                    if (options.EasyInputStr != null && options.EasyInputStr.Length >= 1)
                    {
                        // 標準入力の注入タスク
                        this.EasyInputTask = this.EasyInputOutputStream.SendAsync(this.Options.EasyInputStr !._GetBytes(this.InputEncoding), this.GrandCancel)._LeakCheck();
                    }

                    // 標準出力の読み出しタスク
                    this.EasyOutputTask = this.EasyInputOutputStream._ReadWithMaxBufferSizeAsync(this.Options.EasyOutputMaxSize, this.GrandCancel,
                                                                                                 async(data, cancel) => await EasyPrintRealTimeRecvDataCallbackAsync(data, false, cancel))._LeakCheck();

                    // 標準エラー出力の読み出しタスク
                    this.EasyErrorTask = this.EasyErrorStream._ReadWithMaxBufferSizeAsync(this.Options.EasyOutputMaxSize, this.GrandCancel,
                                                                                          async(data, cancel) => await EasyPrintRealTimeRecvDataCallbackAsync(data, true, cancel))._LeakCheck();

                    // リアルタイムバッファ監視タスク
                    if (Options.EasyRealtimeRecvBufCallbackAsync != null)
                    {
                        this.EasyRealtimeBufferMainteTask = EasyRealtimeBufferMainteTaskAsync(this.GrandCancel)._LeakCheck();
                    }
                }

                this.StartMainLoop(MainLoopAsync);
            }
            catch
            {
                this._DisposeSafe();
                throw;
            }
        }
Esempio n. 5
0
    public LogClient(LogClientOptions options)
    {
        this.Options = options;

        this.Reader = PipePoint.NewDuplexPipeAndGetOneSide(PipePointSide.A_LowerSide, default, this.Options.ClientMaxBufferSize);
Esempio n. 6
0
        public ExecInstance(ExecOptions options)
        {
            try
            {
                this.InputEncoding  = Console.OutputEncoding;
                this.OutputEncoding = this.ErrorEncoding = Console.InputEncoding;

                this.Options = options._NullCheck();

                ProcessStartInfo info = new ProcessStartInfo()
                {
                    FileName               = options.FileName,
                    UseShellExecute        = false,
                    RedirectStandardOutput = true,
                    RedirectStandardError  = true,
                    RedirectStandardInput  = true,
                    CreateNoWindow         = true,
                    WorkingDirectory       = options.CurrentDirectory._NullIfEmpty(),
                };

                if (options.ArgumentsList != null)
                {
                    options.ArgumentsList._DoForEach(a => info.ArgumentList.Add(a._NonNull()));
                }
                else
                {
                    info.Arguments = options.Arguments._NullIfZeroLen();
                }

                StartTick = Time.HighResTick64;

                Proc = Process.Start(info);

                this.ProcessId = Proc.Id;

                // 標準入出力を接続する
                this.StandardPipePoint_MySide = PipePoint.NewDuplexPipeAndGetOneSide(PipePointSide.A_LowerSide);
                this._InputOutputPipePoint    = this.StandardPipePoint_MySide.CounterPart._NullCheck();
                this.StandardPipeWrapper      = new PipePointStreamWrapper(StandardPipePoint_MySide, Proc.StandardOutput.BaseStream, Proc.StandardInput.BaseStream);

                // 標準エラー出力を接続する
                this.ErrorPipePoint_MySide = PipePoint.NewDuplexPipeAndGetOneSide(PipePointSide.A_LowerSide);
                this._ErrorPipePoint       = this.ErrorPipePoint_MySide.CounterPart._NullCheck();
                this.ErrorPipeWrapper      = new PipePointStreamWrapper(ErrorPipePoint_MySide, Proc.StandardError.BaseStream, Stream.Null);

                if (options.Flags.Bit(ExecFlags.EasyInputOutputMode))
                {
                    this.EasyInputOutputStub   = this._InputOutputPipePoint.GetNetAppProtocolStub(noCheckDisconnected: true);
                    this.EasyInputOutputStream = this.EasyInputOutputStub.GetStream();

                    this.EasyErrorStub   = this._ErrorPipePoint.GetNetAppProtocolStub(noCheckDisconnected: true);
                    this.EasyErrorStream = this.EasyErrorStub.GetStream();

                    if (options.EasyInputStr != null)
                    {
                        // 標準入力の注入タスク
                        this.EasyInputTask = this.EasyInputOutputStream.SendAsync(this.Options.EasyInputStr !._GetBytes(this.InputEncoding), this.GrandCancel)._LeakCheck();
                    }

                    // 標準出力の読み出しタスク
                    this.EasyOutputTask = this.EasyInputOutputStream._ReadWithMaxBufferSizeAsync(this.Options.EasyOutputMaxSize, this.GrandCancel)._LeakCheck();

                    // 標準エラー出力の読み出しタスク
                    this.EasyErrorTask = this.EasyErrorStream._ReadWithMaxBufferSizeAsync(this.Options.EasyOutputMaxSize, this.GrandCancel)._LeakCheck();
                }

                this.StartMainLoop(MainLoopAsync);
            }
            catch
            {
                this._DisposeSafe();
                throw;
            }
        }
Esempio n. 7
0
    protected override async Task <PipePoint> ConnectImplAsync(CancellationToken cancel = default)
    {
        await Task.CompletedTask;

        cancel.ThrowIfCancellationRequested();

        SshClient ssh;

        switch (Settings.AuthType)
        {
        case SecureShellClientAuthType.Password:
            ssh = new SshClient(Settings.TargetName, Settings.TargetPort, Settings.Username, Settings.Password);
            break;

        default:
            throw new ArgumentException(nameof(Settings.AuthType));
        }

        ShellStream?stream            = null;
        PipePoint?  pipePointMySide   = null;
        PipePoint?  pipePointUserSide = null;
        PipePointSshShellStreamWrapper?pipePointWrapper = null;

        try
        {
            ssh.ConnectionInfo.Timeout = new TimeSpan(0, 0, 0, 0, Settings.ConnectTimeoutMsecs);

            ssh.Connect();

            stream = ssh.CreateShellStream("test", uint.MaxValue, uint.MaxValue, uint.MaxValue, uint.MaxValue, 65536);

            //while (true)
            //{
            //    byte[] data = new byte[256];

            //    int x = stream.Read(data, 0, data.Length);
            //    x._Print();
            //}

            // Stream に標準入出力を接続する
            pipePointMySide   = PipePoint.NewDuplexPipeAndGetOneSide(PipePointSide.A_LowerSide);
            pipePointUserSide = pipePointMySide.CounterPart._NullCheck();
            pipePointWrapper  = new PipePointSshShellStreamWrapper(pipePointMySide, stream);

            this.PipePointMySide   = pipePointMySide;
            this.PipePointUserSide = pipePointUserSide;
            this.PipePointWrapper  = pipePointWrapper;

            this.Ssh    = ssh;
            this.Stream = stream;

            return(this.PipePointUserSide);
        }
        catch
        {
            pipePointMySide._DisposeSafe();
            pipePointUserSide._DisposeSafe();
            pipePointWrapper._DisposeSafe();
            stream._DisposeSafe();
            ssh._DisposeSafe();

            throw;
        }
    }