diff options
author | Fabio Alessandrelli <fabio.alessandrelli@gmail.com> | 2018-04-10 17:52:10 +0200 |
---|---|---|
committer | Fabio Alessandrelli <fabio.alessandrelli@gmail.com> | 2018-04-12 12:30:51 +0200 |
commit | 6b9ec810c6ccf096b0357ebf3f5fe6e3423d290b (patch) | |
tree | 4620c4da42243341d0343aa581ccecaf72402d63 /modules/websocket/lws_peer.cpp | |
parent | 23fc8ca223acb7a2150a57280a3ddf9abac575e3 (diff) |
Implement get_peer_[address|port] in ENet/WSServer
Also implement get_connected_host and get_connected_port in WebSocketPeer
(not supported in HTML5 due to browser limitation).
Add shorthand disconnect_peer(id) for get_peer(id)->close() like in ENet to
WebSocketServer.
Diffstat (limited to 'modules/websocket/lws_peer.cpp')
-rw-r--r-- | modules/websocket/lws_peer.cpp | 39 |
1 files changed, 37 insertions, 2 deletions
diff --git a/modules/websocket/lws_peer.cpp b/modules/websocket/lws_peer.cpp index ba45d7688f..8a064fb5a4 100644 --- a/modules/websocket/lws_peer.cpp +++ b/modules/websocket/lws_peer.cpp @@ -32,6 +32,13 @@ #include "lws_peer.h" #include "core/io/ip.h" +// Needed for socket_helpers on Android at least. UNIXes has it, just include if not windows +#if !defined(WINDOWS_ENABLED) +#include <netinet/in.h> +#endif + +#include "drivers/unix/socket_helpers.h" + void LWSPeer::set_wsi(struct lws *p_wsi) { wsi = p_wsi; }; @@ -178,12 +185,40 @@ void LWSPeer::close() { IP_Address LWSPeer::get_connected_host() const { - return IP_Address(); + ERR_FAIL_COND_V(!is_connected_to_host(), IP_Address()); + + IP_Address ip; + int port = 0; + + socklen_t len; + struct sockaddr_storage addr; + int fd = lws_get_socket_fd(wsi); + + int ret = getpeername(fd, (struct sockaddr *)&addr, &len); + ERR_FAIL_COND_V(ret != 0, IP_Address()); + + _set_ip_addr_port(ip, port, &addr); + + return ip; }; uint16_t LWSPeer::get_connected_port() const { - return 1025; + ERR_FAIL_COND_V(!is_connected_to_host(), 0); + + IP_Address ip; + int port = 0; + + socklen_t len; + struct sockaddr_storage addr; + int fd = lws_get_socket_fd(wsi); + + int ret = getpeername(fd, (struct sockaddr *)&addr, &len); + ERR_FAIL_COND_V(ret != 0, 0); + + _set_ip_addr_port(ip, port, &addr); + + return port; }; LWSPeer::LWSPeer() { |