Beispiel #1
0
        /// <summary>
        /// WriteMessage - write an array of bytes and return the response from the
        /// server
        /// </summary>
        /// <param name="buffer">bytes to write</param>
        /// <param name="bytesToWrite">number of bytes to write</param>
        /// <returns>true if written successfully</returns>
        public MemoryStream WriteMessage(byte[] buffer,     // the write buffer
                                         uint bytesToWrite) // number of bytes in the write buffer
        // message responses
        {
            // buffer to get the number of bytes read/written back
            byte[]       _numReadWritten = new byte[4];
            MemoryStream responseStream  = null;

            bool success = false;

            // Write the byte buffer to the pipe
            success = NamedPipeInterop.WriteFile(_handle,
                                                 buffer,
                                                 bytesToWrite,
                                                 _numReadWritten,
                                                 0);

            if (success)
            {
                byte[] responseBuffer = new byte[_responseBufferSize];
                responseStream = new MemoryStream(_responseBufferSize);
                {
                    do
                    {
                        // Read the response from the pipe.
                        success = NamedPipeInterop.ReadFile(
                            _handle,                   // pipe handle
                            responseBuffer,            // buffer to receive reply
                            (uint)_responseBufferSize, // size of buffer
                            _numReadWritten,           // number of bytes read
                            0);                        // not overlapped

                        // failed, not just more data to come
                        if (!success && Marshal.GetLastWin32Error() != NamedPipeInterop.ERROR_MORE_DATA)
                        {
                            break;
                        }

                        // concat response to stream
                        responseStream.Write(responseBuffer,
                                             0,
                                             responseBuffer.Length);
                    } while (!success);
                }
            }
            return(responseStream);
        }
        /// <summary>
        /// WaitForMessage - have the server wait for a message
        /// </summary>
        /// <returns>a non-null MessageStream if it got a message, null if timed out or error</returns>
        public MemoryStream WaitForMessage()
        {
            bool   fullyRead = false;
            string errMsg    = "";
            int    errCode   = 0;
            // they want to talk to us, read their messages and write
            // replies
            MemoryStream receiveStream = new MemoryStream();

            byte[] buffer          = new byte[_receiveBufferSize];
            byte[] _numReadWritten = new byte[4];

            // need to read the whole message and put it in one message
            // byte buffer
            do
            {
                // Read the response from the pipe.
                if (!NamedPipeInterop.ReadFile(
                        _handle,                  // pipe handle
                        buffer,                   // buffer to receive reply
                        (uint)_receiveBufferSize, // size of buffer
                        _numReadWritten,          // number of bytes read
                        0))                       // not overlapped
                {
                    // failed, not just more data to come
                    errCode = Marshal.GetLastWin32Error();
                    if (errCode != NamedPipeInterop.ERROR_MORE_DATA)
                    {
                        break;
                    }
                    else
                    {
                        errMsg = string.Format("Could not read from pipe with error {0}", errCode);
                        Trace.WriteLine(errMsg);
                        throw new Win32Exception(errCode, errMsg);
                    }
                }
                else
                {
                    // we succeeded and no more data is coming
                    fullyRead = true;
                }
                // concat the message bytes to the stream
                receiveStream.Write(buffer, 0, buffer.Length);
            } while (!fullyRead);

            if (receiveStream.Length > 0)
            {
                // now set up response with a polite response using the same
                // Unicode string protocol
                string reply    = "Thanks for the message!";
                byte[] msgBytes = Encoding.Unicode.GetBytes(reply);

                uint len = (uint)msgBytes.Length;
                // write the response message provided
                // by the delegate
                if (!NamedPipeInterop.WriteFile(_handle,
                                                msgBytes,
                                                len,
                                                _numReadWritten,
                                                0))
                {
                    errCode = Marshal.GetLastWin32Error();
                    errMsg  = string.Format("Could not write response with error {0}", errCode);
                    Trace.WriteLine(errMsg);
                    throw new Win32Exception(errCode, errMsg);
                }

                // return the message we received
                return(receiveStream);
            }
            else // didn't receive anything
            {
                return(null);
            }
        }