net_socket_android.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**************************************************************************/
  2. /* net_socket_android.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 = nullptr;
  33. jclass NetSocketAndroid::cls = nullptr;
  34. jmethodID NetSocketAndroid::_multicast_lock_acquire = nullptr;
  35. jmethodID NetSocketAndroid::_multicast_lock_release = nullptr;
  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::terminate() {
  45. JNIEnv *env = get_jni_env();
  46. ERR_FAIL_NULL(env);
  47. env->DeleteGlobalRef(cls);
  48. env->DeleteGlobalRef(net_utils);
  49. }
  50. void NetSocketAndroid::multicast_lock_acquire() {
  51. if (_multicast_lock_acquire) {
  52. JNIEnv *env = get_jni_env();
  53. env->CallVoidMethod(net_utils, _multicast_lock_acquire);
  54. }
  55. }
  56. void NetSocketAndroid::multicast_lock_release() {
  57. if (_multicast_lock_release) {
  58. JNIEnv *env = get_jni_env();
  59. env->CallVoidMethod(net_utils, _multicast_lock_release);
  60. }
  61. }
  62. NetSocket *NetSocketAndroid::_create_func() {
  63. return memnew(NetSocketAndroid);
  64. }
  65. void NetSocketAndroid::make_default() {
  66. _create = _create_func;
  67. }
  68. NetSocketAndroid::~NetSocketAndroid() {
  69. close();
  70. }
  71. void NetSocketAndroid::close() {
  72. NetSocketUnix::close();
  73. if (wants_broadcast) {
  74. multicast_lock_release();
  75. }
  76. if (multicast_groups) {
  77. multicast_lock_release();
  78. }
  79. wants_broadcast = false;
  80. multicast_groups = 0;
  81. }
  82. Error NetSocketAndroid::set_broadcasting_enabled(bool p_enabled) {
  83. Error err = NetSocketUnix::set_broadcasting_enabled(p_enabled);
  84. if (err != OK) {
  85. return err;
  86. }
  87. if (p_enabled != wants_broadcast) {
  88. if (p_enabled) {
  89. multicast_lock_acquire();
  90. } else {
  91. multicast_lock_release();
  92. }
  93. wants_broadcast = p_enabled;
  94. }
  95. return OK;
  96. }
  97. Error NetSocketAndroid::join_multicast_group(const IPAddress &p_multi_address, const String &p_if_name) {
  98. Error err = NetSocketUnix::join_multicast_group(p_multi_address, p_if_name);
  99. if (err != OK) {
  100. return err;
  101. }
  102. if (!multicast_groups) {
  103. multicast_lock_acquire();
  104. }
  105. multicast_groups++;
  106. return OK;
  107. }
  108. Error NetSocketAndroid::leave_multicast_group(const IPAddress &p_multi_address, const String &p_if_name) {
  109. Error err = NetSocketUnix::leave_multicast_group(p_multi_address, p_if_name);
  110. if (err != OK) {
  111. return err;
  112. }
  113. ERR_FAIL_COND_V(multicast_groups == 0, ERR_BUG);
  114. multicast_groups--;
  115. if (!multicast_groups) {
  116. multicast_lock_release();
  117. }
  118. return OK;
  119. }