mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-25 04:33:05 +00:00
change skynet_send api (use uint32_t instead of string)
This commit is contained in:
@@ -78,27 +78,6 @@ _query(struct remote_objects *r, int handle) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static uint32_t
|
||||
_getaddr(lua_State *L, int index) {
|
||||
size_t sz;
|
||||
const char * addr = luaL_checklstring(L,index,&sz);
|
||||
if (addr[0] != ':') {
|
||||
luaL_error(L, "Invalid address %s",addr);
|
||||
}
|
||||
return strtoul(addr+1, NULL, 16);
|
||||
}
|
||||
|
||||
static void
|
||||
_id_to_hex(char * str, uint32_t id) {
|
||||
int i;
|
||||
static char hex[16] = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' };
|
||||
str[0] = ':';
|
||||
for (i=0;i<8;i++) {
|
||||
str[i+1] = hex[(id >> ((7-i) * 4))&0xf];
|
||||
}
|
||||
str[9] = '\0';
|
||||
}
|
||||
|
||||
static int
|
||||
lbind(lua_State *L) {
|
||||
uint32_t addr = lua_tounsigned(L,lua_upvalueindex(1));
|
||||
@@ -107,9 +86,7 @@ lbind(lua_State *L) {
|
||||
if (old == 0) {
|
||||
luaL_error(L, "handle %d is not exist", handle);
|
||||
}
|
||||
char tmp[10];
|
||||
_id_to_hex(tmp, old);
|
||||
lua_pushlstring(L,tmp,9);
|
||||
lua_pushnumber(L, old);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -138,9 +115,7 @@ lquery(lua_State *L) {
|
||||
if (addr == 0) {
|
||||
return 0;
|
||||
}
|
||||
char tmp[10];
|
||||
_id_to_hex(tmp, addr);
|
||||
lua_pushlstring(L,tmp,9);
|
||||
lua_pushnumber(L, addr);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -153,7 +128,7 @@ remoteobj_init(lua_State *L) {
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t address = _getaddr(L, -1);
|
||||
uint32_t address = luaL_checkunsigned(L, -1);
|
||||
lua_pushcfunction(L, lquery);
|
||||
lua_pushnumber(L, address);
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
#include <assert.h>
|
||||
|
||||
static int
|
||||
_cb(struct skynet_context * context, void * ud, int session, const char * addr, const void * msg, size_t sz) {
|
||||
_cb(struct skynet_context * context, void * ud, int session, uint32_t source, const void * msg, size_t sz) {
|
||||
lua_State *L = ud;
|
||||
int trace = 1;
|
||||
int top = lua_gettop(L);
|
||||
@@ -21,17 +21,17 @@ _cb(struct skynet_context * context, void * ud, int session, const char * addr,
|
||||
|
||||
int r;
|
||||
if (msg == NULL) {
|
||||
if (addr == NULL) {
|
||||
if (source == 0) {
|
||||
lua_pushinteger(L, session);
|
||||
r = lua_pcall(L, 1, 0 , trace);
|
||||
} else {
|
||||
lua_pushinteger(L, session);
|
||||
lua_pushstring(L, addr);
|
||||
lua_pushnumber(L, source);
|
||||
r = lua_pcall(L, 2, 0 , trace);
|
||||
}
|
||||
} else {
|
||||
lua_pushinteger(L, session);
|
||||
lua_pushstring(L, addr);
|
||||
lua_pushnumber(L, source);
|
||||
lua_pushlightuserdata(L,(void *)msg);
|
||||
lua_pushinteger(L,sz);
|
||||
r = lua_pcall(L, 4, 0 , trace);
|
||||
@@ -41,16 +41,16 @@ _cb(struct skynet_context * context, void * ud, int session, const char * addr,
|
||||
const char * self = skynet_command(context, "REG", NULL);
|
||||
switch (r) {
|
||||
case LUA_ERRRUN:
|
||||
skynet_error(context, "lua call [%s to %s : %d msgsz = %d] error : %s", addr , self, session, sz, lua_tostring(L,-1));
|
||||
skynet_error(context, "lua call [%x to %s : %d msgsz = %d] error : %s", source , self, session, sz, lua_tostring(L,-1));
|
||||
break;
|
||||
case LUA_ERRMEM:
|
||||
skynet_error(context, "lua memory error : [%s to %s : %d]", addr , self, session);
|
||||
skynet_error(context, "lua memory error : [%x to %s : %d]", source , self, session);
|
||||
break;
|
||||
case LUA_ERRERR:
|
||||
skynet_error(context, "lua error in error : [%s to %s : %d]", addr , self, session);
|
||||
skynet_error(context, "lua error in error : [%x to %s : %d]", source , self, session);
|
||||
break;
|
||||
case LUA_ERRGCMM:
|
||||
skynet_error(context, "lua gc error : [%s to %s : %d]", addr , self, session);
|
||||
skynet_error(context, "lua gc error : [%x to %s : %d]", source , self, session);
|
||||
break;
|
||||
};
|
||||
|
||||
@@ -98,37 +98,87 @@ _command(lua_State *L) {
|
||||
static int
|
||||
_genid(lua_State *L) {
|
||||
struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1));
|
||||
int session = skynet_send(context, NULL, NULL, -1, NULL, 0 , 0);
|
||||
int session = skynet_send(context, 0, 0, -1, NULL, 0 , 0);
|
||||
lua_pushinteger(L, session);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
_sendname(lua_State *L, struct skynet_context * context, const char * dest) {
|
||||
int session = 0;
|
||||
int index = 0;
|
||||
|
||||
if (lua_type(L,2) == LUA_TNUMBER) {
|
||||
session = lua_tointeger(L,2);
|
||||
++index;
|
||||
}
|
||||
if (lua_gettop(L) == index + 1) {
|
||||
session = skynet_sendname(context, dest, session , NULL, 0, 0);
|
||||
} else {
|
||||
int type = lua_type(L,index+2);
|
||||
if (type == LUA_TSTRING) {
|
||||
size_t len = 0;
|
||||
void * msg = (void *)lua_tolstring(L,index+2,&len);
|
||||
session = skynet_sendname(context, dest, session , msg, len, 0);
|
||||
} else if (type == LUA_TNIL) {
|
||||
session = skynet_sendname(context, dest, session , NULL, 0, 0);
|
||||
} else {
|
||||
luaL_checktype(L,index+2, LUA_TLIGHTUSERDATA);
|
||||
void * msg = lua_touserdata(L,index+2);
|
||||
int size = luaL_checkinteger(L,index+3);
|
||||
session = skynet_sendname(context, dest, session, msg, size, DONTCOPY);
|
||||
}
|
||||
}
|
||||
lua_pushinteger(L,session);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
_send(lua_State *L) {
|
||||
struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1));
|
||||
int session = 0;
|
||||
int index = 0;
|
||||
const char * dest = luaL_checkstring(L,1);
|
||||
int addr_type = lua_type(L,1);
|
||||
uint32_t dest = 0;
|
||||
switch(addr_type) {
|
||||
case LUA_TNUMBER:
|
||||
dest = lua_tounsigned(L,1);
|
||||
break;
|
||||
case LUA_TSTRING: {
|
||||
const char * addrname = lua_tostring(L,1);
|
||||
if (addrname[0] == '.' || addrname[0] == ':') {
|
||||
dest = skynet_queryname(context, addrname);
|
||||
if (dest == 0) {
|
||||
luaL_error(L, "Invalid name %s", addrname);
|
||||
}
|
||||
} else {
|
||||
return _sendname(L, context, addrname);
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
return luaL_error(L, "address must be number or string, got %s",lua_typename(L,addr_type));
|
||||
}
|
||||
|
||||
int index = 0;
|
||||
if (lua_type(L,2) == LUA_TNUMBER) {
|
||||
session = lua_tointeger(L,2);
|
||||
++index;
|
||||
}
|
||||
if (lua_gettop(L) == index + 1) {
|
||||
session = skynet_send(context, NULL, dest, session , NULL, 0, 0);
|
||||
session = skynet_send(context, 0, dest, session , NULL, 0, 0);
|
||||
} else {
|
||||
int type = lua_type(L,index+2);
|
||||
if (type == LUA_TSTRING) {
|
||||
size_t len = 0;
|
||||
void * msg = (void *)lua_tolstring(L,index+2,&len);
|
||||
session = skynet_send(context, NULL, dest, session , msg, len, 0);
|
||||
session = skynet_send(context, 0, dest, session , msg, len, 0);
|
||||
} else if (type == LUA_TNIL) {
|
||||
session = skynet_send(context, NULL, dest, session , NULL, 0, 0);
|
||||
session = skynet_send(context, 0, dest, session , NULL, 0, 0);
|
||||
} else {
|
||||
luaL_checktype(L,index+2, LUA_TLIGHTUSERDATA);
|
||||
void * msg = lua_touserdata(L,index+2);
|
||||
int size = luaL_checkinteger(L,index+3);
|
||||
session = skynet_send(context, NULL, dest, session, msg, size, DONTCOPY);
|
||||
session = skynet_send(context, 0, dest, session, msg, size, DONTCOPY);
|
||||
}
|
||||
}
|
||||
if (session < 0) {
|
||||
@@ -141,8 +191,8 @@ _send(lua_State *L) {
|
||||
static int
|
||||
_redirect(lua_State *L) {
|
||||
struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1));
|
||||
const char * dest = luaL_checkstring(L,1);
|
||||
const char * source = luaL_checkstring(L,2);
|
||||
uint32_t dest = luaL_checkunsigned(L,1);
|
||||
uint32_t source = luaL_checkunsigned(L,2);
|
||||
int session = luaL_checkinteger(L,3);
|
||||
|
||||
if (lua_gettop(L) == 3) {
|
||||
|
||||
Reference in New Issue
Block a user