[Spice-devel] [PATCH spice-gtk] channel: use base handlers array

Christophe Fergeau cfergeau at redhat.com
Wed Sep 11 02:34:22 PDT 2013


On Tue, Sep 10, 2013 at 04:49:02PM +0200, Marc-André Lureau wrote:
> This allows to simplify a little bit derived class, to avoid overriding
> handle_msg, and allows the base class more flexibility (for example for
> filtering messages, as in the following patch)

This would be much nicer to review if this was split in at least 2 parts,
one adding spice_channel_set_handlers, and the other patch(es) doing the
mechanical change of using this new functionality...



> ---
>  gtk/channel-base.c       | 39 +++++++++++++++++++
>  gtk/channel-cursor.c     | 43 ++++++++-------------
>  gtk/channel-display.c    | 97 +++++++++++++++++++++---------------------------
>  gtk/channel-inputs.c     | 31 +++++-----------
>  gtk/channel-main.c       | 60 +++++++++++++++---------------
>  gtk/channel-playback.c   | 42 +++++++--------------
>  gtk/channel-port.c       | 32 +++++-----------
>  gtk/channel-record.c     | 36 ++++++------------
>  gtk/channel-smartcard.c  | 34 +++++------------
>  gtk/channel-usbredir.c   | 32 +++++-----------
>  gtk/spice-channel-priv.h |  8 +---
>  gtk/spice-channel.c      | 18 +++------
>  gtk/spice-channel.h      |  3 +-
>  13 files changed, 198 insertions(+), 277 deletions(-)
> 
> diff --git a/gtk/channel-base.c b/gtk/channel-base.c
> index dff3024..76d681a 100644
> --- a/gtk/channel-base.c
> +++ b/gtk/channel-base.c
> @@ -189,3 +189,42 @@ void spice_channel_handle_migrate(SpiceChannel *channel, SpiceMsgIn *in)
>          spice_msg_out_send_internal(out);
>      }
>  }
> +
> +
> +static void set_handlers(SpiceChannelClass *klass,
> +                         const spice_msg_handler* handlers, const int n)
> +{
> +    int i;
> +
> +    g_array_set_size(klass->handlers, MAX(klass->handlers->len, n));
> +    for (i = 0; i < n; i++) {
> +        if (handlers[i])
> +            g_array_index(klass->handlers, spice_msg_handler, i) = handlers[i];
> +    }
> +}
> +
> +static void spice_channel_add_base_handlers(SpiceChannelClass *klass)
> +{
> +    static const spice_msg_handler handlers[] = {
> +        [ SPICE_MSG_SET_ACK ]                  = spice_channel_handle_set_ack,
> +        [ SPICE_MSG_PING ]                     = spice_channel_handle_ping,
> +        [ SPICE_MSG_NOTIFY ]                   = spice_channel_handle_notify,
> +        [ SPICE_MSG_DISCONNECTING ]            = spice_channel_handle_disconnect,
> +        [ SPICE_MSG_WAIT_FOR_CHANNELS ]        = spice_channel_handle_wait_for_channels,
> +        [ SPICE_MSG_MIGRATE ]                  = spice_channel_handle_migrate,
> +    };
> +
> +    set_handlers(klass, handlers, G_N_ELEMENTS(handlers));
> +}
> +
> +G_GNUC_INTERNAL
> +void spice_channel_set_handlers(SpiceChannelClass *klass,
> +                                const spice_msg_handler* handlers, const int n)
> +{
> +    /* FIXME: use class private (requires glib 2.24) */

If needed, we can probably raise our minimum glib requirement to 2.24 or
2.26

> diff --git a/gtk/channel-main.c b/gtk/channel-main.c
> index 93995f1..b342e97 100644
> --- a/gtk/channel-main.c
> +++ b/gtk/channel-main.c
>  /* coroutine context */
>  static void spice_main_handle_msg(SpiceChannel *channel, SpiceMsgIn *msg)
> @@ -2346,8 +2353,6 @@ static void spice_main_handle_msg(SpiceChannel *channel, SpiceMsgIn *msg)
>      SpiceChannelClass *parent_class;
>      SpiceChannelPrivate *c = SPICE_CHANNEL(channel)->priv;
>  
> -    g_return_if_fail(type < SPICE_N_ELEMENTS(main_handlers));
> -
>      parent_class = SPICE_CHANNEL_CLASS(spice_main_channel_parent_class);
>  
>      if (c->state == SPICE_CHANNEL_STATE_MIGRATION_HANDSHAKE) {
> @@ -2359,12 +2364,7 @@ static void spice_main_handle_msg(SpiceChannel *channel, SpiceMsgIn *msg)
>          }
>      }
>  
> -    if (main_handlers[type] != NULL)
> -        main_handlers[type](channel, msg);
> -    else if (parent_class->handle_msg)
> -        parent_class->handle_msg(channel, msg);
> -    else
> -        g_return_if_reached();
> +    parent_class->handle_msg(channel, msg);

I'd kind some kind of check for parent_class->handle_msg being non-NULL
here.

Rest looks good from a quick look.

Christophe
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: not available
URL: <http://lists.freedesktop.org/archives/spice-devel/attachments/20130911/5eff7564/attachment-0001.pgp>


More information about the Spice-devel mailing list