diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/buffer.c | 14 | ||||
| -rw-r--r-- | src/buffer.h | 3 | ||||
| -rw-r--r-- | src/tcp.c | 13 |
3 files changed, 26 insertions, 4 deletions
diff --git a/src/buffer.c b/src/buffer.c index aa50db0..baa248a 100644 --- a/src/buffer.c +++ b/src/buffer.c | |||
| @@ -45,6 +45,18 @@ void buf_init(p_buf buf, p_io io, p_tm tm) { | |||
| 45 | buf->first = buf->last = 0; | 45 | buf->first = buf->last = 0; |
| 46 | buf->io = io; | 46 | buf->io = io; |
| 47 | buf->tm = tm; | 47 | buf->tm = tm; |
| 48 | buf->received = buf->sent = 0; | ||
| 49 | buf->birthday = tm_gettime(); | ||
| 50 | } | ||
| 51 | |||
| 52 | /*-------------------------------------------------------------------------*\ | ||
| 53 | * object:getstats() interface | ||
| 54 | \*-------------------------------------------------------------------------*/ | ||
| 55 | int buf_meth_getstats(lua_State *L, p_buf buf) { | ||
| 56 | lua_pushnumber(L, buf->received); | ||
| 57 | lua_pushnumber(L, buf->sent); | ||
| 58 | lua_pushnumber(L, tm_gettime() - buf->birthday); | ||
| 59 | return 3; | ||
| 48 | } | 60 | } |
| 49 | 61 | ||
| 50 | /*-------------------------------------------------------------------------*\ | 62 | /*-------------------------------------------------------------------------*\ |
| @@ -141,6 +153,7 @@ static int sendraw(p_buf buf, const char *data, size_t count, size_t *sent) { | |||
| 141 | total += done; | 153 | total += done; |
| 142 | } | 154 | } |
| 143 | *sent = total; | 155 | *sent = total; |
| 156 | buf->sent += total; | ||
| 144 | return err; | 157 | return err; |
| 145 | } | 158 | } |
| 146 | 159 | ||
| @@ -205,6 +218,7 @@ static int recvline(p_buf buf, luaL_Buffer *b) { | |||
| 205 | * transport layer | 218 | * transport layer |
| 206 | \*-------------------------------------------------------------------------*/ | 219 | \*-------------------------------------------------------------------------*/ |
| 207 | static void buf_skip(p_buf buf, size_t count) { | 220 | static void buf_skip(p_buf buf, size_t count) { |
| 221 | buf->received += count; | ||
| 208 | buf->first += count; | 222 | buf->first += count; |
| 209 | if (buf_isempty(buf)) | 223 | if (buf_isempty(buf)) |
| 210 | buf->first = buf->last = 0; | 224 | buf->first = buf->last = 0; |
diff --git a/src/buffer.h b/src/buffer.h index 4b7563f..3ea2648 100644 --- a/src/buffer.h +++ b/src/buffer.h | |||
| @@ -27,6 +27,8 @@ | |||
| 27 | 27 | ||
| 28 | /* buffer control structure */ | 28 | /* buffer control structure */ |
| 29 | typedef struct t_buf_ { | 29 | typedef struct t_buf_ { |
| 30 | double birthday; /* throttle support info: creation time, */ | ||
| 31 | int sent, received; /* bytes sent, and bytes received */ | ||
| 30 | p_io io; /* IO driver used for this buffer */ | 32 | p_io io; /* IO driver used for this buffer */ |
| 31 | p_tm tm; /* timeout management for this buffer */ | 33 | p_tm tm; /* timeout management for this buffer */ |
| 32 | size_t first, last; /* index of first and last bytes of stored data */ | 34 | size_t first, last; /* index of first and last bytes of stored data */ |
| @@ -38,6 +40,7 @@ int buf_open(lua_State *L); | |||
| 38 | void buf_init(p_buf buf, p_io io, p_tm tm); | 40 | void buf_init(p_buf buf, p_io io, p_tm tm); |
| 39 | int buf_meth_send(lua_State *L, p_buf buf); | 41 | int buf_meth_send(lua_State *L, p_buf buf); |
| 40 | int buf_meth_receive(lua_State *L, p_buf buf); | 42 | int buf_meth_receive(lua_State *L, p_buf buf); |
| 43 | int buf_meth_getstats(lua_State *L, p_buf buf); | ||
| 41 | int buf_isempty(p_buf buf); | 44 | int buf_isempty(p_buf buf); |
| 42 | 45 | ||
| 43 | #endif /* BUF_H */ | 46 | #endif /* BUF_H */ |
| @@ -23,6 +23,7 @@ static int meth_connect(lua_State *L); | |||
| 23 | static int meth_listen(lua_State *L); | 23 | static int meth_listen(lua_State *L); |
| 24 | static int meth_bind(lua_State *L); | 24 | static int meth_bind(lua_State *L); |
| 25 | static int meth_send(lua_State *L); | 25 | static int meth_send(lua_State *L); |
| 26 | static int meth_getstats(lua_State *L); | ||
| 26 | static int meth_getsockname(lua_State *L); | 27 | static int meth_getsockname(lua_State *L); |
| 27 | static int meth_getpeername(lua_State *L); | 28 | static int meth_getpeername(lua_State *L); |
| 28 | static int meth_shutdown(lua_State *L); | 29 | static int meth_shutdown(lua_State *L); |
| @@ -47,6 +48,7 @@ static luaL_reg tcp[] = { | |||
| 47 | {"getfd", meth_getfd}, | 48 | {"getfd", meth_getfd}, |
| 48 | {"getpeername", meth_getpeername}, | 49 | {"getpeername", meth_getpeername}, |
| 49 | {"getsockname", meth_getsockname}, | 50 | {"getsockname", meth_getsockname}, |
| 51 | {"getstats", meth_getstats}, | ||
| 50 | {"listen", meth_listen}, | 52 | {"listen", meth_listen}, |
| 51 | {"receive", meth_receive}, | 53 | {"receive", meth_receive}, |
| 52 | {"send", meth_send}, | 54 | {"send", meth_send}, |
| @@ -100,18 +102,21 @@ int tcp_open(lua_State *L) | |||
| 100 | /*-------------------------------------------------------------------------*\ | 102 | /*-------------------------------------------------------------------------*\ |
| 101 | * Just call buffered IO methods | 103 | * Just call buffered IO methods |
| 102 | \*-------------------------------------------------------------------------*/ | 104 | \*-------------------------------------------------------------------------*/ |
| 103 | static int meth_send(lua_State *L) | 105 | static int meth_send(lua_State *L) { |
| 104 | { | ||
| 105 | p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{client}", 1); | 106 | p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{client}", 1); |
| 106 | return buf_meth_send(L, &tcp->buf); | 107 | return buf_meth_send(L, &tcp->buf); |
| 107 | } | 108 | } |
| 108 | 109 | ||
| 109 | static int meth_receive(lua_State *L) | 110 | static int meth_receive(lua_State *L) { |
| 110 | { | ||
| 111 | p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{client}", 1); | 111 | p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{client}", 1); |
| 112 | return buf_meth_receive(L, &tcp->buf); | 112 | return buf_meth_receive(L, &tcp->buf); |
| 113 | } | 113 | } |
| 114 | 114 | ||
| 115 | static int meth_getstats(lua_State *L) { | ||
| 116 | p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); | ||
| 117 | return buf_meth_getstats(L, &tcp->buf); | ||
| 118 | } | ||
| 119 | |||
| 115 | /*-------------------------------------------------------------------------*\ | 120 | /*-------------------------------------------------------------------------*\ |
| 116 | * Just call option handler | 121 | * Just call option handler |
| 117 | \*-------------------------------------------------------------------------*/ | 122 | \*-------------------------------------------------------------------------*/ |
