diff --git a/src/box/lua/session.cc b/src/box/lua/session.cc index 93927fdb095e992b62c407195533c4b79f4a47bf..f5781785022cf1adbb9c71bb06b1610be66715f8 100644 --- a/src/box/lua/session.cc +++ b/src/box/lua/session.cc @@ -117,7 +117,7 @@ lbox_session_exists(struct lua_State *L) luaL_error(L, "session.exists(sid): bad arguments"); uint32_t sid = luaL_checkint(L, -1); - lua_pushnumber(L, session_exists(sid)); + lua_pushboolean(L, session_find(sid) != NULL); return 1; } @@ -131,7 +131,10 @@ lbox_session_fd(struct lua_State *L) luaL_error(L, "session.fd(sid): bad arguments"); uint32_t sid = luaL_checkint(L, -1); - lua_pushnumber(L, session_fd(sid)); + struct session *session = session_find(sid); + if (session == NULL) + luaL_error(L, "session.fd(): session does not exit"); + lua_pushinteger(L, session->fd); return 1; } @@ -145,10 +148,21 @@ lbox_session_peer(struct lua_State *L) if (lua_gettop(L) > 1) luaL_error(L, "session.peer(sid): bad arguments"); - uint32_t sid = lua_gettop(L) == 1 ? - luaL_checkint(L, -1) : session()->id; + int fd; + if (lua_gettop(L) == 1) { + struct session *session = session_find(luaL_checkint(L, 1)); + if (session == NULL) + luaL_error(L, "session.peer(): session does not exit"); + fd = session->fd; + } else { + fd = session()->fd; + } + + if (fd < 0) { + lua_pushnil(L); /* no associated peer */ + return 1; + } - int fd = session_fd(sid); struct sockaddr_storage addr; socklen_t addrlen = sizeof(addr); sio_getpeername(fd, (struct sockaddr *)&addr, &addrlen); diff --git a/src/box/session.cc b/src/box/session.cc index 8d0f4a5e0e4420696d1100cd504b27d3312a10c0..98de6833f48edb001d9ab0fbf0caf51647ecbe18 100644 --- a/src/box/session.cc +++ b/src/box/session.cc @@ -131,15 +131,14 @@ session_destroy(struct session *session) mempool_free(&session_pool, session); } -int -session_fd(uint32_t sid) +struct session * +session_find(uint32_t sid) { mh_int_t k = mh_i32ptr_find(session_registry, sid, NULL); if (k == mh_end(session_registry)) - return -1; - struct session *session = (struct session *) + return NULL; + return (struct session *) mh_i32ptr_node(session_registry, k)->val; - return session->fd; } void diff --git a/src/box/session.h b/src/box/session.h index 3c31727a42210c23bf41d4261253a5fa87eb0afe..c2fc6570ff469cf637dadc889c5f7de217ff271d 100644 --- a/src/box/session.h +++ b/src/box/session.h @@ -92,21 +92,10 @@ void session_destroy(struct session *); /** - * Return a file descriptor - * associated with a session, or -1 if the - * session doesn't exist. + * Find a session by id. */ -int -session_fd(uint32_t sid); - -/** - * Check whether a session exists or not. - */ -static inline bool -session_exists(uint32_t sid) -{ - return session_fd(sid) >= 0; -} +struct session * +session_find(uint32_t sid); /** Set session auth token and user id. */ static inline void diff --git a/test/box/session.result b/test/box/session.result index 5a613041ad0ade63ce42f66b21f67afe9f07a2de..b0610ea741b85f00382872938bfcf11f28861874 100644 --- a/test/box/session.result +++ b/test/box/session.result @@ -12,7 +12,11 @@ space:create_index('primary', { type = 'hash' }) ... session.exists(session.id()) --- -- 0 +- true +... +session.peer(session.id()) +--- +- null ... session.exists() --- @@ -24,7 +28,7 @@ session.exists(1, 2, 3) ... session.exists(1234567890) --- -- 0 +- false ... -- check session.id() session.id() > 0 diff --git a/test/box/session.test.lua b/test/box/session.test.lua index dcf09e6ee44688ba25256fc925a065d98ccab747..9be90f703391a53abcb661a3562237698bfbd292 100644 --- a/test/box/session.test.lua +++ b/test/box/session.test.lua @@ -5,6 +5,7 @@ space = box.schema.create_space('tweedledum') space:create_index('primary', { type = 'hash' }) session.exists(session.id()) +session.peer(session.id()) session.exists() session.exists(1, 2, 3) session.exists(1234567890)