net_socket_android.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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::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. }
  70. if (multicast_groups) {
  71. multicast_lock_release();
  72. }
  73. wants_broadcast = false;
  74. multicast_groups = 0;
  75. }
  76. Error NetSocketAndroid::set_broadcasting_enabled(bool p_enabled) {
  77. Error err = NetSocketPosix::set_broadcasting_enabled(p_enabled);
  78. if (err != OK) {
  79. return err;
  80. }
  81. if (p_enabled != wants_broadcast) {
  82. if (p_enabled) {
  83. multicast_lock_acquire();
  84. } else {
  85. multicast_lock_release();
  86. }
  87. wants_broadcast = p_enabled;
  88. }
  89. return OK;
  90. }
  91. Error NetSocketAndroid::join_multicast_group(const IPAddress &p_multi_address, String p_if_name) {
  92. Error err = NetSocketPosix::join_multicast_group(p_multi_address, p_if_name);
  93. if (err != OK) {
  94. return err;
  95. }
  96. if (!multicast_groups) {
  97. multicast_lock_acquire();
  98. }
  99. multicast_groups++;
  100. return OK;
  101. }
  102. Error NetSocketAndroid::leave_multicast_group(const IPAddress &p_multi_address, String p_if_name) {
  103. Error err = NetSocketPosix::leave_multicast_group(p_multi_address, p_if_name);
  104. if (err != OK) {
  105. return err;
  106. }
  107. ERR_FAIL_COND_V(multicast_groups == 0, ERR_BUG);
  108. multicast_groups--;
  109. if (!multicast_groups) {
  110. multicast_lock_release();
  111. }
  112. return OK;
  113. }