Example #1
0
        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());
            }
        }
Example #2
0
        /// <summary>
        /// serialize object in a temp file and return a JSON message for data exchange in named pipes
        /// </summary>
        public static string savePayload(IpcOperationType type, object data)
        {
            string tempFilePath = null;

            if (data != null)
            {
                tempFilePath = Path.GetTempFileName();
                File.WriteAllText(tempFilePath, JsonUtils.Serialize(data));
            }

            IpcMessageStore msg = new IpcMessageStore()
            {
                type = (int)type, tempFilePath = tempFilePath
            };

            return(JsonUtils.Serialize(msg));
        }
Example #3
0
        private void sendIpcRequest(IpcOperationType type, object data, Action <object> callback)
        {
            try
            {
                NamedPipeClientStream pipeStream = new NamedPipeClientStream(".", ipcPipeName, PipeDirection.InOut, PipeOptions.Asynchronous);

                // The connect function will indefinitely wait for the pipe to become available
                // If that is not acceptable specify a maximum waiting time (in ms)
                pipeStream.Connect(1000);

                string msg = IpcMessageStore.savePayload(type, data);

                byte[] _buffer = Encoding.UTF8.GetBytes(msg);
                pipeStream.BeginWrite(_buffer, 0, _buffer.Length, ipcAsyncCallback, new Tuple <NamedPipeClientStream, Action <object> >(pipeStream, callback));
            }
            catch (Exception ex)
            {
                MessageBox.Show("Arup Issue Tracker could not finish the operation due to the following error:\n" + ex.ToString());
            }
        }
Example #4
0
        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);
            }
        }