Exemple #1
0
        /// <summary>
        /// Write data to the pipe
        /// </summary>
        /// <param name="data">the data to write</param>
        /// <param name="offset">offset to start write from given data</param>
        /// <param name="dataByteSize">byte size of the data to write</param>
        public void Write(byte[] data, int offset, int dataByteSize)
        {
            if (dataByteSize > m_options.m_numOfWriteBytes)
            {
                throw new ArgumentException();
            }
            PipeWriteElem elem = new PipeWriteElem(data, offset, dataByteSize);

            lock (m_writeQueue)
            {
                if (m_writeQueue.Count > 0)
                {
                    m_writeQueue.Enqueue(elem);
                }
                else
                {
                    try
                    {
                        m_pipeHandle.BeginWrite(elem.m_data, 0, elem.m_dataSize, OnWriteComplete, this);
                    }
                    catch (System.Exception ex)
                    {
                        Console.WriteLine(ex.Message + " >" + ex.StackTrace);
                        if (IsConnectionAlive())
                        {
                            KillConnection();
                        }
                    }
                }
            }
        }
Exemple #2
0
        /// <summary>
        ///Handles when Write is completed
        /// </summary>
        /// <param name="result">AsyncResult</param>
        private void OnWriteComplete(IAsyncResult result)
        {
            IpcPipe pipeInst = (IpcPipe)result.AsyncState;

            try
            {
                pipeInst.m_pipeHandle.EndWrite(result);
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message + " >" + ex.StackTrace);
                KillConnection();
                pipeInst.m_options.m_callBackObj.OnWriteComplete(pipeInst, IpcWriteStatus.FAIL_WRITE_FAILED);
                return;
            }

            lock (pipeInst.m_writeQueue)
            {
                if (pipeInst.m_writeQueue.Count > 0)
                {
                    PipeWriteElem elem = pipeInst.m_writeQueue.Dequeue();
                    if (pipeInst.m_writeQueue.Count() > 0)
                    {
                        PipeWriteElem nextElem = pipeInst.m_writeQueue.Dequeue();

                        try
                        {
                            m_pipeHandle.BeginWrite(nextElem.m_data, nextElem.m_offset, nextElem.m_dataSize, OnWriteComplete, this);
                        }
                        catch (System.Exception ex)
                        {
                            Console.WriteLine(ex.Message + " >" + ex.StackTrace);
                            pipeInst.m_options.m_callBackObj.OnWriteComplete(pipeInst, IpcWriteStatus.SUCCESS);
                            KillConnection();
                            pipeInst.m_options.m_callBackObj.OnWriteComplete(pipeInst, IpcWriteStatus.FAIL_WRITE_FAILED);
                            return;
                        }
                    }
                    pipeInst.m_options.m_callBackObj.OnWriteComplete(pipeInst, IpcWriteStatus.SUCCESS);
                }
            }
        }