aboutsummaryrefslogtreecommitdiff
path: root/src/buffer.c
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2005-10-07 04:40:59 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2005-10-07 04:40:59 +0000
commitf4dadea763c1959a27dead24df3ee6c54c209842 (patch)
treec13b294a8ca5438d59b60e3f5a25a4f7c1fc9a1b /src/buffer.c
parent562d8cceb704a96a7b2f9acc4bc229ab9f5c6541 (diff)
downloadluasocket-f4dadea763c1959a27dead24df3ee6c54c209842.tar.gz
luasocket-f4dadea763c1959a27dead24df3ee6c54c209842.tar.bz2
luasocket-f4dadea763c1959a27dead24df3ee6c54c209842.zip
Before compiling on Windows.
Diffstat (limited to 'src/buffer.c')
-rw-r--r--src/buffer.c74
1 files changed, 37 insertions, 37 deletions
diff --git a/src/buffer.c b/src/buffer.c
index b69a9b8..df1a0bc 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -12,12 +12,12 @@
12/*=========================================================================*\ 12/*=========================================================================*\
13* Internal function prototypes 13* Internal function prototypes
14\*=========================================================================*/ 14\*=========================================================================*/
15static int recvraw(p_buf buf, size_t wanted, luaL_Buffer *b); 15static int recvraw(p_buffer buf, size_t wanted, luaL_Buffer *b);
16static int recvline(p_buf buf, luaL_Buffer *b); 16static int recvline(p_buffer buf, luaL_Buffer *b);
17static int recvall(p_buf buf, luaL_Buffer *b); 17static int recvall(p_buffer buf, luaL_Buffer *b);
18static int buf_get(p_buf buf, const char **data, size_t *count); 18static int buffer_get(p_buffer buf, const char **data, size_t *count);
19static void buf_skip(p_buf buf, size_t count); 19static void buffer_skip(p_buffer buf, size_t count);
20static int sendraw(p_buf buf, const char *data, size_t count, size_t *sent); 20static int sendraw(p_buffer buf, const char *data, size_t count, size_t *sent);
21 21
22/* min and max macros */ 22/* min and max macros */
23#ifndef MIN 23#ifndef MIN
@@ -33,7 +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\*-------------------------------------------------------------------------*/
36int buf_open(lua_State *L) { 36int buffer_open(lua_State *L) {
37 (void) L; 37 (void) L;
38 return 0; 38 return 0;
39} 39}
@@ -41,31 +41,31 @@ int buf_open(lua_State *L) {
41/*-------------------------------------------------------------------------*\ 41/*-------------------------------------------------------------------------*\
42* Initializes C structure 42* Initializes C structure
43\*-------------------------------------------------------------------------*/ 43\*-------------------------------------------------------------------------*/
44void buf_init(p_buf buf, p_io io, p_tm tm) { 44void buffer_init(p_buffer buf, p_io io, p_timeout 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; 48 buf->received = buf->sent = 0;
49 buf->birthday = tm_gettime(); 49 buf->birthday = timeout_gettime();
50} 50}
51 51
52/*-------------------------------------------------------------------------*\ 52/*-------------------------------------------------------------------------*\
53* object:getstats() interface 53* object:getstats() interface
54\*-------------------------------------------------------------------------*/ 54\*-------------------------------------------------------------------------*/
55int buf_meth_getstats(lua_State *L, p_buf buf) { 55int buffer_meth_getstats(lua_State *L, p_buffer buf) {
56 lua_pushnumber(L, buf->received); 56 lua_pushnumber(L, buf->received);
57 lua_pushnumber(L, buf->sent); 57 lua_pushnumber(L, buf->sent);
58 lua_pushnumber(L, tm_gettime() - buf->birthday); 58 lua_pushnumber(L, timeout_gettime() - buf->birthday);
59 return 3; 59 return 3;
60} 60}
61 61
62/*-------------------------------------------------------------------------*\ 62/*-------------------------------------------------------------------------*\
63* object:setstats() interface 63* object:setstats() interface
64\*-------------------------------------------------------------------------*/ 64\*-------------------------------------------------------------------------*/
65int buf_meth_setstats(lua_State *L, p_buf buf) { 65int buffer_meth_setstats(lua_State *L, p_buffer buf) {
66 buf->received = (long) luaL_optnumber(L, 2, buf->received); 66 buf->received = (long) luaL_optnumber(L, 2, buf->received);
67 buf->sent = (long) luaL_optnumber(L, 3, buf->sent); 67 buf->sent = (long) luaL_optnumber(L, 3, buf->sent);
68 if (lua_isnumber(L, 4)) buf->birthday = tm_gettime() - lua_tonumber(L, 4); 68 if (lua_isnumber(L, 4)) buf->birthday = timeout_gettime() - lua_tonumber(L, 4);
69 lua_pushnumber(L, 1); 69 lua_pushnumber(L, 1);
70 return 1; 70 return 1;
71} 71}
@@ -73,9 +73,9 @@ int buf_meth_setstats(lua_State *L, p_buf buf) {
73/*-------------------------------------------------------------------------*\ 73/*-------------------------------------------------------------------------*\
74* object:send() interface 74* object:send() interface
75\*-------------------------------------------------------------------------*/ 75\*-------------------------------------------------------------------------*/
76int buf_meth_send(lua_State *L, p_buf buf) { 76int buffer_meth_send(lua_State *L, p_buffer buf) {
77 int top = lua_gettop(L); 77 int top = lua_gettop(L);
78 p_tm tm = tm_markstart(buf->tm); 78 p_timeout tm = timeout_markstart(buf->tm);
79 int err = IO_DONE; 79 int err = IO_DONE;
80 size_t size = 0, sent = 0; 80 size_t size = 0, sent = 0;
81 const char *data = luaL_checklstring(L, 2, &size); 81 const char *data = luaL_checklstring(L, 2, &size);
@@ -98,7 +98,7 @@ int buf_meth_send(lua_State *L, p_buf buf) {
98 } 98 }
99#ifdef LUASOCKET_DEBUG 99#ifdef LUASOCKET_DEBUG
100 /* push time elapsed during operation as the last return value */ 100 /* push time elapsed during operation as the last return value */
101 lua_pushnumber(L, tm_gettime() - tm_getstart(tm)); 101 lua_pushnumber(L, timeout_gettime() - timeout_getstart(tm));
102#endif 102#endif
103 return lua_gettop(L) - top; 103 return lua_gettop(L) - top;
104} 104}
@@ -106,9 +106,9 @@ int buf_meth_send(lua_State *L, p_buf buf) {
106/*-------------------------------------------------------------------------*\ 106/*-------------------------------------------------------------------------*\
107* object:receive() interface 107* object:receive() interface
108\*-------------------------------------------------------------------------*/ 108\*-------------------------------------------------------------------------*/
109int buf_meth_receive(lua_State *L, p_buf buf) { 109int buffer_meth_receive(lua_State *L, p_buffer buf) {
110 int err = IO_DONE, top = lua_gettop(L); 110 int err = IO_DONE, top = lua_gettop(L);
111 p_tm tm = tm_markstart(buf->tm); 111 p_timeout tm = timeout_markstart(buf->tm);
112 luaL_Buffer b; 112 luaL_Buffer b;
113 size_t size; 113 size_t size;
114 const char *part = luaL_optlstring(L, 3, "", &size); 114 const char *part = luaL_optlstring(L, 3, "", &size);
@@ -141,7 +141,7 @@ int buf_meth_receive(lua_State *L, p_buf buf) {
141 } 141 }
142#ifdef LUASOCKET_DEBUG 142#ifdef LUASOCKET_DEBUG
143 /* push time elapsed during operation as the last return value */ 143 /* push time elapsed during operation as the last return value */
144 lua_pushnumber(L, tm_gettime() - tm_getstart(tm)); 144 lua_pushnumber(L, timeout_gettime() - timeout_getstart(tm));
145#endif 145#endif
146 return lua_gettop(L) - top; 146 return lua_gettop(L) - top;
147} 147}
@@ -149,7 +149,7 @@ int buf_meth_receive(lua_State *L, p_buf buf) {
149/*-------------------------------------------------------------------------*\ 149/*-------------------------------------------------------------------------*\
150* Determines if there is any data in the read buffer 150* Determines if there is any data in the read buffer
151\*-------------------------------------------------------------------------*/ 151\*-------------------------------------------------------------------------*/
152int buf_isempty(p_buf buf) { 152int buffer_isempty(p_buffer buf) {
153 return buf->first >= buf->last; 153 return buf->first >= buf->last;
154} 154}
155 155
@@ -160,9 +160,9 @@ int buf_isempty(p_buf buf) {
160* Sends a block of data (unbuffered) 160* Sends a block of data (unbuffered)
161\*-------------------------------------------------------------------------*/ 161\*-------------------------------------------------------------------------*/
162#define STEPSIZE 8192 162#define STEPSIZE 8192
163static int sendraw(p_buf buf, const char *data, size_t count, size_t *sent) { 163static int sendraw(p_buffer buf, const char *data, size_t count, size_t *sent) {
164 p_io io = buf->io; 164 p_io io = buf->io;
165 p_tm tm = buf->tm; 165 p_timeout tm = buf->tm;
166 size_t total = 0; 166 size_t total = 0;
167 int err = IO_DONE; 167 int err = IO_DONE;
168 while (total < count && err == IO_DONE) { 168 while (total < count && err == IO_DONE) {
@@ -179,15 +179,15 @@ static int sendraw(p_buf buf, const char *data, size_t count, size_t *sent) {
179/*-------------------------------------------------------------------------*\ 179/*-------------------------------------------------------------------------*\
180* Reads a fixed number of bytes (buffered) 180* Reads a fixed number of bytes (buffered)
181\*-------------------------------------------------------------------------*/ 181\*-------------------------------------------------------------------------*/
182static int recvraw(p_buf buf, size_t wanted, luaL_Buffer *b) { 182static int recvraw(p_buffer buf, size_t wanted, luaL_Buffer *b) {
183 int err = IO_DONE; 183 int err = IO_DONE;
184 size_t total = 0; 184 size_t total = 0;
185 while (total < wanted && err == IO_DONE) { 185 while (total < wanted && err == IO_DONE) {
186 size_t count; const char *data; 186 size_t count; const char *data;
187 err = buf_get(buf, &data, &count); 187 err = buffer_get(buf, &data, &count);
188 count = MIN(count, wanted - total); 188 count = MIN(count, wanted - total);
189 luaL_addlstring(b, data, count); 189 luaL_addlstring(b, data, count);
190 buf_skip(buf, count); 190 buffer_skip(buf, count);
191 total += count; 191 total += count;
192 } 192 }
193 return err; 193 return err;
@@ -196,13 +196,13 @@ static int recvraw(p_buf buf, size_t wanted, luaL_Buffer *b) {
196/*-------------------------------------------------------------------------*\ 196/*-------------------------------------------------------------------------*\
197* Reads everything until the connection is closed (buffered) 197* Reads everything until the connection is closed (buffered)
198\*-------------------------------------------------------------------------*/ 198\*-------------------------------------------------------------------------*/
199static int recvall(p_buf buf, luaL_Buffer *b) { 199static int recvall(p_buffer buf, luaL_Buffer *b) {
200 int err = IO_DONE; 200 int err = IO_DONE;
201 while (err == IO_DONE) { 201 while (err == IO_DONE) {
202 const char *data; size_t count; 202 const char *data; size_t count;
203 err = buf_get(buf, &data, &count); 203 err = buffer_get(buf, &data, &count);
204 luaL_addlstring(b, data, count); 204 luaL_addlstring(b, data, count);
205 buf_skip(buf, count); 205 buffer_skip(buf, count);
206 } 206 }
207 if (err == IO_CLOSED) return IO_DONE; 207 if (err == IO_CLOSED) return IO_DONE;
208 else return err; 208 else return err;
@@ -212,11 +212,11 @@ static int recvall(p_buf buf, luaL_Buffer *b) {
212* Reads a line terminated by a CR LF pair or just by a LF. The CR and LF 212* Reads a line terminated by a CR LF pair or just by a LF. The CR and LF
213* are not returned by the function and are discarded from the buffer 213* are not returned by the function and are discarded from the buffer
214\*-------------------------------------------------------------------------*/ 214\*-------------------------------------------------------------------------*/
215static int recvline(p_buf buf, luaL_Buffer *b) { 215static int recvline(p_buffer buf, luaL_Buffer *b) {
216 int err = IO_DONE; 216 int err = IO_DONE;
217 while (err == IO_DONE) { 217 while (err == IO_DONE) {
218 size_t count, pos; const char *data; 218 size_t count, pos; const char *data;
219 err = buf_get(buf, &data, &count); 219 err = buffer_get(buf, &data, &count);
220 pos = 0; 220 pos = 0;
221 while (pos < count && data[pos] != '\n') { 221 while (pos < count && data[pos] != '\n') {
222 /* we ignore all \r's */ 222 /* we ignore all \r's */
@@ -224,10 +224,10 @@ static int recvline(p_buf buf, luaL_Buffer *b) {
224 pos++; 224 pos++;
225 } 225 }
226 if (pos < count) { /* found '\n' */ 226 if (pos < count) { /* found '\n' */
227 buf_skip(buf, pos+1); /* skip '\n' too */ 227 buffer_skip(buf, pos+1); /* skip '\n' too */
228 break; /* we are done */ 228 break; /* we are done */
229 } else /* reached the end of the buffer */ 229 } else /* reached the end of the buffer */
230 buf_skip(buf, pos); 230 buffer_skip(buf, pos);
231 } 231 }
232 return err; 232 return err;
233} 233}
@@ -236,10 +236,10 @@ static int recvline(p_buf buf, luaL_Buffer *b) {
236* Skips a given number of bytes from read buffer. No data is read from the 236* Skips a given number of bytes from read buffer. No data is read from the
237* transport layer 237* transport layer
238\*-------------------------------------------------------------------------*/ 238\*-------------------------------------------------------------------------*/
239static void buf_skip(p_buf buf, size_t count) { 239static void buffer_skip(p_buffer buf, size_t count) {
240 buf->received += count; 240 buf->received += count;
241 buf->first += count; 241 buf->first += count;
242 if (buf_isempty(buf)) 242 if (buffer_isempty(buf))
243 buf->first = buf->last = 0; 243 buf->first = buf->last = 0;
244} 244}
245 245
@@ -247,11 +247,11 @@ static void buf_skip(p_buf buf, size_t count) {
247* Return any data available in buffer, or get more data from transport layer 247* Return any data available in buffer, or get more data from transport layer
248* if buffer is empty 248* if buffer is empty
249\*-------------------------------------------------------------------------*/ 249\*-------------------------------------------------------------------------*/
250static int buf_get(p_buf buf, const char **data, size_t *count) { 250static int buffer_get(p_buffer buf, const char **data, size_t *count) {
251 int err = IO_DONE; 251 int err = IO_DONE;
252 p_io io = buf->io; 252 p_io io = buf->io;
253 p_tm tm = buf->tm; 253 p_timeout tm = buf->tm;
254 if (buf_isempty(buf)) { 254 if (buffer_isempty(buf)) {
255 size_t got; 255 size_t got;
256 err = io->recv(io->ctx, buf->data, BUF_SIZE, &got, tm); 256 err = io->recv(io->ctx, buf->data, BUF_SIZE, &got, tm);
257 buf->first = 0; 257 buf->first = 0;