osmo_io: Add osmo_iofd_notify_connected()

Don't call write_enable() in osmo_iofd_register(). This was used to
detect whether a socket is connected or not, but would always be
enabled, even on unconnected sockets. Instead make this behaviour
explicit by calling osmo_iofd_notify_connected().

Change-Id: Ieed10bc94c8aad821c0a8f7764db0e05c054c1e3
This commit is contained in:
Daniel Willmann 2023-06-30 10:51:53 +02:00
parent 8b7e7f164f
commit e2a8dc4131
5 changed files with 18 additions and 5 deletions

View File

@ -75,6 +75,8 @@ void osmo_iofd_txqueue_clear(struct osmo_io_fd *iofd);
int osmo_iofd_close(struct osmo_io_fd *iofd);
void osmo_iofd_free(struct osmo_io_fd *iofd);
void osmo_iofd_notify_connected(struct osmo_io_fd *iofd);
int osmo_iofd_write_msgb(struct osmo_io_fd *iofd, struct msgb *msg);
int osmo_iofd_sendto_msgb(struct osmo_io_fd *iofd, struct msgb *msg, int sendto_flags,
const struct osmo_sockaddr *dest);

View File

@ -273,6 +273,7 @@ osmo_iofd_txqueue_clear;
osmo_iofd_txqueue_len;
osmo_iofd_unregister;
osmo_iofd_uring_init;
osmo_iofd_notify_connected;
osmo_iofd_write_msgb;
osmo_ip_str_type;
osmo_isdnhdlc_decode;

View File

@ -433,7 +433,9 @@ int osmo_iofd_register(struct osmo_io_fd *iofd, int fd)
IOFD_FLAG_UNSET(iofd, IOFD_FLAG_CLOSED);
osmo_iofd_ops.read_enable(iofd);
osmo_iofd_ops.write_enable(iofd);
if (iofd->tx_queue.current_length > 0)
osmo_iofd_ops.write_enable(iofd);
return rc;
}
@ -603,4 +605,14 @@ void osmo_iofd_set_ioops(struct osmo_io_fd *iofd, const struct osmo_io_ops *ioop
iofd->io_ops = *ioops;
}
/*! Notify the user if/when the socket is connected
* When the socket is connected the write_cb will be called.
* \param[in] iofd the file descriptor */
void osmo_iofd_notify_connected(struct osmo_io_fd *iofd)
{
OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_READ_WRITE);
osmo_iofd_ops.write_enable(iofd);
}
#endif /* defined(__linux__) */

View File

@ -86,6 +86,8 @@ static void test_connected(void)
osmo_iofd_register(iofd1, fds[0]);
iofd2 = osmo_iofd_setup(ctx, fds[1], "ep2", OSMO_IO_FD_MODE_READ_WRITE, &ioops_conn_read_write, NULL);
osmo_iofd_register(iofd2, fds[1]);
// Explicitly check if ep1 is connected through write_cb
osmo_iofd_notify_connected(iofd1);
/* Allow enough cycles to handle the messages */
for (int i = 0; i < 128; i++)

View File

@ -1,10 +1,6 @@
Running test_connected
ep1: write() returned rc=0
ep2: write() returned rc=0
ep1: write() returned rc=16
ep2: write() returned rc=16
ep1: read() msg with len=16
01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10
ep2: read() msg with len=16
01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10
Running test_unconnected