Skip to content

Commit

Permalink
optimize skynet.error
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudwu committed May 6, 2016
1 parent 6cc558b commit a6299a0
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 36 deletions.
5 changes: 2 additions & 3 deletions examples/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local sprotoloader = require "sprotoloader"
local max_client = 64

skynet.start(function()
print("Server start")
skynet.error("Server start")
skynet.uniqueservice("protoloader")
local console = skynet.newservice("console")
skynet.newservice("debug_console",8000)
Expand All @@ -15,7 +15,6 @@ skynet.start(function()
maxclient = max_client,
nodelay = true,
})
print("Watchdog listen on ", 8888)

skynet.error("Watchdog listen on", 8888)
skynet.exit()
end)
4 changes: 2 additions & 2 deletions lualib-src/lua-seri.c
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ seri(lua_State *L, struct block *b, int len) {
}

int
_luaseri_unpack(lua_State *L) {
luaseri_unpack(lua_State *L) {
if (lua_isnoneornil(L,1)) {
return 0;
}
Expand Down Expand Up @@ -595,7 +595,7 @@ _luaseri_unpack(lua_State *L) {
}

int
_luaseri_pack(lua_State *L) {
luaseri_pack(lua_State *L) {
struct block temp;
temp.next = NULL;
struct write_block wb;
Expand Down
4 changes: 2 additions & 2 deletions lualib-src/lua-seri.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <lua.h>

int _luaseri_pack(lua_State *L);
int _luaseri_unpack(lua_State *L);
int luaseri_pack(lua_State *L);
int luaseri_unpack(lua_State *L);

#endif
62 changes: 40 additions & 22 deletions lualib-src/lua-skynet.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ forward_cb(struct skynet_context * context, void * ud, int type, int session, ui
}

static int
_callback(lua_State *L) {
lcallback(lua_State *L) {
struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1));
int forward = lua_toboolean(L, 2);
luaL_checktype(L,1,LUA_TFUNCTION);
Expand All @@ -101,7 +101,7 @@ _callback(lua_State *L) {
}

static int
_command(lua_State *L) {
lcommand(lua_State *L) {
struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1));
const char * cmd = luaL_checkstring(L,1);
const char * result;
Expand All @@ -119,7 +119,7 @@ _command(lua_State *L) {
}

static int
_intcommand(lua_State *L) {
lintcommand(lua_State *L) {
struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1));
const char * cmd = luaL_checkstring(L,1);
const char * result;
Expand All @@ -141,7 +141,7 @@ _intcommand(lua_State *L) {
}

static int
_genid(lua_State *L) {
lgenid(lua_State *L) {
struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1));
int session = skynet_send(context, 0, 0, PTYPE_TAG_ALLOCSESSION , 0 , NULL, 0);
lua_pushinteger(L, session);
Expand All @@ -167,7 +167,7 @@ get_dest_string(lua_State *L, int index) {
integer len
*/
static int
_send(lua_State *L) {
lsend(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;
Expand Down Expand Up @@ -224,7 +224,7 @@ _send(lua_State *L) {
}

static int
_redirect(lua_State *L) {
lredirect(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;
Expand Down Expand Up @@ -267,14 +267,32 @@ _redirect(lua_State *L) {
}

static int
_error(lua_State *L) {
lerror(lua_State *L) {
struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1));
skynet_error(context, "%s", luaL_checkstring(L,1));
int n = lua_gettop(L);
if (n <= 1) {
lua_settop(L, 1);
const char * s = luaL_tolstring(L, 1, NULL);
skynet_error(context, "%s", s);
return 0;
}
luaL_Buffer b;
luaL_buffinit(L, &b);
int i;
for (i=1; i<=n; i++) {
luaL_tolstring(L, i, NULL);
luaL_addvalue(&b);
if (i<n) {
luaL_addchar(&b, ' ');
}
}
luaL_pushresult(&b);
skynet_error(context, "%s", lua_tostring(L, -1));
return 0;
}

static int
_tostring(lua_State *L) {
ltostring(lua_State *L) {
if (lua_isnoneornil(L,1)) {
return 0;
}
Expand All @@ -285,7 +303,7 @@ _tostring(lua_State *L) {
}

static int
_harbor(lua_State *L) {
lharbor(lua_State *L) {
struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1));
uint32_t handle = (uint32_t)luaL_checkinteger(L,1);
int harbor = 0;
Expand All @@ -298,7 +316,7 @@ _harbor(lua_State *L) {

static int
lpackstring(lua_State *L) {
_luaseri_pack(L);
luaseri_pack(L);
char * str = (char *)lua_touserdata(L, -2);
int sz = lua_tointeger(L, -1);
lua_pushlstring(L, str, sz);
Expand Down Expand Up @@ -338,19 +356,19 @@ luaopen_skynet_core(lua_State *L) {
luaL_checkversion(L);

luaL_Reg l[] = {
{ "send" , _send },
{ "genid", _genid },
{ "redirect", _redirect },
{ "command" , _command },
{ "intcommand", _intcommand },
{ "error", _error },
{ "tostring", _tostring },
{ "harbor", _harbor },
{ "pack", _luaseri_pack },
{ "unpack", _luaseri_unpack },
{ "send" , lsend },
{ "genid", lgenid },
{ "redirect", lredirect },
{ "command" , lcommand },
{ "intcommand", lintcommand },
{ "error", lerror },
{ "tostring", ltostring },
{ "harbor", lharbor },
{ "pack", luaseri_pack },
{ "unpack", luaseri_unpack },
{ "packstring", lpackstring },
{ "trash" , ltrash },
{ "callback", _callback },
{ "callback", lcallback },
{ "now", lnow },
{ NULL, NULL },
};
Expand Down
8 changes: 1 addition & 7 deletions lualib/skynet.lua
Original file line number Diff line number Diff line change
Expand Up @@ -548,13 +548,7 @@ function skynet.harbor(addr)
return c.harbor(addr)
end

function skynet.error(...)
local t = {...}
for i=1,#t do
t[i] = tostring(t[i])
end
return c.error(table.concat(t, " "))
end
skynet.error = c.error

----- register protocol
do
Expand Down

0 comments on commit a6299a0

Please sign in to comment.