Code

client: Added an EOF flag to the client object.
authorSebastian Harl <sh@tokkee.org>
Thu, 6 Feb 2014 08:19:31 +0000 (09:19 +0100)
committerSebastian Harl <sh@tokkee.org>
Thu, 6 Feb 2014 08:19:31 +0000 (09:19 +0100)
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).

src/client/sock.c
src/include/client/sock.h

index 59c77873e08411b22a9c1e5a6a857a8e269210f5..042dd63bd99a353764b8176289de65f5276730f8 100644 (file)
@@ -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 : */
 
index 39e7622f3b1959fd1fbb1de11301adc25a1b842b..4fd54baa1c5fa7ac99c3c0f46bbf2e2d8f8f2cd6 100644 (file)
@@ -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