Skip to content

Commit

Permalink
Cleanup net module
Browse files Browse the repository at this point in the history
  • Loading branch information
djphoenix committed Aug 1, 2016
1 parent f3a79d1 commit 552bdbe
Showing 1 changed file with 1 addition and 116 deletions.
117 changes: 1 addition & 116 deletions app/modules/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@

static ip_addr_t host_ip; // for dns

#if 0
static int expose_array(lua_State* L, char *array, unsigned short len);
#endif

#define MAX_SOCKET 5
static int socket_num = 0;
static int socket[MAX_SOCKET];
Expand Down Expand Up @@ -57,15 +53,6 @@ static void net_server_disconnected(void *arg) // for tcp server only
return;
if(gL == NULL)
return;
#if 0
char temp[20] = {0};
c_sprintf(temp, IPSTR, IP2STR( &(pesp_conn->proto.tcp->remote_ip) ) );
NODE_DBG("remote ");
NODE_DBG(temp);
NODE_DBG(":");
NODE_DBG("%d",pesp_conn->proto.tcp->remote_port);
NODE_DBG(" disconnected.\n");
#endif
if(nud->cb_disconnect_ref != LUA_NOREF && nud->self_ref != LUA_NOREF)
{
lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->cb_disconnect_ref);
Expand Down Expand Up @@ -146,18 +133,13 @@ static void net_socket_received(void *arg, char *pdata, unsigned short len)
return;
lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->cb_receive_ref);
lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(server) to callback func in lua
// expose_array(gL, pdata, len);
// *(pdata+len) = 0;
// NODE_DBG(pdata);
// NODE_DBG("\n");
lua_pushlstring(gL, pdata, len);
// lua_pushinteger(gL, len);
lua_call(gL, 2, 0);
}

static void net_socket_sent(void *arg)
{
// NODE_DBG("net_socket_sent is called.\n");
NODE_DBG("net_socket_sent is called.\n");
struct espconn *pesp_conn = arg;
if(pesp_conn == NULL)
return;
Expand Down Expand Up @@ -195,26 +177,6 @@ static void net_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
NODE_DBG("self_ref null.\n");
return;
}
/* original
if(ipaddr == NULL)
{
NODE_ERR( "DNS Fail!\n" );
goto end;
}
// ipaddr->addr is a uint32_t ip
char ip_str[20];
c_memset(ip_str, 0, sizeof(ip_str));
if(ipaddr->addr != 0)
{
c_sprintf(ip_str, IPSTR, IP2STR(&(ipaddr->addr)));
}
lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->cb_dns_found_ref); // the callback function
//lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(conn) to callback func in lua
lua_pushstring(gL, ip_str); // the ip para
*/

// "enhanced"

lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->cb_dns_found_ref); // the callback function
lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(conn) to callback func in lua
Expand All @@ -233,7 +195,6 @@ static void net_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
}
lua_pushstring(gL, ip_str); // the ip para
}
// "enhanced" end

lua_call(gL, 2, 0);

Expand All @@ -258,16 +219,6 @@ static void net_server_connected(void *arg) // for tcp only
if(pesp_conn == NULL)
return;

#if 0
char temp[20] = {0};
c_sprintf(temp, IPSTR, IP2STR( &(pesp_conn->proto.tcp->remote_ip) ) );
NODE_DBG("remote ");
NODE_DBG(temp);
NODE_DBG(":");
NODE_DBG("%d",pesp_conn->proto.tcp->remote_port);
NODE_DBG(" connected.\n");
#endif

for(i=0;i<MAX_SOCKET;i++){
if(socket[i] == LUA_NOREF) // found empty slot
{
Expand Down Expand Up @@ -846,10 +797,6 @@ static int net_close( lua_State* L, const char* mt )
if(isserver && skt == NULL){
if(socket[i] != LUA_NOREF){ // there is client socket exists
lua_rawgeti(L, LUA_REGISTRYINDEX, socket[i]); // get the referenced user_data to stack top
#if 0
socket[i] = LUA_NOREF;
socket_num--;
#endif // do this in net_server_disconnected
i++;
if(lua_isuserdata(L,-1)){
skt = lua_touserdata(L,-1);
Expand Down Expand Up @@ -889,23 +836,9 @@ static int net_close( lua_State* L, const char* mt )
}
}
}
#if 0
// unref the self_ref
if(LUA_NOREF!=skt->self_ref){ // for a server self_ref is NOREF
luaL_unref(L, LUA_REGISTRYINDEX, skt->self_ref);
skt->self_ref = LUA_NOREF; // for a socket, now only var in lua is ref to the userdata
}
#endif
lua_settop(L, n); // reset the stack top
skt = NULL;
} while( isserver && i<MAX_SOCKET);
#if 0
// unref the self_ref, for both socket and server
if(LUA_NOREF!=nud->self_ref){ // for a server self_ref is NOREF
luaL_unref(L, LUA_REGISTRYINDEX, nud->self_ref);
nud->self_ref = LUA_NOREF; // now only var in lua is ref to the userdata
}
#endif

return 0;
}
Expand Down Expand Up @@ -1010,16 +943,6 @@ static int net_send( lua_State* L, const char* mt )
return luaL_error( L, "tcp server send not supported" );
}

#if 0
char temp[20] = {0};
c_sprintf(temp, IPSTR, IP2STR( &(pesp_conn->proto.tcp->remote_ip) ) );
NODE_DBG("remote ");
NODE_DBG(temp);
NODE_DBG(":");
NODE_DBG("%d",pesp_conn->proto.tcp->remote_port);
NODE_DBG(" sending data.\n");
#endif

const char *payload = luaL_checklstring( L, 2, &l );
if (l>1460 || payload == NULL)
return luaL_error( L, "need <1460 payload" );
Expand Down Expand Up @@ -1415,34 +1338,6 @@ static int net_getdnsserver( lua_State* L )
return 1;
}

#if 0
static int net_array_index( lua_State* L )
{
char** parray = luaL_checkudata(L, 1, "net.array");
int index = luaL_checkint(L, 2);
lua_pushnumber(L, (*parray)[index-1]);
return 1;
}

static int net_array_newindex( lua_State* L )
{
char** parray = luaL_checkudata(L, 1, "net.array");
int index = luaL_checkint(L, 2);
int value = luaL_checkint(L, 3);
(*parray)[index-1] = value;
return 0;
}

// expose an array to lua, by storing it in a userdata with the array metatable
static int expose_array(lua_State* L, char *array, unsigned short len) {
char** parray = lua_newuserdata(L, len);
*parray = array;
luaL_getmetatable(L, "net.array");
lua_setmetatable(L, -2);
return 1;
}
#endif

extern const LUA_REG_TYPE tls_cert_map[];

// Module function map
Expand Down Expand Up @@ -1471,13 +1366,6 @@ static const LUA_REG_TYPE net_socket_map[] = {
{ LSTRKEY( "__index" ), LROVAL( net_socket_map ) },
{ LNILKEY, LNILVAL }
};
#if 0
static const LUA_REG_TYPE net_array_map[] = {
{ LSTRKEY( "__index" ), LFUNCVAL( net_array_index ) },
{ LSTRKEY( "__newindex" ), LFUNCVAL( net_array_newindex ) },
{ LNILKEY, LNILVAL }
};
#endif

static const LUA_REG_TYPE net_dns_map[] = {
{ LSTRKEY( "setdnsserver" ), LFUNCVAL( net_setdnsserver ) },
Expand Down Expand Up @@ -1511,9 +1399,6 @@ int luaopen_net( lua_State *L ) {

luaL_rometatable(L, "net.server", (void *)net_server_map); // create metatable for net.server
luaL_rometatable(L, "net.socket", (void *)net_socket_map); // create metatable for net.socket
#if 0
luaL_rometatable(L, "net.array", (void *)net_array_map); // create metatable for net.array
#endif

return 0;
}
Expand Down

0 comments on commit 552bdbe

Please sign in to comment.