diff options
author | June McEnroe <june@causal.agency> | 2019-10-25 03:04:45 -0400 |
---|---|---|
committer | June McEnroe <june@causal.agency> | 2019-10-25 03:04:45 -0400 |
commit | 243a5f930dce0cddd54d7d927774f7ad82b3d88e (patch) | |
tree | f2730a84c336ac408af4fc2a9da795daf6c5234b | |
parent | Implement client reading from ring buffer (diff) | |
download | pounce-243a5f930dce0cddd54d7d927774f7ad82b3d88e.tar.gz pounce-243a5f930dce0cddd54d7d927774f7ad82b3d88e.zip |
Use produce/consume words for ring buffer
To disambiguate clientRecv and clientRead, say clientConsume.
-rw-r--r-- | bounce.c | 2 | ||||
-rw-r--r-- | bounce.h | 10 | ||||
-rw-r--r-- | client.c | 10 | ||||
-rw-r--r-- | ring.c | 22 | ||||
-rw-r--r-- | server.c | 2 |
5 files changed, 23 insertions, 23 deletions
diff --git a/bounce.c b/bounce.c index c771bbe..1907584 100644 --- a/bounce.c +++ b/bounce.c @@ -149,7 +149,7 @@ int main(int argc, char *argv[]) { short revents = event.fds[i].revents; struct Client *client = event.clients[i]; if (revents & POLLIN) clientRecv(client); - if (revents & POLLOUT) clientRead(client); + if (revents & POLLOUT) clientConsume(client); if (clientError(client) || revents & (POLLHUP | POLLERR)) { clientFree(client); close(event.fds[i].fd); diff --git a/bounce.h b/bounce.h index d4ec916..3c95f70 100644 --- a/bounce.h +++ b/bounce.h @@ -53,10 +53,10 @@ static inline struct Message parse(char *line) { return msg; } -void ringWrite(const char *line); -size_t ringReader(const char *name); -size_t ringDiff(size_t reader); -const char *ringRead(time_t *time, size_t reader); +void ringProduce(const char *line); +size_t ringConsumer(const char *name); +size_t ringDiff(size_t consumer); +const char *ringConsume(time_t *time, size_t consumer); void listenConfig(const char *cert, const char *priv); size_t listenBind(int fds[], size_t cap, const char *host, const char *port); @@ -82,7 +82,7 @@ void clientSend(struct Client *client, const char *ptr, size_t len); void clientFormat(struct Client *client, const char *format, ...) __attribute__((format(printf, 2, 3))); size_t clientDiff(const struct Client *client); -void clientRead(struct Client *client); +void clientConsume(struct Client *client); bool stateReady(void); void stateParse(char *line); diff --git a/client.c b/client.c index cdf0009..4ac1ef4 100644 --- a/client.c +++ b/client.c @@ -38,7 +38,7 @@ enum Need { struct Client { bool error; struct tls *tls; - size_t reader; + size_t consumer; enum Need need; bool serverTime; char buf[4096]; @@ -112,7 +112,7 @@ static void handleUser(struct Client *client, struct Message msg) { client->error = true; return; } - client->reader = ringReader(msg.params[0]); + client->consumer = ringConsumer(msg.params[0]); client->need &= ~NeedUser; if (!client->need) stateSync(client); if (client->need == NeedPass) passRequired(client); @@ -231,13 +231,13 @@ void clientRecv(struct Client *client) { size_t clientDiff(const struct Client *client) { if (client->need) return 0; - return ringDiff(client->reader); + return ringDiff(client->consumer); } // TODO: Read several lines based on LOWAT for POLLOUT? -void clientRead(struct Client *client) { +void clientConsume(struct Client *client) { time_t time; - const char *line = ringRead(&time, client->reader); + const char *line = ringConsume(&time, client->consumer); if (!line) return; if (client->serverTime) { char ts[sizeof("YYYY-MM-DDThh:mm:ss.sssZ")]; diff --git a/ring.c b/ring.c index d3b04f6..d66961d 100644 --- a/ring.c +++ b/ring.c @@ -31,7 +31,7 @@ static struct { size_t write; } ring; -void ringWrite(const char *line) { +void ringProduce(const char *line) { size_t i = ring.write++ % RingLen; if (ring.lines[i]) free(ring.lines[i]); ring.times[i] = time(NULL); @@ -45,7 +45,7 @@ static struct { size_t cap, len; } read; -size_t ringReader(const char *name) { +size_t ringConsumer(const char *name) { for (size_t i = 0; i < read.len; ++i) { if (!strcmp(read.names[i], name)) return i; } @@ -64,18 +64,18 @@ size_t ringReader(const char *name) { return read.len++; } -size_t ringDiff(size_t reader) { - assert(reader < read.len); - return ring.write - read.ptrs[reader]; +size_t ringDiff(size_t consumer) { + assert(consumer < read.len); + return ring.write - read.ptrs[consumer]; } -const char *ringRead(time_t *time, size_t reader) { - assert(reader < read.len); - if (!ringDiff(reader)) return NULL; - if (ringDiff(reader) > RingLen) { - read.ptrs[reader] = ring.write - RingLen; +const char *ringConsume(time_t *time, size_t consumer) { + assert(consumer < read.len); + if (!ringDiff(consumer)) return NULL; + if (ringDiff(consumer) > RingLen) { + read.ptrs[consumer] = ring.write - RingLen; } - size_t i = read.ptrs[reader]++ % RingLen; + size_t i = read.ptrs[consumer]++ % RingLen; if (time) *time = ring.times[i]; return ring.lines[i]; } diff --git a/server.c b/server.c index 5bdef6b..00c2950 100644 --- a/server.c +++ b/server.c @@ -185,7 +185,7 @@ void serverRecv(void) { line[1] = 'O'; serverFormat("%s\r\n", line); } else { - if (stateReady()) ringWrite(line); + if (stateReady()) ringProduce(line); stateParse(line); } |