/// <summary> /// Invoked on the background thread. /// </summary> private void CloseImpl() { ShutDown = true; _cts.Cancel(true); _streamWrapper.Close(); _writeSignal.Set(); }
private void WaitForConnection(string pipeName, PipeSecurity pipeSecurity) { NamedPipeServerStream handshakePipe = null; NamedPipeServerStream dataPipe = null; NamedPipeConnection <TRead, TWrite> connection = null; var connectionPipeName = GetNextConnectionPipeName(pipeName); try { // Send the client the name of the data pipe to use handshakePipe = PipeServerFactory.CreateAndConnectPipe(pipeName, pipeSecurity); var handshakeWrapper = new PipeStreamWrapper <string, string>(handshakePipe); handshakeWrapper.WriteObject(connectionPipeName); handshakeWrapper.WaitForPipeDrain(); handshakeWrapper.Close(); // Wait for the client to connect to the data pipe dataPipe = PipeServerFactory.CreatePipe(connectionPipeName, pipeSecurity); dataPipe.WaitForConnection(); // Add the client's connection to the list of connections connection = ConnectionFactory.CreateConnection <TRead, TWrite>(dataPipe); connection.ReceiveMessage += ClientOnReceiveMessage; connection.Disconnected += ClientOnDisconnected; connection.Error += ConnectionOnError; connection.Open(); if (System.Threading.Monitor.TryEnter(_connections, 10000)) { try { _connections.Add(connection); } finally { System.Threading.Monitor.Exit(_connections); } } ClientOnConnected(connection); } // Catch the IOException that is raised if the pipe is broken or disconnected. catch (Exception e) { Console.Error.WriteLine("Named pipe is broken or disconnected: {0}", e); Cleanup(handshakePipe); Cleanup(dataPipe); ClientOnDisconnected(connection); } }
/// <summary> /// Invoked on the background thread. /// </summary> private void CloseImpl() { //Pushing known reference to write queue will wake up the writer and free it _writeQueue.Add(_writeQueue); var sw = new SpinWait(); while (_writeQueue.Count > 0 && IsConnected && _streamWrapper.CanWrite) { sw.SpinOnce(); } _streamWrapper.Close(); }
private void WaitForConnection(string pipeName) { NamedPipeServerStream handshakePipe = null; NamedPipeServerStream dataPipe = null; NamedPipeConnection <TRead, TWrite> connection = null; var connectionPipeName = GetNextConnectionPipeName(pipeName); try { // Send the client the name of the data pipe to use handshakePipe = PipeServerFactory.CreateAndConnectPipe(pipeName); var handshakeWrapper = new PipeStreamWrapper <string, string>(handshakePipe); handshakeWrapper.WriteObject(connectionPipeName); // Wait for the client to connect to the data pipe dataPipe = PipeServerFactory.CreatePipe(connectionPipeName); dataPipe.WaitForConnection(); // Client has now connected (Which means that they found our data pipe) // We can now close the handshake pipe handshakeWrapper.Close(); // Add the client's connection to the list of connections connection = ConnectionFactory.CreateConnection <TRead, TWrite>(dataPipe); connection.ReceiveMessage += ClientOnReceiveMessage; connection.Disconnected += ClientOnDisconnected; connection.Error += ConnectionOnError; connection.Open(); lock (_connections) { _connections.Add(connection); } ClientOnConnected(connection); } // Catch the IOException that is raised if the pipe is broken or disconnected. catch (Exception e) { Console.Error.WriteLine("Named pipe is broken or disconnected: {0}", e); Cleanup(handshakePipe); Cleanup(dataPipe); ClientOnDisconnected(connection); } }
private void WaitForConnection(string pipeName, PipeSecurity pipeSecurity) { NamedPipeServerStream handshakePipe = null; NamedPipeServerStream dataPipe = null; NamedPipeConnection <TRead, TWrite> connection = null; var connectionPipeName = GetNextConnectionPipeName(pipeName); Console.WriteLine(connectionPipeName); try { // Send the client the name of the data pipe to use将要使用的数据管道的名称发送给客户机 handshakePipe = PipeServerFactory.CreateAndConnectPipe(pipeName, pipeSecurity); var handshakeWrapper = new PipeStreamWrapper <string, string>(handshakePipe); handshakeWrapper.WriteObject(connectionPipeName); handshakeWrapper.WaitForPipeDrain(); handshakeWrapper.Close(); // Wait for the client to connect to the data pipe等待客户机连接到数据管道 dataPipe = PipeServerFactory.CreatePipe(connectionPipeName, pipeSecurity); dataPipe.WaitForConnection(); // Add the client's connection to the list of connections将客户端连接添加到连接列表中 connection = ConnectionFactory.CreateConnection <TRead, TWrite>(dataPipe); connection.ReceiveMessage += ClientOnReceiveMessage; connection.Disconnected += ClientOnDisconnected; connection.Error += ConnectionOnError; connection.Open(); lock (_connections) { _connections.Add(connection); } ClientOnConnected(connection); } // Catch the IOException that is raised if the pipe is broken or disconnected. catch (Exception e) { Console.Error.WriteLine("Named pipe is broken or disconnected: {0}", e); Cleanup(handshakePipe); Cleanup(dataPipe); ClientOnDisconnected(connection); } }
private void WaitForConnection(string p_PipeName) { NamedPipeServerStream s_HandshakePipe = null; NamedPipeServerStream s_DataPipe = null; PipeConnection s_Connection = null; var s_ConnectionPipeName = GetNextConnectionPipeName(p_PipeName); try { s_HandshakePipe = PipeServerFactory.CreateAndConnectPipe(p_PipeName); var s_HandshakeWrapper = new PipeStreamWrapper(s_HandshakePipe); s_HandshakeWrapper.WriteMessage(new PipeMessage() { Module = "SM", Type = "HS", Content = s_ConnectionPipeName }); s_HandshakeWrapper.WaitForPipeDrain(); s_HandshakeWrapper.Close(); s_DataPipe = PipeServerFactory.CreatePipe(s_ConnectionPipeName); s_DataPipe.WaitForConnection(); s_Connection = ConnectionFactory.CreateConnection(s_DataPipe); s_Connection.ReceiveMessage += ClientOnReceiveMessage; s_Connection.Disconnected += ClientOnDisconnected; s_Connection.Open(); lock (m_Connections) m_Connections.Add(s_Connection); ClientOnConnected(s_Connection); } catch (Exception) { Cleanup(s_HandshakePipe); Cleanup(s_DataPipe); ClientOnDisconnected(s_Connection); } }
/// <summary> /// Invoked on the background thread. /// </summary> private void CloseImpl() { _streamWrapper.Close(); _writeSignal.Set(); }
/// <summary> /// Invoked on the background thread. /// </summary> private void CloseImpl() { _streamWrapper.Close(); _writeQueue.CompleteAdding(); }
private void WaitForConnection() { LogDebug("WaitForConnection"); NamedPipeServerStream handshakePipe = null; NamedPipeServerStream dataPipe = null; NamedPipeConnection <TRead, TWrite> connection = null; var connectionPipeName = GetNextConnectionPipeName(); try { dataPipe = CreatePipe(connectionPipeName); LogDebug("dataPipe created"); // Send the client the name of the data pipe to use LogDebug("Send the client the name of the data pipe to use"); handshakePipe = CreateAndConnectPipe(); LogDebug("handshakePipe created"); var handshakeWrapper = new PipeStreamWrapper <string, string>(handshakePipe); LogDebug("handshakeWrapper created"); handshakeWrapper.SetLogger(_logger); handshakeWrapper.WriteObject(connectionPipeName); handshakeWrapper.WaitForPipeDrain(); handshakeWrapper.Close(); LogDebug("handshakeWrapper closed"); // Wait for the client to connect to the data pipe LogDebug("Wait for the client to connect to the data pipe"); dataPipe.WaitForConnection(); LogDebug("Add the client's connection to the list of connections"); // Add the client's connection to the list of connections connection = ConnectionFactory.CreateConnection <TRead, TWrite>(dataPipe); connection.ReceiveMessage += ClientOnReceiveMessage; connection.Disconnected += ClientOnDisconnected; connection.Error += ConnectionOnError; connection.Open(); LogDebug("Open connection"); lock (_connections) { _connections.Add(connection); } ClientOnConnected(connection); } // Catch the IOException that is raised if the pipe is broken or disconnected. catch (Exception e) { LogError(e, "Named pipe is broken or disconnected"); Console.Error.WriteLine("Named pipe is broken or disconnected: {0}", e); LogDebug("Cleanup handshakePipe"); Cleanup(handshakePipe); LogDebug("Cleanup dataPipe"); Cleanup(dataPipe); ClientOnDisconnected(connection); } }
private void CloseImpl() { m_StreamWrapper.Close(); m_WriteSignal.Set(); }