Exemplo n.º 1
0
        private void BeginReadCallback(IAsyncResult iar)
        {
            pipeClient = (NamedPipeClientStream)iar.AsyncState;

            int bytesRead = pipeClient.EndRead(iar);

            if (bytesRead > 0) {
                readStringBuffer += Encoding.UTF8.GetString(readBuffer);

            //				if (pipeClient.IsMessageComplete) {
                    textBox1.Text += readStringBuffer + Environment.NewLine;
            //					readStringBuffer = "";
            //				}
            }

            BeginRead();
        }
Exemplo n.º 2
0
        private void BeginReadCallback(IAsyncResult iar)
        {
            pipeClient = (NamedPipeClientStream)iar.AsyncState;

            int bytesRead = pipeClient.EndRead(iar);

            if (bytesRead > 0) {
                // trim message starting from \0 to prevent from reading excessive data causing exception being thrown out.
                readStringBuffer += System.Text.Encoding.UTF8.GetString(readBuffer);
                readStringBuffer += readStringBuffer.Substring(0, readStringBuffer.IndexOf('\0'));
                if (pipeClient.IsMessageComplete) {
                    ParseReceiveMessage(readStringBuffer);
                    readStringBuffer = "";
                }
            }

            BeginRead();
        }
Exemplo n.º 3
0
        private void QvxCommandWorker()
        {
            try
            {
                if (pipeName == null) return;

                object state = new object();
                object connection = null;

                using (NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", pipeName, PipeDirection.InOut))
                {
                    var buf = new byte[4];
                    var buf2 = new byte[4];
                    Int32 count = 0;
                    Int32 datalength = 0;
                    pipeClient.Connect(1000);
                    while (pipeClient.IsConnected)
                    {
                        try
                        {
                            #region Get QvxRequest
                            var iar = pipeClient.BeginRead(buf, 0, 4, null, state);
                            while (!iar.IsCompleted) Thread.Sleep(1);
                            count = pipeClient.EndRead(iar);
                            if (count != 4) throw new Exception("Invalid Count Length");
                            buf2[0] = buf[3];
                            buf2[1] = buf[2];
                            buf2[2] = buf[1];
                            buf2[3] = buf[0];
                            datalength = BitConverter.ToInt32(buf2, 0);
                            var data = new byte[datalength];
                            count = pipeClient.Read(data, 0, datalength);
                            if (count != datalength) throw new Exception("Invalid Data Length");

                            var sdata = ASCIIEncoding.ASCII.GetString(data);
                            sdata = sdata.Replace("\0", "");
                            QvxRequest request;
                            try
                            {
                                request = QvxRequest.Deserialize(sdata);

                            }
                            catch (Exception ex)
                            {
                                logger.Error(ex);
                                throw ex;
                            }
                            request.QVWindow = QVWindow;
                            request.Connection = connection;
                            #endregion

                            #region Handle QvxRequets
                            QvxReply result = null;
                            if (HandleQvxRequest != null)
                                result = HandleQvxRequest(request);

                            if (result == null)
                                result = new QvxReply() { Result = QvxResult.QVX_UNKNOWN_ERROR };
                            #endregion

                            #region Send QvxReply
                            sdata = "    " + result.Serialize() + "\0";
                            datalength = sdata.Length - 4;
                            buf2 = ASCIIEncoding.ASCII.GetBytes(sdata);
                            buf = BitConverter.GetBytes(datalength);
                            buf2[0] = buf[3];
                            buf2[1] = buf[2];
                            buf2[2] = buf[1];
                            buf2[3] = buf[0];
                            pipeClient.Write(buf2, 0, buf2.Length);
                            pipeClient.WaitForPipeDrain();
                            #endregion

                            #region Handle result States
                            if (result.Terminate)
                                close = true;
                            if (result.Connection != null)
                                connection = result.Connection;
                            if (result.SetConnectionNULL)
                                connection = null;
                            #endregion
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex);
                            Thread.Sleep(500);
                            close = true;
                        }

                        if (close)
                        {
                            close = false;
                            pipeClient.Close();
                        }

                        Thread.Sleep(5);
                    }
                }
                running = false;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }
        }