@@ -290,47 +290,40 @@ pub const Ctx = struct {
290290// I/O Send
291291// --------
292292
293+ // NOTE: to allow concurrent send we create each time a dedicated context
294+ // (with its own completion), allocated on the heap.
295+ // After the send (on the sendCbk) the dedicated context will be destroy
296+ // and the msg slice will be free.
293297const Send = struct {
294298 ctx : * Ctx ,
295- buf : []const u8 ,
296-
297- fn init (ctx : * Ctx , msg : []const u8 ) ! struct {
298- ctx : * Send ,
299- completion : * Completion ,
300- } {
301- // NOTE: it seems we can't use the same completion for concurrent
302- // recv and timeout operations, that's why we create a new completion here
303- const completion = try ctx .alloc ().create (Completion );
304- // NOTE: to handle concurrent calls we create each time a new context
305- // If no concurrent calls where required we could just use the main Ctx
299+ msg : []const u8 ,
300+ completion : Completion = undefined ,
301+
302+ fn init (ctx : * Ctx , msg : []const u8 ) ! * Send {
306303 const sd = try ctx .alloc ().create (Send );
307- sd .* = .{
308- .ctx = ctx ,
309- .buf = msg ,
310- };
311- return .{ .ctx = sd , .completion = completion };
304+ sd .* = .{ .ctx = ctx , .msg = msg };
305+ return sd ;
312306 }
313307
314- fn deinit (self : * Send , completion : * Completion ) void {
315- self .ctx .alloc ().destroy (completion );
316- self .ctx .alloc ().free (self .buf );
308+ fn deinit (self : * Send ) void {
309+ self .ctx .alloc ().free (self .msg );
317310 self .ctx .alloc ().destroy (self );
318311 }
319312
320- fn asyncCbk (self : * Send , completion : * Completion , result : SendError ! usize ) void {
313+ fn asyncCbk (self : * Send , _ : * Completion , result : SendError ! usize ) void {
321314 const size = result catch | err | {
322315 self .ctx .err = err ;
323316 return ;
324317 };
325318
326319 std .log .debug ("send async {d} bytes" , .{size });
327- self .deinit (completion );
320+ self .deinit ();
328321 }
329322};
330323
331324pub fn sendAsync (ctx : * Ctx , msg : []const u8 ) ! void {
332325 const sd = try Send .init (ctx , msg );
333- ctx .loop .io .send (* Send , sd . ctx , Send .asyncCbk , sd .completion , ctx .conn_socket , msg );
326+ ctx .loop .io .send (* Send , sd , Send .asyncCbk , & sd .completion , ctx .conn_socket , msg );
334327}
335328
336329pub fn sendSync (ctx : * Ctx , msg : []const u8 ) ! void {
0 commit comments