1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-12 13:20:55 +00:00

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.
This commit is contained in:
Fabio Alessandrelli
2018-04-10 17:52:10 +02:00
parent 23fc8ca223
commit 6b9ec810c6
11 changed files with 121 additions and 5 deletions

View File

@@ -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() {