2
0

godot.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*************************************************************************/
  2. /* godot.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. @file godot.cpp
  32. @brief ENet Godot specific functions
  33. */
  34. #include "core/io/ip.h"
  35. #include "core/io/net_socket.h"
  36. #include "core/os/os.h"
  37. // This must be last for windows to compile (tested with MinGW)
  38. #include "enet/enet.h"
  39. static enet_uint32 timeBase = 0;
  40. int enet_initialize(void) {
  41. return 0;
  42. }
  43. void enet_deinitialize(void) {
  44. }
  45. enet_uint32 enet_host_random_seed(void) {
  46. return (enet_uint32)OS::get_singleton()->get_unix_time();
  47. }
  48. enet_uint32 enet_time_get(void) {
  49. return OS::get_singleton()->get_ticks_msec() - timeBase;
  50. }
  51. void enet_time_set(enet_uint32 newTimeBase) {
  52. timeBase = OS::get_singleton()->get_ticks_msec() - newTimeBase;
  53. }
  54. int enet_address_set_host(ENetAddress *address, const char *name) {
  55. IP_Address ip = IP::get_singleton()->resolve_hostname(name);
  56. ERR_FAIL_COND_V(!ip.is_valid(), -1);
  57. enet_address_set_ip(address, ip.get_ipv6(), 16);
  58. return 0;
  59. }
  60. void enet_address_set_ip(ENetAddress *address, const uint8_t *ip, size_t size) {
  61. int len = size > 16 ? 16 : size;
  62. memset(address->host, 0, 16);
  63. memcpy(address->host, ip, len);
  64. }
  65. int enet_address_get_host_ip(const ENetAddress *address, char *name, size_t nameLength) {
  66. return -1;
  67. }
  68. int enet_address_get_host(const ENetAddress *address, char *name, size_t nameLength) {
  69. return -1;
  70. }
  71. ENetSocket enet_socket_create(ENetSocketType type) {
  72. NetSocket *socket = NetSocket::create();
  73. IP::Type ip_type = IP::TYPE_ANY;
  74. socket->open(NetSocket::TYPE_UDP, ip_type);
  75. return socket;
  76. }
  77. int enet_socket_bind(ENetSocket socket, const ENetAddress *address) {
  78. IP_Address ip;
  79. if (address->wildcard) {
  80. ip = IP_Address("*");
  81. } else {
  82. ip.set_ipv6(address->host);
  83. }
  84. NetSocket *sock = (NetSocket *)socket;
  85. if (sock->bind(ip, address->port) != OK) {
  86. return -1;
  87. }
  88. return 0;
  89. }
  90. void enet_socket_destroy(ENetSocket socket) {
  91. NetSocket *sock = (NetSocket *)socket;
  92. sock->close();
  93. memdelete(sock);
  94. }
  95. int enet_socket_send(ENetSocket socket, const ENetAddress *address, const ENetBuffer *buffers, size_t bufferCount) {
  96. ERR_FAIL_COND_V(address == NULL, -1);
  97. NetSocket *sock = (NetSocket *)socket;
  98. IP_Address dest;
  99. Error err;
  100. size_t i = 0;
  101. dest.set_ipv6(address->host);
  102. // Create a single packet.
  103. PoolVector<uint8_t> out;
  104. PoolVector<uint8_t>::Write w;
  105. int size = 0;
  106. int pos = 0;
  107. for (i = 0; i < bufferCount; i++) {
  108. size += buffers[i].dataLength;
  109. }
  110. out.resize(size);
  111. w = out.write();
  112. for (i = 0; i < bufferCount; i++) {
  113. memcpy(&w[pos], buffers[i].data, buffers[i].dataLength);
  114. pos += buffers[i].dataLength;
  115. }
  116. int sent = 0;
  117. err = sock->sendto((const uint8_t *)&w[0], size, sent, dest, address->port);
  118. if (err != OK) {
  119. if (err == ERR_BUSY) { // Blocking call
  120. return 0;
  121. }
  122. WARN_PRINT("Sending failed!");
  123. return -1;
  124. }
  125. return sent;
  126. }
  127. int enet_socket_receive(ENetSocket socket, ENetAddress *address, ENetBuffer *buffers, size_t bufferCount) {
  128. ERR_FAIL_COND_V(bufferCount != 1, -1);
  129. NetSocket *sock = (NetSocket *)socket;
  130. Error ret = sock->poll(NetSocket::POLL_TYPE_IN, 0);
  131. if (ret == ERR_BUSY)
  132. return 0;
  133. if (ret != OK)
  134. return -1;
  135. int read;
  136. IP_Address ip;
  137. Error err = sock->recvfrom((uint8_t *)buffers[0].data, buffers[0].dataLength, read, ip, address->port);
  138. if (err == ERR_BUSY)
  139. return 0;
  140. if (err != OK)
  141. return -1;
  142. enet_address_set_ip(address, ip.get_ipv6(), 16);
  143. return read;
  144. }
  145. // Not implemented
  146. int enet_socket_wait(ENetSocket socket, enet_uint32 *condition, enet_uint32 timeout) {
  147. return 0; // do we need this function?
  148. }
  149. int enet_socket_get_address(ENetSocket socket, ENetAddress *address) {
  150. return -1; // do we need this function?
  151. }
  152. int enet_socketset_select(ENetSocket maxSocket, ENetSocketSet *readSet, ENetSocketSet *writeSet, enet_uint32 timeout) {
  153. return -1;
  154. }
  155. int enet_socket_listen(ENetSocket socket, int backlog) {
  156. return -1;
  157. }
  158. int enet_socket_set_option(ENetSocket socket, ENetSocketOption option, int value) {
  159. NetSocket *sock = (NetSocket *)socket;
  160. switch (option) {
  161. case ENET_SOCKOPT_NONBLOCK: {
  162. sock->set_blocking_enabled(value ? false : true);
  163. return 0;
  164. } break;
  165. case ENET_SOCKOPT_BROADCAST: {
  166. sock->set_broadcasting_enabled(value ? true : false);
  167. return 0;
  168. } break;
  169. case ENET_SOCKOPT_REUSEADDR: {
  170. sock->set_reuse_address_enabled(value ? true : false);
  171. return 0;
  172. } break;
  173. case ENET_SOCKOPT_RCVBUF: {
  174. return -1;
  175. } break;
  176. case ENET_SOCKOPT_SNDBUF: {
  177. return -1;
  178. } break;
  179. case ENET_SOCKOPT_RCVTIMEO: {
  180. return -1;
  181. } break;
  182. case ENET_SOCKOPT_SNDTIMEO: {
  183. return -1;
  184. } break;
  185. case ENET_SOCKOPT_NODELAY: {
  186. sock->set_tcp_no_delay_enabled(value ? true : false);
  187. return 0;
  188. } break;
  189. }
  190. return -1;
  191. }
  192. int enet_socket_get_option(ENetSocket socket, ENetSocketOption option, int *value) {
  193. return -1;
  194. }
  195. int enet_socket_connect(ENetSocket socket, const ENetAddress *address) {
  196. return -1;
  197. }
  198. ENetSocket enet_socket_accept(ENetSocket socket, ENetAddress *address) {
  199. return NULL;
  200. }
  201. int enet_socket_shutdown(ENetSocket socket, ENetSocketShutdown how) {
  202. return -1;
  203. }