From de85285f3e15a779adef18b874cbfea99fd2bcb2 Mon Sep 17 00:00:00 2001 From: Laurent Ghigonis Date: Sun, 2 Dec 2012 15:35:31 +0100 Subject: add functions to flush send buffers --- libglouglou/libglouglou.c | 18 ++++++++++++++++++ libglouglou/libglouglou.h | 2 ++ libglouglou/sendbuf.c | 23 ++++++++++++----------- libglouglou/sendbuf.h | 1 + 4 files changed, 33 insertions(+), 11 deletions(-) (limited to 'libglouglou') diff --git a/libglouglou/libglouglou.c b/libglouglou/libglouglou.c index 090e774..0b0ab48 100644 --- a/libglouglou/libglouglou.c +++ b/libglouglou/libglouglou.c @@ -145,6 +145,18 @@ gg_server_send(struct gg_server *srv, struct gg_packet *pkt, struct gg_user *usr return res; } +void +gg_server_send_flush(struct gg_server *srv, struct gg_user *usr) +{ + struct gg_user *u; + + if (usr) + sendbuf_flush(usr->sbuf); + else + LIST_FOREACH(u, &srv->user_list, entry) + sendbuf_flush(u->sbuf); +} + /* * Server - private */ @@ -369,6 +381,12 @@ gg_client_send(struct gg_client *cli, struct gg_packet *pkt) return pkt_encode(pkt, newpkt); } +void +gg_client_send_flush(struct gg_client *cli) +{ + sendbuf_flush(cli->sbuf); +} + /* * Client - private */ diff --git a/libglouglou/libglouglou.h b/libglouglou/libglouglou.h index 3fdd2ae..473c7a2 100644 --- a/libglouglou/libglouglou.h +++ b/libglouglou/libglouglou.h @@ -162,6 +162,7 @@ struct gg_server *gg_server_start(struct event_base *, char *, int, void *); void gg_server_stop(struct gg_server *); int gg_server_send(struct gg_server *, struct gg_packet *, struct gg_user *); +void gg_server_send_flush(struct gg_server *, struct gg_user *); struct gg_client *gg_client_connect(struct event_base *, char *, int, int (*handle_conn)(struct gg_client *), @@ -169,6 +170,7 @@ struct gg_client *gg_client_connect(struct event_base *, char *, int, void *); void gg_client_disconnect(struct gg_client *); int gg_client_send(struct gg_client *, struct gg_packet *); +void gg_client_send_flush(struct gg_client *); int gg_verbosity_get(void); void gg_verbosity_set(int); diff --git a/libglouglou/sendbuf.c b/libglouglou/sendbuf.c index 15a7d23..4e83294 100644 --- a/libglouglou/sendbuf.c +++ b/libglouglou/sendbuf.c @@ -3,7 +3,6 @@ #include "sendbuf.h" -static int flushbuf(struct sendbuf *); static void cb_timer(evutil_socket_t, short, void *); /* @@ -50,7 +49,7 @@ sendbuf_free(struct sendbuf *sbuf) if (sbuf->ev_timer) event_del(sbuf->ev_timer); if (sbuf->buffer && sbuf->send_func) - flushbuf(sbuf); + sendbuf_flush(sbuf); if (sbuf->buffer) free(sbuf->buffer); free(sbuf); @@ -65,7 +64,7 @@ int sendbuf_append(struct sendbuf *sbuf, void *token, int size) { if (sbuf->buffer_pos + size >= sbuf->buffer_size) - if (flushbuf(sbuf) == -1) + if (sendbuf_flush(sbuf) == -1) return -1; memcpy(sbuf->buffer + sbuf->buffer_pos, token, size); @@ -85,7 +84,7 @@ sendbuf_gettoken(struct sendbuf *sbuf, int size) void *token; if (sbuf->buffer_pos + size >= sbuf->buffer_size) - if (flushbuf(sbuf) == -1) + if (sendbuf_flush(sbuf) == -1) return NULL; token = sbuf->buffer + sbuf->buffer_pos; @@ -95,16 +94,13 @@ sendbuf_gettoken(struct sendbuf *sbuf, int size) } /* - * Private - */ - -/* + * Send buffer immediatly * Note that you can still add data to the buffer even if flushing is in * progress * returns 0 on success or -1 on error */ -static int -flushbuf(struct sendbuf *sbuf) +int +sendbuf_flush(struct sendbuf *sbuf) { int tosend, sent; @@ -130,12 +126,17 @@ flushbuf(struct sendbuf *sbuf) return 0; } + +/* + * Private + */ + static void cb_timer(evutil_socket_t fd, short what, void *arg) { struct sendbuf *sbuf; sbuf = arg; - flushbuf(sbuf); + sendbuf_flush(sbuf); evtimer_add(sbuf->ev_timer, &sbuf->ev_timer_tv); } diff --git a/libglouglou/sendbuf.h b/libglouglou/sendbuf.h index 0290202..6d163d4 100644 --- a/libglouglou/sendbuf.h +++ b/libglouglou/sendbuf.h @@ -21,3 +21,4 @@ struct sendbuf *sendbuf_new(struct event_base *, int, int, void sendbuf_free(struct sendbuf *); int sendbuf_append(struct sendbuf *, void *, int); void *sendbuf_gettoken(struct sendbuf *, int); +int sendbuf_flush(struct sendbuf *); -- cgit v1.2.3-59-g8ed1b