Пример #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
        /// <summary>
        /// Publish a message to a given exchange.
        /// </summary>
        /// <param name="virtualHost"></param>
        /// <param name="exchangeName"></param>
        /// <param name="routingKey">binding key</param>
        /// <param name="payload">message data</param>
        /// <param name="payloadEncoding">The payload_encoding key should be either "string" (in which case the payload will be taken to be the UTF-8 encoding of the payload field) or "base64" (in which case the payload field is taken to be base64 encoded).</param>
        /// <param name="properties"></param>
        /// <returns>true if the message was sent to at least one queue.</returns>
        public async Task <bool> PublishMessage(
            string virtualHost, string exchangeName, string routingKey, dynamic payload,
            PayloadEncoding payloadEncoding = PayloadEncoding.String, Properties properties = null)
        {
            if (exchangeName == String.Empty)
            {
                throw new ArgumentException("Cannot send message using default exchange in HTTP API");
            }

            var request = new PublishMessageRequest
            {
                payload          = JsonConvert.SerializeObject(payload),
                routing_key      = routingKey,
                properties       = new Properties(),
                payload_encoding = payloadEncoding.ToString("G").ToLower()
            };

            string path     = $"/api/exchanges/{virtualHost.Encode()}/{exchangeName.Encode()}/publish";
            var    response = await DoCall <PublishMessageResponse>(path, HttpMethod.Post, request);

            return(response.Routed);
        }
Пример #5
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));
                }
            }
        }
Пример #6
0
 public void DecodedPayloadLength_ShouldReturnExpectedValue(int length, int expected)
 {
     Assert.AreEqual(expected, PayloadEncoding.DecodedPayloadLength(length));
 }
Пример #7
0
        /// <summary>
        /// Get messages from a queue. (This is not an HTTP GET as it will alter the state of the queue.)
        /// </summary>
        /// <param name="virtualHost"></param>
        /// <param name="queueName"></param>
        /// <param name="count">ols the maximum number of messages to get. You may get fewer messages than this if the queue cannot immediately provide them.</param>
        /// <param name="requeue">determines whether the messages will be removed from the queue. If requeue is true they will be requeued - but their redelivered flag will be set.</param>
        /// <param name="encoding">must be either "auto" (in which case the payload will be returned as a string if it is valid UTF-8, and base64 encoded otherwise), or "base64" (in which case the payload will always be base64 encoded).</param>
        /// <returns></returns>
        public async Task <IEnumerable <QueueMessage> > GetQueueMessages(string virtualHost, string queueName,
                                                                         long count = Int64.MaxValue, bool requeue = true, PayloadEncoding encoding = PayloadEncoding.Auto)
        {
            var request = new GetQueueMessagesRequest
            {
                count    = count,
                requeue  = requeue,
                encoding = encoding.ToString("G").ToLower(),
            };

            string path = $"/api/queues/{virtualHost.Encode()}/{queueName.Encode()}/get";

            return(await DoCall <IEnumerable <QueueMessage> >(path, HttpMethod.Post, request));
        }