net_socket_android.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*************************************************************************/
  2. /* net_socket_android.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. #include "net_socket_android.h"
  31. #include "thread_jandroid.h"
  32. jobject NetSocketAndroid::net_utils = 0;
  33. jclass NetSocketAndroid::cls = 0;
  34. jmethodID NetSocketAndroid::_multicast_lock_acquire = 0;
  35. jmethodID NetSocketAndroid::_multicast_lock_release = 0;
  36. void NetSocketAndroid::setup(jobject p_net_utils) {
  37. JNIEnv *env = get_jni_env();
  38. net_utils = env->NewGlobalRef(p_net_utils);
  39. jclass c = env->GetObjectClass(net_utils);
  40. cls = (jclass)env->NewGlobalRef(c);
  41. _multicast_lock_acquire = env->GetMethodID(cls, "multicastLockAcquire", "()V");
  42. _multicast_lock_release = env->GetMethodID(cls, "multicastLockRelease", "()V");
  43. }
  44. void NetSocketAndroid::multicast_lock_acquire() {
  45. if (_multicast_lock_acquire) {
  46. JNIEnv *env = get_jni_env();
  47. env->CallVoidMethod(net_utils, _multicast_lock_acquire);
  48. }
  49. }
  50. void NetSocketAndroid::multicast_lock_release() {
  51. if (_multicast_lock_release) {
  52. JNIEnv *env = get_jni_env();
  53. env->CallVoidMethod(net_utils, _multicast_lock_release);
  54. }
  55. }
  56. NetSocket *NetSocketAndroid::_create_func() {
  57. return memnew(NetSocketAndroid);
  58. }
  59. void NetSocketAndroid::make_default() {
  60. _create = _create_func;
  61. }
  62. NetSocketAndroid::~NetSocketAndroid() {
  63. close();
  64. }
  65. void NetSocketAndroid::close() {
  66. NetSocketPosix::close();
  67. if (wants_broadcast)
  68. multicast_lock_release();
  69. if (multicast_groups)
  70. multicast_lock_release();
  71. wants_broadcast = false;
  72. multicast_groups = 0;
  73. }
  74. Error NetSocketAndroid::set_broadcasting_enabled(bool p_enabled) {
  75. Error err = NetSocketPosix::set_broadcasting_enabled(p_enabled);
  76. if (err != OK)
  77. return err;
  78. if (p_enabled != wants_broadcast) {
  79. if (p_enabled) {
  80. multicast_lock_acquire();
  81. } else {
  82. multicast_lock_release();
  83. }
  84. wants_broadcast = p_enabled;
  85. }
  86. return OK;
  87. }
  88. Error NetSocketAndroid::join_multicast_group(const IPAddress &p_multi_address, String p_if_name) {
  89. Error err = NetSocketPosix::join_multicast_group(p_multi_address, p_if_name);
  90. if (err != OK)
  91. return err;
  92. if (!multicast_groups)
  93. multicast_lock_acquire();
  94. multicast_groups++;
  95. return OK;
  96. }
  97. Error NetSocketAndroid::leave_multicast_group(const IPAddress &p_multi_address, String p_if_name) {
  98. Error err = NetSocketPosix::leave_multicast_group(p_multi_address, p_if_name);
  99. if (err != OK)
  100. return err;
  101. ERR_FAIL_COND_V(multicast_groups == 0, ERR_BUG);
  102. multicast_groups--;
  103. if (!multicast_groups)
  104. multicast_lock_release();
  105. return OK;
  106. }