diff options
author | RĂ©mi Verschelde <rverschelde@gmail.com> | 2020-05-14 23:09:03 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-14 23:09:03 +0200 |
commit | 00949f0c5fcc6a4f8382a4a97d5591fd9ec380f8 (patch) | |
tree | 2b1c31f45add24085b64425ce440f577424c16a1 /modules/websocket | |
parent | 5046f666a1181675b39f156c38346525dc1c444e (diff) | |
parent | 0ee0fa42e6639b6fa474b7cf6afc6b1a78142185 (diff) |
Merge pull request #38738 from akien-mga/cause-we-never-go-out-of-style
Style: Remove new line at block start, enforce line between functions, enforce braces in if and loop blocks
Diffstat (limited to 'modules/websocket')
24 files changed, 96 insertions, 157 deletions
diff --git a/modules/websocket/editor_debugger_server_websocket.cpp b/modules/websocket/editor_debugger_server_websocket.cpp index 0cf78eaa9e..95ea7ceafa 100644 --- a/modules/websocket/editor_debugger_server_websocket.cpp +++ b/modules/websocket/editor_debugger_server_websocket.cpp @@ -1,12 +1,12 @@ /*************************************************************************/ -/* script_editor_debugger_websocket.cpp */ +/* editor_debugger_server_websocket.cpp */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -39,8 +39,9 @@ void EditorDebuggerServerWebSocket::_peer_connected(int p_id, String _protocol) } void EditorDebuggerServerWebSocket::_peer_disconnected(int p_id, bool p_was_clean) { - if (pending_peers.find(p_id)) + if (pending_peers.find(p_id)) { pending_peers.erase(p_id); + } } void EditorDebuggerServerWebSocket::poll() { diff --git a/modules/websocket/editor_debugger_server_websocket.h b/modules/websocket/editor_debugger_server_websocket.h index 81a31d8364..c66db1b72a 100644 --- a/modules/websocket/editor_debugger_server_websocket.h +++ b/modules/websocket/editor_debugger_server_websocket.h @@ -1,12 +1,12 @@ /*************************************************************************/ -/* script_editor_debugger_websocket.h */ +/* editor_debugger_server_websocket.h */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -36,7 +36,6 @@ #include "editor/debugger/editor_debugger_server.h" class EditorDebuggerServerWebSocket : public EditorDebuggerServer { - GDCLASS(EditorDebuggerServerWebSocket, EditorDebuggerServer); private: diff --git a/modules/websocket/emws_client.cpp b/modules/websocket/emws_client.cpp index bc9d75d327..7c31449709 100644 --- a/modules/websocket/emws_client.cpp +++ b/modules/websocket/emws_client.cpp @@ -65,7 +65,6 @@ EMSCRIPTEN_KEEPALIVE void _esws_on_close(void *obj, int code, char *reason, int } Error EMWSClient::connect_to_host(String p_host, String p_path, uint16_t p_port, bool p_ssl, const Vector<String> p_protocols, const Vector<String> p_custom_headers) { - String proto_string; for (int i = 0; i < p_protocols.size(); i++) { if (i != 0) @@ -190,12 +189,10 @@ void EMWSClient::poll() { } Ref<WebSocketPeer> EMWSClient::get_peer(int p_peer_id) const { - return _peer; } NetworkedMultiplayerPeer::ConnectionStatus EMWSClient::get_connection_status() const { - if (_peer->is_connected_to_host()) { if (_is_connecting) return CONNECTION_CONNECTING; @@ -206,17 +203,14 @@ NetworkedMultiplayerPeer::ConnectionStatus EMWSClient::get_connection_status() c }; void EMWSClient::disconnect_from_host(int p_code, String p_reason) { - _peer->close(p_code, p_reason); }; IP_Address EMWSClient::get_connected_host() const { - ERR_FAIL_V_MSG(IP_Address(), "Not supported in HTML5 export."); }; uint16_t EMWSClient::get_connected_port() const { - ERR_FAIL_V_MSG(0, "Not supported in HTML5 export."); }; @@ -244,7 +238,6 @@ EMWSClient::EMWSClient() { }; EMWSClient::~EMWSClient() { - disconnect_from_host(); _peer = Ref<EMWSPeer>(); /* clang-format off */ diff --git a/modules/websocket/emws_client.h b/modules/websocket/emws_client.h index 9f1c220ed4..ab8a0612bb 100644 --- a/modules/websocket/emws_client.h +++ b/modules/websocket/emws_client.h @@ -38,7 +38,6 @@ #include "websocket_client.h" class EMWSClient : public WebSocketClient { - GDCIIMPL(EMWSClient, WebSocketClient); private: diff --git a/modules/websocket/emws_peer.cpp b/modules/websocket/emws_peer.cpp index 9472daa620..749f45451a 100644 --- a/modules/websocket/emws_peer.cpp +++ b/modules/websocket/emws_peer.cpp @@ -34,7 +34,6 @@ #include "core/io/ip.h" void EMWSPeer::set_sock(int p_sock, unsigned int p_in_buf_size, unsigned int p_in_pkt_size) { - peer_sock = p_sock; _in_buffer.resize(p_in_pkt_size, p_in_buf_size); _packet_buffer.resize((1 << p_in_buf_size)); @@ -49,13 +48,11 @@ EMWSPeer::WriteMode EMWSPeer::get_write_mode() const { } Error EMWSPeer::read_msg(uint8_t *p_data, uint32_t p_size, bool p_is_string) { - uint8_t is_string = p_is_string ? 1 : 0; return _in_buffer.write_packet(p_data, p_size, &is_string); } Error EMWSPeer::put_packet(const uint8_t *p_buffer, int p_buffer_size) { - int is_bin = write_mode == WebSocketPeer::WRITE_MODE_BINARY ? 1 : 0; /* clang-format off */ @@ -86,7 +83,6 @@ Error EMWSPeer::put_packet(const uint8_t *p_buffer, int p_buffer_size) { }; Error EMWSPeer::get_packet(const uint8_t **r_buffer, int &r_buffer_size) { - if (_in_buffer.packets_left() == 0) return ERR_UNAVAILABLE; @@ -101,22 +97,18 @@ Error EMWSPeer::get_packet(const uint8_t **r_buffer, int &r_buffer_size) { }; int EMWSPeer::get_available_packet_count() const { - return _in_buffer.packets_left(); }; bool EMWSPeer::was_string_packet() const { - return _is_string; }; bool EMWSPeer::is_connected_to_host() const { - return peer_sock != -1; }; void EMWSPeer::close(int p_code, String p_reason) { - if (peer_sock != -1) { /* clang-format off */ EM_ASM({ @@ -134,17 +126,14 @@ void EMWSPeer::close(int p_code, String p_reason) { }; IP_Address EMWSPeer::get_connected_host() const { - ERR_FAIL_V_MSG(IP_Address(), "Not supported in HTML5 export."); }; uint16_t EMWSPeer::get_connected_port() const { - ERR_FAIL_V_MSG(0, "Not supported in HTML5 export."); }; void EMWSPeer::set_no_delay(bool p_enabled) { - ERR_FAIL_MSG("'set_no_delay' is not supported in HTML5 export."); } @@ -155,7 +144,6 @@ EMWSPeer::EMWSPeer() { }; EMWSPeer::~EMWSPeer() { - close(); }; diff --git a/modules/websocket/emws_peer.h b/modules/websocket/emws_peer.h index 6308ebe490..9c00f2d58b 100644 --- a/modules/websocket/emws_peer.h +++ b/modules/websocket/emws_peer.h @@ -41,7 +41,6 @@ #include "websocket_peer.h" class EMWSPeer : public WebSocketPeer { - GDCIIMPL(EMWSPeer, WebSocketPeer); private: diff --git a/modules/websocket/emws_server.cpp b/modules/websocket/emws_server.cpp index 95cffb4775..9d43283d3e 100644 --- a/modules/websocket/emws_server.cpp +++ b/modules/websocket/emws_server.cpp @@ -34,7 +34,6 @@ #include "core/os/os.h" Error EMWSServer::listen(int p_port, Vector<String> p_protocols, bool gd_mp_api) { - return FAILED; } @@ -60,12 +59,10 @@ Vector<String> EMWSServer::get_protocols() const { } IP_Address EMWSServer::get_peer_address(int p_peer_id) const { - return IP_Address(); } int EMWSServer::get_peer_port(int p_peer_id) const { - return 0; } diff --git a/modules/websocket/emws_server.h b/modules/websocket/emws_server.h index f273fd078f..bb6f35a711 100644 --- a/modules/websocket/emws_server.h +++ b/modules/websocket/emws_server.h @@ -38,7 +38,6 @@ #include "websocket_server.h" class EMWSServer : public WebSocketServer { - GDCIIMPL(EMWSServer, WebSocketServer); public: diff --git a/modules/websocket/packet_buffer.h b/modules/websocket/packet_buffer.h index 5f5f0e20cd..9973efe297 100644 --- a/modules/websocket/packet_buffer.h +++ b/modules/websocket/packet_buffer.h @@ -36,7 +36,6 @@ template <class T> class PacketBuffer { - private: typedef struct { uint32_t size; diff --git a/modules/websocket/remote_debugger_peer_websocket.cpp b/modules/websocket/remote_debugger_peer_websocket.cpp index d156a39f53..a67a959e31 100644 --- a/modules/websocket/remote_debugger_peer_websocket.cpp +++ b/modules/websocket/remote_debugger_peer_websocket.cpp @@ -1,12 +1,12 @@ /*************************************************************************/ -/* script_debugger_websocket.cpp */ +/* remote_debugger_peer_websocket.cpp */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -33,7 +33,6 @@ #include "core/project_settings.h" Error RemoteDebuggerPeerWebSocket::connect_to_host(const String &p_uri) { - Vector<String> protocols; protocols.push_back("binary"); // Compatibility for emscripten TCP-to-WebSocket. @@ -41,7 +40,6 @@ Error RemoteDebuggerPeerWebSocket::connect_to_host(const String &p_uri) { ws_client->poll(); if (ws_client->get_connection_status() == WebSocketClient::CONNECTION_DISCONNECTED) { - ERR_PRINT("Remote Debugger: Unable to connect. Status: " + String::num(ws_client->get_connection_status()) + "."); return FAILED; } @@ -90,8 +88,9 @@ Array RemoteDebuggerPeerWebSocket::get_message() { } Error RemoteDebuggerPeerWebSocket::put_message(const Array &p_arr) { - if (out_queue.size() >= max_queued_messages) + if (out_queue.size() >= max_queued_messages) { return ERR_OUT_OF_MEMORY; + } out_queue.push_back(p_arr); return OK; } diff --git a/modules/websocket/remote_debugger_peer_websocket.h b/modules/websocket/remote_debugger_peer_websocket.h index fe46533bed..bb03e5e892 100644 --- a/modules/websocket/remote_debugger_peer_websocket.h +++ b/modules/websocket/remote_debugger_peer_websocket.h @@ -1,12 +1,12 @@ /*************************************************************************/ -/* script_debugger_websocket.h */ +/* remote_debugger_peer_websocket.h */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -39,7 +39,6 @@ #include "core/debugger/remote_debugger_peer.h" class RemoteDebuggerPeerWebSocket : public RemoteDebuggerPeer { - Ref<WebSocketClient> ws_client; Ref<WebSocketPeer> ws_peer; List<Array> in_queue; diff --git a/modules/websocket/websocket_client.cpp b/modules/websocket/websocket_client.cpp index 7ee4b990b5..3900180739 100644 --- a/modules/websocket/websocket_client.cpp +++ b/modules/websocket/websocket_client.cpp @@ -33,7 +33,6 @@ GDCINULL(WebSocketClient); WebSocketClient::WebSocketClient() { - verify_ssl = true; } @@ -54,8 +53,9 @@ Error WebSocketClient::connect_to_url(String p_url, const Vector<String> p_proto port = 443; } else { ssl = false; - if (host.begins_with("ws://")) + if (host.begins_with("ws://")) { host = host.substr(5, host.length() - 5); + } } // Path @@ -76,33 +76,27 @@ Error WebSocketClient::connect_to_url(String p_url, const Vector<String> p_proto } void WebSocketClient::set_verify_ssl_enabled(bool p_verify_ssl) { - verify_ssl = p_verify_ssl; } bool WebSocketClient::is_verify_ssl_enabled() const { - return verify_ssl; } Ref<X509Certificate> WebSocketClient::get_trusted_ssl_certificate() const { - return ssl_cert; } void WebSocketClient::set_trusted_ssl_certificate(Ref<X509Certificate> p_cert) { - ERR_FAIL_COND(get_connection_status() != CONNECTION_DISCONNECTED); ssl_cert = p_cert; } bool WebSocketClient::is_server() const { - return false; } void WebSocketClient::_on_peer_packet() { - if (_is_multiplayer) { _process_multiplayer(get_peer(1), 1); } else { @@ -111,7 +105,6 @@ void WebSocketClient::_on_peer_packet() { } void WebSocketClient::_on_connect(String p_protocol) { - if (_is_multiplayer) { // need to wait for ID confirmation... } else { @@ -120,12 +113,10 @@ void WebSocketClient::_on_connect(String p_protocol) { } void WebSocketClient::_on_close_request(int p_code, String p_reason) { - emit_signal("server_close_request", p_code, p_reason); } void WebSocketClient::_on_disconnect(bool p_was_clean) { - if (_is_multiplayer) { emit_signal("connection_failed"); } else { @@ -134,7 +125,6 @@ void WebSocketClient::_on_disconnect(bool p_was_clean) { } void WebSocketClient::_on_error() { - if (_is_multiplayer) { emit_signal("connection_failed"); } else { diff --git a/modules/websocket/websocket_client.h b/modules/websocket/websocket_client.h index 4dc1224066..ba8e21aed6 100644 --- a/modules/websocket/websocket_client.h +++ b/modules/websocket/websocket_client.h @@ -37,7 +37,6 @@ #include "websocket_peer.h" class WebSocketClient : public WebSocketMultiplayerPeer { - GDCLASS(WebSocketClient, WebSocketMultiplayerPeer); GDCICLASS(WebSocketClient); diff --git a/modules/websocket/websocket_multiplayer_peer.cpp b/modules/websocket/websocket_multiplayer_peer.cpp index 9b71b32e33..fa2fe891a5 100644 --- a/modules/websocket/websocket_multiplayer_peer.cpp +++ b/modules/websocket/websocket_multiplayer_peer.cpp @@ -33,7 +33,6 @@ #include "core/os/os.h" WebSocketMultiplayerPeer::WebSocketMultiplayerPeer() { - _is_multiplayer = false; _peer_id = 0; _target_peer = 0; @@ -46,16 +45,13 @@ WebSocketMultiplayerPeer::WebSocketMultiplayerPeer() { } WebSocketMultiplayerPeer::~WebSocketMultiplayerPeer() { - _clear(); } int WebSocketMultiplayerPeer::_gen_unique_id() const { - uint32_t hash = 0; while (hash == 0 || hash == 1) { - hash = hash_djb2_one_32( (uint32_t)OS::get_singleton()->get_ticks_usec()); hash = hash_djb2_one_32( @@ -71,11 +67,12 @@ int WebSocketMultiplayerPeer::_gen_unique_id() const { return hash; } -void WebSocketMultiplayerPeer::_clear() { +void WebSocketMultiplayerPeer::_clear() { _peer_map.clear(); - if (_current_packet.data != nullptr) + if (_current_packet.data != nullptr) { memfree(_current_packet.data); + } for (List<Packet>::Element *E = _incoming_packets.front(); E; E = E->next()) { memfree(E->get().data); @@ -86,7 +83,6 @@ void WebSocketMultiplayerPeer::_clear() { } void WebSocketMultiplayerPeer::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_buffers", "input_buffer_size_kb", "input_max_packets", "output_buffer_size_kb", "output_max_packets"), &WebSocketMultiplayerPeer::set_buffers); ClassDB::bind_method(D_METHOD("get_peer", "peer_id"), &WebSocketMultiplayerPeer::get_peer); @@ -97,14 +93,12 @@ void WebSocketMultiplayerPeer::_bind_methods() { // PacketPeer // int WebSocketMultiplayerPeer::get_available_packet_count() const { - ERR_FAIL_COND_V_MSG(!_is_multiplayer, 0, "Please use get_peer(ID).get_available_packet_count to get available packet count from peers when not using the MultiplayerAPI."); return _incoming_packets.size(); } Error WebSocketMultiplayerPeer::get_packet(const uint8_t **r_buffer, int &r_buffer_size) { - ERR_FAIL_COND_V_MSG(!_is_multiplayer, ERR_UNCONFIGURED, "Please use get_peer(ID).get_packet/var to communicate with peers when not using the MultiplayerAPI."); r_buffer_size = 0; @@ -124,7 +118,6 @@ Error WebSocketMultiplayerPeer::get_packet(const uint8_t **r_buffer, int &r_buff } Error WebSocketMultiplayerPeer::put_packet(const uint8_t *p_buffer, int p_buffer_size) { - ERR_FAIL_COND_V_MSG(!_is_multiplayer, ERR_UNCONFIGURED, "Please use get_peer(ID).put_packet/var to communicate with peers when not using the MultiplayerAPI."); Vector<uint8_t> buffer = _make_pkt(SYS_NONE, get_unique_id(), _target_peer, p_buffer, p_buffer_size); @@ -140,23 +133,19 @@ Error WebSocketMultiplayerPeer::put_packet(const uint8_t *p_buffer, int p_buffer // NetworkedMultiplayerPeer // void WebSocketMultiplayerPeer::set_transfer_mode(TransferMode p_mode) { - // Websocket uses TCP, reliable } NetworkedMultiplayerPeer::TransferMode WebSocketMultiplayerPeer::get_transfer_mode() const { - // Websocket uses TCP, reliable return TRANSFER_MODE_RELIABLE; } void WebSocketMultiplayerPeer::set_target_peer(int p_target_peer) { - _target_peer = p_target_peer; } int WebSocketMultiplayerPeer::get_packet_peer() const { - ERR_FAIL_COND_V_MSG(!_is_multiplayer, 1, "This function is not available when not using the MultiplayerAPI."); ERR_FAIL_COND_V(_incoming_packets.size() == 0, 1); @@ -164,22 +153,18 @@ int WebSocketMultiplayerPeer::get_packet_peer() const { } int WebSocketMultiplayerPeer::get_unique_id() const { - return _peer_id; } void WebSocketMultiplayerPeer::set_refuse_new_connections(bool p_enable) { - _refusing = p_enable; } bool WebSocketMultiplayerPeer::is_refusing_new_connections() const { - return _refusing; } void WebSocketMultiplayerPeer::_send_sys(Ref<WebSocketPeer> p_peer, uint8_t p_type, int32_t p_peer_id) { - ERR_FAIL_COND(!p_peer.is_valid()); ERR_FAIL_COND(!p_peer->is_connected_to_host()); @@ -188,7 +173,6 @@ void WebSocketMultiplayerPeer::_send_sys(Ref<WebSocketPeer> p_peer, uint8_t p_ty } Vector<uint8_t> WebSocketMultiplayerPeer::_make_pkt(uint8_t p_type, int32_t p_from, int32_t p_to, const uint8_t *p_data, uint32_t p_data_size) { - Vector<uint8_t> out; out.resize(PROTO_SIZE + p_data_size); @@ -202,7 +186,6 @@ Vector<uint8_t> WebSocketMultiplayerPeer::_make_pkt(uint8_t p_type, int32_t p_fr } void WebSocketMultiplayerPeer::_send_add(int32_t p_peer_id) { - // First of all, confirm the ID! _send_sys(get_peer(p_peer_id), SYS_ID, p_peer_id); @@ -211,8 +194,9 @@ void WebSocketMultiplayerPeer::_send_add(int32_t p_peer_id) { for (Map<int, Ref<WebSocketPeer>>::Element *E = _peer_map.front(); E; E = E->next()) { int32_t id = E->key(); - if (p_peer_id == id) + if (p_peer_id == id) { continue; // Skip the newwly added peer (already confirmed) + } // Send new peer to others _send_sys(get_peer(id), SYS_ADD, p_peer_id); @@ -224,8 +208,9 @@ void WebSocketMultiplayerPeer::_send_add(int32_t p_peer_id) { void WebSocketMultiplayerPeer::_send_del(int32_t p_peer_id) { for (Map<int, Ref<WebSocketPeer>>::Element *E = _peer_map.front(); E; E = E->next()) { int32_t id = E->key(); - if (p_peer_id != id) + if (p_peer_id != id) { _send_sys(get_peer(id), SYS_DEL, p_peer_id); + } } } @@ -242,27 +227,25 @@ void WebSocketMultiplayerPeer::_store_pkt(int32_t p_source, int32_t p_dest, cons Error WebSocketMultiplayerPeer::_server_relay(int32_t p_from, int32_t p_to, const uint8_t *p_buffer, uint32_t p_buffer_size) { if (p_to == 1) { - return OK; // Will not send to self } else if (p_to == 0) { - for (Map<int, Ref<WebSocketPeer>>::Element *E = _peer_map.front(); E; E = E->next()) { - if (E->key() != p_from) + if (E->key() != p_from) { E->get()->put_packet(p_buffer, p_buffer_size); + } } return OK; // Sent to all but sender } else if (p_to < 0) { - for (Map<int, Ref<WebSocketPeer>>::Element *E = _peer_map.front(); E; E = E->next()) { - if (E->key() != p_from && E->key() != -p_to) + if (E->key() != p_from && E->key() != -p_to) { E->get()->put_packet(p_buffer, p_buffer_size); + } } return OK; // Sent to all but sender and excluded } else { - ERR_FAIL_COND_V(p_to == p_from, FAILED); Ref<WebSocketPeer> peer_to = get_peer(p_to); @@ -273,7 +256,6 @@ Error WebSocketMultiplayerPeer::_server_relay(int32_t p_from, int32_t p_to, cons } void WebSocketMultiplayerPeer::_process_multiplayer(Ref<WebSocketPeer> p_peer, uint32_t p_peer_id) { - ERR_FAIL_COND(!p_peer.is_valid()); const uint8_t *in_buffer; @@ -304,21 +286,19 @@ void WebSocketMultiplayerPeer::_process_multiplayer(Ref<WebSocketPeer> p_peer, u _store_pkt(from, to, in_buffer, data_size); } else if (to == 0) { - // Broadcast, for us too _store_pkt(from, to, in_buffer, data_size); } else if (to < 0) { - // All but one, for us if not excluded - if (_peer_id != -(int32_t)p_peer_id) + if (_peer_id != -(int32_t)p_peer_id) { _store_pkt(from, to, in_buffer, data_size); + } } // Relay if needed (i.e. "to" includes a peer that is not the server) _server_relay(from, to, in_buffer, size); } else { - if (type == SYS_NONE) { // Payload message _store_pkt(from, to, in_buffer, data_size); @@ -331,12 +311,12 @@ void WebSocketMultiplayerPeer::_process_multiplayer(Ref<WebSocketPeer> p_peer, u copymem(&id, &in_buffer[PROTO_SIZE], 4); switch (type) { - case SYS_ADD: // Add peer _peer_map[id] = Ref<WebSocketPeer>(); emit_signal("peer_connected", id); - if (id == 1) // We just connected to the server + if (id == 1) { // We just connected to the server emit_signal("connection_succeeded"); + } break; case SYS_DEL: // Remove peer diff --git a/modules/websocket/websocket_multiplayer_peer.h b/modules/websocket/websocket_multiplayer_peer.h index c6669c730c..3ee26e2c5b 100644 --- a/modules/websocket/websocket_multiplayer_peer.h +++ b/modules/websocket/websocket_multiplayer_peer.h @@ -37,7 +37,6 @@ #include "websocket_peer.h" class WebSocketMultiplayerPeer : public NetworkedMultiplayerPeer { - GDCLASS(WebSocketMultiplayerPeer, NetworkedMultiplayerPeer); private: diff --git a/modules/websocket/websocket_peer.h b/modules/websocket/websocket_peer.h index d4173600ec..5f7d1c768b 100644 --- a/modules/websocket/websocket_peer.h +++ b/modules/websocket/websocket_peer.h @@ -36,7 +36,6 @@ #include "websocket_macros.h" class WebSocketPeer : public PacketPeer { - GDCLASS(WebSocketPeer, PacketPeer); GDCICLASS(WebSocketPeer); diff --git a/modules/websocket/websocket_server.cpp b/modules/websocket/websocket_server.cpp index a7ced65543..b20b925dec 100644 --- a/modules/websocket/websocket_server.cpp +++ b/modules/websocket/websocket_server.cpp @@ -41,7 +41,6 @@ WebSocketServer::~WebSocketServer() { } void WebSocketServer::_bind_methods() { - ClassDB::bind_method(D_METHOD("is_listening"), &WebSocketServer::is_listening); ClassDB::bind_method(D_METHOD("listen", "port", "protocols", "gd_mp_api"), &WebSocketServer::listen, DEFVAL(Vector<String>()), DEFVAL(false)); ClassDB::bind_method(D_METHOD("stop"), &WebSocketServer::stop); @@ -110,19 +109,18 @@ void WebSocketServer::set_ca_chain(Ref<X509Certificate> p_ca_chain) { } NetworkedMultiplayerPeer::ConnectionStatus WebSocketServer::get_connection_status() const { - if (is_listening()) + if (is_listening()) { return CONNECTION_CONNECTED; + } return CONNECTION_DISCONNECTED; } bool WebSocketServer::is_server() const { - return true; } void WebSocketServer::_on_peer_packet(int32_t p_peer_id) { - if (_is_multiplayer) { _process_multiplayer(get_peer(p_peer_id), p_peer_id); } else { @@ -131,7 +129,6 @@ void WebSocketServer::_on_peer_packet(int32_t p_peer_id) { } void WebSocketServer::_on_connect(int32_t p_peer_id, String p_protocol) { - if (_is_multiplayer) { // Send add to clients _send_add(p_peer_id); @@ -142,7 +139,6 @@ void WebSocketServer::_on_connect(int32_t p_peer_id, String p_protocol) { } void WebSocketServer::_on_disconnect(int32_t p_peer_id, bool p_was_clean) { - if (_is_multiplayer) { // Send delete to clients _send_del(p_peer_id); @@ -153,6 +149,5 @@ void WebSocketServer::_on_disconnect(int32_t p_peer_id, bool p_was_clean) { } void WebSocketServer::_on_close_request(int32_t p_peer_id, int p_code, String p_reason) { - emit_signal("client_close_request", p_peer_id, p_code, p_reason); } diff --git a/modules/websocket/websocket_server.h b/modules/websocket/websocket_server.h index 3ce4dbe711..5df0a37945 100644 --- a/modules/websocket/websocket_server.h +++ b/modules/websocket/websocket_server.h @@ -37,7 +37,6 @@ #include "websocket_peer.h" class WebSocketServer : public WebSocketMultiplayerPeer { - GDCLASS(WebSocketServer, WebSocketMultiplayerPeer); GDCICLASS(WebSocketServer); diff --git a/modules/websocket/wsl_client.cpp b/modules/websocket/wsl_client.cpp index 0eaafe2d66..7d16c2e99f 100644 --- a/modules/websocket/wsl_client.cpp +++ b/modules/websocket/wsl_client.cpp @@ -117,10 +117,11 @@ bool WSLClient::_verify_headers(String &r_protocol) { ERR_FAIL_COND_V_MSG(header.size() != 2, false, "Invalid header -> " + psa[i] + "."); String name = header[0].to_lower(); String value = header[1].strip_edges(); - if (headers.has(name)) + if (headers.has(name)) { headers[name] += "," + value; - else + } else { headers[name] = value; + } } #define _WSL_CHECK(NAME, VALUE) \ @@ -142,19 +143,20 @@ bool WSLClient::_verify_headers(String &r_protocol) { r_protocol = headers["sec-websocket-protocol"]; bool valid = false; for (int i = 0; i < _protocols.size(); i++) { - if (_protocols[i] != r_protocol) + if (_protocols[i] != r_protocol) { continue; + } valid = true; break; } - if (!valid) + if (!valid) { return false; + } } return true; } Error WSLClient::connect_to_host(String p_host, String p_path, uint16_t p_port, bool p_ssl, const Vector<String> p_protocols, const Vector<String> p_custom_headers) { - ERR_FAIL_COND_V(_connection.is_valid(), ERR_ALREADY_IN_USE); _peer = Ref<WSLPeer>(memnew(WSLPeer)); @@ -200,8 +202,9 @@ Error WSLClient::connect_to_host(String p_host, String p_path, uint16_t p_port, if (p_protocols.size() > 0) { request += "Sec-WebSocket-Protocol: "; for (int i = 0; i < p_protocols.size(); i++) { - if (i != 0) + if (i != 0) { request += ","; + } request += p_protocols[i]; } request += "\r\n"; @@ -229,8 +232,9 @@ void WSLClient::poll() { return; } - if (_connection.is_null()) + if (_connection.is_null()) { return; // Not connected. + } switch (_tcp->get_status()) { case StreamPeerTCP::STATUS_NONE: @@ -257,9 +261,9 @@ void WSLClient::poll() { ERR_FAIL_COND(ssl.is_null()); // Bug? ssl->poll(); } - if (ssl->get_status() == StreamPeerSSL::STATUS_HANDSHAKING) + if (ssl->get_status() == StreamPeerSSL::STATUS_HANDSHAKING) { return; // Need more polling. - else if (ssl->get_status() != StreamPeerSSL::STATUS_CONNECTED) { + } else if (ssl->get_status() != StreamPeerSSL::STATUS_CONNECTED) { disconnect_from_host(); _on_error(); return; // Error. @@ -278,25 +282,24 @@ void WSLClient::poll() { } Ref<WebSocketPeer> WSLClient::get_peer(int p_peer_id) const { - ERR_FAIL_COND_V(p_peer_id != 1, nullptr); return _peer; } NetworkedMultiplayerPeer::ConnectionStatus WSLClient::get_connection_status() const { - - if (_peer->is_connected_to_host()) + if (_peer->is_connected_to_host()) { return CONNECTION_CONNECTED; + } - if (_tcp->is_connected_to_host()) + if (_tcp->is_connected_to_host()) { return CONNECTION_CONNECTING; + } return CONNECTION_DISCONNECTED; } void WSLClient::disconnect_from_host(int p_code, String p_reason) { - _peer->close(p_code, p_reason); _connection = Ref<StreamPeer>(nullptr); _tcp = Ref<StreamPeerTCP>(memnew(StreamPeerTCP)); @@ -314,13 +317,11 @@ void WSLClient::disconnect_from_host(int p_code, String p_reason) { } IP_Address WSLClient::get_connected_host() const { - ERR_FAIL_COND_V(!_peer->is_connected_to_host(), IP_Address()); return _peer->get_connected_host(); } uint16_t WSLClient::get_connected_port() const { - ERR_FAIL_COND_V(!_peer->is_connected_to_host(), 0); return _peer->get_connected_port(); } @@ -347,7 +348,6 @@ WSLClient::WSLClient() { } WSLClient::~WSLClient() { - _peer->close_now(); _peer->invalidate(); disconnect_from_host(); diff --git a/modules/websocket/wsl_client.h b/modules/websocket/wsl_client.h index 2cecfd97ee..8355a5a737 100644 --- a/modules/websocket/wsl_client.h +++ b/modules/websocket/wsl_client.h @@ -41,7 +41,6 @@ #include "wslay/wslay.h" class WSLClient : public WebSocketClient { - GDCIIMPL(WSLClient, WebSocketClient); private: diff --git a/modules/websocket/wsl_peer.cpp b/modules/websocket/wsl_peer.cpp index 44b71f70f4..bf1ba43f8a 100644 --- a/modules/websocket/wsl_peer.cpp +++ b/modules/websocket/wsl_peer.cpp @@ -60,8 +60,9 @@ String WSLPeer::compute_key_response(String p_key) { } void WSLPeer::_wsl_destroy(struct PeerData **p_data) { - if (!p_data || !(*p_data)) + if (!p_data || !(*p_data)) { return; + } struct PeerData *data = *p_data; if (data->polling) { data->destroy = true; @@ -147,8 +148,9 @@ void wsl_msg_recv_callback(wslay_event_context_ptr ctx, const struct wslay_event } WSLPeer *peer = (WSLPeer *)peer_data->peer; - if (peer->parse_message(arg) != OK) + if (peer->parse_message(arg) != OK) { return; + } if (peer_data->is_server) { WSLServer *helper = (WSLServer *)peer_data->obj; @@ -209,10 +211,11 @@ void WSLPeer::make_context(PeerData *p_data, unsigned int p_in_buf_size, unsigne _data->peer = this; _data->valid = true; - if (_data->is_server) + if (_data->is_server) { wslay_event_context_server_init(&(_data->ctx), &wsl_callbacks, _data); - else + } else { wslay_event_context_client_init(&(_data->ctx), &wsl_callbacks, _data); + } wslay_event_config_set_max_recv_msg_length(_data->ctx, (1ULL << p_in_buf_size)); } @@ -225,8 +228,9 @@ WSLPeer::WriteMode WSLPeer::get_write_mode() const { } void WSLPeer::poll() { - if (!_data) + if (!_data) { return; + } if (_wsl_poll(_data)) { _data = nullptr; @@ -234,7 +238,6 @@ void WSLPeer::poll() { } Error WSLPeer::put_packet(const uint8_t *p_buffer, int p_buffer_size) { - ERR_FAIL_COND_V(!is_connected_to_host(), FAILED); struct wslay_event_msg msg; // Should I use fragmented? @@ -251,13 +254,13 @@ Error WSLPeer::put_packet(const uint8_t *p_buffer, int p_buffer_size) { } Error WSLPeer::get_packet(const uint8_t **r_buffer, int &r_buffer_size) { - r_buffer_size = 0; ERR_FAIL_COND_V(!is_connected_to_host(), FAILED); - if (_in_buffer.packets_left() == 0) + if (_in_buffer.packets_left() == 0) { return ERR_UNAVAILABLE; + } int read = 0; uint8_t *rw = _packet_buffer.ptrw(); @@ -270,20 +273,18 @@ Error WSLPeer::get_packet(const uint8_t **r_buffer, int &r_buffer_size) { } int WSLPeer::get_available_packet_count() const { - - if (!is_connected_to_host()) + if (!is_connected_to_host()) { return 0; + } return _in_buffer.packets_left(); } bool WSLPeer::was_string_packet() const { - return _is_string; } bool WSLPeer::is_connected_to_host() const { - return _data != nullptr; } @@ -305,28 +306,26 @@ void WSLPeer::close(int p_code, String p_reason) { } IP_Address WSLPeer::get_connected_host() const { - ERR_FAIL_COND_V(!is_connected_to_host() || _data->tcp.is_null(), IP_Address()); return _data->tcp->get_connected_host(); } uint16_t WSLPeer::get_connected_port() const { - ERR_FAIL_COND_V(!is_connected_to_host() || _data->tcp.is_null(), 0); return _data->tcp->get_connected_port(); } void WSLPeer::set_no_delay(bool p_enabled) { - ERR_FAIL_COND(!is_connected_to_host() || _data->tcp.is_null()); _data->tcp->set_no_delay(p_enabled); } void WSLPeer::invalidate() { - if (_data) + if (_data) { _data->valid = false; + } } WSLPeer::WSLPeer() { @@ -337,7 +336,6 @@ WSLPeer::WSLPeer() { } WSLPeer::~WSLPeer() { - close(); invalidate(); _wsl_destroy(&_data); diff --git a/modules/websocket/wsl_peer.h b/modules/websocket/wsl_peer.h index 3b0639831a..fe4abfb64c 100644 --- a/modules/websocket/wsl_peer.h +++ b/modules/websocket/wsl_peer.h @@ -44,7 +44,6 @@ #define WSL_MAX_HEADER_SIZE 4096 class WSLPeer : public WebSocketPeer { - GDCIIMPL(WSLPeer, WebSocketPeer); public: diff --git a/modules/websocket/wsl_server.cpp b/modules/websocket/wsl_server.cpp index cc4685973e..da7bfc70c0 100644 --- a/modules/websocket/wsl_server.cpp +++ b/modules/websocket/wsl_server.cpp @@ -60,10 +60,11 @@ bool WSLServer::PendingPeer::_parse_request(const Vector<String> p_protocols) { ERR_FAIL_COND_V_MSG(header.size() != 2, false, "Invalid header -> " + psa[i]); String name = header[0].to_lower(); String value = header[1].strip_edges(); - if (headers.has(name)) + if (headers.has(name)) { headers[name] += "," + value; - else + } else { headers[name] = value; + } } #define _WSL_CHECK(NAME, VALUE) \ ERR_FAIL_COND_V_MSG(!headers.has(NAME) || headers[NAME].to_lower() != VALUE, false, \ @@ -83,44 +84,52 @@ bool WSLServer::PendingPeer::_parse_request(const Vector<String> p_protocols) { String proto = protos[i].strip_edges(); // Check if we have the given protocol for (int j = 0; j < p_protocols.size(); j++) { - if (proto != p_protocols[j]) + if (proto != p_protocols[j]) { continue; + } protocol = proto; break; } // Found a protocol - if (protocol != "") + if (protocol != "") { break; + } } - if (protocol == "") // Invalid protocol(s) requested + if (protocol == "") { // Invalid protocol(s) requested return false; - } else if (p_protocols.size() > 0) // No protocol requested, but we need one + } + } else if (p_protocols.size() > 0) { // No protocol requested, but we need one return false; + } return true; } Error WSLServer::PendingPeer::do_handshake(const Vector<String> p_protocols) { - if (OS::get_singleton()->get_ticks_msec() - time > WSL_SERVER_TIMEOUT) + if (OS::get_singleton()->get_ticks_msec() - time > WSL_SERVER_TIMEOUT) { return ERR_TIMEOUT; + } if (use_ssl) { Ref<StreamPeerSSL> ssl = static_cast<Ref<StreamPeerSSL>>(connection); - if (ssl.is_null()) + if (ssl.is_null()) { return FAILED; + } ssl->poll(); - if (ssl->get_status() == StreamPeerSSL::STATUS_HANDSHAKING) + if (ssl->get_status() == StreamPeerSSL::STATUS_HANDSHAKING) { return ERR_BUSY; - else if (ssl->get_status() != StreamPeerSSL::STATUS_CONNECTED) + } else if (ssl->get_status() != StreamPeerSSL::STATUS_CONNECTED) { return FAILED; + } } if (!has_request) { int read = 0; while (true) { ERR_FAIL_COND_V_MSG(req_pos >= WSL_MAX_HEADER_SIZE, ERR_OUT_OF_MEMORY, "Response headers too big."); Error err = connection->get_partial_data(&req_buf[req_pos], 1, read); - if (err != OK) // Got an error + if (err != OK) { // Got an error return FAILED; - else if (read != 1) // Busy, wait next poll + } else if (read != 1) { // Busy, wait next poll return ERR_BUSY; + } char *r = (char *)req_buf; int l = req_pos; if (l > 3 && r[l] == '\n' && r[l - 1] == '\r' && r[l - 2] == '\n' && r[l - 3] == '\r') { @@ -132,8 +141,9 @@ Error WSLServer::PendingPeer::do_handshake(const Vector<String> p_protocols) { s += "Upgrade: websocket\r\n"; s += "Connection: Upgrade\r\n"; s += "Sec-WebSocket-Accept: " + WSLPeer::compute_key_response(key) + "\r\n"; - if (protocol != "") + if (protocol != "") { s += "Sec-WebSocket-Protocol: " + protocol + "\r\n"; + } s += "\r\n"; response = s.utf8(); has_request = true; @@ -150,8 +160,9 @@ Error WSLServer::PendingPeer::do_handshake(const Vector<String> p_protocols) { } response_sent += sent; } - if (response_sent < response.size() - 1) + if (response_sent < response.size() - 1) { return ERR_BUSY; + } return OK; } @@ -169,7 +180,6 @@ Error WSLServer::listen(int p_port, const Vector<String> p_protocols, bool gd_mp } void WSLServer::poll() { - List<int> remove_ids; for (Map<int, Ref<WebSocketPeer>>::Element *E = _peer_map.front(); E; E = E->next()) { Ref<WSLPeer> peer = (WSLPeer *)E->get().ptr(); @@ -217,13 +227,15 @@ void WSLServer::poll() { } remove_peers.clear(); - if (!_server->is_listening()) + if (!_server->is_listening()) { return; + } while (_server->is_connection_available()) { Ref<StreamPeerTCP> conn = _server->take_connection(); - if (is_refusing_new_connections()) + if (is_refusing_new_connections()) { continue; // Conn will go out-of-scope and be closed. + } Ref<PendingPeer> peer = memnew(PendingPeer); if (private_key.is_valid() && ssl_cert.is_valid()) { diff --git a/modules/websocket/wsl_server.h b/modules/websocket/wsl_server.h index 2e893d6727..f86de02797 100644 --- a/modules/websocket/wsl_server.h +++ b/modules/websocket/wsl_server.h @@ -43,12 +43,10 @@ #define WSL_SERVER_TIMEOUT 1000 class WSLServer : public WebSocketServer { - GDCIIMPL(WSLServer, WebSocketServer); private: class PendingPeer : public Reference { - private: bool _parse_request(const Vector<String> p_protocols); |