some bugfix for gate/socket lib

This commit is contained in:
云风
2013-08-24 00:14:01 +08:00
parent 424c84915b
commit 52cf864037
5 changed files with 549 additions and 18 deletions

View File

@@ -357,8 +357,24 @@ lunpack(lua_State *L) {
static int
lconnect(lua_State *L) {
const char * host = luaL_checkstring(L,1);
int port = luaL_checkinteger(L,2);
size_t sz = 0;
const char * addr = luaL_checklstring(L,1,&sz);
char tmp[sz];
int port;
const char * host;
if (lua_isnoneornil(L,2)) {
const char * sep = strchr(addr,':');
if (sep == NULL) {
return luaL_error(L, "Connect to invalid address %s.",addr);
}
memcpy(tmp, addr, sep-addr);
tmp[sep-addr] = '\0';
host = tmp;
port = strtoul(sep+1,NULL,10);
} else {
host = addr;
port = luaL_checkinteger(L,2);
}
struct skynet_context * ctx = lua_touserdata(L, lua_upvalueindex(1));
int id = skynet_socket_connect(ctx, host, port);
lua_pushinteger(L, id);