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);
        }
 public void Unsubscribe(PipePoint pipePoint)
 {
     lock (LockObj)
     {
         this.SubscribersList.Remove(pipePoint);
     }
 }
Esempio n. 3
0
 public ShellClientSock(ShellClientBase client, PipePoint pipePoint, NetAppStub stub, PipeStream stream)
 {
     this.Client    = client;
     this.PipePoint = pipePoint;
     this.Stub      = stub;
     this.Stream    = stream;
 }
Esempio n. 4
0
    public async Task UnsubscribeAsync(PipePoint pipePoint)
    {
        lock (LockObj)
        {
            this.SubscribersList.Remove(pipePoint);
        }

        await Task.CompletedTask;
    }
Esempio n. 5
0
    public ComPortStreamWrapper(PipePoint pipePoint, SerialPort serialPort, CancellationToken cancel = default)
        : base(pipePoint, cancel)
    {
        try
        {
            this.SerialPort = serialPort;
            this.Stream     = this.SerialPort.BaseStream;

            this.StartBaseAsyncLoops();
        }
        catch
        {
            this._DisposeSafe();
            throw;
        }
    }
        public NetAppStubBase(PipePoint lower, NetAppStubOptionsBase options, CancellationToken cancel = default, bool noCheckDisconnected = false)
            : base(options, cancel)
        {
            try
            {
                LowerAttach = lower.Attach(AttachDirection.B_UpperSide, noCheckDisconnected: noCheckDisconnected);
                Lower       = lower;

                AddIndirectDisposeLink(Lower);
                AddIndirectDisposeLink(LowerAttach);
            }
            catch
            {
                this._DisposeSafe();
                throw;
            }
        }
Esempio n. 7
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. 9
0
    public async Task <PipePoint> ConnectAsync(CancellationToken cancel = default)
    {
        await Task.Yield();

        using (await Lock.LockWithAwait(cancel))
        {
            if (this.Connected)
            {
                throw new CoresException("Already connected.");
            }

            PipePoint ret = await ConnectImplAsync(cancel);

            this.Connected = true;

            return(ret);
        }
    }
Esempio n. 10
0
    public PipePointSshShellStreamWrapper(PipePoint pipePoint, ShellStream stream, CancellationToken cancel = default) : base(pipePoint, cancel)
    {
        try
        {
            this.Stream = stream;

            this.Stream.DataReceived += Stream_DataReceived;

            // この時点で届いているデータを吸い上げる
            int size = 4096;
            while (true)
            {
                Memory <byte> tmp = new byte[size];
                int           r   = this.Stream.Read(tmp.Span);

                if (r == 0)
                {
                    break;
                }

                var fifo = this.PipePoint.StreamWriter;

                ReadOnlyMemory <byte>[]? recvList = new ReadOnlyMemory <byte>[] { tmp.Slice(0, r) };

                fifo.EnqueueAllWithLock(recvList);

                fifo.CompleteWrite();
            }

            this.StartBaseAsyncLoops();
        }
        catch
        {
            this._DisposeSafe();
            throw;
        }
    }
 protected abstract Task UnsubscribeImplAsync(PipePoint pipe);
 protected override Task UnsubscribeImplAsync(PipePoint pipe)
 {
     LocalLogRouter.BufferedLogRoute.Unsubscribe(pipe);
     return(Task.CompletedTask);
 }
Esempio n. 13
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;
        }
    }
Esempio n. 14
0
 public NetAppStub(PipePoint lower, CancellationToken cancel = default, NetAppStubOptions?options = null, bool noCheckDisconnected = false)
     : base(lower, options ?? new NetAppStubOptions(), cancel, noCheckDisconnected)
 {
     this.NoCheckDisconnected = noCheckDisconnected;
 }
Esempio n. 15
0
    public LogClient(LogClientOptions options)
    {
        this.Options = options;

        this.Reader = PipePoint.NewDuplexPipeAndGetOneSide(PipePointSide.A_LowerSide, default, this.Options.ClientMaxBufferSize);
Esempio n. 16
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;
            }
        }
 protected override Task UnsubscribeImplAsync(PipePoint pipe)
 {
     return(LocalLogRouter.BufferedLogRoute.UnsubscribeAsync(pipe));
 }
Esempio n. 18
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. 19
0
 public NetWebSocketProtocolStack(PipePoint lower, PipePoint?upper, NetMiddleProtocolOptionsBase options, CancellationToken cancel = default) : base(lower, upper, options, cancel)
 {
     this.LowerStream = this.LowerAttach.GetStream();
     this.UpperStream = this.UpperAttach.GetStream();
 }