net_socket_android.cpp 4.7 KB

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