Beispiel #1
0
        public void partial_header()
        {
            var    args       = new SocketBufferFake();
            var    body       = "Hello world";
            var    type       = body.GetType().AssemblyQualifiedName;
            var    serializer = new StringSerializer();
            object actual     = null;

            BitConverter2.GetBytes(MicroMessageEncoder.FixedHeaderLength + type.Length, args.Buffer, 0);
            args.Buffer[2] = MicroMessageDecoder.Version;
            BitConverter2.GetBytes(body.Length, args.Buffer, 3);
            args.Buffer[7] = (byte)(sbyte)type.Length;
            Encoding.ASCII.GetBytes(type, 0, type.Length, args.Buffer, HeaderLengthSize + MicroMessageEncoder.FixedHeaderLength);
            Encoding.ASCII.GetBytes(body, 0, body.Length, args.Buffer, HeaderLengthSize + type.Length + MicroMessageEncoder.FixedHeaderLength);

            var sut = new MicroMessageDecoder(serializer);

            sut.MessageReceived   = o => actual = o;
            args.BytesTransferred = MicroMessageDecoder.FixedHeaderLength - 1;
            sut.ProcessReadBytes(args);

            args.Offset          += args.BytesTransferred;
            args.BytesTransferred = body.Length + type.Length + 1 + HeaderLengthSize;
            sut.ProcessReadBytes(args);


            actual.Should().NotBeNull();
            actual.Should().Be("Hello world");
        }
Beispiel #2
0
        public void Receive_one_message()
        {
            var slice1    = new BufferSlice(new byte[65535], 0, 65535);
            var encoder1  = new StringEncoder();
            var decoder1  = new StringDecoder();
            var expected  = "Hello".PadRight(5000);
            var outBuffer = new byte[expected.Length + 4];

            BitConverter2.GetBytes(expected.Length, outBuffer, 0);
            Encoding.UTF8.GetBytes(expected, 0, expected.Length, outBuffer, 4);
            object actual = null;
            var    evt    = new ManualResetEvent(false);
            var    stream = new SslStream(new NetworkStream(_helper.Server));

            stream.BeginAuthenticateAsServer(_certificate, OnAuthenticated, stream);

            var sut1 = CreateClientChannel(slice1, encoder1, decoder1);

            sut1.MessageReceived = (channel, message) =>
            {
                actual = message;
                evt.Set();
            };
            sut1.Assign(_helper.Client);
            evt.WaitOne(500); // wait on authentication
            evt.Reset();
            stream.Write(outBuffer);

            evt.WaitOne(500).Should().BeTrue();
            actual.Should().Be(expected);
        }
        /// <summary>
        ///     Prepare the encoder so that the specified object can be encoded next.
        /// </summary>
        /// <param name="message">Message to send</param>
        /// <remarks>
        ///     Can be used to prepare the next message. for instance serialize it etc.
        /// </remarks>
        /// <exception cref="NotSupportedException">Message is of a type that the encoder cannot handle.</exception>
        public void Prepare(object message)
        {
            var len = Encoding.UTF8.GetByteCount(message.ToString());

            BitConverter2.GetBytes(len, _buffer, 0);
            Encoding.UTF8.GetBytes(message.ToString(), 0, message.ToString().Length, _buffer, 4);
            _bytesLeft = len + 4;
        }
Beispiel #4
0
        private int CreateHeader()
        {
            string contentType;

            if (_message is Stream)
            {
                _bodyStream = (Stream)_message;
                contentType = "stream";
            }
            else if (_message is byte[])
            {
                var buffer = (byte[])_message;
                _bodyStream = new MemoryStream(buffer);
                _bodyStream.SetLength(buffer.Length);
                contentType = "byte[]";
            }
            else
            {
                _bodyStream = _internalStream;
                _serializer.Serialize(_message, _bodyStream, out contentType);
                if (contentType == null)
                {
                    contentType = _message.GetType().AssemblyQualifiedName;
                }
                if (contentType.Length > byte.MaxValue)
                {
                    throw new InvalidOperationException(
                              "The AssemblyQualifiedName (type name) may not be larger than 255 characters. Your type: " +
                              _message.GetType().AssemblyQualifiedName);
                }
            }

            var sliceOffset = _bufferSlice.Offset;
            var sliceBuffer = _bufferSlice.Buffer;

            _bodyStream.Position = 0;
            _headerSize          = FixedHeaderLength + contentType.Length;

            BitConverter2.GetBytes((ushort)_headerSize, sliceBuffer, sliceOffset);
            _bufferSlice.Buffer[sliceOffset + 2] = Version;
            BitConverter2.GetBytes((int)_bodyStream.Length, sliceBuffer, sliceOffset + 2 + 1);
            BitConverter2.GetBytes((byte)contentType.Length, sliceBuffer, sliceOffset + 2 + 1 + 4);
            Encoding.UTF8.GetBytes(contentType, 0, contentType.Length, sliceBuffer, sliceOffset + 2 + 1 + 4 + 1);

            // the header length field is not included in _headerSize as it's a header prefix.
            // hence the +2
            return(_headerSize + 2);
        }
        public void Prepare(object message)
        {
            // Clear the memory stream
            _memoryStream.SetLength(0);

            // start at position 4 so that we can insert the length header.
            _memoryStream.Position = 4;
            var writer = new StreamWriter(_memoryStream);

            _serializer.Serialize(writer, message, typeof(object));
            writer.Flush();

            // insert length header.
            // BitConverter2 exists in a library and uses an existing buffer instead of allocating a new one.
            BitConverter2.GetBytes((int)_memoryStream.Length - 4, _memoryStream.GetBuffer(), 0);

            // and save how much we should send
            _bytesLeft = (int)_memoryStream.Length;
            _offset    = 0;
        }
Beispiel #6
0
        public void complete_header_then_half_typename_then_full_message()
        {
            var    args       = new SocketBufferFake();
            var    body       = "Hello world";
            var    type       = body.GetType().AssemblyQualifiedName;
            var    serializer = new StringSerializer();
            object actual     = null;

            BitConverter2.GetBytes(MicroMessageEncoder.FixedHeaderLength + type.Length, args.Buffer, 0);                                        //header length
            args.Buffer[2] = MicroMessageDecoder.Version;
            BitConverter2.GetBytes(body.Length, args.Buffer, 3);                                                                                //content lkength
            args.Buffer[7] = (byte)(sbyte)type.Length;                                                                                          // type len
            Encoding.ASCII.GetBytes(type, 0, type.Length, args.Buffer, HeaderLengthSize + MicroMessageEncoder.FixedHeaderLength);               // type name
            Encoding.ASCII.GetBytes(body, 0, body.Length, args.Buffer, HeaderLengthSize + type.Length + MicroMessageEncoder.FixedHeaderLength); //body

            var sut = new MicroMessageDecoder(serializer);

            sut.MessageReceived   = o => actual = o;
            args.BytesTransferred = sizeof(short) + MicroMessageDecoder.FixedHeaderLength;
            sut.ProcessReadBytes(args);

            args.Offset          += args.BytesTransferred;
            args.BytesTransferred = type.Length - 31;
            sut.ProcessReadBytes(args);

            args.Offset          += args.BytesTransferred;
            args.BytesTransferred = 31;
            sut.ProcessReadBytes(args);

            args.Offset          += args.BytesTransferred;
            args.BytesTransferred = body.Length;
            sut.ProcessReadBytes(args);


            actual.Should().NotBeNull();
            actual.Should().Be("Hello world");
        }
Beispiel #7
0
 byte[] getBytes(int x)
 {
     return(BitConverter2.getBytes(x));
 }
Beispiel #8
0
 public static int ToIPv4Address(this IPAddress address, bool isBigEndian = true)
 {
     return(BitConverter2.MergeByte4(address.GetAddressBytes(), isBigEndian));
 }
Beispiel #9
0
 public static IPAddress ToIPv4Address(this int address, bool isBigEndian = true)
 {
     return(new IPAddress(BitConverter2.SplitByte4(address, isBigEndian)));
 }