/// <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> /// <remarks> /// Do not dispose connection till callback is invoked, /// otherwise underlying network stream will be closed. /// </remarks> public void BeginWrite(ProducerRequest request, MessageSent <ProducerRequest> callback) { this.EnsuresNotDisposed(); Guard.NotNull(request, "request"); if (callback == null) { this.BeginWrite(request); return; } NetworkStream stream = client.GetStream(); var ctx = new RequestContext <ProducerRequest>(stream, request); byte[] data = request.RequestBuffer.GetBuffer(); stream.BeginWrite( data, 0, data.Length, delegate(IAsyncResult asyncResult) { var context = (RequestContext <ProducerRequest>)asyncResult.AsyncState; callback(context); context.NetworkStream.EndWrite(asyncResult); }, ctx); }
/// <summary> /// Writes a producer request to the server asynchronously. /// </summary> /// <param name="request">The request to make.</param> public void BeginWrite(ProducerRequest request) { this.EnsuresNotDisposed(); Guard.NotNull(request, "request"); NetworkStream stream = client.GetStream(); byte[] data = request.RequestBuffer.GetBuffer(); stream.BeginWrite(data, 0, data.Length, asyncResult => ((NetworkStream)asyncResult.AsyncState).EndWrite(asyncResult), stream); }
/// <summary> /// Sends a request to Kafka. /// </summary> /// <param name="request">The request to send to Kafka.</param> public void Send(ProducerRequest request) { if (request.IsValid()) { using (KafkaConnection connection = new KafkaConnection(Server, Port)) { connection.Write(request); } } }
/// <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> /// <remarks> /// Do not dispose connection till callback is invoked, /// otherwise underlying network stream will be closed. /// </remarks> public void BeginWrite(ProducerRequest request, MessageSent <ProducerRequest> callback) { this.EnsuresNotDisposed(); Guard.NotNull(request, "request"); if (callback == null) { this.BeginWrite(request); return; } try { NetworkStream stream = client.GetStream(); var ctx = new RequestContext <ProducerRequest>(stream, request); byte[] data = request.RequestBuffer.GetBuffer(); if (this.socketPollingLevel == SocketPollingLevel.DOUBLE) { PollSocket(); } stream.BeginWrite( data, 0, data.Length, delegate(IAsyncResult asyncResult) { var context = (RequestContext <ProducerRequest>)asyncResult.AsyncState; callback(context); context.NetworkStream.EndWrite(asyncResult); }, ctx); if (this.socketPollingLevel == SocketPollingLevel.SINGLE || this.socketPollingLevel == SocketPollingLevel.DOUBLE) { PollSocket(); } } catch (InvalidOperationException e) { throw new KafkaConnectionException(e); } catch (IOException e) { throw new KafkaConnectionException(e); } }
/// <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); } } }
/// <summary> /// Writes a producer request to the server asynchronously. /// </summary> /// <param name="request">The request to make.</param> public void BeginWrite(ProducerRequest request) { this.EnsuresNotDisposed(); Guard.NotNull(request, "request"); try { NetworkStream stream = client.GetStream(); byte[] data = request.RequestBuffer.GetBuffer(); stream.BeginWrite(data, 0, data.Length, asyncResult => ((NetworkStream)asyncResult.AsyncState).EndWrite(asyncResult), stream); } catch (InvalidOperationException e) { throw new KafkaConnectionException(e); } catch (IOException e) { throw new KafkaConnectionException(e); } }
/// <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); }
/// <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); }
/// <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()); }
/// <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) { this.EnsuresNotDisposed(); Guard.NotNull(request, "request"); this.Write(request.RequestBuffer.GetBuffer()); }
/// <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 ProducerResponse Send(ProducerRequest request) { this.EnsuresNotDisposed(); Guard.NotNull(request, "request"); return(this.Handle(request.RequestBuffer.GetBuffer(), new ProducerResponse.Parser(), request.RequiredAcks != 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); } } }