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

Cherry-picks for the 3.2 branch (future 3.2.3) - 3rd batch #40413

Merged
merged 18 commits into from
Jul 15, 2020
Merged
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
73 changes: 44 additions & 29 deletions core/io/packet_peer_udp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,22 @@
#include "packet_peer_udp.h"

#include "core/io/ip.h"
#include "core/io/udp_server.h"

void PacketPeerUDP::set_blocking_mode(bool p_enable) {

blocking = p_enable;
}

void PacketPeerUDP::set_broadcast_enabled(bool p_enabled) {
ERR_FAIL_COND(udp_server);
broadcast = p_enabled;
if (_sock.is_valid() && _sock->is_open())
_sock->set_broadcasting_enabled(p_enabled);
}

Error PacketPeerUDP::join_multicast_group(IP_Address p_multi_address, String p_if_name) {

ERR_FAIL_COND_V(udp_server, ERR_LOCKED);
ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE);
ERR_FAIL_COND_V(!p_multi_address.is_valid(), ERR_INVALID_PARAMETER);

Expand All @@ -59,7 +61,7 @@ Error PacketPeerUDP::join_multicast_group(IP_Address p_multi_address, String p_i
}

Error PacketPeerUDP::leave_multicast_group(IP_Address p_multi_address, String p_if_name) {

ERR_FAIL_COND_V(udp_server, ERR_LOCKED);
ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE);
ERR_FAIL_COND_V(!_sock->is_open(), ERR_UNCONFIGURED);
return _sock->leave_multicast_group(p_multi_address, p_if_name);
Expand Down Expand Up @@ -133,7 +135,7 @@ Error PacketPeerUDP::put_packet(const uint8_t *p_buffer, int p_buffer_size) {
}

do {
if (connected) {
if (connected && !udp_server) {
err = _sock->send(p_buffer, p_buffer_size, sent);
} else {
err = _sock->sendto(p_buffer, p_buffer_size, sent, peer_addr, peer_port);
Expand Down Expand Up @@ -188,26 +190,25 @@ Error PacketPeerUDP::listen(int p_port, const IP_Address &p_bind_address, int p_
return OK;
}

Error PacketPeerUDP::connect_socket(Ref<NetSocket> p_sock) {
Error err;
int read = 0;
uint16_t r_port;
IP_Address r_ip;

err = p_sock->recvfrom(recv_buffer, sizeof(recv_buffer), read, r_ip, r_port, true);
ERR_FAIL_COND_V(err != OK, err);
err = p_sock->connect_to_host(r_ip, r_port);
ERR_FAIL_COND_V(err != OK, err);
Error PacketPeerUDP::connect_shared_socket(Ref<NetSocket> p_sock, IP_Address p_ip, uint16_t p_port, UDPServer *p_server) {
udp_server = p_server;
connected = true;
_sock = p_sock;
peer_addr = r_ip;
peer_port = r_port;
peer_addr = p_ip;
peer_port = p_port;
packet_ip = peer_addr;
packet_port = peer_port;
connected = true;
return OK;
}

void PacketPeerUDP::disconnect_shared_socket() {
udp_server = nullptr;
_sock = Ref<NetSocket>(NetSocket::create());
close();
}

Error PacketPeerUDP::connect_to_host(const IP_Address &p_host, int p_port) {
ERR_FAIL_COND_V(udp_server, ERR_LOCKED);
ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE);
ERR_FAIL_COND_V(!p_host.is_valid(), ERR_INVALID_PARAMETER);

Expand Down Expand Up @@ -245,9 +246,13 @@ bool PacketPeerUDP::is_connected_to_host() const {
}

void PacketPeerUDP::close() {

if (_sock.is_valid())
if (udp_server) {
udp_server->remove_peer(peer_addr, peer_port);
udp_server = nullptr;
_sock = Ref<NetSocket>(NetSocket::create());
} else if (_sock.is_valid()) {
_sock->close();
}
rb.resize(16);
queue_count = 0;
connected = false;
Expand All @@ -266,6 +271,9 @@ Error PacketPeerUDP::_poll() {
if (!_sock->is_open()) {
return FAILED;
}
if (udp_server) {
return OK; // Handled by UDPServer.
}

Error err;
int read;
Expand All @@ -287,23 +295,29 @@ Error PacketPeerUDP::_poll() {
return FAILED;
}

if (rb.space_left() < read + 24) {
err = store_packet(ip, port, recv_buffer, read);
#ifdef TOOLS_ENABLED
WARN_PRINTS("Buffer full, dropping packets!");
#endif
continue;
if (err != OK) {
WARN_PRINT("Buffer full, dropping packets!");
}

uint32_t port32 = port;
rb.write(ip.get_ipv6(), 16);
rb.write((uint8_t *)&port32, 4);
rb.write((uint8_t *)&read, 4);
rb.write(recv_buffer, read);
++queue_count;
#endif
}

return OK;
}

Error PacketPeerUDP::store_packet(IP_Address p_ip, uint32_t p_port, uint8_t *p_buf, int p_buf_size) {
if (rb.space_left() < p_buf_size + 24) {
return ERR_OUT_OF_MEMORY;
}
rb.write(p_ip.get_ipv6(), 16);
rb.write((uint8_t *)&p_port, 4);
rb.write((uint8_t *)&p_buf_size, 4);
rb.write(p_buf, p_buf_size);
++queue_count;
return OK;
}

bool PacketPeerUDP::is_listening() const {

return _sock.is_valid() && _sock->is_open();
Expand Down Expand Up @@ -349,6 +363,7 @@ PacketPeerUDP::PacketPeerUDP() :
connected(false),
blocking(true),
broadcast(false),
udp_server(nullptr),
_sock(Ref<NetSocket>(NetSocket::create())) {
rb.resize(16);
}
Expand Down
7 changes: 6 additions & 1 deletion core/io/packet_peer_udp.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
#include "core/io/net_socket.h"
#include "core/io/packet_peer.h"

class UDPServer;

class PacketPeerUDP : public PacketPeer {
GDCLASS(PacketPeerUDP, PacketPeer);

Expand All @@ -55,6 +57,7 @@ class PacketPeerUDP : public PacketPeer {
bool connected;
bool blocking;
bool broadcast;
UDPServer *udp_server;
Ref<NetSocket> _sock;

static void _bind_methods();
Expand All @@ -72,7 +75,9 @@ class PacketPeerUDP : public PacketPeer {
Error wait();
bool is_listening() const;

Error connect_socket(Ref<NetSocket> p_sock); // Used by UDPServer
Error connect_shared_socket(Ref<NetSocket> p_sock, IP_Address p_ip, uint16_t p_port, UDPServer *ref); // Used by UDPServer
void disconnect_shared_socket(); // Used by UDPServer
Error store_packet(IP_Address p_ip, uint32_t p_port, uint8_t *p_buf, int p_buf_size); // Used internally and by UDPServer
Error connect_to_host(const IP_Address &p_host, int p_port);
bool is_connected_to_host() const;

Expand Down
27 changes: 14 additions & 13 deletions core/io/translation_loader_po.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@
#include "core/os/file_access.h"
#include "core/translation.h"

RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error, const String &p_path) {

RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error) {
enum Status {

STATUS_NONE,
STATUS_READING_ID,
STATUS_READING_STRING,
Expand All @@ -56,6 +54,7 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error, const S
bool skip_this = false;
bool skip_next = false;
bool is_eof = false;
const String path = f->get_path();

while (!is_eof) {

Expand All @@ -67,7 +66,7 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error, const S

if (status == STATUS_READING_ID) {
memdelete(f);
ERR_FAIL_V_MSG(RES(), p_path + ":" + itos(line) + " Unexpected EOF while reading 'msgid' at file: ");
ERR_FAIL_V_MSG(RES(), "Unexpected EOF while reading 'msgid' at: " + path + ":" + itos(line));
} else {
break;
}
Expand All @@ -76,9 +75,8 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error, const S
if (l.begins_with("msgid")) {

if (status == STATUS_READING_ID) {

memdelete(f);
ERR_FAIL_V_MSG(RES(), p_path + ":" + itos(line) + " Unexpected 'msgid', was expecting 'msgstr' while parsing: ");
ERR_FAIL_V_MSG(RES(), "Unexpected 'msgid', was expecting 'msgstr' while parsing: " + path + ":" + itos(line));
}

if (msg_id != "") {
Expand All @@ -98,9 +96,8 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error, const S
if (l.begins_with("msgstr")) {

if (status != STATUS_READING_ID) {

memdelete(f);
ERR_FAIL_V_MSG(RES(), p_path + ":" + itos(line) + " Unexpected 'msgstr', was expecting 'msgid' while parsing: ");
ERR_FAIL_V_MSG(RES(), "Unexpected 'msgstr', was expecting 'msgid' while parsing: " + path + ":" + itos(line));
}

l = l.substr(6, l.length()).strip_edges();
Expand All @@ -115,7 +112,10 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error, const S
continue; //nothing to read or comment
}

ERR_FAIL_COND_V_MSG(!l.begins_with("\"") || status == STATUS_NONE, RES(), p_path + ":" + itos(line) + " Invalid line '" + l + "' while parsing: ");
if (!l.begins_with("\"") || status == STATUS_NONE) {
memdelete(f);
ERR_FAIL_V_MSG(RES(), "Invalid line '" + l + "' while parsing: " + path + ":" + itos(line));
}

l = l.substr(1, l.length());
//find final quote
Expand All @@ -128,7 +128,10 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error, const S
}
}

ERR_FAIL_COND_V_MSG(end_pos == -1, RES(), p_path + ":" + itos(line) + " Expected '\"' at end of message while parsing file: ");
if (end_pos == -1) {
memdelete(f);
ERR_FAIL_V_MSG(RES(), "Expected '\"' at end of message while parsing: " + path + ":" + itos(line));
}

l = l.substr(0, end_pos);
l = l.c_unescape();
Expand All @@ -141,7 +144,6 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error, const S
line++;
}

f->close();
memdelete(f);

if (status == STATUS_READING_STRING) {
Expand All @@ -153,7 +155,7 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error, const S
config = msg_str;
}

ERR_FAIL_COND_V_MSG(config == "", RES(), "No config found in file: " + p_path + ".");
ERR_FAIL_COND_V_MSG(config == "", RES(), "No config found in file: " + path + ".");

Vector<String> configs = config.split("\n");
for (int i = 0; i < configs.size(); i++) {
Expand Down Expand Up @@ -190,7 +192,6 @@ RES TranslationLoaderPO::load(const String &p_path, const String &p_original_pat
void TranslationLoaderPO::get_recognized_extensions(List<String> *p_extensions) const {

p_extensions->push_back("po");
//p_extensions->push_back("mo"); //mo in the future...
}
bool TranslationLoaderPO::handles_type(const String &p_type) const {

Expand Down
2 changes: 1 addition & 1 deletion core/io/translation_loader_po.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

class TranslationLoaderPO : public ResourceFormatLoader {
public:
static RES load_translation(FileAccess *f, Error *r_error, const String &p_path = String());
static RES load_translation(FileAccess *f, Error *r_error);
virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = NULL);
virtual void get_recognized_extensions(List<String> *p_extensions) const;
virtual bool handles_type(const String &p_type) const;
Expand Down
Loading