/// <summary> /// Accepts client async /// </summary> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> public async Task AcceptClientAsync() { if (this.tcpListener != null) { this.clientConnectedEvent.Reset(); var client = await this.tcpListener.AcceptTcpClientAsync(); this.socket = client.Client; this.socket.NoDelay = true; // Using Buffered stream only in case of write, and Network stream in case of read. var bufferedStream = new PlatformStream().CreateBufferedStream(client.GetStream(), JSTest.Constants.StreamBufferSize); var networkStream = client.GetStream(); this.binaryReader = new BinaryReader(networkStream); this.binaryWriter = new BinaryWriter(bufferedStream); this.clientConnectedEvent.Set(); if (EqtTrace.IsInfoEnabled) { EqtTrace.Info("Using the buffer size of {0} bytes", JSTest.Constants.StreamBufferSize); EqtTrace.Info("Accepted Client request and set the flag"); } } }
public override void Close() { var cmd = (int)(StopTpm ? TcpTpmCommands.Stop : TcpTpmCommands.SessionEnd); if (CommandStream != null) { try { WriteInt(CommandStream, cmd); } catch (Exception) {} CommandStream.Flush(); CommandStream.Dispose(); CommandStream = null; } if (PlatformStream != null) { try { WriteInt(PlatformStream, cmd); } catch (Exception) { } PlatformStream.Flush(); PlatformStream.Dispose(); PlatformStream = null; } }
/// <summary> /// Connects to server async /// </summary> /// <param name="endpoint">EndPointAddress for client to connect</param> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> public async Task SetupClientAsync(IPEndPoint endpoint) { // TODO: pass cancellation token, if user cancels the operation, so we don't wait 50 secs to connect // for now added a check for validation of this.tcpclient this.clientConnectionAcceptedEvent.Reset(); EqtTrace.Info("Trying to connect to server on socket : {0} ", endpoint); this.tcpClient = new TcpClient { NoDelay = true }; this.socket = this.tcpClient.Client; Stopwatch watch = new Stopwatch(); watch.Start(); var connectionTimeout = EnvironmentHelper.GetConnectionTimeout() * 1000; do { try { EqtTrace.Verbose("SocketCommunicationManager : SetupClientAsync : Attempting to connect to the server."); await this.tcpClient.ConnectAsync(endpoint.Address, endpoint.Port); if (this.tcpClient.Connected) { // Using Buffered stream only in case of write, and Network stream in case of read. var bufferedStream = new PlatformStream().CreateBufferedStream(this.tcpClient.GetStream(), SocketConstants.BufferSize); var networkStream = this.tcpClient.GetStream(); this.binaryReader = new BinaryReader(networkStream); this.binaryWriter = new BinaryWriter(bufferedStream); if (EqtTrace.IsInfoEnabled) { EqtTrace.Info("Connected to the server successfully "); EqtTrace.Info("Using the buffer size of {0} bytes", SocketConstants.BufferSize); } this.clientConnectionAcceptedEvent.Set(); } } catch (Exception ex) { EqtTrace.Error("Connection Failed with error {0}, retrying", ex.ToString()); } }while ((this.tcpClient != null) && !this.tcpClient.Connected && watch.ElapsedMilliseconds < connectionTimeout); }
protected sealed override void Dispose(bool disposing) { if (disposing) { var cmd = (int)(StopTpm ? TcpTpmCommands.Stop : TcpTpmCommands.SessionEnd); if (CommandStream != null) { WriteInt(CommandStream, cmd); CommandStream.Flush(); CommandStream.Dispose(); } if (PlatformStream != null) { WriteInt(PlatformStream, cmd); PlatformStream.Flush(); PlatformStream.Dispose(); } } base.Dispose(disposing); }