From: Sebastian Harl Date: Thu, 6 Feb 2014 08:19:31 +0000 (+0100) Subject: client: Added an EOF flag to the client object. X-Git-Tag: sysdb-0.1.0~218 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=bcc0deb7ce9e8d135afe01ab0f09aab0c378d47c;p=sysdb.git client: Added an EOF flag to the client object. In a lot of cases, a client is passed through a couple of functions in order to handle I/O. Adding the flag will make it easier to access the information in the right location without having to worry about 0 return codes (which mean different things in some cases anyway, e.g. in sdb_client_recv() which returns zero if an empty message (header only) has been received). --- diff --git a/src/client/sock.c b/src/client/sock.c index 59c7787..042dd63 100644 --- a/src/client/sock.c +++ b/src/client/sock.c @@ -52,6 +52,7 @@ struct sdb_client { char *address; int fd; + _Bool eof; }; /* @@ -104,6 +105,7 @@ sdb_client_create(const char *address) } memset(client, 0, sizeof(*client)); client->fd = -1; + client->eof = 1; client->address = strdup(address); if (! client->address) { @@ -154,6 +156,7 @@ sdb_client_connect(sdb_client_t *client, const char *username) if (client->fd < 0) return -1; + client->eof = 0; /* XXX */ if (! username) @@ -206,6 +209,7 @@ sdb_client_close(sdb_client_t *client) close(client->fd); client->fd = -1; + client->eof = 1; } /* sdb_client_close */ ssize_t @@ -251,8 +255,10 @@ sdb_client_recv(sdb_client_t *client, continue; return status; } - else if (! status) /* EOF */ + else if (! status) { + client->eof = 1; break; + } total += (size_t)status; @@ -290,5 +296,13 @@ sdb_client_recv(sdb_client_t *client, return (ssize_t)total; } /* sdb_client_recv */ +_Bool +sdb_client_eof(sdb_client_t *client) +{ + if ((! client) || (client->fd < 0)) + return 1; + return client->eof; +} /* sdb_client_eof */ + /* vim: set tw=78 sw=4 ts=4 noexpandtab : */ diff --git a/src/include/client/sock.h b/src/include/client/sock.h index 39e7622..4fd54ba 100644 --- a/src/include/client/sock.h +++ b/src/include/client/sock.h @@ -121,6 +121,14 @@ ssize_t sdb_client_recv(sdb_client_t *client, uint32_t *code, sdb_strbuf_t *buf); +/* + * sdb_client_eof: + * Returns true if end of file on the client connection was reached, that is, + * if the remote side closed the connection. + */ +_Bool +sdb_client_eof(sdb_client_t *client); + #ifdef __cplusplus } /* extern "C" */ #endif