Skip to content

Commit

Permalink
socket: Add num connections to qio_channel_socket_async()
Browse files Browse the repository at this point in the history
Reviewed-by: Daniel P. Berrangé <[email protected]>
Signed-off-by: Juan Quintela <[email protected]>
  • Loading branch information
Juan Quintela committed Sep 3, 2019
1 parent 4e2d8bf commit 7959e29
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
2 changes: 2 additions & 0 deletions include/io/channel-socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ int qio_channel_socket_listen_sync(QIOChannelSocket *ioc,
* qio_channel_socket_listen_async:
* @ioc: the socket channel object
* @addr: the address to listen to
* @num: the expected ammount of connections
* @callback: the function to invoke on completion
* @opaque: user data to pass to @callback
* @destroy: the function to free @opaque
Expand All @@ -155,6 +156,7 @@ int qio_channel_socket_listen_sync(QIOChannelSocket *ioc,
*/
void qio_channel_socket_listen_async(QIOChannelSocket *ioc,
SocketAddress *addr,
int num,
QIOTaskFunc callback,
gpointer opaque,
GDestroyNotify destroy,
Expand Down
30 changes: 23 additions & 7 deletions io/channel-socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,38 +220,54 @@ int qio_channel_socket_listen_sync(QIOChannelSocket *ioc,
}


struct QIOChannelListenWorkerData {
SocketAddress *addr;
int num; /* amount of expected connections */
};

static void qio_channel_listen_worker_free(gpointer opaque)
{
struct QIOChannelListenWorkerData *data = opaque;

qapi_free_SocketAddress(data->addr);
g_free(data);
}

static void qio_channel_socket_listen_worker(QIOTask *task,
gpointer opaque)
{
QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
SocketAddress *addr = opaque;
struct QIOChannelListenWorkerData *data = opaque;
Error *err = NULL;

qio_channel_socket_listen_sync(ioc, addr, 1, &err);
qio_channel_socket_listen_sync(ioc, data->addr, data->num, &err);

qio_task_set_error(task, err);
}


void qio_channel_socket_listen_async(QIOChannelSocket *ioc,
SocketAddress *addr,
int num,
QIOTaskFunc callback,
gpointer opaque,
GDestroyNotify destroy,
GMainContext *context)
{
QIOTask *task = qio_task_new(
OBJECT(ioc), callback, opaque, destroy);
SocketAddress *addrCopy;
struct QIOChannelListenWorkerData *data;

addrCopy = QAPI_CLONE(SocketAddress, addr);
data = g_new0(struct QIOChannelListenWorkerData, 1);
data->addr = QAPI_CLONE(SocketAddress, addr);
data->num = num;

/* socket_listen() blocks in DNS lookups, so we must use a thread */
trace_qio_channel_socket_listen_async(ioc, addr);
trace_qio_channel_socket_listen_async(ioc, addr, num);
qio_task_run_in_thread(task,
qio_channel_socket_listen_worker,
addrCopy,
(GDestroyNotify)qapi_free_SocketAddress,
data,
qio_channel_listen_worker_free,
context);
}

Expand Down
2 changes: 1 addition & 1 deletion io/trace-events
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ qio_channel_socket_connect_async(void *ioc, void *addr) "Socket connect async io
qio_channel_socket_connect_fail(void *ioc) "Socket connect fail ioc=%p"
qio_channel_socket_connect_complete(void *ioc, int fd) "Socket connect complete ioc=%p fd=%d"
qio_channel_socket_listen_sync(void *ioc, void *addr, int num) "Socket listen sync ioc=%p addr=%p num=%d"
qio_channel_socket_listen_async(void *ioc, void *addr) "Socket listen async ioc=%p addr=%p"
qio_channel_socket_listen_async(void *ioc, void *addr, int num) "Socket listen async ioc=%p addr=%p num=%d"
qio_channel_socket_listen_fail(void *ioc) "Socket listen fail ioc=%p"
qio_channel_socket_listen_complete(void *ioc, int fd) "Socket listen complete ioc=%p fd=%d"
qio_channel_socket_dgram_sync(void *ioc, void *localAddr, void *remoteAddr) "Socket dgram sync ioc=%p localAddr=%p remoteAddr=%p"
Expand Down
2 changes: 1 addition & 1 deletion tests/test-io-channel-socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ static void test_io_channel_setup_async(SocketAddress *listen_addr,

lioc = qio_channel_socket_new();
qio_channel_socket_listen_async(
lioc, listen_addr,
lioc, listen_addr, 1,
test_io_channel_complete, &data, NULL, NULL);

g_main_loop_run(data.loop);
Expand Down

0 comments on commit 7959e29

Please sign in to comment.