private void WaitForConnectionCallBack(IAsyncResult iar) { try { // Get the pipe NamedPipeServerStream pipeServer = (NamedPipeServerStream)iar.AsyncState; // End waiting for the connection pipeServer.EndWaitForConnection(iar); byte[] buffer = new byte[1024]; // Read the incoming message pipeServer.Read(buffer, 0, 1024); // Convert byte buffer to string string jsonRequestMsg = Encoding.UTF8.GetString(buffer, 0, buffer.Length); IpcOperationType type = IpcMessageStore.getIpcOperationType(jsonRequestMsg); string jsonResponseMsg = null; if (type == IpcOperationType.AddIssueRequest) { AddIssueResponse response = handleAddIssueRequest(); jsonResponseMsg = IpcMessageStore.savePayload(IpcOperationType.AddIssueResponse, response); } else if (type == IpcOperationType.OpenViewpointRequest) { VisualizationInfo visInfo = IpcMessageStore.getPayload <VisualizationInfo>(jsonRequestMsg); jsonResponseMsg = "{}"; doOpen3DView(visInfo); } else if (type == IpcOperationType.SelectElementsRequest) { List <Component> components = IpcMessageStore.getPayload <List <Component> >(jsonRequestMsg); jsonResponseMsg = "{}"; selectElements(components, true); } // Send response byte[] _buffer = Encoding.UTF8.GetBytes(jsonResponseMsg); pipeServer.Write(_buffer, 0, _buffer.Length); pipeServer.Flush(); // Kill original sever and create new wait server pipeServer.Close(); pipeServer = null; pipeServer = new NamedPipeServerStream(pipeName, PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous); // Recursively wait for the connection again and again.... pipeServer.BeginWaitForConnection(new AsyncCallback(WaitForConnectionCallBack), pipeServer); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
private void ipcAsyncCallback(IAsyncResult iar) { try { // Get the pipe NamedPipeClientStream pipeStream = ((Tuple <NamedPipeClientStream, Action <object> >)iar.AsyncState).Item1; // End the write pipeStream.EndWrite(iar); byte[] buffer = new byte[1024]; pipeStream.Read(buffer, 0, 1024); string jsonMsg = Encoding.UTF8.GetString(buffer, 0, buffer.Length); IpcOperationType type = IpcMessageStore.getIpcOperationType(jsonMsg); if (type == IpcOperationType.AddIssueResponse) { AddIssueResponse response = IpcMessageStore.getPayload <AddIssueResponse>(jsonMsg); // return payload object in callback in the same UI thread this.Dispatcher.Invoke(() => ((Tuple <NamedPipeClientStream, Action <object> >)iar.AsyncState).Item2(response)); } else if (type == IpcOperationType.Invalid) { // extends other types of IPC responses here... } pipeStream.Flush(); pipeStream.Close(); pipeStream.Dispose(); } catch (Exception oEX) { MessageBox.Show(oEX.Message); } }