net_socket_android.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 = ThreadAndroid::get_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 = ThreadAndroid::get_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 = ThreadAndroid::get_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. if (multicast_groups)
  74. multicast_lock_release();
  75. wants_broadcast = false;
  76. multicast_groups = 0;
  77. }
  78. Error NetSocketAndroid::set_broadcasting_enabled(bool p_enabled) {
  79. Error err = NetSocketPosix::set_broadcasting_enabled(p_enabled);
  80. if (err != OK)
  81. return err;
  82. if (p_enabled != wants_broadcast) {
  83. if (p_enabled) {
  84. multicast_lock_acquire();
  85. } else {
  86. multicast_lock_release();
  87. }
  88. wants_broadcast = p_enabled;
  89. }
  90. return OK;
  91. }
  92. Error NetSocketAndroid::join_multicast_group(const IP_Address &p_multi_address, String p_if_name) {
  93. Error err = NetSocketPosix::join_multicast_group(p_multi_address, p_if_name);
  94. if (err != OK)
  95. return err;
  96. if (!multicast_groups)
  97. multicast_lock_acquire();
  98. multicast_groups++;
  99. return OK;
  100. }
  101. Error NetSocketAndroid::leave_multicast_group(const IP_Address &p_multi_address, String p_if_name) {
  102. Error err = NetSocketPosix::leave_multicast_group(p_multi_address, p_if_name);
  103. if (err != OK)
  104. return err;
  105. ERR_FAIL_COND_V(multicast_groups == 0, ERR_BUG);
  106. multicast_groups--;
  107. if (!multicast_groups)
  108. multicast_lock_release();
  109. return OK;
  110. }