示例#1
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="TcpServerBase{TServerClient}" /> class.
 /// </summary>
 /// <param name="expectedMaxPayloadSize"> (Optional) Size of the expected maximum payload. </param>
 private protected TcpServerBase(ushort expectedMaxPayloadSize = Constants.TCP_PAYLOAD_SIZE_MAX)
 {
     _maxPayloadSize = expectedMaxPayloadSize > 0 && expectedMaxPayloadSize < Constants.TCP_PAYLOAD_SIZE_MAX
         ? expectedMaxPayloadSize
         : Constants.TCP_PAYLOAD_SIZE_MAX;
     _payloadSize = (ushort)(PayloadEncoding.EncodedPayloadLength(_maxPayloadSize) + 1);
 }
示例#2
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="TcpServerBase{TServerClient}" /> class.
        /// </summary>
        /// <param name="expectedMaxPayloadSize"> (Optional) Size of the expected maximum payload. </param>
        private protected TcpClientBase(ushort expectedMaxPayloadSize = Constants.TCP_PAYLOAD_SIZE_MAX)
        {
            _maxPayloadSize = expectedMaxPayloadSize > 0 && expectedMaxPayloadSize < Constants.TCP_PAYLOAD_SIZE_MAX
                ? expectedMaxPayloadSize
                : Constants.TCP_PAYLOAD_SIZE_MAX;
            _payloadSize = (ushort)(PayloadEncoding.EncodedPayloadLength(_maxPayloadSize) + 1);

            _bufferRead     = new byte[_payloadSize + Constants.TCP_HEADER_OFFSET];
            _circularBuffer = new CircularBuffer(_bufferRead.Length * 2);
            _bigDataHandler = new BigDataHandler <int> .Default();
        }
示例#3
0
        public void Encode_WithRandomData_ShouldNotFail(int length)
        {
            Random r = new Random((int)DateTime.Now.Ticks);

            byte[] buffer  = new byte[length];
            byte[] buffer2 = new byte[PayloadEncoding.EncodedPayloadLength(length)];
            r.NextBytes(buffer);
            fixed(byte *src = buffer)
            fixed(byte *dst = buffer2)
            {
                PayloadEncoding.Encode(src, length, dst, out int bufferLength);
                Assert.AreEqual(buffer2.Length, bufferLength);
                Assert.IsTrue(buffer2.All(b => b != 0));
            }
        }
示例#4
0
        public void Decode_WithEncodedRandomData_ShouldNotFail(int length)
        {
            Random r = new Random((int)DateTime.Now.Ticks);

            byte[] buffer  = new byte[length];
            byte[] buffer2 = new byte[PayloadEncoding.EncodedPayloadLength(length)];

            r.NextBytes(buffer);
            fixed(byte *src = buffer)
            fixed(byte *dst = buffer2)
            {
                ushort checksum1 = PayloadEncoding.Encode(src, length, dst, out int bufferLength);

                byte[] buffer3 = new byte[bufferLength];
                fixed(byte *dcp = buffer3)
                {
                    ushort checksum2 = PayloadEncoding.Decode(dst, bufferLength, dcp, out int dstLength);

                    Assert.AreEqual(length, dstLength);
                    Assert.AreEqual(checksum1, checksum2);
                    Assert.IsTrue(buffer3.Take(dstLength).SequenceEqual(buffer));
                }
            }
        }
示例#5
0
 public void EncodedPayloadLength_ShouldReturnExpectedValue(int length, int expected)
 {
     Assert.AreEqual(expected, PayloadEncoding.EncodedPayloadLength(length));
 }