private async void HandleSocketAsync(Socket socket, CancellationToken cancellationToken) { try { var adapter = new PipeAdapter(socket); adapter.Settings.PipeSequenceChunkifier = new PipeSequenceChunkifier(Encoding.UTF8.GetBytes("\n")); adapter.Settings.ExceptionHandler = e => Logger.Error(e); adapter.Received += AdapterOnReceived; if (socket.Connected) { await adapter.ListenAsync(); } } catch (OperationCanceledException) { } catch (Exception e) { Logger.Error(e); } finally { if (socket.Connected) { socket.Disconnect(true); } Logger.Info($"Client disconnected [{socket.RemoteEndPoint}]."); } }
public void PipeAdapterThrowsOnInvalidWriteTimeout(int timeout) { // Arrange var streamResourceMock = new Mock <IStreamResource <TcpClient> >(); streamResourceMock.Setup(x => x.GetStream()).Returns(new MemoryStream()); var pipeAdapter = new PipeAdapter <TcpClient>(streamResourceMock.Object); // Act/Assert Assert.Throws <ArgumentOutOfRangeException>(() => pipeAdapter.WriteTimeout = timeout); }
public void SetsWriteTimeoutSuccessfully(int timeout) { // Arrange var streamResourceMock = new Mock <IStreamResource <TcpClient> >(); streamResourceMock.Setup(x => x.GetStream()).Returns(new MemoryStream()); var pipeAdapter = new PipeAdapter <TcpClient>(streamResourceMock.Object); // Act pipeAdapter.WriteTimeout = timeout; // Assert Assert.Equal(timeout, pipeAdapter.WriteTimeout); }
public void DisposesStreamResource() { // Arrange var streamResourceMock = new Mock <IStreamResource <TcpClient> >(); streamResourceMock.Setup(x => x.GetStream()).Returns(new MemoryStream()); var pipeAdapter = new PipeAdapter <TcpClient>(streamResourceMock.Object); // Act pipeAdapter.Dispose(); // Assert streamResourceMock.Verify(x => x.Dispose(), Times.Once()); }
private async Task ProcessCommandAsync(RemoteCommand command, PipeAdapter adapter) { try { await CommandHandler.HandleAsync(command, adapter.Socket); await Task.Delay(100); await CommandHandler.ExecuteCommandAsync(new DisplayMessageCommand($"Command \"{command.CommandName}\" executed."), adapter.Socket); } catch (Exception e) { Logger.Error(e); } }
public void ExposesUnderlyingResource() { // Arrange var tcpClient = new TcpClient(); var streamResourceMock = new Mock <IStreamResource <TcpClient> >(); streamResourceMock.Setup(x => x.GetStream()).Returns(new MemoryStream()); streamResourceMock.Setup(x => x.Resource).Returns(tcpClient); var pipeAdapter = new PipeAdapter <TcpClient>(streamResourceMock.Object); // Act var actual = pipeAdapter.Resource; // Assert Assert.Equal(tcpClient, actual); }
public async Task ReadsOnceIfEnoughData() { // Arrange var streamMock = new Mock <Stream> { CallBase = false }; streamMock.Setup(x => x.ReadAsync(It.IsAny <Memory <byte> >(), It.IsAny <CancellationToken>())).ReturnsAsync(10); var streamResourceMock = new Mock <IStreamResource <TcpClient> >(); streamResourceMock.Setup(x => x.GetStream()).Returns(streamMock.Object); var pipeAdapter = new PipeAdapter <TcpClient>(streamResourceMock.Object); // Act var actual = await pipeAdapter.ReadAsync(10, CancellationToken.None); // Assert Assert.Equal(10, actual.Length); }
public static async Task ExecuteCommandAsync(RemoteCommand remoteCommand, Socket socket) { var encryptionKey = ConfigurationManager.AppSettings["EncryptionPhrase"]; Logger.Info($"Sending command [{remoteCommand.CommandName}] to [{socket.RemoteEndPoint}]."); var message = remoteCommand.ToBytes(encryptionKey); Logger.Debug($"Length of command is [{message.Length}]."); // var sent = await adapter.Socket.SendAsync(new ArraySegment<byte>(message), SocketFlags.None); // var sent = await adapter.Socket.SendToAsync(new ArraySegment<byte>(message), SocketFlags.None, adapter.Socket.RemoteEndPoint); var settings = new PipeAdapterSettings(); settings.PipeSequenceChunkifier = new PipeSequenceChunkifier( Encoding.UTF8.GetBytes("\n") ); var adapter = new PipeAdapter(socket, settings); var sent = await adapter.SendAsync(message); Logger.Debug($"Sent [{sent}] bytes to [{socket.RemoteEndPoint}]."); }
public async Task TimesOutOnWriteAsync() { // Arrange var streamMock = new Mock <Stream>() { CallBase = false }; streamMock .Setup(x => x.WriteAsync(It.IsAny <ReadOnlyMemory <byte> >(), It.IsAny <CancellationToken>())) .Returns(new ValueTask(Task.Delay(1000))); var streamResourceMock = new Mock <IStreamResource <TcpClient> >(); streamResourceMock.Setup(x => x.GetStream()).Returns(streamMock.Object); var pipeAdapter = new PipeAdapter <TcpClient>(streamResourceMock.Object) { WriteTimeout = 500 }; // Act/Assert await Assert.ThrowsAsync <TimeoutException>(() => pipeAdapter.WriteAsync(new byte[] { 1, 2, 3 }, CancellationToken.None)); }