private void BeginRead(IpcPipeData pd) { // Asynchronously read a request from the client bool isConnected = pd.Pipe.IsConnected; if (isConnected) { try { pd.Pipe.BeginRead(pd.Data, 0, pd.Data.Length, OnAsyncMessage, pd); } catch (Exception) { isConnected = false; } } if (isConnected) { return; } pd.Pipe.Close(); _iipcCallback.OnAsyncDisconnect(pd.Pipe, pd.State); lock (_pipes) { bool removed = _pipes.Remove(pd.Pipe); Debug.Assert(removed); } }
private void OnAsyncMessage(IAsyncResult result) { // Async read from client completed IpcPipeData pd = (IpcPipeData)result.AsyncState; Int32 bytesRead = pd.Pipe.EndRead(result); if (bytesRead != 0) { _iipcCallback.OnAsyncMessage(pd.Pipe, pd.Data, bytesRead, pd.State); } BeginRead(pd); }
private void OnClientConnected(IAsyncResult result) { // Complete the client connection NamedPipeServerStream pipe = (NamedPipeServerStream)result.AsyncState; pipe.EndWaitForConnection(result); // Create client pipe structure IpcPipeData pd = new IpcPipeData { Pipe = pipe, State = null, Data = new Byte[ServerInBufferSize] }; // Add connection to connection list bool running; lock (_pipes) { running = _running; if (running) { _pipes.Add(pd.Pipe, pd); } } // If server is still running if (running) { // Prepare for next connection IpcServerPipeCreate(); // Alert server that client connection exists _iipcCallback.OnAsyncConnect(pipe, out pd.State); // Accept messages BeginRead(pd); } else { pipe.Close(); } }