From 9f63e0e965ae822daa69b953bcf833b6dbe6cb74 Mon Sep 17 00:00:00 2001 From: Andreas Eversberg Date: Tue, 24 Jan 2023 18:07:52 +0100 Subject: [PATCH] Updated ph-socket interface --- src/ph_socket.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/ph_socket.c b/src/ph_socket.c index 1224c15..7967280 100644 --- a/src/ph_socket.c +++ b/src/ph_socket.c @@ -32,14 +32,14 @@ #include "logging.h" #include "ph_socket.h" -int ph_socket_listen_cb(struct osmo_fd *ofd, unsigned int __attribute__((unused)) what); -int ph_socket_connect_cb(struct osmo_fd *ofd, unsigned int __attribute__((unused)) what); +static int ph_socket_listen_cb(struct osmo_fd *ofd, unsigned int __attribute__((unused)) what); +static int ph_socket_connect_cb(struct osmo_fd *ofd, unsigned int __attribute__((unused)) what); static void ph_socket_timeout_cb(void *data); static void open_connection(ph_socket_t *s) { uint8_t enable = PH_CTRL_UNBLOCK; - int rc; + int rc, flags; if (s->connect_ofd.fd > 0) return; @@ -54,8 +54,12 @@ static void open_connection(ph_socket_t *s) s->connect_ofd.fd = rc; s->connect_ofd.data = s; s->connect_ofd.when = BSC_FD_READ; - s->connect_ofd.cb = ph_socket_listen_cb; + s->connect_ofd.cb = ph_socket_connect_cb; osmo_fd_register(&s->connect_ofd); + /* set nonblocking io, because we do multiple reads when handling read event */ + flags = fcntl(s->connect_ofd.fd, F_GETFL); + flags |= O_NONBLOCK; + fcntl(s->connect_ofd.fd, F_SETFL, flags); /* connect */ rc = connect(s->connect_ofd.fd, (struct sockaddr *)&s->sock_address, sizeof(s->sock_address)); if (rc < 0 && errno != EAGAIN) { @@ -176,12 +180,12 @@ void ph_socket_exit(ph_socket_t *s) osmo_timer_del(&s->retry_timer); } -int ph_socket_listen_cb(struct osmo_fd *ofd, unsigned int __attribute__((unused)) what) +static int ph_socket_listen_cb(struct osmo_fd *ofd, unsigned int __attribute__((unused)) what) { ph_socket_t *s = ofd->data; struct sockaddr_un sock_address; uint8_t enable = PH_CTRL_UNBLOCK; - int rc; + int rc, flags; socklen_t sock_len = sizeof(sock_address); /* see if there is an incoming connection */ @@ -198,6 +202,10 @@ int ph_socket_listen_cb(struct osmo_fd *ofd, unsigned int __attribute__((unused) s->connect_ofd.when = BSC_FD_READ; s->connect_ofd.cb = ph_socket_connect_cb; osmo_fd_register(&s->connect_ofd); + /* set nonblocking io, because we do multiple reads when handling read event */ + flags = fcntl(s->connect_ofd.fd, F_GETFL); + flags |= O_NONBLOCK; + fcntl(s->connect_ofd.fd, F_SETFL, flags); /* reset rx buffer */ s->rx_header_index = 0; s->rx_data_index = 0; @@ -209,7 +217,7 @@ int ph_socket_listen_cb(struct osmo_fd *ofd, unsigned int __attribute__((unused) return 0; } -int ph_socket_connect_cb(struct osmo_fd *ofd, unsigned __attribute__((unused)) int what) +static int ph_socket_connect_cb(struct osmo_fd *ofd, unsigned __attribute__((unused)) int what) { ph_socket_t *s = ofd->data; int rc;