godot.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*************************************************************************/
  2. /* godot.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. socket->set_blocking_enabled(false);
  76. return socket;
  77. }
  78. int enet_socket_bind(ENetSocket socket, const ENetAddress *address) {
  79. IP_Address ip;
  80. if (address->wildcard) {
  81. ip = IP_Address("*");
  82. } else {
  83. ip.set_ipv6(address->host);
  84. }
  85. NetSocket *sock = (NetSocket *)socket;
  86. if (sock->bind(ip, address->port) != OK) {
  87. return -1;
  88. }
  89. return 0;
  90. }
  91. void enet_socket_destroy(ENetSocket socket) {
  92. NetSocket *sock = (NetSocket *)socket;
  93. sock->close();
  94. memdelete(sock);
  95. }
  96. int enet_socket_send(ENetSocket socket, const ENetAddress *address, const ENetBuffer *buffers, size_t bufferCount) {
  97. ERR_FAIL_COND_V(address == NULL, -1);
  98. NetSocket *sock = (NetSocket *)socket;
  99. IP_Address dest;
  100. Error err;
  101. size_t i = 0;
  102. dest.set_ipv6(address->host);
  103. // Create a single packet.
  104. PoolVector<uint8_t> out;
  105. PoolVector<uint8_t>::Write w;
  106. int size = 0;
  107. int pos = 0;
  108. for (i = 0; i < bufferCount; i++) {
  109. size += buffers[i].dataLength;
  110. }
  111. out.resize(size);
  112. w = out.write();
  113. for (i = 0; i < bufferCount; i++) {
  114. memcpy(&w[pos], buffers[i].data, buffers[i].dataLength);
  115. pos += buffers[i].dataLength;
  116. }
  117. int sent = 0;
  118. err = sock->sendto((const uint8_t *)&w[0], size, sent, dest, address->port);
  119. if (err != OK) {
  120. if (err == ERR_BUSY) { // Blocking call
  121. return 0;
  122. }
  123. WARN_PRINT("Sending failed!");
  124. return -1;
  125. }
  126. return sent;
  127. }
  128. int enet_socket_receive(ENetSocket socket, ENetAddress *address, ENetBuffer *buffers, size_t bufferCount) {
  129. ERR_FAIL_COND_V(bufferCount != 1, -1);
  130. NetSocket *sock = (NetSocket *)socket;
  131. Error ret = sock->poll(NetSocket::POLL_TYPE_IN, 0);
  132. if (ret == ERR_BUSY)
  133. return 0;
  134. if (ret != OK)
  135. return -1;
  136. int read;
  137. IP_Address ip;
  138. Error err = sock->recvfrom((uint8_t *)buffers[0].data, buffers[0].dataLength, read, ip, address->port);
  139. if (err == ERR_BUSY)
  140. return 0;
  141. if (err != OK)
  142. return -1;
  143. enet_address_set_ip(address, ip.get_ipv6(), 16);
  144. return read;
  145. }
  146. // Not implemented
  147. int enet_socket_wait(ENetSocket socket, enet_uint32 *condition, enet_uint32 timeout) {
  148. return 0; // do we need this function?
  149. }
  150. int enet_socket_get_address(ENetSocket socket, ENetAddress *address) {
  151. return -1; // do we need this function?
  152. }
  153. int enet_socketset_select(ENetSocket maxSocket, ENetSocketSet *readSet, ENetSocketSet *writeSet, enet_uint32 timeout) {
  154. return -1;
  155. }
  156. int enet_socket_listen(ENetSocket socket, int backlog) {
  157. return -1;
  158. }
  159. int enet_socket_set_option(ENetSocket socket, ENetSocketOption option, int value) {
  160. return -1;
  161. }
  162. int enet_socket_get_option(ENetSocket socket, ENetSocketOption option, int *value) {
  163. return -1;
  164. }
  165. int enet_socket_connect(ENetSocket socket, const ENetAddress *address) {
  166. return -1;
  167. }
  168. ENetSocket enet_socket_accept(ENetSocket socket, ENetAddress *address) {
  169. return NULL;
  170. }
  171. int enet_socket_shutdown(ENetSocket socket, ENetSocketShutdown how) {
  172. return -1;
  173. }