Ejemplo n.º 1
0
        //	Client IPC Cycle.
        //	Send a request. Receive the response.
        static public async Task <RequestResponseFrame> RequestReceiveResponse(BufferedStream bufferedStream, ITcpFrame sendTcpFrame, ILogger logger)
        {
            //	Send the request to the server.
            await SendFrame(sendTcpFrame, bufferedStream, logger);

            //	Receive the response from the server.
            ITcpFrame tcpFrame;

            tcpFrame = await ReceiveFrame(bufferedStream, logger);

            //	The response is ready to be processed.
            RequestResponseFrame responseFrame = new RequestResponseFrame(tcpFrame.FramePacket);

            return(responseFrame);
        }
Ejemplo n.º 2
0
        static public async Task <ITcpFrame> ReceiveFrame(BufferedStream bufferedStream, ILogger logger)
        {
            int    count;
            int    numberRead;
            int    offset;
            string methodName = "ReceiveFrame";

            logger.WriteEntry(methodName + " creating receive buffer.");

            //	Read the number of bytes of the packet.
            count = sizeof(Int32);
            byte[] receiveLengthBytes = new byte[count];
            offset = 0;
            while (count > 0)
            {
                numberRead = await bufferedStream.ReadAsync(receiveLengthBytes, offset, count);

                offset += numberRead;
                count  -= numberRead;
            }
            logger.WriteEntry(methodName + " received response size.");

            //	Convert the raw bytes to an integer.
            int receiveFrameSize = BitConverter.ToInt32(receiveLengthBytes, 0);

            //	Receive the packet.
            byte[] receiveFrameBytes = new byte[receiveFrameSize];
            if (receiveFrameSize > 0)
            {
                //	Receive the data frame.
                offset = 0;
                count  = receiveFrameSize;
                while (count > 0)
                {
                    numberRead = await bufferedStream.ReadAsync(receiveFrameBytes, offset, count);

                    offset += numberRead;
                    count  -= numberRead;
                }
            }
            RequestResponseFrame tcpFrame = new RequestResponseFrame(receiveFrameBytes);

            return(tcpFrame);
        }