add socket.netstat

This commit is contained in:
Cloud Wu
2018-09-07 16:11:27 +08:00
parent 35176383b7
commit 61980c8a1f
9 changed files with 299 additions and 9 deletions

View File

@@ -680,6 +680,70 @@ ludp_address(lua_State *L) {
return 2;
}
static void
getinfo(lua_State *L, struct socket_info *si) {
lua_newtable(L);
lua_pushinteger(L, si->id);
lua_setfield(L, -2, "id");
lua_pushinteger(L, si->opaque);
lua_setfield(L, -2, "address");
switch(si->type) {
case SOCKET_INFO_LISTEN:
lua_pushstring(L, "LISTEN");
lua_setfield(L, -2, "type");
lua_pushinteger(L, si->read);
lua_setfield(L, -2, "accept");
if (si->name[0]) {
lua_pushstring(L, si->name);
lua_setfield(L, -2, "sock");
}
return;
case SOCKET_INFO_TCP:
lua_pushstring(L, "TCP");
break;
case SOCKET_INFO_UDP:
lua_pushstring(L, "UDP");
break;
case SOCKET_INFO_BIND:
lua_pushstring(L, "BIND");
break;
default:
lua_pushstring(L, "UNKNOWN");
lua_setfield(L, -2, "type");
return;
}
lua_setfield(L, -2, "type");
lua_pushinteger(L, si->read);
lua_setfield(L, -2, "read");
lua_pushinteger(L, si->write);
lua_setfield(L, -2, "write");
lua_pushinteger(L, si->wbuffer);
lua_setfield(L, -2, "wbuffer");
lua_pushinteger(L, si->rtime);
lua_setfield(L, -2, "rtime");
lua_pushinteger(L, si->wtime);
lua_setfield(L, -2, "wtime");
if (si->name[0]) {
lua_pushstring(L, si->name);
lua_setfield(L, -2, "peer");
}
}
static int
linfo(lua_State *L) {
lua_newtable(L);
struct socket_info * si = skynet_socket_info();
struct socket_info * temp = si;
int n = 0;
while (temp) {
getinfo(L, temp);
lua_seti(L, -2, ++n);
temp = temp->next;
}
socket_info_release(si);
return 1;
}
LUAMOD_API int
luaopen_skynet_socketdriver(lua_State *L) {
luaL_checkversion(L);
@@ -693,6 +757,7 @@ luaopen_skynet_socketdriver(lua_State *L) {
{ "readline", lreadline },
{ "str2p", lstr2p },
{ "header", lheader },
{ "info", linfo },
{ "unpack", lunpack },
{ NULL, NULL },