diff options
Diffstat (limited to 'src/buffer.c')
-rw-r--r-- | src/buffer.c | 68 |
1 files changed, 24 insertions, 44 deletions
diff --git a/src/buffer.c b/src/buffer.c index 60e42ae..aa50db0 100644 --- a/src/buffer.c +++ b/src/buffer.c | |||
@@ -33,8 +33,7 @@ static int sendraw(p_buf buf, const char *data, size_t count, size_t *sent); | |||
33 | /*-------------------------------------------------------------------------*\ | 33 | /*-------------------------------------------------------------------------*\ |
34 | * Initializes module | 34 | * Initializes module |
35 | \*-------------------------------------------------------------------------*/ | 35 | \*-------------------------------------------------------------------------*/ |
36 | int buf_open(lua_State *L) | 36 | int buf_open(lua_State *L) { |
37 | { | ||
38 | (void) L; | 37 | (void) L; |
39 | return 0; | 38 | return 0; |
40 | } | 39 | } |
@@ -42,8 +41,7 @@ int buf_open(lua_State *L) | |||
42 | /*-------------------------------------------------------------------------*\ | 41 | /*-------------------------------------------------------------------------*\ |
43 | * Initializes C structure | 42 | * Initializes C structure |
44 | \*-------------------------------------------------------------------------*/ | 43 | \*-------------------------------------------------------------------------*/ |
45 | void buf_init(p_buf buf, p_io io, p_tm tm) | 44 | void buf_init(p_buf buf, p_io io, p_tm tm) { |
46 | { | ||
47 | buf->first = buf->last = 0; | 45 | buf->first = buf->last = 0; |
48 | buf->io = io; | 46 | buf->io = io; |
49 | buf->tm = tm; | 47 | buf->tm = tm; |
@@ -52,13 +50,11 @@ void buf_init(p_buf buf, p_io io, p_tm tm) | |||
52 | /*-------------------------------------------------------------------------*\ | 50 | /*-------------------------------------------------------------------------*\ |
53 | * object:send() interface | 51 | * object:send() interface |
54 | \*-------------------------------------------------------------------------*/ | 52 | \*-------------------------------------------------------------------------*/ |
55 | int buf_meth_send(lua_State *L, p_buf buf) | 53 | int buf_meth_send(lua_State *L, p_buf buf) { |
56 | { | ||
57 | int top = lua_gettop(L); | 54 | int top = lua_gettop(L); |
58 | size_t total = 0; | 55 | size_t total = 0; |
59 | int arg, err = IO_DONE; | 56 | int arg, err = IO_DONE; |
60 | p_tm tm = buf->tm; | 57 | p_tm tm = tm_markstart(buf->tm); |
61 | tm_markstart(tm); | ||
62 | for (arg = 2; arg <= top; arg++) { /* first arg is socket object */ | 58 | for (arg = 2; arg <= top; arg++) { /* first arg is socket object */ |
63 | size_t sent, count; | 59 | size_t sent, count; |
64 | const char *data = luaL_optlstring(L, arg, NULL, &count); | 60 | const char *data = luaL_optlstring(L, arg, NULL, &count); |
@@ -69,7 +65,7 @@ int buf_meth_send(lua_State *L, p_buf buf) | |||
69 | /* check if there was an error */ | 65 | /* check if there was an error */ |
70 | if (err != IO_DONE) { | 66 | if (err != IO_DONE) { |
71 | lua_pushnil(L); | 67 | lua_pushnil(L); |
72 | io_pusherror(L, err); | 68 | io_pusherror(L, buf->io, err); |
73 | lua_pushnumber(L, total); | 69 | lua_pushnumber(L, total); |
74 | } else { | 70 | } else { |
75 | lua_pushnumber(L, total); | 71 | lua_pushnumber(L, total); |
@@ -78,7 +74,7 @@ int buf_meth_send(lua_State *L, p_buf buf) | |||
78 | } | 74 | } |
79 | #ifdef LUASOCKET_DEBUG | 75 | #ifdef LUASOCKET_DEBUG |
80 | /* push time elapsed during operation as the last return value */ | 76 | /* push time elapsed during operation as the last return value */ |
81 | lua_pushnumber(L, (tm_gettime() - tm_getstart(tm))/1000.0); | 77 | lua_pushnumber(L, tm_gettime() - tm_getstart(tm)); |
82 | #endif | 78 | #endif |
83 | return lua_gettop(L) - top; | 79 | return lua_gettop(L) - top; |
84 | } | 80 | } |
@@ -86,13 +82,11 @@ int buf_meth_send(lua_State *L, p_buf buf) | |||
86 | /*-------------------------------------------------------------------------*\ | 82 | /*-------------------------------------------------------------------------*\ |
87 | * object:receive() interface | 83 | * object:receive() interface |
88 | \*-------------------------------------------------------------------------*/ | 84 | \*-------------------------------------------------------------------------*/ |
89 | int buf_meth_receive(lua_State *L, p_buf buf) | 85 | int buf_meth_receive(lua_State *L, p_buf buf) { |
90 | { | ||
91 | int err = IO_DONE, top = lua_gettop(L); | 86 | int err = IO_DONE, top = lua_gettop(L); |
92 | p_tm tm = buf->tm; | 87 | p_tm tm = tm_markstart(buf->tm); |
93 | luaL_Buffer b; | 88 | luaL_Buffer b; |
94 | luaL_buffinit(L, &b); | 89 | luaL_buffinit(L, &b); |
95 | tm_markstart(tm); | ||
96 | /* receive all patterns */ | 90 | /* receive all patterns */ |
97 | if (!lua_isnumber(L, 2)) { | 91 | if (!lua_isnumber(L, 2)) { |
98 | static const char *patternnames[] = {"*l", "*a", NULL}; | 92 | static const char *patternnames[] = {"*l", "*a", NULL}; |
@@ -107,7 +101,7 @@ int buf_meth_receive(lua_State *L, p_buf buf) | |||
107 | /* check if there was an error */ | 101 | /* check if there was an error */ |
108 | if (err != IO_DONE) { | 102 | if (err != IO_DONE) { |
109 | luaL_pushresult(&b); | 103 | luaL_pushresult(&b); |
110 | io_pusherror(L, err); | 104 | io_pusherror(L, buf->io, err); |
111 | lua_pushvalue(L, -2); | 105 | lua_pushvalue(L, -2); |
112 | lua_pushnil(L); | 106 | lua_pushnil(L); |
113 | lua_replace(L, -4); | 107 | lua_replace(L, -4); |
@@ -118,7 +112,7 @@ int buf_meth_receive(lua_State *L, p_buf buf) | |||
118 | } | 112 | } |
119 | #ifdef LUASOCKET_DEBUG | 113 | #ifdef LUASOCKET_DEBUG |
120 | /* push time elapsed during operation as the last return value */ | 114 | /* push time elapsed during operation as the last return value */ |
121 | lua_pushnumber(L, (tm_gettime() - tm_getstart(tm))/1000.0); | 115 | lua_pushnumber(L, tm_gettime() - tm_getstart(tm)); |
122 | #endif | 116 | #endif |
123 | return lua_gettop(L) - top; | 117 | return lua_gettop(L) - top; |
124 | } | 118 | } |
@@ -126,8 +120,7 @@ int buf_meth_receive(lua_State *L, p_buf buf) | |||
126 | /*-------------------------------------------------------------------------*\ | 120 | /*-------------------------------------------------------------------------*\ |
127 | * Determines if there is any data in the read buffer | 121 | * Determines if there is any data in the read buffer |
128 | \*-------------------------------------------------------------------------*/ | 122 | \*-------------------------------------------------------------------------*/ |
129 | int buf_isempty(p_buf buf) | 123 | int buf_isempty(p_buf buf) { |
130 | { | ||
131 | return buf->first >= buf->last; | 124 | return buf->first >= buf->last; |
132 | } | 125 | } |
133 | 126 | ||
@@ -137,16 +130,14 @@ int buf_isempty(p_buf buf) | |||
137 | /*-------------------------------------------------------------------------*\ | 130 | /*-------------------------------------------------------------------------*\ |
138 | * Sends a block of data (unbuffered) | 131 | * Sends a block of data (unbuffered) |
139 | \*-------------------------------------------------------------------------*/ | 132 | \*-------------------------------------------------------------------------*/ |
140 | static | 133 | static int sendraw(p_buf buf, const char *data, size_t count, size_t *sent) { |
141 | int sendraw(p_buf buf, const char *data, size_t count, size_t *sent) | ||
142 | { | ||
143 | p_io io = buf->io; | 134 | p_io io = buf->io; |
144 | p_tm tm = buf->tm; | 135 | p_tm tm = buf->tm; |
145 | size_t total = 0; | 136 | size_t total = 0; |
146 | int err = IO_DONE; | 137 | int err = IO_DONE; |
147 | while (total < count && (err == IO_DONE || err == IO_RETRY)) { | 138 | while (total < count && err == IO_DONE) { |
148 | size_t done; | 139 | size_t done; |
149 | err = io->send(io->ctx, data+total, count-total, &done, tm_get(tm)); | 140 | err = io->send(io->ctx, data+total, count-total, &done, tm); |
150 | total += done; | 141 | total += done; |
151 | } | 142 | } |
152 | *sent = total; | 143 | *sent = total; |
@@ -156,12 +147,10 @@ int sendraw(p_buf buf, const char *data, size_t count, size_t *sent) | |||
156 | /*-------------------------------------------------------------------------*\ | 147 | /*-------------------------------------------------------------------------*\ |
157 | * Reads a fixed number of bytes (buffered) | 148 | * Reads a fixed number of bytes (buffered) |
158 | \*-------------------------------------------------------------------------*/ | 149 | \*-------------------------------------------------------------------------*/ |
159 | static | 150 | static int recvraw(p_buf buf, size_t wanted, luaL_Buffer *b) { |
160 | int recvraw(p_buf buf, size_t wanted, luaL_Buffer *b) | 151 | int err = IO_DONE; |
161 | { | ||
162 | int err = IO_DONE; | ||
163 | size_t total = 0; | 152 | size_t total = 0; |
164 | while (total < wanted && (err == IO_DONE || err == IO_RETRY)) { | 153 | while (total < wanted && err == IO_DONE) { |
165 | size_t count; const char *data; | 154 | size_t count; const char *data; |
166 | err = buf_get(buf, &data, &count); | 155 | err = buf_get(buf, &data, &count); |
167 | count = MIN(count, wanted - total); | 156 | count = MIN(count, wanted - total); |
@@ -175,11 +164,9 @@ int recvraw(p_buf buf, size_t wanted, luaL_Buffer *b) | |||
175 | /*-------------------------------------------------------------------------*\ | 164 | /*-------------------------------------------------------------------------*\ |
176 | * Reads everything until the connection is closed (buffered) | 165 | * Reads everything until the connection is closed (buffered) |
177 | \*-------------------------------------------------------------------------*/ | 166 | \*-------------------------------------------------------------------------*/ |
178 | static | 167 | static int recvall(p_buf buf, luaL_Buffer *b) { |
179 | int recvall(p_buf buf, luaL_Buffer *b) | ||
180 | { | ||
181 | int err = IO_DONE; | 168 | int err = IO_DONE; |
182 | while (err == IO_DONE || err == IO_RETRY) { | 169 | while (err == IO_DONE) { |
183 | const char *data; size_t count; | 170 | const char *data; size_t count; |
184 | err = buf_get(buf, &data, &count); | 171 | err = buf_get(buf, &data, &count); |
185 | luaL_addlstring(b, data, count); | 172 | luaL_addlstring(b, data, count); |
@@ -193,11 +180,9 @@ int recvall(p_buf buf, luaL_Buffer *b) | |||
193 | * Reads a line terminated by a CR LF pair or just by a LF. The CR and LF | 180 | * Reads a line terminated by a CR LF pair or just by a LF. The CR and LF |
194 | * are not returned by the function and are discarded from the buffer | 181 | * are not returned by the function and are discarded from the buffer |
195 | \*-------------------------------------------------------------------------*/ | 182 | \*-------------------------------------------------------------------------*/ |
196 | static | 183 | static int recvline(p_buf buf, luaL_Buffer *b) { |
197 | int recvline(p_buf buf, luaL_Buffer *b) | ||
198 | { | ||
199 | int err = IO_DONE; | 184 | int err = IO_DONE; |
200 | while (err == IO_DONE || err == IO_RETRY) { | 185 | while (err == IO_DONE) { |
201 | size_t count, pos; const char *data; | 186 | size_t count, pos; const char *data; |
202 | err = buf_get(buf, &data, &count); | 187 | err = buf_get(buf, &data, &count); |
203 | pos = 0; | 188 | pos = 0; |
@@ -219,9 +204,7 @@ int recvline(p_buf buf, luaL_Buffer *b) | |||
219 | * Skips a given number of bytes from read buffer. No data is read from the | 204 | * Skips a given number of bytes from read buffer. No data is read from the |
220 | * transport layer | 205 | * transport layer |
221 | \*-------------------------------------------------------------------------*/ | 206 | \*-------------------------------------------------------------------------*/ |
222 | static | 207 | static void buf_skip(p_buf buf, size_t count) { |
223 | void buf_skip(p_buf buf, size_t count) | ||
224 | { | ||
225 | buf->first += count; | 208 | buf->first += count; |
226 | if (buf_isempty(buf)) | 209 | if (buf_isempty(buf)) |
227 | buf->first = buf->last = 0; | 210 | buf->first = buf->last = 0; |
@@ -231,15 +214,13 @@ void buf_skip(p_buf buf, size_t count) | |||
231 | * Return any data available in buffer, or get more data from transport layer | 214 | * Return any data available in buffer, or get more data from transport layer |
232 | * if buffer is empty | 215 | * if buffer is empty |
233 | \*-------------------------------------------------------------------------*/ | 216 | \*-------------------------------------------------------------------------*/ |
234 | static | 217 | static int buf_get(p_buf buf, const char **data, size_t *count) { |
235 | int buf_get(p_buf buf, const char **data, size_t *count) | ||
236 | { | ||
237 | int err = IO_DONE; | 218 | int err = IO_DONE; |
238 | p_io io = buf->io; | 219 | p_io io = buf->io; |
239 | p_tm tm = buf->tm; | 220 | p_tm tm = buf->tm; |
240 | if (buf_isempty(buf)) { | 221 | if (buf_isempty(buf)) { |
241 | size_t got; | 222 | size_t got; |
242 | err = io->recv(io->ctx, buf->data, BUF_SIZE, &got, tm_get(tm)); | 223 | err = io->recv(io->ctx, buf->data, BUF_SIZE, &got, tm); |
243 | buf->first = 0; | 224 | buf->first = 0; |
244 | buf->last = got; | 225 | buf->last = got; |
245 | } | 226 | } |
@@ -247,4 +228,3 @@ int buf_get(p_buf buf, const char **data, size_t *count) | |||
247 | *data = buf->data + buf->first; | 228 | *data = buf->data + buf->first; |
248 | return err; | 229 | return err; |
249 | } | 230 | } |
250 | |||