-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
udp and getpeer #223
Comments
Have been investigating further on. function pr(c,pl)
print ("parameter info c")
print(type(c), " ",getmetatable(c));
print ("metadatainfo c");
mt = getmetatable(c);
for n,s in pairs(mt) do print(n," ",s) end
end
if srv ~= nil then srv:close() end
if udpsrv ~= nil then udpsrv:close() end
counter=1
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive",pr)
conn:on("sent",function(conn) print("tcp package sent, Heap = "..node.heap()) conn:close() end)
end)
udpsrv=net.createServer(net.UDP)
udpsrv:on("receive",pr)
udpsrv:on("sent", function(conn) print("udp packet sent \n") end)
udpsrv:listen(8000) which results for UDP in
And for TCP in :
Still trying to find why there is this difference. |
LS, static const LUA_REG_TYPE net_server_map[] =
{
{ LSTRKEY( "listen" ), LFUNCVAL ( net_server_listen ) },
{ LSTRKEY( "close" ), LFUNCVAL ( net_server_close ) },
{ LSTRKEY( "on" ), LFUNCVAL ( net_udpserver_on ) },
{ LSTRKEY( "send" ), LFUNCVAL ( net_udpserver_send ) },
// { LSTRKEY( "delete" ), LFUNCVAL ( net_server_delete ) },
{ LSTRKEY( "getpeer" ), LFUNCVAL ( net_socket_getpeer ) },
{ LSTRKEY( "__gc" ), LFUNCVAL ( net_server_delete ) },
#if LUA_OPTIMIZE_MEMORY > 0
{ LSTRKEY( "__index" ), LROVAL ( net_server_map ) },
#endif
{ LNILKEY, LNILVAL }
}; 2 : Update getpeer to : static int net_socket_getpeer( lua_State* L )
{
NODE_DBG("Entered getpeer \n");
lnet_userdata *nud;
nud = (lnet_userdata *)luaL_testudata(L, 1, "net.server");
if (nud == NULL)
{
nud = (lnet_userdata *)luaL_testudata(L, 1, "net.socket");
}
luaL_argcheck(L, nud, 1, "Server/Socket expected");
if(nud!=NULL && nud->pesp_conn!=NULL ){
char temp[20] = {0};
int tempPort = 0;
c_sprintf(temp, IPSTR, IP2STR((nud->pesp_conn->type == TCP) ? &(nud->pesp_conn->proto.tcp->remote_ip) : &(nud->pesp_conn->proto.udp->remote_ip) ) );
tempPort = (nud->pesp_conn->type == TCP) ? nud->pesp_conn->proto.tcp->remote_port : nud->pesp_conn->proto.udp->remote_port;
if ( tempPort != 0 ) {
lua_pushstring( L, temp );
lua_pushinteger( L, tempPort );
} else {
lua_pushnil( L );
lua_pushnil( L );
}
} else {
lua_pushnil( L );
lua_pushnil( L );
}
return 2;
} 3: As I needed to check userdata to both net.server and net.socket, I cannot use the luaL_checkudata function. I exits with error instead of "just returning NULL" when not found. LUALIB_API void *luaL_testudata (lua_State *L, int ud, const char *tname) {
void *p = lua_touserdata(L, ud);
if (p != NULL) { /* value is a userdata? */
if (lua_getmetatable(L, ud)) { /* does it have a metatable? */
luaL_getmetatable(L, tname); /* get correct metatable */
if (!lua_rawequal(L, -1, -2)) /* not the same? */
p = NULL; /* value is a userdata with wrong metatable */
lua_pop(L, 2); /* remove both metatables */
return p;
}
}
return NULL; /* value is not a userdata with a metatable */
} If you need more information, let me know. |
SDK 1.4 Release notes Item 24:
So the SDK now facilitates this and so we could consider this enhancement for the 1.4-based dev |
As this is my first message on this project, thank you to all creaters/contributors of this project, nodemcu is a great firmware for esp modules. @hreintke, I tried your patch and got "nil, nil" for getpeer() function. After some investigation, I found that both "temp" and "tempPort" are assigned 0 (in "net_socket_getpeer" function). I could not go further. My environment: I use esp8266-01 and test my udp server code accross android phone, running "UDP Sender/Receiver" app from Google Play. I need the peer IP to let the esp module reply to clients. Right now, I am planning to workaround this problem by appending client IP to its message and get it parsed in esp code so it can use this IP to create udp connection reply back to the sender. However, I still want getpeer() function to work. Any help would be great. |
@lazy21tr : As you can see in the above comment, the project took the SDK 1.4.0 functionality to solve the issue. |
Is this issue resolved. Still getting same error attempt to call method 'getpeer' (a nil value)) client=net.createConnection(net.UDP, 0)
port=8972
print("IP:"..wifi.sta.getip()..", Port:"..port)
srv=net.createServer(net.UDP)
srv:on("receive", function(srv, pl)
-- rIP='192.168.42.1'
rIP,rPort=srv:getpeer()
print("From "..rIP..", reveived:"..pl)
client:connect(8972,rIP)
client:send("1234")
client:close()
client = nil
end
)
srv:listen(port) I tried to use both dev and master branch. thanks |
OK, I will have a look at this and update this comment accordingly in 5 hours or so. BTW, why are you doing this if you want to use the same port? You could just do |
OK, I've tracked this one down. It is related to #1080. The underlying issue is that the UDP implementation is a bit of a botch. A A As a quick workaround, I've just added the remote and local ip and ports as arguments to the receive callback and the remote IP:port is always reported as 0.0.0.0:0 in the espconn structure so I need to look at this further.. |
Hi... Thanks. Will |
Yes and it will send back to the same port as you are listening on. |
OK, as well as this, there's a bug in --- a/app/modules/net.c
+++ b/app/modules/net.c
@@ -145,6 +145,7 @@ static void net_socket_received(void *arg, char *pdata, unsigned short len)
{
NODE_DBG("net_socket_received is called.\n");
struct espconn *pesp_conn = arg;
+ int nargs = 2;
if(pesp_conn == NULL)
return;
lnet_userdata *nud = (lnet_userdata *)pesp_conn->reverse;
@@ -161,8 +162,18 @@ static void net_socket_received(void *arg, char *pdata, unsigned short len)
// NODE_DBG(pdata);
// NODE_DBG("\n");
lua_pushlstring(gL, pdata, len);
- // lua_pushinteger(gL, len);
- lua_call(gL, 2, 0);
+ if (pesp_conn->type == ESPCONN_UDP) {
+ esp_udp *u = pesp_conn->proto.udp;
+ char ip_str[20] = {0};
+ c_sprintf(ip_str, IPSTR, IP2STR(&(u->remote_ip)));
+ lua_pushstring(gL, ip_str); // the ip para
+ lua_pushinteger(gL, u->remote_port);
+ c_sprintf(ip_str, IPSTR, IP2STR(&(u->local_ip)));
+ lua_pushstring(gL, ip_str); // the ip para
+ lua_pushinteger(gL, u->local_port);
+ nargs+=4;
+ }
+ lua_call(gL, nargs, 0);
}
static void net_socket_sent(void *arg)
--- a/app/lwip/app/espconn_udp.c
+++ b/app/lwip/app/espconn_udp.c
@@ -308,6 +308,11 @@ espconn_udp_recv(void *arg, struct udp_pcb *upcb, struct pbuf *p,
wifi_get_ip_info(0, &ipconfig);
}
+ precv->pespconn->proto.udp->remote_ip[0] = ip4_addr1_16(addr);
+ precv->pespconn->proto.udp->remote_ip[1] = ip4_addr2_16(addr);
+ precv->pespconn->proto.udp->remote_ip[2] = ip4_addr3_16(addr);
+ precv->pespconn->proto.udp->remote_ip[3] = ip4_addr4_16(addr);
+ precv->pespconn->proto.udp->remote_port = port;
precv->pespconn->proto.udp->local_ip[0] = ip4_addr1_16(&ipconfig.ip);
precv->pespconn->proto.udp->local_ip[1] = ip4_addr2_16(&ipconfig.ip);
precv->pespconn->proto.udp->local_ip[2] = ip4_addr3_16(&ipconfig.ip); and now a |
Yes.. client=net.createConnection(net.UDP, 0)
port=8972
print(("IP:"%s, Port: %u):format(wifi.sta.getip(), port))
srv=net.createServer(net.UDP)
srv:on("receive", function(srv, pl)
print("received :"..pl)
srv:send("1234")
end)
srv:listen(port) Node JS code: var PORT = 8972;
var dgram = require('dgram');
var server = dgram.createSocket('udp4');
server.on('listening', function () {
var address = server.address();
console.log('UDP Server listening on ' + address.address + ":" + address.port);
});
server.on('message', function (message, remote) {
console.log(remote.address + ':' + remote.port +' - ' + message);
});
server.bind(PORT);
var HOST = '192.168.42.239';
var message = new Buffer('Are you there?');
server.send(message, 0, message.length, PORT, HOST, function(err, bytes) {
if (err) throw err;
console.log('UDP message sent to ' + HOST +':'+ PORT);
}); @sekarpdkt: Please use markup to make other readers lives easier!! |
See issue nodemcu#223. This fixes the bug in the Espressif LiWP UDP wrapper and adds getpeer processing for UDP.
I have decided not to add the extra parameters to the receive callback but to implement the |
Used the dev branch to compile a version with getpeer() option.
Works OK for tcp connections but get error
"PANIC: unprotected error in call to Lua API (servertest1.lua:2: attempt to call method 'getpeer' (a nil value))"
when using it on an UDP server connection.
The text was updated successfully, but these errors were encountered: