rpc: simplify calling convention of virNetServerClientDispatchFunc

Currently virNetServerClientDispatchFunc implementations are only
responsible for free'ing the "msg" parameter upon success. Simplify the
calling convention by making it their unconditional responsibility to
free the "msg", and close the client if desired.

Reviewed-by: John Ferlan <jferlan@redhat.com>
Reviewed-by: Jim Fehlig <jfehlig@suse.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrangé 2018-03-06 16:44:34 +00:00
parent 464889fff8
commit c6f1d5190b
3 changed files with 22 additions and 17 deletions

View File

@ -182,15 +182,14 @@ static void virNetServerHandleJob(void *jobOpaque, void *opaque)
VIR_FREE(job); VIR_FREE(job);
} }
static int virNetServerDispatchNewMessage(virNetServerClientPtr client, static void virNetServerDispatchNewMessage(virNetServerClientPtr client,
virNetMessagePtr msg, virNetMessagePtr msg,
void *opaque) void *opaque)
{ {
virNetServerPtr srv = opaque; virNetServerPtr srv = opaque;
virNetServerProgramPtr prog = NULL; virNetServerProgramPtr prog = NULL;
unsigned int priority = 0; unsigned int priority = 0;
size_t i; size_t i;
int ret = -1;
VIR_DEBUG("server=%p client=%p message=%p", VIR_DEBUG("server=%p client=%p message=%p",
srv, client, msg); srv, client, msg);
@ -207,7 +206,7 @@ static int virNetServerDispatchNewMessage(virNetServerClientPtr client,
virNetServerJobPtr job; virNetServerJobPtr job;
if (VIR_ALLOC(job) < 0) if (VIR_ALLOC(job) < 0)
goto cleanup; goto error;
job->client = client; job->client = client;
job->msg = msg; job->msg = msg;
@ -218,21 +217,24 @@ static int virNetServerDispatchNewMessage(virNetServerClientPtr client,
} }
virObjectRef(client); virObjectRef(client);
ret = virThreadPoolSendJob(srv->workers, priority, job); if (virThreadPoolSendJob(srv->workers, priority, job) < 0) {
if (ret < 0) {
virObjectUnref(client); virObjectUnref(client);
VIR_FREE(job); VIR_FREE(job);
virObjectUnref(prog); virObjectUnref(prog);
goto error;
} }
} else { } else {
ret = virNetServerProcessMsg(srv, client, prog, msg); if (virNetServerProcessMsg(srv, client, prog, msg) < 0)
goto error;
} }
cleanup:
virObjectUnlock(srv); virObjectUnlock(srv);
return;
return ret; error:
virNetMessageFree(msg);
virNetServerClientClose(client);
virObjectUnlock(srv);
} }
/** /**

View File

@ -1315,11 +1315,11 @@ static void virNetServerClientDispatchRead(virNetServerClientPtr client)
/* Send off to for normal dispatch to workers */ /* Send off to for normal dispatch to workers */
if (msg) { if (msg) {
if (!client->dispatchFunc || if (!client->dispatchFunc) {
client->dispatchFunc(client, msg, client->dispatchOpaque) < 0) {
virNetMessageFree(msg); virNetMessageFree(msg);
client->wantClose = true; client->wantClose = true;
return; } else {
client->dispatchFunc(client, msg, client->dispatchOpaque);
} }
} }

View File

@ -36,9 +36,12 @@ typedef virNetServer *virNetServerPtr;
typedef struct _virNetServerClient virNetServerClient; typedef struct _virNetServerClient virNetServerClient;
typedef virNetServerClient *virNetServerClientPtr; typedef virNetServerClient *virNetServerClientPtr;
typedef int (*virNetServerClientDispatchFunc)(virNetServerClientPtr client, /* This function owns the "msg" pointer it is passed and
virNetMessagePtr msg, * must arrange for virNetMessageFree to be called on it
void *opaque); */
typedef void (*virNetServerClientDispatchFunc)(virNetServerClientPtr client,
virNetMessagePtr msg,
void *opaque);
typedef int (*virNetServerClientFilterFunc)(virNetServerClientPtr client, typedef int (*virNetServerClientFilterFunc)(virNetServerClientPtr client,
virNetMessagePtr msg, virNetMessagePtr msg,