//向某服务发送消息 static int _send(lua_State *L) { struct server_context * context = lua_touserdata(L, lua_upvalueindex(1)); //接收方handle或名称 uint32_t dest = (uint32_t)lua_tointeger(L, 1); const char * dest_string = NULL; if (dest == 0) { dest_string = get_dest_string(L, 1); } //发送类型 int type = luaL_checkinteger(L, 2); //会话id int session = 0; if (lua_isnil(L,3)) { type |= PTYPE_TAG_ALLOCSESSION; } else { session = luaL_checkinteger(L,3); } //发送消息内容 int mtype = lua_type(L,4); switch (mtype) { case LUA_TSTRING: {//内容为字符串 size_t len = 0; void * msg = (void *)lua_tolstring(L,4,&len); if (len == 0) { msg = NULL; } if (dest_string) { session = server_sendname(context, 0, dest_string, type, session , msg, len); } else { session = server_send(context, 0, dest, type, session , msg, len); } break; } case LUA_TLIGHTUSERDATA: {//内容为C数据 void * msg = lua_touserdata(L,4);//获取指向该内存的地址 int size = luaL_checkinteger(L,5); if (dest_string) { session = server_sendname(context, 0, dest_string, type | PTYPE_TAG_DONTCOPY, session, msg, size);//发送时不拷贝内容 } else { session = server_send(context, 0, dest, type | PTYPE_TAG_DONTCOPY, session, msg, size); } break; } default: luaL_error(L, "server.send invalid param %s", lua_typename(L, lua_type(L,4))); } if (session < 0) { // send to invalid address // todo: maybe throw error whould be better return 0; } //返回会话id lua_pushinteger(L, session); return 1; }
static int send_message(lua_State *L, int source, int idx_type) { struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1)); uint32_t dest = (uint32_t)lua_tointeger(L, 1); const char * dest_string = NULL; if (dest == 0) { if (lua_type(L,1) == LUA_TNUMBER) { return luaL_error(L, "Invalid service address 0"); } dest_string = get_dest_string(L, 1); } int type = luaL_checkinteger(L, idx_type+0); int session = 0; if (lua_isnil(L,idx_type+1)) { type |= PTYPE_TAG_ALLOCSESSION; } else { session = luaL_checkinteger(L,idx_type+1); } int mtype = lua_type(L,idx_type+2); switch (mtype) { case LUA_TSTRING: { size_t len = 0; void * msg = (void *)lua_tolstring(L,idx_type+2,&len); if (len == 0) { msg = NULL; } if (dest_string) { session = skynet_sendname(context, source, dest_string, type, session , msg, len); } else { session = skynet_send(context, source, dest, type, session , msg, len); } break; } case LUA_TLIGHTUSERDATA: { void * msg = lua_touserdata(L,idx_type+2); int size = luaL_checkinteger(L,idx_type+3); if (dest_string) { session = skynet_sendname(context, source, dest_string, type | PTYPE_TAG_DONTCOPY, session, msg, size); } else { session = skynet_send(context, source, dest, type | PTYPE_TAG_DONTCOPY, session, msg, size); } break; } default: luaL_error(L, "invalid param %s", lua_typename(L, lua_type(L,idx_type+2))); } if (session < 0) { // send to invalid address // todo: maybe throw an error would be better return 0; } lua_pushinteger(L,session); return 1; }
static int _redirect(lua_State *L) { struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1)); uint32_t dest = (uint32_t)lua_tointeger(L,1); const char * dest_string = NULL; if (dest == 0) { dest_string = get_dest_string(L, 1); } uint32_t source = (uint32_t)luaL_checkinteger(L,2); int type = luaL_checkinteger(L,3); int session = luaL_checkinteger(L,4); int mtype = lua_type(L,5); switch (mtype) { case LUA_TSTRING: { size_t len = 0; void * msg = (void *)lua_tolstring(L,5,&len); if (len == 0) { msg = NULL; } if (dest_string) { session = skynet_sendname(context, source, dest_string, type, session , msg, len); } else { session = skynet_send(context, source, dest, type, session , msg, len); } break; } case LUA_TLIGHTUSERDATA: { void * msg = lua_touserdata(L,5); int size = luaL_checkinteger(L,6); if (dest_string) { session = skynet_sendname(context, source, dest_string, type | PTYPE_TAG_DONTCOPY, session, msg, size); } else { session = skynet_send(context, source, dest, type | PTYPE_TAG_DONTCOPY, session, msg, size); } break; } default: luaL_error(L, "skynet.redirect invalid param %s", lua_typename(L,mtype)); } return 0; }