1 /*************************************************************************/
2 /*  multiplayer_peer_gdnative.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 "multiplayer_peer_gdnative.h"
32 
MultiplayerPeerGDNative()33 MultiplayerPeerGDNative::MultiplayerPeerGDNative() {
34 	interface = NULL;
35 }
36 
~MultiplayerPeerGDNative()37 MultiplayerPeerGDNative::~MultiplayerPeerGDNative() {
38 }
39 
set_native_multiplayer_peer(const godot_net_multiplayer_peer * p_interface)40 void MultiplayerPeerGDNative::set_native_multiplayer_peer(const godot_net_multiplayer_peer *p_interface) {
41 	interface = p_interface;
42 }
43 
get_packet(const uint8_t ** r_buffer,int & r_buffer_size)44 Error MultiplayerPeerGDNative::get_packet(const uint8_t **r_buffer, int &r_buffer_size) {
45 	ERR_FAIL_COND_V(interface == NULL, ERR_UNCONFIGURED);
46 	return (Error)interface->get_packet(interface->data, r_buffer, &r_buffer_size);
47 }
48 
put_packet(const uint8_t * p_buffer,int p_buffer_size)49 Error MultiplayerPeerGDNative::put_packet(const uint8_t *p_buffer, int p_buffer_size) {
50 	ERR_FAIL_COND_V(interface == NULL, ERR_UNCONFIGURED);
51 	return (Error)interface->put_packet(interface->data, p_buffer, p_buffer_size);
52 }
53 
get_max_packet_size() const54 int MultiplayerPeerGDNative::get_max_packet_size() const {
55 	ERR_FAIL_COND_V(interface == NULL, 0);
56 	return interface->get_max_packet_size(interface->data);
57 }
58 
get_available_packet_count() const59 int MultiplayerPeerGDNative::get_available_packet_count() const {
60 	ERR_FAIL_COND_V(interface == NULL, 0);
61 	return interface->get_available_packet_count(interface->data);
62 }
63 
64 /* NetworkedMultiplayerPeer */
set_transfer_mode(TransferMode p_mode)65 void MultiplayerPeerGDNative::set_transfer_mode(TransferMode p_mode) {
66 	ERR_FAIL_COND(interface == NULL);
67 	interface->set_transfer_mode(interface->data, (godot_int)p_mode);
68 }
69 
get_transfer_mode() const70 NetworkedMultiplayerPeer::TransferMode MultiplayerPeerGDNative::get_transfer_mode() const {
71 	ERR_FAIL_COND_V(interface == NULL, TRANSFER_MODE_UNRELIABLE);
72 	return (TransferMode)interface->get_transfer_mode(interface->data);
73 }
74 
set_target_peer(int p_peer_id)75 void MultiplayerPeerGDNative::set_target_peer(int p_peer_id) {
76 	ERR_FAIL_COND(interface == NULL);
77 	interface->set_target_peer(interface->data, p_peer_id);
78 }
79 
get_packet_peer() const80 int MultiplayerPeerGDNative::get_packet_peer() const {
81 	ERR_FAIL_COND_V(interface == NULL, 0);
82 	return interface->get_packet_peer(interface->data);
83 }
84 
is_server() const85 bool MultiplayerPeerGDNative::is_server() const {
86 	ERR_FAIL_COND_V(interface == NULL, false);
87 	return interface->is_server(interface->data);
88 }
89 
poll()90 void MultiplayerPeerGDNative::poll() {
91 	ERR_FAIL_COND(interface == NULL);
92 	interface->poll(interface->data);
93 }
94 
get_unique_id() const95 int MultiplayerPeerGDNative::get_unique_id() const {
96 	ERR_FAIL_COND_V(interface == NULL, 0);
97 	return interface->get_unique_id(interface->data);
98 }
99 
set_refuse_new_connections(bool p_enable)100 void MultiplayerPeerGDNative::set_refuse_new_connections(bool p_enable) {
101 	ERR_FAIL_COND(interface == NULL);
102 	interface->set_refuse_new_connections(interface->data, p_enable);
103 }
104 
is_refusing_new_connections() const105 bool MultiplayerPeerGDNative::is_refusing_new_connections() const {
106 	ERR_FAIL_COND_V(interface == NULL, true);
107 	return interface->is_refusing_new_connections(interface->data);
108 }
109 
get_connection_status() const110 NetworkedMultiplayerPeer::ConnectionStatus MultiplayerPeerGDNative::get_connection_status() const {
111 	ERR_FAIL_COND_V(interface == NULL, CONNECTION_DISCONNECTED);
112 	return (ConnectionStatus)interface->get_connection_status(interface->data);
113 }
114 
_bind_methods()115 void MultiplayerPeerGDNative::_bind_methods() {
116 	ADD_PROPERTY_DEFAULT("transfer_mode", TRANSFER_MODE_UNRELIABLE);
117 	ADD_PROPERTY_DEFAULT("refuse_new_connections", true);
118 }
119 
120 extern "C" {
121 
godot_net_bind_multiplayer_peer(godot_object * p_obj,const godot_net_multiplayer_peer * p_impl)122 void GDAPI godot_net_bind_multiplayer_peer(godot_object *p_obj, const godot_net_multiplayer_peer *p_impl) {
123 
124 	((MultiplayerPeerGDNative *)p_obj)->set_native_multiplayer_peer(p_impl);
125 }
126 }
127