@@ -256,6 +256,7 @@ static int connect_handler(struct closure *cb, union mqtt_packet *pkt) {
256256 new_client -> fd = cb -> fd ;
257257 const char * cid = (const char * ) pkt -> connect .payload .client_id ;
258258 new_client -> client_id = sol_strdup (cid );
259+ buffer_create (& new_client -> buf );
259260 hashtable_put (sol .clients , cid , new_client );
260261
261262 /* Substitute fd on callback with closure */
@@ -653,12 +654,31 @@ static int pingreq_handler(struct closure *cb, union mqtt_packet *pkt) {
653654static void on_write (struct evloop * loop , void * arg ) {
654655
655656 struct closure * cb = arg ;
657+ struct sol_client * c = cb -> obj ;
656658
657659 ssize_t sent ;
660+
661+ if (buffer_empty (& c -> buf ) == 0 ) {
662+
663+ /* Check if there's still some remaning bytes to send out */
664+ if ((sent = send_bytes (cb -> fd , c -> buf .bytes + c -> buf .start , c -> buf .end )) < 0 )
665+ sol_error ("Error writing on socket to client %s: %s" ,
666+ ((struct sol_client * ) cb -> obj )-> client_id , strerror (errno ));
667+
668+ /* Update buffer pointers */
669+ c -> buf .start += sent ;
670+
671+ }
672+
658673 if ((sent = send_bytes (cb -> fd , cb -> payload -> data , cb -> payload -> size )) < 0 )
659674 sol_error ("Error writing on socket to client %s: %s" ,
660675 ((struct sol_client * ) cb -> obj )-> client_id , strerror (errno ));
661676
677+ /* Update client buffer for remaining bytes to send */
678+ if (sent < cb -> payload -> size )
679+ buffer_push_back (& c -> buf , cb -> payload -> data + sent ,
680+ cb -> payload -> size - sent );
681+
662682 // Update information stats
663683 info .bytes_sent += sent ;
664684 bytestring_release (cb -> payload );
@@ -977,6 +997,8 @@ static int client_destructor(struct hashtable_entry *entry) {
977997 if (client -> client_id )
978998 sol_free (client -> client_id );
979999
1000+ buffer_release (& client -> buf );
1001+
9801002 sol_free (client );
9811003
9821004 return 0 ;
0 commit comments