| 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 | |
| 31 | #include "net_socket_android.h" |
| 32 | |
| 33 | #include "thread_jandroid.h" |
| 34 | |
| 35 | jobject NetSocketAndroid::net_utils = nullptr; |
| 36 | jclass NetSocketAndroid::cls = nullptr; |
| 37 | jmethodID NetSocketAndroid::_multicast_lock_acquire = nullptr; |
| 38 | jmethodID NetSocketAndroid::_multicast_lock_release = nullptr; |
| 39 | |
| 40 | void NetSocketAndroid::setup(jobject p_net_utils) { |
| 41 | JNIEnv *env = get_jni_env(); |
| 42 | |
| 43 | net_utils = env->NewGlobalRef(p_net_utils); |
| 44 | |
| 45 | jclass c = env->GetObjectClass(net_utils); |
| 46 | cls = (jclass)env->NewGlobalRef(c); |
| 47 | |
| 48 | _multicast_lock_acquire = env->GetMethodID(cls, "multicastLockAcquire", "()V"); |
| 49 | _multicast_lock_release = env->GetMethodID(cls, "multicastLockRelease", "()V"); |
| 50 | } |
| 51 | |
| 52 | void NetSocketAndroid::terminate() { |
| 53 | JNIEnv *env = get_jni_env(); |
| 54 | ERR_FAIL_NULL(env); |
| 55 | |
| 56 | env->DeleteGlobalRef(cls); |
| 57 | env->DeleteGlobalRef(net_utils); |
| 58 | } |
| 59 | |
| 60 | void NetSocketAndroid::multicast_lock_acquire() { |
| 61 | if (_multicast_lock_acquire) { |
| 62 | JNIEnv *env = get_jni_env(); |
| 63 | env->CallVoidMethod(net_utils, _multicast_lock_acquire); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | void NetSocketAndroid::multicast_lock_release() { |
| 68 | if (_multicast_lock_release) { |
| 69 | JNIEnv *env = get_jni_env(); |
| 70 | env->CallVoidMethod(net_utils, _multicast_lock_release); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | NetSocket *NetSocketAndroid::_create_func() { |
| 75 | return memnew(NetSocketAndroid); |
| 76 | } |
| 77 | |
| 78 | void NetSocketAndroid::make_default() { |
| 79 | _create = _create_func; |
| 80 | } |
| 81 | |
| 82 | NetSocketAndroid::~NetSocketAndroid() { |
| 83 | close(); |
| 84 | } |
| 85 | |
| 86 | void NetSocketAndroid::close() { |
| 87 | NetSocketUnix::close(); |
| 88 | if (wants_broadcast) { |
| 89 | multicast_lock_release(); |
| 90 | } |
| 91 | if (multicast_groups) { |
| 92 | multicast_lock_release(); |
| 93 | } |
| 94 | wants_broadcast = false; |
| 95 | multicast_groups = 0; |
| 96 | } |
| 97 | |
| 98 | Error NetSocketAndroid::set_broadcasting_enabled(bool p_enabled) { |
| 99 | Error err = NetSocketUnix::set_broadcasting_enabled(p_enabled); |
| 100 | if (err != OK) { |
| 101 | return err; |
| 102 | } |
| 103 | |
| 104 | if (p_enabled != wants_broadcast) { |
| 105 | if (p_enabled) { |
| 106 | multicast_lock_acquire(); |
| 107 | } else { |
| 108 | multicast_lock_release(); |
| 109 | } |
| 110 | |
| 111 | wants_broadcast = p_enabled; |
| 112 | } |
| 113 | |
| 114 | return OK; |
| 115 | } |
| 116 | |
| 117 | Error NetSocketAndroid::join_multicast_group(const IPAddress &p_multi_address, const String &p_if_name) { |
| 118 | Error err = NetSocketUnix::join_multicast_group(p_multi_address, p_if_name); |
| 119 | if (err != OK) { |
| 120 | return err; |
| 121 | } |
| 122 | |
| 123 | if (!multicast_groups) { |
| 124 | multicast_lock_acquire(); |
| 125 | } |
| 126 | multicast_groups++; |
| 127 | |
| 128 | return OK; |
| 129 | } |
| 130 | |
| 131 | Error NetSocketAndroid::leave_multicast_group(const IPAddress &p_multi_address, const String &p_if_name) { |
| 132 | Error err = NetSocketUnix::leave_multicast_group(p_multi_address, p_if_name); |
| 133 | if (err != OK) { |
| 134 | return err; |
| 135 | } |
| 136 | |
| 137 | ERR_FAIL_COND_V(multicast_groups == 0, ERR_BUG); |
| 138 | |
| 139 | multicast_groups--; |
| 140 | if (!multicast_groups) { |
| 141 | multicast_lock_release(); |
| 142 | } |
| 143 | |
| 144 | return OK; |
| 145 | } |