@@ -673,7 +673,7 @@ static void write_callback(struct ev_ctx *ctx, void *arg) {
673673 struct client * client = arg ;
674674 int err = write_data (client );
675675 switch (err ) {
676- case SOL_OK : // OK
676+ case SOL_OK :
677677 /*
678678 * Rearm descriptor making it ready to receive input,
679679 * read_callback will be the callback to be used; also reset the
@@ -683,6 +683,12 @@ static void write_callback(struct ev_ctx *ctx, void *arg) {
683683 ev_fire_event (ctx , client -> conn .fd , EV_READ , read_callback , client );
684684 break ;
685685 case - ERREAGAIN :
686+ /*
687+ * We have an EAGAIN error, which is really just signaling that
688+ * for some reasons the kernel is not ready to write more bytes at
689+ * the moment and it would block, so we just want to re-try some
690+ * time later, re-enqueuing a new write event
691+ */
686692 enqueue_event_write (client );
687693 break ;
688694 default :
@@ -779,6 +785,7 @@ static void read_callback(struct ev_ctx *ctx, void *data) {
779785 case - ERRSOCKETERR :
780786 case - ERRPACKETERR :
781787 case - ERRMAXREQSIZE :
788+ // TODO move to default branch
782789 /*
783790 * We got an unexpected error or a disconnection from the
784791 * client side, remove client from the global map and
@@ -794,7 +801,8 @@ static void read_callback(struct ev_ctx *ctx, void *data) {
794801 if (c -> has_lwt == true) {
795802 char * tname = (char * ) c -> session -> lwt_msg .publish .topic ;
796803 struct topic * t = topic_store_get (server .store , tname );
797- publish_message (& c -> session -> lwt_msg , t );
804+ if (t )
805+ publish_message (& c -> session -> lwt_msg , t );
798806 }
799807 // Clean resources
800808 ev_del_fd (ctx , c -> conn .fd );
@@ -814,6 +822,12 @@ static void read_callback(struct ev_ctx *ctx, void *data) {
814822 info .nconnections -- ;
815823 break ;
816824 case - ERREAGAIN :
825+ /*
826+ * We have an EAGAIN error, which is really just signaling that
827+ * for some reasons the kernel is not ready to read more bytes at
828+ * the moment and it would block, so we just want to re-try some
829+ * time later, re-enqueuing a new read event
830+ */
817831 ev_fire_event (ctx , c -> conn .fd , EV_READ , read_callback , c );
818832 break ;
819833 }
@@ -914,22 +928,28 @@ static void eventloop_start(void *args) {
914928 * ===================
915929 */
916930
917- /* Fire a write callback to reply after a client request */
931+ /*
932+ * Fire a write callback to reply after a client request, under the hood it
933+ * schedules an EV_WRITE event with a client pointer set to write carried
934+ * contents out on the socket descriptor.
935+ */
918936void enqueue_event_write (const struct client * c ) {
919937 ev_fire_event (c -> ctx , c -> conn .fd , EV_WRITE , write_callback , (void * ) c );
920938}
921939
922940/*
923941 * Main entry point for the server, to be called with an address and a port
924- * to start listening
942+ * to start listening. The function may fail only in the case of Out of memory
943+ * error occurs or listen call fails, on the other cases it should just log
944+ * unexpected errors.
925945 */
926946int start_server (const char * addr , const char * port ) {
927947
928948 INIT_INFO ;
929949
930950 /* Initialize global Sol instance */
931951 server .store = topic_store_new ();
932- server .authentications = NULL ;
952+ server .auths = NULL ;
933953 server .pool = memorypool_new (BASE_CLIENTS_NUM , sizeof (struct client ));
934954 if (!server .pool )
935955 log_fatal ("Failed to allocate %d sized memory pool for clients" ,
@@ -939,7 +959,8 @@ int start_server(const char *addr, const char *port) {
939959 pthread_mutex_init (& mutex , NULL );
940960
941961 if (conf -> allow_anonymous == false)
942- config_read_passwd_file (conf -> password_file , & server .authentications );
962+ if (!config_read_passwd_file (conf -> password_file , & server .auths ))
963+ log_error ("Failed to read password file" );
943964
944965 /* Generate stats topics */
945966 for (int i = 0 ; i < SYS_TOPICS ; i ++ ) {
@@ -981,7 +1002,7 @@ int start_server(const char *addr, const char *port) {
9811002#endif
9821003
9831004 close (sfd );
984- AUTH_DESTROY (server .authentications );
1005+ AUTH_DESTROY (server .auths );
9851006 topic_store_destroy (server .store );
9861007
9871008 /* Destroy SSL context, if any present */
@@ -997,7 +1018,7 @@ int start_server(const char *addr, const char *port) {
9971018}
9981019
9991020/*
1000- * Make the entire process a daemon
1021+ * Make the entire process a daemon running in background
10011022 */
10021023void daemonize (void ) {
10031024
0 commit comments