Skip to content

Commit adf4b2f

Browse files
committed
Add some documentation to server module
1 parent 5ec1727 commit adf4b2f

File tree

4 files changed

+48
-12
lines changed

4 files changed

+48
-12
lines changed

src/handlers.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,8 +363,7 @@ static int connect_handler(struct io_event *e) {
363363
goto bad_auth;
364364
else {
365365
struct authentication *auth = NULL;
366-
HASH_FIND_STR(server.authentications,
367-
(char *) c->payload.username, auth);
366+
HASH_FIND_STR(server.auths, (char *) c->payload.username, auth);
368367
if (!auth || !check_passwd((char *) c->payload.password, auth->salt))
369368
goto bad_auth;
370369
}

src/server.c

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
*/
918936
void 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
*/
926946
int 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
*/
10021023
void daemonize(void) {
10031024

src/server.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,31 @@ struct server {
123123
// NULL
124124
struct client_session *sessions;
125125
// UTHASH handle pointer for authentications
126-
struct authentication *authentications;
126+
struct authentication *auths;
127127
// Application TLS context
128128
SSL_CTX *ssl_ctx;
129129
};
130130

131131
extern struct server server;
132132

133+
/*
134+
* Main entry point for the server, to be called with an address and a port
135+
* to start listening. The function may fail only in the case of Out of memory
136+
* error occurs or listen call fails, on the other cases it should just log
137+
* unexpected errors.
138+
*/
133139
int start_server(const char *, const char *);
140+
141+
/*
142+
* Fire a write callback to reply after a client request, under the hood it
143+
* schedules an EV_WRITE event with a client pointer set to write carried
144+
* contents out on the socket descriptor.
145+
*/
134146
void enqueue_event_write(const struct client *);
147+
148+
/*
149+
* Make the entire process a daemon running in background
150+
*/
135151
void daemonize(void);
136152

137153
#endif

tests/sol_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* BSD 2-Clause License
22
*
3-
* Copyright (c) 2019, Andrea Giacomo Baldan
3+
* Copyright (c) 2020, Andrea Giacomo Baldan
44
* All rights reserved.
55
*
66
* Redistribution and use in source and binary forms, with or without

0 commit comments

Comments
 (0)