Split handler_loop from handler_listen
Some applications might set up a struct handler object to listen on any port i.e., 0, but still need a way to determine which port number was eventually selected by the implementation. Therefore, handler_listen has been reduced to the server initialization bit, whereas the main loop has been split into its own function, namely handler_loop. Because of these changes, it no longer made sense for libweb to write the selected port to standard output, as this is something now applications can do on their own.
This commit is contained in:
parent
8280cc40b9
commit
98f5f52461
10
handler.c
10
handler.c
|
@ -182,14 +182,20 @@ end:
|
|||
return ret;
|
||||
}
|
||||
|
||||
int handler_listen(struct handler *const h, const unsigned short port)
|
||||
int handler_listen(struct handler *const h, const unsigned short port,
|
||||
unsigned short *const outport)
|
||||
{
|
||||
if (!(h->server = server_init(port)))
|
||||
if (!(h->server = server_init(port, outport)))
|
||||
{
|
||||
fprintf(stderr, "%s: server_init failed\n", __func__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int handler_loop(struct handler *const h)
|
||||
{
|
||||
for (;;)
|
||||
{
|
||||
bool exit, io;
|
||||
|
|
|
@ -20,6 +20,8 @@ struct handler *handler_alloc(const struct handler_cfg *cfg);
|
|||
void handler_free(struct handler *h);
|
||||
int handler_add(struct handler *h, const char *url, enum http_op op,
|
||||
handler_fn f, void *user);
|
||||
int handler_listen(struct handler *h, unsigned short port);
|
||||
int handler_listen(struct handler *h, unsigned short port,
|
||||
unsigned short *outport);
|
||||
int handler_loop(struct handler *h);
|
||||
|
||||
#endif /* HANDLER_H */
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
struct server *server_init(unsigned short port);
|
||||
struct server *server_init(unsigned short port, unsigned short *outport);
|
||||
struct server_client *server_poll(struct server *s, bool *io, bool *exit);
|
||||
int server_read(void *buf, size_t n, struct server_client *c);
|
||||
int server_write(const void *buf, size_t n, struct server_client *c);
|
||||
|
|
6
server.c
6
server.c
|
@ -320,7 +320,8 @@ static int init_signals(void)
|
|||
return 0;
|
||||
}
|
||||
|
||||
struct server *server_init(const unsigned short port)
|
||||
struct server *server_init(const unsigned short port,
|
||||
unsigned short *const outport)
|
||||
{
|
||||
struct server *const s = malloc(sizeof *s);
|
||||
|
||||
|
@ -373,8 +374,9 @@ struct server *server_init(const unsigned short port)
|
|||
fprintf(stderr, "%s: getsockname(2): %s\n", __func__, strerror(errno));
|
||||
goto failure;
|
||||
}
|
||||
else if (outport)
|
||||
*outport = ntohs(in.sin_port);
|
||||
|
||||
printf("Listening on port %hu\n", ntohs(in.sin_port));
|
||||
return s;
|
||||
|
||||
failure:
|
||||
|
|
Loading…
Reference in New Issue