2017-12-21 10:13:23 +08:00
/**************************************************************************/
/* emws_peer.cpp */
/**************************************************************************/
/* This file is part of: */
2017-12-21 10:47:16 +08:00
/* GODOT ENGINE */
/* https://godotengine.org */
2017-12-21 10:13:23 +08:00
/**************************************************************************/
2018-03-21 17:07:51 +08:00
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
2017-12-21 10:13:23 +08:00
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
2019-01-01 19:46:36 +08:00
2017-12-21 10:13:23 +08:00
# include "emws_peer.h"
2022-02-16 20:56:32 +08:00
2023-06-13 22:56:21 +08:00
# ifdef WEB_ENABLED
2017-12-21 10:13:23 +08:00
# include "core/io/ip.h"
[WebSocket] Refactor websocket module.
This commit is a huge refactor of the websocket module.
The module is really old, and some design choices had to be
re-evaluated.
The WebSocketClient and WebSocketServer classes are now gone, and
WebSocketPeer can act as either client or server.
The WebSocketMultiplayerPeer class is no longer abstract, and implements
the Multiplayer API on top of the lower level WebSocketPeer.
WebSocketPeer is now a "raw" peer, like StreamPeerTCP and StreamPeerTLS,
so it emits no signal, and just needs polling to update its internal
state.
To use it as a client, simply call WebSocketPeer.coonect_to_url, then
frequently poll the peer until STATE_OPEN is reached and then you can
write or read from it, or STATE_CLOSED and then you can check the
disconnect code and reason).
To implement a server instead, a TCPServer must be created, and the
accepted connections needs to be provided to
WebSocketPeer.accept_stream (which will perform the HTTP handshake).
A full example of a WebSocketServer using TLS will be provided in the
demo repository.
2022-09-25 04:44:44 +08:00
void EMWSPeer : : _esws_on_connect ( void * p_obj , char * p_proto ) {
EMWSPeer * peer = static_cast < EMWSPeer * > ( p_obj ) ;
peer - > ready_state = STATE_OPEN ;
peer - > selected_protocol . parse_utf8 ( p_proto ) ;
2017-12-21 10:13:23 +08:00
}
[WebSocket] Refactor websocket module.
This commit is a huge refactor of the websocket module.
The module is really old, and some design choices had to be
re-evaluated.
The WebSocketClient and WebSocketServer classes are now gone, and
WebSocketPeer can act as either client or server.
The WebSocketMultiplayerPeer class is no longer abstract, and implements
the Multiplayer API on top of the lower level WebSocketPeer.
WebSocketPeer is now a "raw" peer, like StreamPeerTCP and StreamPeerTLS,
so it emits no signal, and just needs polling to update its internal
state.
To use it as a client, simply call WebSocketPeer.coonect_to_url, then
frequently poll the peer until STATE_OPEN is reached and then you can
write or read from it, or STATE_CLOSED and then you can check the
disconnect code and reason).
To implement a server instead, a TCPServer must be created, and the
accepted connections needs to be provided to
WebSocketPeer.accept_stream (which will perform the HTTP handshake).
A full example of a WebSocketServer using TLS will be provided in the
demo repository.
2022-09-25 04:44:44 +08:00
void EMWSPeer : : _esws_on_message ( void * p_obj , const uint8_t * p_data , int p_data_size , int p_is_string ) {
EMWSPeer * peer = static_cast < EMWSPeer * > ( p_obj ) ;
uint8_t is_string = p_is_string ? 1 : 0 ;
peer - > in_buffer . write_packet ( p_data , p_data_size , & is_string ) ;
2017-12-21 10:13:23 +08:00
}
[WebSocket] Refactor websocket module.
This commit is a huge refactor of the websocket module.
The module is really old, and some design choices had to be
re-evaluated.
The WebSocketClient and WebSocketServer classes are now gone, and
WebSocketPeer can act as either client or server.
The WebSocketMultiplayerPeer class is no longer abstract, and implements
the Multiplayer API on top of the lower level WebSocketPeer.
WebSocketPeer is now a "raw" peer, like StreamPeerTCP and StreamPeerTLS,
so it emits no signal, and just needs polling to update its internal
state.
To use it as a client, simply call WebSocketPeer.coonect_to_url, then
frequently poll the peer until STATE_OPEN is reached and then you can
write or read from it, or STATE_CLOSED and then you can check the
disconnect code and reason).
To implement a server instead, a TCPServer must be created, and the
accepted connections needs to be provided to
WebSocketPeer.accept_stream (which will perform the HTTP handshake).
A full example of a WebSocketServer using TLS will be provided in the
demo repository.
2022-09-25 04:44:44 +08:00
void EMWSPeer : : _esws_on_error ( void * p_obj ) {
EMWSPeer * peer = static_cast < EMWSPeer * > ( p_obj ) ;
peer - > ready_state = STATE_CLOSED ;
2017-12-21 10:13:23 +08:00
}
[WebSocket] Refactor websocket module.
This commit is a huge refactor of the websocket module.
The module is really old, and some design choices had to be
re-evaluated.
The WebSocketClient and WebSocketServer classes are now gone, and
WebSocketPeer can act as either client or server.
The WebSocketMultiplayerPeer class is no longer abstract, and implements
the Multiplayer API on top of the lower level WebSocketPeer.
WebSocketPeer is now a "raw" peer, like StreamPeerTCP and StreamPeerTLS,
so it emits no signal, and just needs polling to update its internal
state.
To use it as a client, simply call WebSocketPeer.coonect_to_url, then
frequently poll the peer until STATE_OPEN is reached and then you can
write or read from it, or STATE_CLOSED and then you can check the
disconnect code and reason).
To implement a server instead, a TCPServer must be created, and the
accepted connections needs to be provided to
WebSocketPeer.accept_stream (which will perform the HTTP handshake).
A full example of a WebSocketServer using TLS will be provided in the
demo repository.
2022-09-25 04:44:44 +08:00
void EMWSPeer : : _esws_on_close ( void * p_obj , int p_code , const char * p_reason , int p_was_clean ) {
EMWSPeer * peer = static_cast < EMWSPeer * > ( p_obj ) ;
peer - > close_code = p_code ;
peer - > close_reason . parse_utf8 ( p_reason ) ;
peer - > ready_state = STATE_CLOSED ;
2017-12-21 10:13:23 +08:00
}
2023-01-20 08:51:35 +08:00
Error EMWSPeer : : connect_to_url ( const String & p_url , Ref < TLSOptions > p_tls_options ) {
2024-07-10 18:49:27 +08:00
ERR_FAIL_COND_V ( p_url . is_empty ( ) , ERR_INVALID_PARAMETER ) ;
2023-01-20 08:51:35 +08:00
ERR_FAIL_COND_V ( p_tls_options . is_valid ( ) & & p_tls_options - > is_server ( ) , ERR_INVALID_PARAMETER ) ;
2024-07-10 18:49:27 +08:00
ERR_FAIL_COND_V ( ready_state ! = STATE_CLOSED & & ready_state ! = STATE_CLOSING , ERR_ALREADY_IN_USE ) ;
[WebSocket] Refactor websocket module.
This commit is a huge refactor of the websocket module.
The module is really old, and some design choices had to be
re-evaluated.
The WebSocketClient and WebSocketServer classes are now gone, and
WebSocketPeer can act as either client or server.
The WebSocketMultiplayerPeer class is no longer abstract, and implements
the Multiplayer API on top of the lower level WebSocketPeer.
WebSocketPeer is now a "raw" peer, like StreamPeerTCP and StreamPeerTLS,
so it emits no signal, and just needs polling to update its internal
state.
To use it as a client, simply call WebSocketPeer.coonect_to_url, then
frequently poll the peer until STATE_OPEN is reached and then you can
write or read from it, or STATE_CLOSED and then you can check the
disconnect code and reason).
To implement a server instead, a TCPServer must be created, and the
accepted connections needs to be provided to
WebSocketPeer.accept_stream (which will perform the HTTP handshake).
A full example of a WebSocketServer using TLS will be provided in the
demo repository.
2022-09-25 04:44:44 +08:00
_clear ( ) ;
String host ;
String path ;
String scheme ;
2024-05-22 10:22:50 +08:00
String fragment ;
[WebSocket] Refactor websocket module.
This commit is a huge refactor of the websocket module.
The module is really old, and some design choices had to be
re-evaluated.
The WebSocketClient and WebSocketServer classes are now gone, and
WebSocketPeer can act as either client or server.
The WebSocketMultiplayerPeer class is no longer abstract, and implements
the Multiplayer API on top of the lower level WebSocketPeer.
WebSocketPeer is now a "raw" peer, like StreamPeerTCP and StreamPeerTLS,
so it emits no signal, and just needs polling to update its internal
state.
To use it as a client, simply call WebSocketPeer.coonect_to_url, then
frequently poll the peer until STATE_OPEN is reached and then you can
write or read from it, or STATE_CLOSED and then you can check the
disconnect code and reason).
To implement a server instead, a TCPServer must be created, and the
accepted connections needs to be provided to
WebSocketPeer.accept_stream (which will perform the HTTP handshake).
A full example of a WebSocketServer using TLS will be provided in the
demo repository.
2022-09-25 04:44:44 +08:00
int port = 0 ;
2024-05-22 10:22:50 +08:00
Error err = p_url . parse_url ( scheme , host , port , path , fragment ) ;
[WebSocket] Refactor websocket module.
This commit is a huge refactor of the websocket module.
The module is really old, and some design choices had to be
re-evaluated.
The WebSocketClient and WebSocketServer classes are now gone, and
WebSocketPeer can act as either client or server.
The WebSocketMultiplayerPeer class is no longer abstract, and implements
the Multiplayer API on top of the lower level WebSocketPeer.
WebSocketPeer is now a "raw" peer, like StreamPeerTCP and StreamPeerTLS,
so it emits no signal, and just needs polling to update its internal
state.
To use it as a client, simply call WebSocketPeer.coonect_to_url, then
frequently poll the peer until STATE_OPEN is reached and then you can
write or read from it, or STATE_CLOSED and then you can check the
disconnect code and reason).
To implement a server instead, a TCPServer must be created, and the
accepted connections needs to be provided to
WebSocketPeer.accept_stream (which will perform the HTTP handshake).
A full example of a WebSocketServer using TLS will be provided in the
demo repository.
2022-09-25 04:44:44 +08:00
ERR_FAIL_COND_V_MSG ( err ! = OK , err , " Invalid URL: " + p_url ) ;
if ( scheme . is_empty ( ) ) {
scheme = " ws:// " ;
}
ERR_FAIL_COND_V_MSG ( scheme ! = " ws:// " & & scheme ! = " wss:// " , ERR_INVALID_PARAMETER , vformat ( " Invalid protocol: \" %s \" (must be either \" ws:// \" or \" wss:// \" ). " , scheme ) ) ;
String proto_string ;
for ( int i = 0 ; i < supported_protocols . size ( ) ; i + + ) {
if ( i ! = 0 ) {
proto_string + = " , " ;
}
proto_string + = supported_protocols [ i ] ;
}
if ( handshake_headers . size ( ) ) {
WARN_PRINT_ONCE ( " Custom headers are not supported in Web platform. " ) ;
}
2021-07-30 04:11:27 +08:00
[WebSocket] Refactor websocket module.
This commit is a huge refactor of the websocket module.
The module is really old, and some design choices had to be
re-evaluated.
The WebSocketClient and WebSocketServer classes are now gone, and
WebSocketPeer can act as either client or server.
The WebSocketMultiplayerPeer class is no longer abstract, and implements
the Multiplayer API on top of the lower level WebSocketPeer.
WebSocketPeer is now a "raw" peer, like StreamPeerTCP and StreamPeerTLS,
so it emits no signal, and just needs polling to update its internal
state.
To use it as a client, simply call WebSocketPeer.coonect_to_url, then
frequently poll the peer until STATE_OPEN is reached and then you can
write or read from it, or STATE_CLOSED and then you can check the
disconnect code and reason).
To implement a server instead, a TCPServer must be created, and the
accepted connections needs to be provided to
WebSocketPeer.accept_stream (which will perform the HTTP handshake).
A full example of a WebSocketServer using TLS will be provided in the
demo repository.
2022-09-25 04:44:44 +08:00
requested_url = scheme + host ;
2021-07-30 04:11:27 +08:00
[WebSocket] Refactor websocket module.
This commit is a huge refactor of the websocket module.
The module is really old, and some design choices had to be
re-evaluated.
The WebSocketClient and WebSocketServer classes are now gone, and
WebSocketPeer can act as either client or server.
The WebSocketMultiplayerPeer class is no longer abstract, and implements
the Multiplayer API on top of the lower level WebSocketPeer.
WebSocketPeer is now a "raw" peer, like StreamPeerTCP and StreamPeerTLS,
so it emits no signal, and just needs polling to update its internal
state.
To use it as a client, simply call WebSocketPeer.coonect_to_url, then
frequently poll the peer until STATE_OPEN is reached and then you can
write or read from it, or STATE_CLOSED and then you can check the
disconnect code and reason).
To implement a server instead, a TCPServer must be created, and the
accepted connections needs to be provided to
WebSocketPeer.accept_stream (which will perform the HTTP handshake).
A full example of a WebSocketServer using TLS will be provided in the
demo repository.
2022-09-25 04:44:44 +08:00
if ( port & & ( ( scheme = = " ws:// " & & port ! = 80 ) | | ( scheme = = " wss:// " & & port ! = 443 ) ) ) {
requested_url + = " : " + String : : num ( port ) ;
}
2022-11-03 14:29:39 +08:00
if ( ! path . is_empty ( ) ) {
requested_url + = path ;
}
[WebSocket] Refactor websocket module.
This commit is a huge refactor of the websocket module.
The module is really old, and some design choices had to be
re-evaluated.
The WebSocketClient and WebSocketServer classes are now gone, and
WebSocketPeer can act as either client or server.
The WebSocketMultiplayerPeer class is no longer abstract, and implements
the Multiplayer API on top of the lower level WebSocketPeer.
WebSocketPeer is now a "raw" peer, like StreamPeerTCP and StreamPeerTLS,
so it emits no signal, and just needs polling to update its internal
state.
To use it as a client, simply call WebSocketPeer.coonect_to_url, then
frequently poll the peer until STATE_OPEN is reached and then you can
write or read from it, or STATE_CLOSED and then you can check the
disconnect code and reason).
To implement a server instead, a TCPServer must be created, and the
accepted connections needs to be provided to
WebSocketPeer.accept_stream (which will perform the HTTP handshake).
A full example of a WebSocketServer using TLS will be provided in the
demo repository.
2022-09-25 04:44:44 +08:00
peer_sock = godot_js_websocket_create ( this , requested_url . utf8 ( ) . get_data ( ) , proto_string . utf8 ( ) . get_data ( ) , & _esws_on_connect , & _esws_on_message , & _esws_on_error , & _esws_on_close ) ;
if ( peer_sock = = - 1 ) {
2021-10-28 16:47:44 +08:00
return FAILED ;
}
[WebSocket] Refactor websocket module.
This commit is a huge refactor of the websocket module.
The module is really old, and some design choices had to be
re-evaluated.
The WebSocketClient and WebSocketServer classes are now gone, and
WebSocketPeer can act as either client or server.
The WebSocketMultiplayerPeer class is no longer abstract, and implements
the Multiplayer API on top of the lower level WebSocketPeer.
WebSocketPeer is now a "raw" peer, like StreamPeerTCP and StreamPeerTLS,
so it emits no signal, and just needs polling to update its internal
state.
To use it as a client, simply call WebSocketPeer.coonect_to_url, then
frequently poll the peer until STATE_OPEN is reached and then you can
write or read from it, or STATE_CLOSED and then you can check the
disconnect code and reason).
To implement a server instead, a TCPServer must be created, and the
accepted connections needs to be provided to
WebSocketPeer.accept_stream (which will perform the HTTP handshake).
A full example of a WebSocketServer using TLS will be provided in the
demo repository.
2022-09-25 04:44:44 +08:00
in_buffer . resize ( nearest_shift ( inbound_buffer_size ) , max_queued_packets ) ;
packet_buffer . resize ( inbound_buffer_size ) ;
ready_state = STATE_CONNECTING ;
return OK ;
}
Error EMWSPeer : : accept_stream ( Ref < StreamPeer > p_stream ) {
WARN_PRINT_ONCE ( " Acting as WebSocket server is not supported in Web platforms. " ) ;
return ERR_UNAVAILABLE ;
}
Error EMWSPeer : : _send ( const uint8_t * p_buffer , int p_buffer_size , bool p_binary ) {
ERR_FAIL_COND_V ( outbound_buffer_size > 0 & & ( get_current_outbound_buffered_amount ( ) + p_buffer_size > = outbound_buffer_size ) , ERR_OUT_OF_MEMORY ) ;
2021-10-28 16:47:44 +08:00
[WebSocket] Refactor websocket module.
This commit is a huge refactor of the websocket module.
The module is really old, and some design choices had to be
re-evaluated.
The WebSocketClient and WebSocketServer classes are now gone, and
WebSocketPeer can act as either client or server.
The WebSocketMultiplayerPeer class is no longer abstract, and implements
the Multiplayer API on top of the lower level WebSocketPeer.
WebSocketPeer is now a "raw" peer, like StreamPeerTCP and StreamPeerTLS,
so it emits no signal, and just needs polling to update its internal
state.
To use it as a client, simply call WebSocketPeer.coonect_to_url, then
frequently poll the peer until STATE_OPEN is reached and then you can
write or read from it, or STATE_CLOSED and then you can check the
disconnect code and reason).
To implement a server instead, a TCPServer must be created, and the
accepted connections needs to be provided to
WebSocketPeer.accept_stream (which will perform the HTTP handshake).
A full example of a WebSocketServer using TLS will be provided in the
demo repository.
2022-09-25 04:44:44 +08:00
if ( godot_js_websocket_send ( peer_sock , p_buffer , p_buffer_size , p_binary ? 1 : 0 ) ! = 0 ) {
return FAILED ;
}
2017-12-21 10:13:23 +08:00
return OK ;
2020-06-17 14:14:58 +08:00
}
2017-12-21 10:13:23 +08:00
[WebSocket] Refactor websocket module.
This commit is a huge refactor of the websocket module.
The module is really old, and some design choices had to be
re-evaluated.
The WebSocketClient and WebSocketServer classes are now gone, and
WebSocketPeer can act as either client or server.
The WebSocketMultiplayerPeer class is no longer abstract, and implements
the Multiplayer API on top of the lower level WebSocketPeer.
WebSocketPeer is now a "raw" peer, like StreamPeerTCP and StreamPeerTLS,
so it emits no signal, and just needs polling to update its internal
state.
To use it as a client, simply call WebSocketPeer.coonect_to_url, then
frequently poll the peer until STATE_OPEN is reached and then you can
write or read from it, or STATE_CLOSED and then you can check the
disconnect code and reason).
To implement a server instead, a TCPServer must be created, and the
accepted connections needs to be provided to
WebSocketPeer.accept_stream (which will perform the HTTP handshake).
A full example of a WebSocketServer using TLS will be provided in the
demo repository.
2022-09-25 04:44:44 +08:00
Error EMWSPeer : : send ( const uint8_t * p_buffer , int p_buffer_size , WriteMode p_mode ) {
2022-10-13 22:46:48 +08:00
return _send ( p_buffer , p_buffer_size , p_mode = = WRITE_MODE_BINARY ) ;
[WebSocket] Refactor websocket module.
This commit is a huge refactor of the websocket module.
The module is really old, and some design choices had to be
re-evaluated.
The WebSocketClient and WebSocketServer classes are now gone, and
WebSocketPeer can act as either client or server.
The WebSocketMultiplayerPeer class is no longer abstract, and implements
the Multiplayer API on top of the lower level WebSocketPeer.
WebSocketPeer is now a "raw" peer, like StreamPeerTCP and StreamPeerTLS,
so it emits no signal, and just needs polling to update its internal
state.
To use it as a client, simply call WebSocketPeer.coonect_to_url, then
frequently poll the peer until STATE_OPEN is reached and then you can
write or read from it, or STATE_CLOSED and then you can check the
disconnect code and reason).
To implement a server instead, a TCPServer must be created, and the
accepted connections needs to be provided to
WebSocketPeer.accept_stream (which will perform the HTTP handshake).
A full example of a WebSocketServer using TLS will be provided in the
demo repository.
2022-09-25 04:44:44 +08:00
}
Error EMWSPeer : : put_packet ( const uint8_t * p_buffer , int p_buffer_size ) {
return _send ( p_buffer , p_buffer_size , true ) ;
}
2017-12-21 10:13:23 +08:00
Error EMWSPeer : : get_packet ( const uint8_t * * r_buffer , int & r_buffer_size ) {
[WebSocket] Refactor websocket module.
This commit is a huge refactor of the websocket module.
The module is really old, and some design choices had to be
re-evaluated.
The WebSocketClient and WebSocketServer classes are now gone, and
WebSocketPeer can act as either client or server.
The WebSocketMultiplayerPeer class is no longer abstract, and implements
the Multiplayer API on top of the lower level WebSocketPeer.
WebSocketPeer is now a "raw" peer, like StreamPeerTCP and StreamPeerTLS,
so it emits no signal, and just needs polling to update its internal
state.
To use it as a client, simply call WebSocketPeer.coonect_to_url, then
frequently poll the peer until STATE_OPEN is reached and then you can
write or read from it, or STATE_CLOSED and then you can check the
disconnect code and reason).
To implement a server instead, a TCPServer must be created, and the
accepted connections needs to be provided to
WebSocketPeer.accept_stream (which will perform the HTTP handshake).
A full example of a WebSocketServer using TLS will be provided in the
demo repository.
2022-09-25 04:44:44 +08:00
if ( in_buffer . packets_left ( ) = = 0 ) {
2017-12-21 10:13:23 +08:00
return ERR_UNAVAILABLE ;
2022-02-16 20:56:32 +08:00
}
2017-12-21 10:13:23 +08:00
2018-11-03 12:46:38 +08:00
int read = 0 ;
[WebSocket] Refactor websocket module.
This commit is a huge refactor of the websocket module.
The module is really old, and some design choices had to be
re-evaluated.
The WebSocketClient and WebSocketServer classes are now gone, and
WebSocketPeer can act as either client or server.
The WebSocketMultiplayerPeer class is no longer abstract, and implements
the Multiplayer API on top of the lower level WebSocketPeer.
WebSocketPeer is now a "raw" peer, like StreamPeerTCP and StreamPeerTLS,
so it emits no signal, and just needs polling to update its internal
state.
To use it as a client, simply call WebSocketPeer.coonect_to_url, then
frequently poll the peer until STATE_OPEN is reached and then you can
write or read from it, or STATE_CLOSED and then you can check the
disconnect code and reason).
To implement a server instead, a TCPServer must be created, and the
accepted connections needs to be provided to
WebSocketPeer.accept_stream (which will perform the HTTP handshake).
A full example of a WebSocketServer using TLS will be provided in the
demo repository.
2022-09-25 04:44:44 +08:00
Error err = in_buffer . read_packet ( packet_buffer . ptrw ( ) , packet_buffer . size ( ) , & was_string , read ) ;
2018-11-03 12:46:38 +08:00
ERR_FAIL_COND_V ( err ! = OK , err ) ;
2017-12-21 10:13:23 +08:00
[WebSocket] Refactor websocket module.
This commit is a huge refactor of the websocket module.
The module is really old, and some design choices had to be
re-evaluated.
The WebSocketClient and WebSocketServer classes are now gone, and
WebSocketPeer can act as either client or server.
The WebSocketMultiplayerPeer class is no longer abstract, and implements
the Multiplayer API on top of the lower level WebSocketPeer.
WebSocketPeer is now a "raw" peer, like StreamPeerTCP and StreamPeerTLS,
so it emits no signal, and just needs polling to update its internal
state.
To use it as a client, simply call WebSocketPeer.coonect_to_url, then
frequently poll the peer until STATE_OPEN is reached and then you can
write or read from it, or STATE_CLOSED and then you can check the
disconnect code and reason).
To implement a server instead, a TCPServer must be created, and the
accepted connections needs to be provided to
WebSocketPeer.accept_stream (which will perform the HTTP handshake).
A full example of a WebSocketServer using TLS will be provided in the
demo repository.
2022-09-25 04:44:44 +08:00
* r_buffer = packet_buffer . ptr ( ) ;
2018-11-03 12:46:38 +08:00
r_buffer_size = read ;
2017-12-21 10:13:23 +08:00
return OK ;
2020-06-17 14:14:58 +08:00
}
2017-12-21 10:13:23 +08:00
int EMWSPeer : : get_available_packet_count ( ) const {
[WebSocket] Refactor websocket module.
This commit is a huge refactor of the websocket module.
The module is really old, and some design choices had to be
re-evaluated.
The WebSocketClient and WebSocketServer classes are now gone, and
WebSocketPeer can act as either client or server.
The WebSocketMultiplayerPeer class is no longer abstract, and implements
the Multiplayer API on top of the lower level WebSocketPeer.
WebSocketPeer is now a "raw" peer, like StreamPeerTCP and StreamPeerTLS,
so it emits no signal, and just needs polling to update its internal
state.
To use it as a client, simply call WebSocketPeer.coonect_to_url, then
frequently poll the peer until STATE_OPEN is reached and then you can
write or read from it, or STATE_CLOSED and then you can check the
disconnect code and reason).
To implement a server instead, a TCPServer must be created, and the
accepted connections needs to be provided to
WebSocketPeer.accept_stream (which will perform the HTTP handshake).
A full example of a WebSocketServer using TLS will be provided in the
demo repository.
2022-09-25 04:44:44 +08:00
return in_buffer . packets_left ( ) ;
2020-06-17 14:14:58 +08:00
}
2017-12-21 10:13:23 +08:00
2021-07-30 04:11:27 +08:00
int EMWSPeer : : get_current_outbound_buffered_amount ( ) const {
if ( peer_sock ! = - 1 ) {
return godot_js_websocket_buffered_amount ( peer_sock ) ;
}
return 0 ;
}
2017-12-21 10:13:23 +08:00
bool EMWSPeer : : was_string_packet ( ) const {
[WebSocket] Refactor websocket module.
This commit is a huge refactor of the websocket module.
The module is really old, and some design choices had to be
re-evaluated.
The WebSocketClient and WebSocketServer classes are now gone, and
WebSocketPeer can act as either client or server.
The WebSocketMultiplayerPeer class is no longer abstract, and implements
the Multiplayer API on top of the lower level WebSocketPeer.
WebSocketPeer is now a "raw" peer, like StreamPeerTCP and StreamPeerTLS,
so it emits no signal, and just needs polling to update its internal
state.
To use it as a client, simply call WebSocketPeer.coonect_to_url, then
frequently poll the peer until STATE_OPEN is reached and then you can
write or read from it, or STATE_CLOSED and then you can check the
disconnect code and reason).
To implement a server instead, a TCPServer must be created, and the
accepted connections needs to be provided to
WebSocketPeer.accept_stream (which will perform the HTTP handshake).
A full example of a WebSocketServer using TLS will be provided in the
demo repository.
2022-09-25 04:44:44 +08:00
return was_string ;
2020-06-17 14:14:58 +08:00
}
2017-12-21 10:13:23 +08:00
[WebSocket] Refactor websocket module.
This commit is a huge refactor of the websocket module.
The module is really old, and some design choices had to be
re-evaluated.
The WebSocketClient and WebSocketServer classes are now gone, and
WebSocketPeer can act as either client or server.
The WebSocketMultiplayerPeer class is no longer abstract, and implements
the Multiplayer API on top of the lower level WebSocketPeer.
WebSocketPeer is now a "raw" peer, like StreamPeerTCP and StreamPeerTLS,
so it emits no signal, and just needs polling to update its internal
state.
To use it as a client, simply call WebSocketPeer.coonect_to_url, then
frequently poll the peer until STATE_OPEN is reached and then you can
write or read from it, or STATE_CLOSED and then you can check the
disconnect code and reason).
To implement a server instead, a TCPServer must be created, and the
accepted connections needs to be provided to
WebSocketPeer.accept_stream (which will perform the HTTP handshake).
A full example of a WebSocketServer using TLS will be provided in the
demo repository.
2022-09-25 04:44:44 +08:00
void EMWSPeer : : _clear ( ) {
if ( peer_sock ! = - 1 ) {
godot_js_websocket_destroy ( peer_sock ) ;
peer_sock = - 1 ;
}
ready_state = STATE_CLOSED ;
was_string = 0 ;
close_code = - 1 ;
close_reason . clear ( ) ;
selected_protocol . clear ( ) ;
requested_url . clear ( ) ;
in_buffer . clear ( ) ;
packet_buffer . clear ( ) ;
2020-06-17 14:14:58 +08:00
}
2017-12-21 10:13:23 +08:00
2018-09-24 03:14:20 +08:00
void EMWSPeer : : close ( int p_code , String p_reason ) {
[WebSocket] Refactor websocket module.
This commit is a huge refactor of the websocket module.
The module is really old, and some design choices had to be
re-evaluated.
The WebSocketClient and WebSocketServer classes are now gone, and
WebSocketPeer can act as either client or server.
The WebSocketMultiplayerPeer class is no longer abstract, and implements
the Multiplayer API on top of the lower level WebSocketPeer.
WebSocketPeer is now a "raw" peer, like StreamPeerTCP and StreamPeerTLS,
so it emits no signal, and just needs polling to update its internal
state.
To use it as a client, simply call WebSocketPeer.coonect_to_url, then
frequently poll the peer until STATE_OPEN is reached and then you can
write or read from it, or STATE_CLOSED and then you can check the
disconnect code and reason).
To implement a server instead, a TCPServer must be created, and the
accepted connections needs to be provided to
WebSocketPeer.accept_stream (which will perform the HTTP handshake).
A full example of a WebSocketServer using TLS will be provided in the
demo repository.
2022-09-25 04:44:44 +08:00
if ( p_code < 0 ) {
if ( peer_sock ! = - 1 ) {
godot_js_websocket_destroy ( peer_sock ) ;
peer_sock = - 1 ;
}
ready_state = STATE_CLOSED ;
}
if ( ready_state = = STATE_CONNECTING | | ready_state = = STATE_OPEN ) {
ready_state = STATE_CLOSING ;
if ( peer_sock ! = - 1 ) {
godot_js_websocket_close ( peer_sock , p_code , p_reason . utf8 ( ) . get_data ( ) ) ;
} else {
ready_state = STATE_CLOSED ;
}
2017-12-21 10:13:23 +08:00
}
[WebSocket] Refactor websocket module.
This commit is a huge refactor of the websocket module.
The module is really old, and some design choices had to be
re-evaluated.
The WebSocketClient and WebSocketServer classes are now gone, and
WebSocketPeer can act as either client or server.
The WebSocketMultiplayerPeer class is no longer abstract, and implements
the Multiplayer API on top of the lower level WebSocketPeer.
WebSocketPeer is now a "raw" peer, like StreamPeerTCP and StreamPeerTLS,
so it emits no signal, and just needs polling to update its internal
state.
To use it as a client, simply call WebSocketPeer.coonect_to_url, then
frequently poll the peer until STATE_OPEN is reached and then you can
write or read from it, or STATE_CLOSED and then you can check the
disconnect code and reason).
To implement a server instead, a TCPServer must be created, and the
accepted connections needs to be provided to
WebSocketPeer.accept_stream (which will perform the HTTP handshake).
A full example of a WebSocketServer using TLS will be provided in the
demo repository.
2022-09-25 04:44:44 +08:00
in_buffer . clear ( ) ;
packet_buffer . clear ( ) ;
}
void EMWSPeer : : poll ( ) {
// Automatically polled by the navigator.
}
WebSocketPeer : : State EMWSPeer : : get_ready_state ( ) const {
return ready_state ;
}
int EMWSPeer : : get_close_code ( ) const {
return close_code ;
}
String EMWSPeer : : get_close_reason ( ) const {
return close_reason ;
}
String EMWSPeer : : get_selected_protocol ( ) const {
return selected_protocol ;
}
String EMWSPeer : : get_requested_url ( ) const {
return requested_url ;
2020-06-17 14:14:58 +08:00
}
2017-12-21 10:13:23 +08:00
2021-05-06 08:48:18 +08:00
IPAddress EMWSPeer : : get_connected_host ( ) const {
2022-08-29 02:27:45 +08:00
ERR_FAIL_V_MSG ( IPAddress ( ) , " Not supported in Web export. " ) ;
2022-02-16 20:56:32 +08:00
}
2017-12-21 10:13:23 +08:00
uint16_t EMWSPeer : : get_connected_port ( ) const {
2022-08-29 02:27:45 +08:00
ERR_FAIL_V_MSG ( 0 , " Not supported in Web export. " ) ;
2020-06-17 14:14:58 +08:00
}
2017-12-21 10:13:23 +08:00
2020-01-28 21:06:28 +08:00
void EMWSPeer : : set_no_delay ( bool p_enabled ) {
2022-08-29 02:27:45 +08:00
ERR_FAIL_MSG ( " 'set_no_delay' is not supported in Web export. " ) ;
2020-01-28 21:06:28 +08:00
}
2017-12-21 10:13:23 +08:00
EMWSPeer : : EMWSPeer ( ) {
2020-06-17 14:14:58 +08:00
}
2017-12-21 10:13:23 +08:00
EMWSPeer : : ~ EMWSPeer ( ) {
[WebSocket] Refactor websocket module.
This commit is a huge refactor of the websocket module.
The module is really old, and some design choices had to be
re-evaluated.
The WebSocketClient and WebSocketServer classes are now gone, and
WebSocketPeer can act as either client or server.
The WebSocketMultiplayerPeer class is no longer abstract, and implements
the Multiplayer API on top of the lower level WebSocketPeer.
WebSocketPeer is now a "raw" peer, like StreamPeerTCP and StreamPeerTLS,
so it emits no signal, and just needs polling to update its internal
state.
To use it as a client, simply call WebSocketPeer.coonect_to_url, then
frequently poll the peer until STATE_OPEN is reached and then you can
write or read from it, or STATE_CLOSED and then you can check the
disconnect code and reason).
To implement a server instead, a TCPServer must be created, and the
accepted connections needs to be provided to
WebSocketPeer.accept_stream (which will perform the HTTP handshake).
A full example of a WebSocketServer using TLS will be provided in the
demo repository.
2022-09-25 04:44:44 +08:00
_clear ( ) ;
2020-06-17 14:14:58 +08:00
}
2017-12-21 10:13:23 +08:00
2022-08-29 02:27:45 +08:00
# endif // WEB_ENABLED