Exemplo n.º 1
0
        private void SendResponseOverNamedPipe(byte[] arrPayload)
        {
            // First, send the payload header that is being awaited for.
            PipeServer.Write(arrPayload, 0, GatewayPayloadConst.payloadHeaderSize);
            PipeServer.Flush();
            PipeServer.WaitForPipeDrain();

            // Named pipes can only send fixed block size data in a single go over the network.
            // Thus, we will chunk our payload accordingly and send it over.
            int iOffsetToWriteFrom      = GatewayPayloadConst.payloadHeaderSize;
            int iRemainingLengthToWrite = arrPayload.Length - GatewayPayloadConst.payloadHeaderSize;

            if (iRemainingLengthToWrite > 0)
            {
                int iCurrentLengthToWrite = iRemainingLengthToWrite % GatewayPayloadConst.maxNamedPipePayloadSize;
                if (iCurrentLengthToWrite == 0)
                {
                    iCurrentLengthToWrite = GatewayPayloadConst.maxNamedPipePayloadSize;
                }

                while (iRemainingLengthToWrite > 0)
                {
                    // Send the payload and wait for it to be read by the receiver before sending any remaining chunks.
                    PipeServer.Write(arrPayload, iOffsetToWriteFrom, iCurrentLengthToWrite);
                    PipeServer.Flush();
                    PipeServer.WaitForPipeDrain();

                    iRemainingLengthToWrite -= iCurrentLengthToWrite;
                    iOffsetToWriteFrom      += iCurrentLengthToWrite;
                    iCurrentLengthToWrite    = GatewayPayloadConst.maxNamedPipePayloadSize;
                }
            }
        }