Message ID | 1457544504-8548-17-git-send-email-berrange@redhat.com |
---|---|
State | New |
Headers | show |
On 09/03/2016 18:28, Daniel P. Berrange wrote: > The qemu_chr_open_socket_fd method takes care of either doing a > synchronous socket connect, or creating a listener socket. Part > of the work when creating the listener socket is to register a > watch for incoming clients. The caller of qemu_chr_open_socket_fd > may not want this watch created, as it might be doing a synchronous > wait for the first client. Rather than passing yet more parameters > into qemu_chr_open_socket_fd to let it handle this, just remove > the qemu_chr_open_socket_fd method an inline its functionality > into the caller. This allows for a clearer control flow and shorter > code. > > Signed-off-by: Daniel P. Berrange <berrange@redhat.com> > --- > qemu-char.c | 61 +++++++++++++++++++++++++------------------------------------ > 1 file changed, 25 insertions(+), 36 deletions(-) > > diff --git a/qemu-char.c b/qemu-char.c > index 579c620..fee2540 100644 > --- a/qemu-char.c > +++ b/qemu-char.c > @@ -3109,32 +3109,6 @@ static void qemu_chr_socket_connected(Object *src, Error *err, void *opaque) > object_unref(OBJECT(sioc)); > } > > -static bool qemu_chr_open_socket_fd(CharDriverState *chr, Error **errp) > -{ > - TCPCharDriver *s = chr->opaque; > - QIOChannelSocket *sioc = qio_channel_socket_new(); > - > - if (s->is_listen) { > - if (qio_channel_socket_listen_sync(sioc, s->addr, errp) < 0) { > - goto fail; > - } > - s->listen_ioc = sioc; > - s->listen_tag = qio_channel_add_watch( > - QIO_CHANNEL(s->listen_ioc), G_IO_IN, tcp_chr_accept, chr, NULL); > - } else { > - if (qio_channel_socket_connect_sync(sioc, s->addr, errp) < 0) { > - goto fail; > - } > - tcp_chr_new_client(chr, sioc); > - object_unref(OBJECT(sioc)); > - } > - > - return true; > - > - fail: > - object_unref(OBJECT(sioc)); > - return false; > -} > > /*********************************************************/ > /* Ring buffer chardev */ > @@ -4349,26 +4323,41 @@ static CharDriverState *qmp_chardev_open_socket(const char *id, > s->reconnect_time = reconnect; > } > > + sioc = qio_channel_socket_new(); > + qio_channel_set_blocking(QIO_CHANNEL(sioc), true, NULL); This might not be needed after all. Paolo > if (s->reconnect_time) { > - sioc = qio_channel_socket_new(); > qio_channel_socket_connect_async(sioc, s->addr, > qemu_chr_socket_connected, > chr, NULL); > - } else if (!qemu_chr_open_socket_fd(chr, errp)) { > - goto error; > - } > - > - if (is_listen && is_waitconnect) { > - fprintf(stderr, "QEMU waiting for connection on: %s\n", > - chr->filename); > - qio_channel_set_blocking(QIO_CHANNEL(s->listen_ioc), true, NULL); > - tcp_chr_accept(QIO_CHANNEL(s->listen_ioc), G_IO_IN, chr); > + } else if (s->is_listen) { > + if (qio_channel_socket_listen_sync(sioc, s->addr, errp) < 0) { > + goto error; > + } > + s->listen_ioc = sioc; > + if (is_waitconnect) { > + fprintf(stderr, "QEMU waiting for connection on: %s\n", > + chr->filename); > + tcp_chr_accept(QIO_CHANNEL(s->listen_ioc), G_IO_IN, chr); > + } > qio_channel_set_blocking(QIO_CHANNEL(s->listen_ioc), false, NULL); > + if (!s->ioc) { > + s->listen_tag = qio_channel_add_watch( > + QIO_CHANNEL(s->listen_ioc), G_IO_IN, tcp_chr_accept, chr, NULL); > + } > + } else { > + if (qio_channel_socket_connect_sync(sioc, s->addr, errp) < 0) { > + goto error; > + } > + tcp_chr_new_client(chr, sioc); > + object_unref(OBJECT(sioc)); > } > > return chr; > > error: > + if (sioc) { > + object_unref(OBJECT(sioc)); > + } > if (s->tls_creds) { > object_unref(OBJECT(s->tls_creds)); > } >
On Wed, Mar 09, 2016 at 06:53:29PM +0100, Paolo Bonzini wrote: > > > On 09/03/2016 18:28, Daniel P. Berrange wrote: > > The qemu_chr_open_socket_fd method takes care of either doing a > > synchronous socket connect, or creating a listener socket. Part > > of the work when creating the listener socket is to register a > > watch for incoming clients. The caller of qemu_chr_open_socket_fd > > may not want this watch created, as it might be doing a synchronous > > wait for the first client. Rather than passing yet more parameters > > into qemu_chr_open_socket_fd to let it handle this, just remove > > the qemu_chr_open_socket_fd method an inline its functionality > > into the caller. This allows for a clearer control flow and shorter > > code. > > > > Signed-off-by: Daniel P. Berrange <berrange@redhat.com> > > --- > > qemu-char.c | 61 +++++++++++++++++++++++++------------------------------------ > > 1 file changed, 25 insertions(+), 36 deletions(-) > > > > diff --git a/qemu-char.c b/qemu-char.c > > index 579c620..fee2540 100644 > > --- a/qemu-char.c > > +++ b/qemu-char.c > > @@ -3109,32 +3109,6 @@ static void qemu_chr_socket_connected(Object *src, Error *err, void *opaque) > > object_unref(OBJECT(sioc)); > > } > > > > -static bool qemu_chr_open_socket_fd(CharDriverState *chr, Error **errp) > > -{ > > - TCPCharDriver *s = chr->opaque; > > - QIOChannelSocket *sioc = qio_channel_socket_new(); > > - > > - if (s->is_listen) { > > - if (qio_channel_socket_listen_sync(sioc, s->addr, errp) < 0) { > > - goto fail; > > - } > > - s->listen_ioc = sioc; > > - s->listen_tag = qio_channel_add_watch( > > - QIO_CHANNEL(s->listen_ioc), G_IO_IN, tcp_chr_accept, chr, NULL); > > - } else { > > - if (qio_channel_socket_connect_sync(sioc, s->addr, errp) < 0) { > > - goto fail; > > - } > > - tcp_chr_new_client(chr, sioc); > > - object_unref(OBJECT(sioc)); > > - } > > - > > - return true; > > - > > - fail: > > - object_unref(OBJECT(sioc)); > > - return false; > > -} > > > > /*********************************************************/ > > /* Ring buffer chardev */ > > @@ -4349,26 +4323,41 @@ static CharDriverState *qmp_chardev_open_socket(const char *id, > > s->reconnect_time = reconnect; > > } > > > > + sioc = qio_channel_socket_new(); > > + qio_channel_set_blocking(QIO_CHANNEL(sioc), true, NULL); > > This might not be needed after all. Indeed, it isn't needed anymore. Regards, Daniel
diff --git a/qemu-char.c b/qemu-char.c index 579c620..fee2540 100644 --- a/qemu-char.c +++ b/qemu-char.c @@ -3109,32 +3109,6 @@ static void qemu_chr_socket_connected(Object *src, Error *err, void *opaque) object_unref(OBJECT(sioc)); } -static bool qemu_chr_open_socket_fd(CharDriverState *chr, Error **errp) -{ - TCPCharDriver *s = chr->opaque; - QIOChannelSocket *sioc = qio_channel_socket_new(); - - if (s->is_listen) { - if (qio_channel_socket_listen_sync(sioc, s->addr, errp) < 0) { - goto fail; - } - s->listen_ioc = sioc; - s->listen_tag = qio_channel_add_watch( - QIO_CHANNEL(s->listen_ioc), G_IO_IN, tcp_chr_accept, chr, NULL); - } else { - if (qio_channel_socket_connect_sync(sioc, s->addr, errp) < 0) { - goto fail; - } - tcp_chr_new_client(chr, sioc); - object_unref(OBJECT(sioc)); - } - - return true; - - fail: - object_unref(OBJECT(sioc)); - return false; -} /*********************************************************/ /* Ring buffer chardev */ @@ -4349,26 +4323,41 @@ static CharDriverState *qmp_chardev_open_socket(const char *id, s->reconnect_time = reconnect; } + sioc = qio_channel_socket_new(); + qio_channel_set_blocking(QIO_CHANNEL(sioc), true, NULL); if (s->reconnect_time) { - sioc = qio_channel_socket_new(); qio_channel_socket_connect_async(sioc, s->addr, qemu_chr_socket_connected, chr, NULL); - } else if (!qemu_chr_open_socket_fd(chr, errp)) { - goto error; - } - - if (is_listen && is_waitconnect) { - fprintf(stderr, "QEMU waiting for connection on: %s\n", - chr->filename); - qio_channel_set_blocking(QIO_CHANNEL(s->listen_ioc), true, NULL); - tcp_chr_accept(QIO_CHANNEL(s->listen_ioc), G_IO_IN, chr); + } else if (s->is_listen) { + if (qio_channel_socket_listen_sync(sioc, s->addr, errp) < 0) { + goto error; + } + s->listen_ioc = sioc; + if (is_waitconnect) { + fprintf(stderr, "QEMU waiting for connection on: %s\n", + chr->filename); + tcp_chr_accept(QIO_CHANNEL(s->listen_ioc), G_IO_IN, chr); + } qio_channel_set_blocking(QIO_CHANNEL(s->listen_ioc), false, NULL); + if (!s->ioc) { + s->listen_tag = qio_channel_add_watch( + QIO_CHANNEL(s->listen_ioc), G_IO_IN, tcp_chr_accept, chr, NULL); + } + } else { + if (qio_channel_socket_connect_sync(sioc, s->addr, errp) < 0) { + goto error; + } + tcp_chr_new_client(chr, sioc); + object_unref(OBJECT(sioc)); } return chr; error: + if (sioc) { + object_unref(OBJECT(sioc)); + } if (s->tls_creds) { object_unref(OBJECT(s->tls_creds)); }
The qemu_chr_open_socket_fd method takes care of either doing a synchronous socket connect, or creating a listener socket. Part of the work when creating the listener socket is to register a watch for incoming clients. The caller of qemu_chr_open_socket_fd may not want this watch created, as it might be doing a synchronous wait for the first client. Rather than passing yet more parameters into qemu_chr_open_socket_fd to let it handle this, just remove the qemu_chr_open_socket_fd method an inline its functionality into the caller. This allows for a clearer control flow and shorter code. Signed-off-by: Daniel P. Berrange <berrange@redhat.com> --- qemu-char.c | 61 +++++++++++++++++++++++++------------------------------------ 1 file changed, 25 insertions(+), 36 deletions(-)