Ejemplo n.º 1
0
        /// <summary>
        /// Send a request to Kafka asynchronously.
        /// </summary>
        /// <remarks>
        /// If the callback is not specified then the method behaves as a fire-and-forget call
        /// with the callback being ignored.  By the time this callback is executed, the
        /// <see cref="RequestContext.NetworkStream"/> will already have been closed given an
        /// internal call <see cref="NetworkStream.EndWrite"/>.
        /// </remarks>
        /// <param name="request">The request to send to Kafka.</param>
        /// <param name="callback">
        /// A block of code to execute once the request has been sent to Kafka.  This value may
        /// be set to null.
        /// </param>
        public void SendAsync(ProducerRequest request, MessageSent <ProducerRequest> callback)
        {
            if (request.IsValid())
            {
                KafkaConnection connection = new KafkaConnection(Server, Port);

                if (callback == null)
                {
                    // fire and forget
                    connection.BeginWrite(request.GetBytes());
                }
                else
                {
                    // execute with callback
                    connection.BeginWrite(request, callback);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Writes a producer request to the server asynchronously.
        /// </summary>
        /// <param name="request">The request to make.</param>
        /// <param name="callback">The code to execute once the message is completely sent.</param>
        public void BeginWrite(ProducerRequest request, MessageSent <ProducerRequest> callback)
        {
            NetworkStream stream = _client.GetStream();
            RequestContext <ProducerRequest> ctx = new RequestContext <ProducerRequest>(stream, request);

            byte[] data = request.GetBytes();
            stream.BeginWrite(
                data,
                0,
                data.Length,
                delegate(IAsyncResult asyncResult)
            {
                RequestContext <ProducerRequest> context = (RequestContext <ProducerRequest>)asyncResult.AsyncState;

                if (callback != null)
                {
                    callback(context);
                }

                context.NetworkStream.EndWrite(asyncResult);
                context.NetworkStream.Dispose();
            },
                ctx);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Writes a producer request to the server asynchronously.
        /// </summary>
        /// <param name="request">The request to make.</param>
        /// <param name="callback">The code to execute once the message is completely sent.</param>
        public void BeginWrite(ProducerRequest request, MessageSent<ProducerRequest> callback)
        {
            NetworkStream stream = _client.GetStream();
            RequestContext<ProducerRequest> ctx = new RequestContext<ProducerRequest>(stream, request);

            byte[] data = request.GetBytes();
            stream.BeginWrite(
                data,
                0,
                data.Length,
                delegate(IAsyncResult asyncResult)
                {
                    RequestContext<ProducerRequest> context = (RequestContext<ProducerRequest>)asyncResult.AsyncState;

                    if (callback != null)
                    {
                        callback(context);
                    }

                    context.NetworkStream.EndWrite(asyncResult);
                    context.NetworkStream.Dispose();
                },
                ctx);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Writes a producer request to the server.
 /// </summary>
 /// <remarks>
 /// Write timeout is defaulted to infitite.
 /// </remarks>
 /// <param name="request">The <see cref="ProducerRequest"/> to send to the server.</param>
 public void Write(ProducerRequest request)
 {
     Write(request.GetBytes());
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Writes a producer request to the server.
 /// </summary>
 /// <remarks>
 /// Write timeout is defaulted to infitite.
 /// </remarks>
 /// <param name="request">The <see cref="ProducerRequest"/> to send to the server.</param>
 public void Write(ProducerRequest request)
 {
     Write(request.GetBytes());
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Send a request to Kafka asynchronously.
        /// </summary>
        /// <remarks>
        /// If the callback is not specified then the method behaves as a fire-and-forget call
        /// with the callback being ignored.  By the time this callback is executed, the 
        /// <see cref="RequestContext.NetworkStream"/> will already have been closed given an 
        /// internal call <see cref="NetworkStream.EndWrite"/>.
        /// </remarks>
        /// <param name="request">The request to send to Kafka.</param>
        /// <param name="callback">
        /// A block of code to execute once the request has been sent to Kafka.  This value may 
        /// be set to null.
        /// </param>
        public void SendAsync(ProducerRequest request, MessageSent<ProducerRequest> callback)
        {
            if (request.IsValid())
            {
                KafkaConnection connection = new KafkaConnection(Server, Port);

                if (callback == null)
                {
                    // fire and forget
                    connection.BeginWrite(request.GetBytes());
                }
                else
                {
                    // execute with callback
                    connection.BeginWrite(request, callback);
                }
            }
        }