예제 #1
0
        public void Send(Socket socket)
        {
            var padding_size = (byte)((8 - (BodyLength % 8)) % 8);

            CompatArraySegment <byte> header = buffers.EnforceHeaderLength(HeaderSize);

            header [0] = version;
            header [1] = (byte)type;
            header [2] = (byte)(request_id >> 8);
            header [3] = (byte)(request_id & 0xFF);
            header [4] = (byte)(BodyLength >> 8);
            header [5] = (byte)(BodyLength & 0xFF);
            header [6] = padding_size;

            CompatArraySegment <byte> padding = buffers.EnforcePaddingLength(padding_size);

            for (int i = 0; i < padding_size; i++)
            {
                padding [i] = 0;
            }

            Logger.Write(LogLevel.Debug, Strings.Record_Sent, Type, RequestID, BodyLength);

            SendAll(socket, header, HeaderSize);
            SendAll(socket, buffers.Body, BodyLength);
            SendAll(socket, padding, padding_size);
        }
예제 #2
0
        public Record(Socket socket, Buffers receive_buffer) : this()
        {
            if (socket == null)
            {
                throw new ArgumentNullException("socket");
            }

            CompatArraySegment <byte> header_buffer = receive_buffer.EnforceHeaderLength(HeaderSize);

            // Read the 8 byte record header.
            ReceiveAll(socket, header_buffer, HeaderSize);

            // Read the values from the data.
            version    = header_buffer [0];
            type       = (RecordType)header_buffer [1];
            request_id = ReadUInt16(header_buffer, 2);
            BodyLength = ReadUInt16(header_buffer, 4);
            byte padding_length = header_buffer [6];

            CompatArraySegment <byte> body_buffer = receive_buffer.EnforceBodyLength(BodyLength);

            // Read the record data, and throw an exception if the
            // complete data cannot be read.
            if (BodyLength > 0)
            {
                ReceiveAll(socket, body_buffer, BodyLength);
            }

            CompatArraySegment <byte> padding_buffer = receive_buffer.EnforcePaddingLength(padding_length);

            if (padding_length > 0)
            {
                ReceiveAll(socket, padding_buffer, padding_length);
            }

            buffers = receive_buffer;

            Logger.Write(LogLevel.Debug, Strings.Record_Received, Type, RequestID, BodyLength);
        }