Merge branch 'master' into dev

This commit is contained in:
Cloud Wu
2014-06-21 18:03:24 +08:00
3 changed files with 24 additions and 19 deletions

View File

@@ -188,10 +188,29 @@ pop_lstring(lua_State *L, struct socket_buffer *sb, int sz, int skip) {
luaL_pushresult(&b); luaL_pushresult(&b);
} }
static int
lheader(lua_State *L) {
size_t len;
const uint8_t * s = (const uint8_t *)luaL_checklstring(L, 1, &len);
if (len > 4 || len < 1) {
return luaL_error(L, "Invalid read %s", s);
}
int i;
size_t sz = 0;
for (i=0;i<(int)len;i++) {
sz <<= 8;
sz |= s[i];
}
lua_pushunsigned(L, sz);
return 1;
}
/* /*
userdata send_buffer userdata send_buffer
table pool table pool
integer sz or string (big endian) integer sz
*/ */
static int static int
lpopbuffer(lua_State *L) { lpopbuffer(lua_State *L) {
@@ -200,23 +219,7 @@ lpopbuffer(lua_State *L) {
return luaL_error(L, "Need buffer object at param 1"); return luaL_error(L, "Need buffer object at param 1");
} }
luaL_checktype(L,2,LUA_TTABLE); luaL_checktype(L,2,LUA_TTABLE);
int type = lua_type(L,3); int sz = luaL_checkinteger(L,3);
int sz;
if (type == LUA_TNUMBER) {
sz = lua_tointeger(L,3);
} else {
size_t len;
const uint8_t * s = (const uint8_t *)luaL_checklstring(L, 3, &len);
if (len > 4 || len < 1) {
return luaL_error(L, "Invalid read %s", s);
}
int i;
sz = 0;
for (i=0;i<(int)len;i++) {
sz <<= 8;
sz |= s[i];
}
}
if (sb->size < sz || sz == 0) { if (sb->size < sz || sz == 0) {
lua_pushnil(L); lua_pushnil(L);
} else { } else {
@@ -485,6 +488,7 @@ luaopen_socketdriver(lua_State *L) {
{ "clear", lclearbuffer }, { "clear", lclearbuffer },
{ "readline", lreadline }, { "readline", lreadline },
{ "str2p", lstr2p }, { "str2p", lstr2p },
{ "header", lheader },
{ "unpack", lunpack }, { "unpack", lunpack },
{ NULL, NULL }, { NULL, NULL },

View File

@@ -266,6 +266,7 @@ end
socket.write = assert(driver.send) socket.write = assert(driver.send)
socket.lwrite = assert(driver.lsend) socket.lwrite = assert(driver.lsend)
socket.header = assert(driver.header)
function socket.invalid(id) function socket.invalid(id)
return socket_pool[id] == nil return socket_pool[id] == nil

View File

@@ -10,7 +10,7 @@ local node_session = {}
local command = {} local command = {}
local function read_response(sock) local function read_response(sock)
local sz = sock:read(2) local sz = socket.header(sock:read(2))
local msg = sock:read(sz) local msg = sock:read(sz)
return cluster.unpackresponse(msg) -- session, ok, data return cluster.unpackresponse(msg) -- session, ok, data
end end