1 /*************************************************************************/
2 /*  tcp_server.cpp                                                       */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
10 /*                                                                       */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the       */
13 /* "Software"), to deal in the Software without restriction, including   */
14 /* without limitation the rights to use, copy, modify, merge, publish,   */
15 /* distribute, sublicense, and/or sell copies of the Software, and to    */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions:                                             */
18 /*                                                                       */
19 /* The above copyright notice and this permission notice shall be        */
20 /* included in all copies or substantial portions of the Software.       */
21 /*                                                                       */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
29 /*************************************************************************/
30 
31 #include "tcp_server.h"
32 
_bind_methods()33 void TCP_Server::_bind_methods() {
34 
35 	ClassDB::bind_method(D_METHOD("listen", "port", "bind_address"), &TCP_Server::listen, DEFVAL("*"));
36 	ClassDB::bind_method(D_METHOD("is_connection_available"), &TCP_Server::is_connection_available);
37 	ClassDB::bind_method(D_METHOD("is_listening"), &TCP_Server::is_listening);
38 	ClassDB::bind_method(D_METHOD("take_connection"), &TCP_Server::take_connection);
39 	ClassDB::bind_method(D_METHOD("stop"), &TCP_Server::stop);
40 }
41 
listen(uint16_t p_port,const IP_Address & p_bind_address)42 Error TCP_Server::listen(uint16_t p_port, const IP_Address &p_bind_address) {
43 
44 	ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE);
45 	ERR_FAIL_COND_V(_sock->is_open(), ERR_ALREADY_IN_USE);
46 	ERR_FAIL_COND_V(!p_bind_address.is_valid() && !p_bind_address.is_wildcard(), ERR_INVALID_PARAMETER);
47 
48 	Error err;
49 	IP::Type ip_type = IP::TYPE_ANY;
50 
51 	// If the bind address is valid use its type as the socket type
52 	if (p_bind_address.is_valid())
53 		ip_type = p_bind_address.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
54 
55 	err = _sock->open(NetSocket::TYPE_TCP, ip_type);
56 
57 	ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE);
58 
59 	_sock->set_blocking_enabled(false);
60 	_sock->set_reuse_address_enabled(true);
61 
62 	err = _sock->bind(p_bind_address, p_port);
63 
64 	if (err != OK) {
65 
66 		_sock->close();
67 		return ERR_ALREADY_IN_USE;
68 	}
69 
70 	err = _sock->listen(MAX_PENDING_CONNECTIONS);
71 
72 	if (err != OK) {
73 		_sock->close();
74 		return FAILED;
75 	}
76 	return OK;
77 }
78 
is_listening() const79 bool TCP_Server::is_listening() const {
80 	ERR_FAIL_COND_V(!_sock.is_valid(), false);
81 
82 	return _sock->is_open();
83 }
84 
is_connection_available() const85 bool TCP_Server::is_connection_available() const {
86 
87 	ERR_FAIL_COND_V(!_sock.is_valid(), false);
88 
89 	if (!_sock->is_open())
90 		return false;
91 
92 	Error err = _sock->poll(NetSocket::POLL_TYPE_IN, 0);
93 	return (err == OK);
94 }
95 
take_connection()96 Ref<StreamPeerTCP> TCP_Server::take_connection() {
97 
98 	Ref<StreamPeerTCP> conn;
99 	if (!is_connection_available()) {
100 		return conn;
101 	}
102 
103 	Ref<NetSocket> ns;
104 	IP_Address ip;
105 	uint16_t port = 0;
106 	ns = _sock->accept(ip, port);
107 	if (!ns.is_valid())
108 		return conn;
109 
110 	conn = Ref<StreamPeerTCP>(memnew(StreamPeerTCP));
111 	conn->accept_socket(ns, ip, port);
112 	return conn;
113 }
114 
stop()115 void TCP_Server::stop() {
116 
117 	if (_sock.is_valid()) {
118 		_sock->close();
119 	}
120 }
121 
TCP_Server()122 TCP_Server::TCP_Server() :
123 		_sock(Ref<NetSocket>(NetSocket::create())) {
124 }
125 
~TCP_Server()126 TCP_Server::~TCP_Server() {
127 
128 	stop();
129 }
130