aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLaurent Ghigonis <laurent@p1sec.com>2012-12-01 16:36:37 +0100
committerLaurent Ghigonis <laurent@p1sec.com>2012-12-01 16:36:37 +0100
commitc112e5013454b48c9f05b3ac2153656cdcf549a4 (patch)
treeee34f55863ae900c045f42c9540153f43b7b37fd
parentwork in progress on implementing a send buffer (diff)
downloadglouglou-c112e5013454b48c9f05b3ac2153656cdcf549a4.tar.xz
glouglou-c112e5013454b48c9f05b3ac2153656cdcf549a4.zip
work in progress on sendbuf
-rw-r--r--libglouglou/libglouglou.c45
-rw-r--r--libglouglou/libglouglou.h2
-rw-r--r--libglouglou/sendbuf.h20
3 files changed, 41 insertions, 26 deletions
diff --git a/libglouglou/libglouglou.c b/libglouglou/libglouglou.c
index c9c0df8..85993ce 100644
--- a/libglouglou/libglouglou.c
+++ b/libglouglou/libglouglou.c
@@ -28,10 +28,11 @@
int server_send(struct gg_server *, struct gg_user *,
void *, int);
void cb_srv_receive(evutil_socket_t, short, void *);
-int user_send(struct gg_user *, void *, int);
struct gg_user *user_add(struct gg_server *, struct sockaddr_in *);
void user_del(struct gg_server *, struct gg_user *);
struct gg_user * user_find(struct gg_server *, struct sockaddr_in *);
+int user_send(struct gg_user *, void *, int);
+int cb_usr_send(void *, int, void *);
int client_send(struct gg_client *, void *, int);
void cb_cli_receive(evutil_socket_t, short, void *);
void cb_cli_timer(evutil_socket_t, short, void *);
@@ -59,7 +60,6 @@ gg_server_start(struct event_base *ev_base, char *ip, int port,
struct gg_server *srv;
struct sockaddr_in sock_addr;
struct event *ev;
- struct sendbuf *sbuf;
int s;
int sock_on = 1;
@@ -71,12 +71,6 @@ gg_server_start(struct event_base *ev_base, char *ip, int port,
srv->handle_packet = handle_packet;
srv->usrdata = usrdata;
- sbuf = sendbuf_init(ev_base, PACKET_SNDBUF_MAX * 2, PACKET_SNDBUF_MAX, 200,
- cb_srv_send, srv);
- if (!sbuf)
- goto err;
- srv->sbuf = sbuf;
-
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s < 0)
goto err;
@@ -99,6 +93,12 @@ gg_server_start(struct event_base *ev_base, char *ip, int port,
event_add(ev, NULL);
srv->ev = ev;
+ sbuf = sendbuf_init(srv->ev_base, PACKET_SNDBUF_MAX * 2, PACKET_SNDBUF_MAX,
+ 200);
+ if (!sbuf)
+ goto err;
+ srv->sbuf = sbuf;
+
return srv;
err:
@@ -108,23 +108,16 @@ err:
}
int
-cb_srv_send(void *data, int size, void *usrdata)
-{
- struct gg_server *srv;
-
- srv = usrdata;
-}
-
-int
gg_server_send(struct gg_server *srv, struct gg_packet *pkt, struct gg_user *usr)
{
static struct gg_packet *newpkt;
int size;
// XXX IN PROGRESS get size before
- newpkt = sendbuf_gettoken(srv->sndbuf, size);
-
- pkt_encode(pkt, &size, newpkt);
+ size = pkt_getsize(pkt);
+ newpkt = sendbuf_token_get(srv->sndbuf, size);
+ pkt_encode(pkt, newpkt);
+ sendbuf_token_ready(srv->sndbuf, newpkt);
if (!newpkt)
return -1;
@@ -210,12 +203,16 @@ struct gg_user *
user_add(struct gg_server *srv, struct sockaddr_in *remote)
{
struct gg_user *usr;
+ struct sendbuf *sbuf;
usr = xcalloc(1, sizeof(struct gg_user));
usr->id = srv->user_id_count;
srv->user_id_count++;
usr->sock = srv->sock;
addrcpy(&usr->addr, remote);
+
+ sendbuf_queue_add(srv->sbuf, cb_usr_send, usr);
+
LIST_INSERT_HEAD(&srv->user_list, usr, entry);
verbose("Add user %d !", usr->id);
@@ -256,6 +253,16 @@ user_send(struct gg_user *usr, void *data, int size)
return 0;
}
+int
+cb_usr_send(void *data, int size, void *usrdata)
+{
+ struct gg_user *usr;
+
+ usr = usrdata;
+ user_send(usr, NULL, data, size);
+}
+
+
/*
* Client
*/
diff --git a/libglouglou/libglouglou.h b/libglouglou/libglouglou.h
index 63fa93a..4cd17f2 100644
--- a/libglouglou/libglouglou.h
+++ b/libglouglou/libglouglou.h
@@ -118,7 +118,7 @@ struct gg_user {
int id;
int sock;
struct sockaddr_in addr;
- char *buf[16384];
+ struct sendbuf *sbuf;
};
struct gg_server {
diff --git a/libglouglou/sendbuf.h b/libglouglou/sendbuf.h
index 90dc4bc..340e1c7 100644
--- a/libglouglou/sendbuf.h
+++ b/libglouglou/sendbuf.h
@@ -1,4 +1,5 @@
#include <event.h>
+#include <sys/queue.h>
struct sendbuf {
struct event_base *ev_base;
@@ -6,16 +7,23 @@ struct sendbuf {
struct timeval ev_timer_tv;
int sndbuf_max;
int msec_max;
- int (*send_func)(void *, int, void *);
- void *usrdata;
+};
+
+struct sendbuf_queue {
void *buffer;
int buffer_size;
int buffer_pos;
+ int (*send_func)(void *, int, void *);
+ void *usrdata;
int flushing;
int flushed_len;
-};
+}
-struct sendbuf *sendbuf_init(struct event_base *, int, int,
- int (*send_func)(void *, int, void *), void *);
+struct sendbuf *sendbuf_init(struct event_base *, int, int, int);
void sendbuf_shutdown(struct sendbuf *);
-void *sendbuf_gettoken(struct sendbuf *, int);
+void sendbuf_queue_add(struct sendbuf *,
+ int (*send_func)(void *, int, void *),
+ void *);
+void *sendbuf_token_get(struct sendbuf *, int);
+void sendbuf_token_ready(struct sendbuf *, void *);
+