Skip to content
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

Implemented getsockname and poll #41600

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/io/net_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class NetSocket : public Reference {
virtual Error recvfrom(uint8_t *p_buffer, int p_len, int &r_read, IP_Address &r_ip, uint16_t &r_port, bool p_peek = false) = 0;
virtual Error send(const uint8_t *p_buffer, int p_len, int &r_sent) = 0;
virtual Error sendto(const uint8_t *p_buffer, int p_len, int &r_sent, IP_Address p_ip, uint16_t p_port) = 0;
virtual Error getsockname(IP_Address &r_ip, uint16_t &r_port) = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should likely be get_socket_name. Other methods here are not snake_case, but that's a separate problem.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And in godot.cpp too?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this name would need to be changed in many places if it's changed on this line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The godot.cpp is a separate class and is in the thirdparty folder, that's why I asked.
Ok, I'll modify it tomorrow (it's late at night here).

virtual Ref<NetSocket> accept(IP_Address &r_ip, uint16_t &r_port) = 0;

virtual bool is_open() const = 0;
Expand Down
31 changes: 31 additions & 0 deletions drivers/unix/net_socket_posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,37 @@ Error NetSocketPosix::sendto(const uint8_t *p_buffer, int p_len, int &r_sent, IP
return OK;
}

Error NetSocketPosix::getsockname(IP_Address &r_ip, uint16_t &r_port) {
ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);

struct sockaddr_storage local;
socklen_t len = sizeof(struct sockaddr_storage);
int ret = ::getsockname(_sock, (struct sockaddr *)&local, &len);

if (ret != 0) {
NetError err = _get_socket_error();
if (err == ERR_NET_IN_PROGRESS) {
return ERR_BUSY;
}
return FAILED;
}

if (local.ss_family == AF_INET) {
struct sockaddr_in *sin_local = (struct sockaddr_in *)&local;
r_ip.set_ipv4((uint8_t *)&sin_local->sin_addr);
r_port = ntohs(sin_local->sin_port);
} else if (local.ss_family == AF_INET6) {
struct sockaddr_in6 *s6_local = (struct sockaddr_in6 *)&local;
r_ip.set_ipv6((uint8_t *)&s6_local->sin6_addr);
r_port = ntohs(s6_local->sin6_port);
} else {
// Unsupported socket family, should never happen.
ERR_FAIL_V(FAILED);
}

return OK;
}

Error NetSocketPosix::set_broadcasting_enabled(bool p_enabled) {
ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
// IPv6 has no broadcast support.
Expand Down
1 change: 1 addition & 0 deletions drivers/unix/net_socket_posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class NetSocketPosix : public NetSocket {
virtual Error recvfrom(uint8_t *p_buffer, int p_len, int &r_read, IP_Address &r_ip, uint16_t &r_port, bool p_peek = false);
virtual Error send(const uint8_t *p_buffer, int p_len, int &r_sent);
virtual Error sendto(const uint8_t *p_buffer, int p_len, int &r_sent, IP_Address p_ip, uint16_t p_port);
virtual Error getsockname(IP_Address &r_ip, uint16_t &r_port);
virtual Ref<NetSocket> accept(IP_Address &r_ip, uint16_t &r_port);

virtual bool is_open() const;
Expand Down
63 changes: 60 additions & 3 deletions thirdparty/enet/godot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class ENetGodotSocket {
virtual Error sendto(const uint8_t *p_buffer, int p_len, int &r_sent, IP_Address p_ip, uint16_t p_port) = 0;
virtual Error recvfrom(uint8_t *p_buffer, int p_len, int &r_read, IP_Address &r_ip, uint16_t &r_port) = 0;
virtual int set_option(ENetSocketOption p_option, int p_value) = 0;
virtual Error getsockname(IP_Address &r_ip, uint16_t &r_port) = 0;
virtual Error poll(NetSocket::PollType p_type, int p_timeout) = 0;
virtual void close() = 0;
virtual void set_refuse_new_connections(bool p_enable) {} /* Only used by dtls server */
virtual ~ENetGodotSocket() {}
Expand Down Expand Up @@ -140,6 +142,14 @@ class ENetUDP : public ENetGodotSocket {
return -1;
}

Error getsockname(IP_Address &r_ip, uint16_t &r_port) {
return sock->getsockname(r_ip, r_port);
}

Error poll(NetSocket::PollType p_type, int p_timeout) {
return sock->poll(p_type, p_timeout);
}

void close() {
sock->close();
}
Expand Down Expand Up @@ -221,6 +231,14 @@ class ENetDTLSClient : public ENetGodotSocket {
return -1;
}

Error getsockname(IP_Address &r_ip, uint16_t &r_port) {
return FAILED;
}

Error poll(NetSocket::PollType p_type, int p_timeout) {
return FAILED;
}

void close() {
dtls->disconnect_from_peer();
udp->close();
Expand Down Expand Up @@ -334,6 +352,14 @@ class ENetDTLSServer : public ENetGodotSocket {
return -1;
}

Error getsockname(IP_Address &r_ip, uint16_t &r_port) {
return FAILED;
}

Error poll(NetSocket::PollType p_type, int p_timeout) {
return FAILED;
}

void close() {
for (Map<String, Ref<PacketPeerDTLS>>::Element *E = peers.front(); E; E = E->next()) {
E->get()->disconnect_from_peer();
Expand Down Expand Up @@ -493,13 +519,44 @@ int enet_socket_receive(ENetSocket socket, ENetAddress *address, ENetBuffer *buf
return read;
}

// Not implemented
int enet_socket_wait(ENetSocket socket, enet_uint32 *condition, enet_uint32 timeout) {
return 0; // do we need this function?
ENetGodotSocket *sock = (ENetGodotSocket *)socket;

NetSocket::PollType pollType;

if (*condition & ENET_SOCKET_WAIT_SEND && *condition & ENET_SOCKET_WAIT_RECEIVE) {
pollType = NetSocket::PollType::POLL_TYPE_IN_OUT;
} else if (*condition & ENET_SOCKET_WAIT_SEND) {
pollType = NetSocket::PollType::POLL_TYPE_OUT;
} else {
pollType = NetSocket::PollType::POLL_TYPE_IN;
}
Error err = sock->poll(pollType, timeout);

if (err != OK) {
return -1;
}

return OK;
}

int enet_socket_get_address(ENetSocket socket, ENetAddress *address) {
return -1; // do we need this function?
ERR_FAIL_COND_V(address == NULL, -1);

ENetGodotSocket *sock = (ENetGodotSocket *)socket;

IP_Address ip;
uint16_t port;

if (sock->getsockname(ip, port) != OK) {
return -1;
}

enet_address_set_ip(address, ip.get_ipv6(), 16);

address->port = port;

return 0;
}

int enet_socketset_select(ENetSocket maxSocket, ENetSocketSet *readSet, ENetSocketSet *writeSet, enet_uint32 timeout) {
Expand Down