private async Task ReadLoop() { while (!_cancellationTokenSource.IsCancellationRequested && _pipe.IsConnected) { byte[] buffer = ArrayPool <byte> .Shared.Rent(_pipe.InBufferSize); int position = 0; do { position += await _pipe.ReadAsync(buffer.AsMemory(position), _cancellationTokenSource.Token); } while (!_pipe.IsMessageComplete); if (position == 0) { //I don't know why, but this happens when closing the pipe. //return the buffer to the pool and skip the rest ArrayPool <byte> .Shared.Return(buffer); continue; } var dataLength = (int)BitConverter.ToUInt32(buffer, 0); var actualData = buffer.AsMemory(4, dataLength - 4); CommandReceived?.Invoke(this, actualData); ArrayPool <byte> .Shared.Return(buffer); } await _pipe.DisposeAsync(); Disconnected?.Invoke(this, EventArgs.Empty); }
static async Task ResponseAsync(NamedPipeServerStream stream, int clientId) { var jsonRpc = JsonRpc.Attach(stream, new GreeterServer()); await jsonRpc.Completion; Console.WriteLine($"客户端 #{clientId} 的已断开连接"); jsonRpc.Dispose(); await stream.DisposeAsync(); }
/// <summary> /// Dispose all resources /// </summary> public async ValueTask DisposeAsync() { cancellationToken.Dispose(); if (pipe != null) { await pipe.DisposeAsync(); } if (server != null) { await server.DisposeAsync(); } }
public async Task <bool> StartAsync() { if (State != ServerState.Stopped) { return(false); } lock (_stateLock) { if (State != ServerState.Stopped) { return(false); } State = ServerState.Starting; } if (_pipe != null) { await _pipe.DisposeAsync().ConfigureAwait(false); } try { _pipe = new NamedPipeServerStream(_configuration.PipeName, PipeDirection.InOut, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous | PipeOptions.WriteThrough); _pipe.BeginWaitForConnection(OnConnectionReceived, null); lock (_stateLock) { State = ServerState.Started; } return(true); } catch (Exception ex) { _logger.Error(ex); lock (_stateLock) { State = ServerState.Stopped; } return(false); } }
public bool SendAUMIMessage(IpcMessage_t ipMessage, ref IpcReply_t outReply) { // Implementation Copy-pasted from UndertaleModTool/MainWindow.xaml.cs // By Archie const int replySize = 132; // Create the pipe using var pPipeServer = new NamedPipeServerStream("AUMI-IPC", PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous); // Wait 1/8th of a second for AUMI to connect. // If it doesn't connect in time (which it should), just return false to avoid a deadlock. if (!pPipeServer.IsConnected) { pPipeServer.WaitForConnectionAsync(); Thread.Sleep(125); if (!pPipeServer.IsConnected) { pPipeServer.DisposeAsync(); return(false); } } try { //Send the message pPipeServer.Write(ipMessage.RawBytes()); pPipeServer.Flush(); } catch (Exception e) { // Catch any errors that might arise if the connection is broken ScriptError("Could not write data to the pipe!\nError: " + e.Message); return(false); } // Read the reply, the length of which is always a pre-set amount of bytes. byte[] bBuffer = new byte[replySize]; pPipeServer.Read(bBuffer, 0, replySize); outReply = IpcReply_t.FromBytes(bBuffer); return(true); }
private async Task Reader() { try { while (true) { await _reader.WaitForConnectionAsync(_cancellationToken.Token); using var buffer = MemoryPool <byte> .Shared.Rent(4); if (!await TryRead(buffer, 4, _cancellationToken.Token)) { continue; } var count = BitConverter.ToInt32(buffer.Memory.Span); using var dataBuffer = MemoryPool <byte> .Shared.Rent(count); if (await TryRead(buffer, count, _cancellationToken.Token)) { ParseAndRunData(Encoding.UTF8.GetString(dataBuffer.Memory.Slice(0, count).Span)); } } } catch (OperationCanceledException) { } catch (Exception e) { Log.Logger.Error(e, "Error on Read CommandLine from outer process"); } await _reader.DisposeAsync(); Handler = null; }
public ValueTask DisposeAsync() { return(_serverStream.DisposeAsync()); }
public async ValueTask DisposeAsync() { await _pipeServer.DisposeAsync(); IsDisposed = true; }