Skip to content

Commit ce50c47

Browse files
committed
socket.send support strings table
1 parent d283d7f commit ce50c47

File tree

1 file changed

+51
-4
lines changed

1 file changed

+51
-4
lines changed

Diff for: lualib-src/lua-socket.c

+51-4
Original file line numberDiff line numberDiff line change
@@ -484,18 +484,65 @@ llisten(lua_State *L) {
484484
return 1;
485485
}
486486

487+
static size_t
488+
count_size(lua_State *L, int index) {
489+
size_t tlen = 0;
490+
int i;
491+
for (i=1;lua_geti(L, index, i) != LUA_TNIL; ++i) {
492+
size_t len;
493+
luaL_checklstring(L, -1, &len);
494+
tlen += len;
495+
lua_pop(L,1);
496+
}
497+
lua_pop(L,1);
498+
return tlen;
499+
}
500+
501+
static void
502+
concat_table(lua_State *L, int index, void *buffer, size_t tlen) {
503+
char *ptr = buffer;
504+
int i;
505+
for (i=1;lua_geti(L, index, i) != LUA_TNIL; ++i) {
506+
size_t len;
507+
const char * str = lua_tolstring(L, -1, &len);
508+
if (str == NULL || tlen < len) {
509+
break;
510+
}
511+
memcpy(ptr, str, len);
512+
ptr += len;
513+
tlen -= len;
514+
lua_pop(L,1);
515+
}
516+
if (tlen != 0) {
517+
skynet_free(buffer);
518+
luaL_error(L, "Invalid strings table");
519+
}
520+
lua_pop(L,1);
521+
}
522+
487523
static void *
488524
get_buffer(lua_State *L, int index, int *sz) {
489525
void *buffer;
490-
if (lua_isuserdata(L,index)) {
526+
switch(lua_type(L, index)) {
527+
const char * str;
528+
size_t len;
529+
case LUA_TUSERDATA:
491530
buffer = lua_touserdata(L,index);
492531
*sz = luaL_checkinteger(L,index+1);
493-
} else {
494-
size_t len = 0;
495-
const char * str = luaL_checklstring(L, index, &len);
532+
break;
533+
case LUA_TTABLE:
534+
// concat the table as a string
535+
len = count_size(L, index);
536+
buffer = skynet_malloc(len);
537+
concat_table(L, index, buffer, len);
538+
*sz = (int)len;
539+
break;
540+
default:
541+
str = luaL_checklstring(L, index, &len);
496542
buffer = skynet_malloc(len);
497543
memcpy(buffer, str, len);
498544
*sz = (int)len;
545+
break;
499546
}
500547
return buffer;
501548
}

0 commit comments

Comments
 (0)