--- ./net.c 2015-01-14 18:10:29.771027123 +0800 +++ ./net.c.new 2015-01-14 18:10:15.675131954 +0800 @@ -914,6 +914,28 @@ return 0; } +// Lua: ip,port = sk:getpeer() +static int net_socket_getpeer( lua_State* L ) +{ + lnet_userdata *nud; + const char *mt = "net.socket"; + nud = (lnet_userdata *)luaL_checkudata(L, 1, mt); + luaL_argcheck(L, nud, 1, "Server/Socket expected"); + + if(nud!=NULL && nud->pesp_conn!=NULL ){ + char temp[20] = {0}; + c_sprintf(temp, IPSTR, IP2STR( &(nud->pesp_conn->proto.tcp->remote_ip) ) ); + if ( nud->pesp_conn->proto.tcp->remote_port != 0 ) { + lua_pushstring( L, temp ); + lua_pushinteger( L, nud->pesp_conn->proto.tcp->remote_port ); + } else { + lua_pushnil( L ); + lua_pushnil( L ); + } + } else { + lua_pushnil( L ); + lua_pushnil( L ); + } + return 2; +} + // Lua: socket/udpserver:on( "method", function(s) ) static int net_on( lua_State* L, const char* mt ) { @@ -1235,6 +1257,7 @@ static const LUA_REG_TYPE net_socket_map[] = { + { LSTRKEY( "getpeer" ), LFUNCVAL ( net_socket_getpeer ) }, { LSTRKEY( "connect" ), LFUNCVAL( net_socket_connect ) }, { LSTRKEY( "close" ), LFUNCVAL ( net_socket_close ) }, { LSTRKEY( "on" ), LFUNCVAL ( net_socket_on ) },