/// <summary>
        /// The callback method for when client is connected to the pipe
        /// </summary>
        /// <param name="cb">The asynchronous result of the connect operation</param>
        public void ClientConnected(IAsyncResult cb)
        {
            System.Diagnostics.Trace.WriteLine("Client connected to the pipe server.", "OutboundTestHelper");

            OutboundTestHelper testHelper = (OutboundTestHelper)cb.AsyncState;

            try
            {
                testHelper.pipeServer.EndWaitForConnection(cb);

                System.Diagnostics.Trace.WriteLine("Starting to read from the pipe.", "OutboundTestHelper");
                //We read from the pipe
                int  byteCountRead = 0;
                bool eofReached    = false;
                while (!eofReached)
                {
                    byteCountRead = testHelper.pipeServer.Read(outBuffer, 0, outBuffer.Length);

                    if (byteCountRead > 2)
                    {
                        eofReached = (outBuffer[byteCountRead - 1] == 0x0 &&
                                      outBuffer[byteCountRead - 2] != 0x0 &&
                                      outBuffer[byteCountRead - 3] != 0x0);
                    }
                    else if (byteCountRead > 1 && !eofReached)
                    {
                        eofReached = (outBuffer[byteCountRead - 1] == 0x0 &&
                                      outBuffer[byteCountRead - 2] == 0x0);
                    }
                    else if (byteCountRead == 1)
                    {
                        eofReached = outBuffer[byteCountRead - 1] == 0x0;
                    }

                    memStream.Write(outBuffer, 0,
                                    eofReached ? byteCountRead - 1 : byteCountRead);
                }

                System.Diagnostics.Trace.WriteLine("Finished reading from the pipe.", "OutboundTestHelper");
            }
            finally
            {
                //We signal the event
                testHelper.syncEvent.Set();
            }
        }
Beispiel #2
0
        /// <summary>
        /// The callback method for when client is connected to the pipe
        /// </summary>
        /// <param name="cb">The asynchronous result of the connect operation</param>
        public void ClientConnected(IAsyncResult cb)
        {
            System.Diagnostics.Trace.WriteLine("Client connected to the pipe server.", "OutboundTestHelper");

            OutboundTestHelper testHelper = (OutboundTestHelper)cb.AsyncState;

            try
            {
                testHelper.pipeServer.EndWaitForConnection(cb);

                System.Diagnostics.Trace.WriteLine("Starting to read from the pipe.", "OutboundTestHelper");
                //We read from the pipe
                int  byteCountRead = 0, totalBytesCount = 0;
                bool eofReached = false;

                while (!eofReached)
                {
                    byteCountRead    = testHelper.pipeServer.Read(outBuffer, 0, outBuffer.Length);
                    totalBytesCount += byteCountRead;

                    eofReached = IsEndOfMessage(outBuffer, byteCountRead);

                    if (eofReached)
                    {
                        byteCountRead -= EndOfMessage.Length;
                    }

                    memStream.Write(outBuffer, 0, byteCountRead);
                }

                System.Diagnostics.Trace.WriteLine(
                    string.Format(
                        "Finished reading from the pipe. Total bytes read: {0}",
                        totalBytesCount),
                    "OutboundTestHelper");
            }
            finally
            {
                //We signal the event
                testHelper.syncEvent.Set();
            }
        }
Beispiel #3
0
        /// <summary>
        /// The callback method for when clien is connected to the pipe in two-way communication scenarios
        /// </summary>
        /// <param name="cb">The asynchronous result of the connect operation</param>
        public void ClientConnectedSyncronous(IAsyncResult cb, Action <OutboundTestHelper> responseHandler)
        {
            OutboundTestHelper testHelper = (OutboundTestHelper)cb.AsyncState;

            try
            {
                testHelper.pipeServer.EndWaitForConnection(cb);

                if (!testHelper.pipeServer.IsConnected)
                {
                    //The method was invoked in the the case of closing the pipe.
                    //Exiting with no room for discussion
                    return;
                }

                //We read from the pipe
                int  byteCountRead = 0, totalBytesCount = 0;
                bool eofReached = false;

                while (!eofReached)
                {
                    byteCountRead = testHelper.pipeServer.Read(outBuffer, 0, outBuffer.Length);

                    totalBytesCount += byteCountRead;

                    eofReached = byteCountRead < outBuffer.Length ||
                                 byteCountRead == 0;

                    memStream.Write(outBuffer, 0, byteCountRead);
                }

                // Invoking the response handler
                responseHandler(testHelper);
            }
            finally
            {
                //We signal the event
                testHelper.syncEvent.Set();
            }
        }
        /// <summary>
        /// The callback method for when clien is connected to the pipe in two-way communication scenarios
        /// </summary>
        /// <param name="cb">The asynchronous result of the connect operation</param>
        public void ClientConnectedSyncronous(IAsyncResult cb)
        {
            OutboundTestHelper testHelper = (OutboundTestHelper)cb.AsyncState;

            try
            {
                testHelper.pipeServer.EndWaitForConnection(cb);

                if (!testHelper.pipeServer.IsConnected)
                {
                    //The method was invoked in the the case of closing the pipe.
                    //Exiting with no room for discussion
                    return;
                }

                //We read from the pipe
                int  byteCountRead = 0;
                bool eofReached    = false;

                while (!eofReached)
                {
                    byteCountRead = testHelper.pipeServer.Read(outBuffer, 0, outBuffer.Length);

                    if (byteCountRead > 0)
                    {
                        if (byteCountRead > 2)
                        {
                            eofReached = (outBuffer[byteCountRead - 1] == 0x0 &&
                                          outBuffer[byteCountRead - 2] != 0x0 &&
                                          outBuffer[byteCountRead - 3] != 0x0);
                        }
                        else if (byteCountRead > 1 && !eofReached)
                        {
                            eofReached = (outBuffer[byteCountRead - 1] == 0x0 &&
                                          outBuffer[byteCountRead - 2] == 0x0);
                        }
                        else if (byteCountRead == 1)
                        {
                            eofReached = outBuffer[byteCountRead - 1] == 0x0;
                        }

                        memStream.Write(outBuffer, 0,
                                        eofReached ? byteCountRead - 1 : byteCountRead);
                    }
                    else
                    {
                        eofReached = true;
                    }
                }

                //Sending a response
                if (!string.IsNullOrEmpty(testHelper.responseXml))
                {
                    //TODO: Set the proper encoding
                    byte[] xmlBytes = Encoding.UTF8.GetBytes(testHelper.responseXml);
                    //We write the response content back and flush it down the drain.
                    testHelper.pipeServer.Write(xmlBytes, 0, xmlBytes.Length);
                }
                else if (!string.IsNullOrEmpty(testHelper.responsePath))
                {
                    byteCountRead = 0;
                    using (System.IO.FileStream fs = System.IO.File.OpenRead(testHelper.responsePath))
                    {
                        //Streaming respoonse from file
                        while ((byteCountRead = fs.Read(outBuffer, 0, outBuffer.Length)) > 0)
                        {
                            testHelper.pipeServer.Write(outBuffer, 0, byteCountRead);
                        }
                    }
                }
                else
                {
                    throw new InvalidOperationException("There was no response content defined");
                }

                //Write the EOF bytes
                testHelper.pipeServer.WriteByte(0x00);
                testHelper.pipeServer.Flush();

                testHelper.pipeServer.WaitForPipeDrain();
            }
            finally
            {
                //We signal the event
                testHelper.syncEvent.Set();
            }
        }