Add Android NetSocket implementation.

Automatically acquire and release multicast lock when needed.
This commit is contained in:
Fabio Alessandrelli 2019-11-25 15:01:44 +01:00
parent fafda80a4b
commit 04c917899f
6 changed files with 222 additions and 0 deletions

View file

@ -47,6 +47,7 @@
<description>
Joins the multicast group specified by [code]multicast_address[/code] using the interface identified by [code]interface_name[/code].
You can join the same multicast group with multiple interfaces. Use [method IP.get_local_interfaces] to know which are available.
Note: Some Android devices might require the [code]CHANGE_WIFI_MULTICAST_STATE[/code] permission for multicast to work.
</description>
</method>
<method name="leave_multicast_group">
@ -83,6 +84,7 @@
</argument>
<description>
Enable or disable sending of broadcast packets (e.g. [code]set_dest_address("255.255.255.255", 4343)[/code]. This option is disabled by default.
Note: Some Android devices might require the [code]CHANGE_WIFI_MULTICAST_STATE[/code] permission and this option to be enabled to receive broadcast packets too.
</description>
</method>
<method name="set_dest_address">

View file

@ -12,6 +12,7 @@ android_files = [
'file_access_jandroid.cpp',
'dir_access_jandroid.cpp',
'thread_jandroid.cpp',
'net_socket_android.cpp',
'audio_driver_jandroid.cpp',
'java_godot_lib_jni.cpp',
'java_class_wrapper.cpp',

View file

@ -43,6 +43,7 @@
#include "java_class_wrapper.h"
#include "main/input_default.h"
#include "main/main.h"
#include "net_socket_android.h"
#include "os_android.h"
#include "string_android.h"
#include "thread_jandroid.h"
@ -635,6 +636,7 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_initialize(JNIEnv *en
DirAccessJAndroid::setup(godot_io_java->get_instance());
AudioDriverAndroid::setup(godot_io_java->get_instance());
NetSocketAndroid::setup(godot_java->get_member_object("netUtils", "Lorg/godotengine/godot/utils/GodotNetUtils;", env));
os_android = new OS_Android(godot_java, godot_io_java, p_use_apk_expansion);

View file

@ -0,0 +1,136 @@
/*************************************************************************/
/* net_socket_android.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "net_socket_android.h"
#include "thread_jandroid.h"
jobject NetSocketAndroid::net_utils = 0;
jclass NetSocketAndroid::cls = 0;
jmethodID NetSocketAndroid::_multicast_lock_acquire = 0;
jmethodID NetSocketAndroid::_multicast_lock_release = 0;
void NetSocketAndroid::setup(jobject p_net_utils) {
JNIEnv *env = ThreadAndroid::get_env();
net_utils = env->NewGlobalRef(p_net_utils);
jclass c = env->GetObjectClass(net_utils);
cls = (jclass)env->NewGlobalRef(c);
_multicast_lock_acquire = env->GetMethodID(cls, "multicastLockAcquire", "()V");
_multicast_lock_release = env->GetMethodID(cls, "multicastLockRelease", "()V");
}
void NetSocketAndroid::multicast_lock_acquire() {
if (_multicast_lock_acquire) {
JNIEnv *env = ThreadAndroid::get_env();
env->CallVoidMethod(net_utils, _multicast_lock_acquire);
}
}
void NetSocketAndroid::multicast_lock_release() {
if (_multicast_lock_release) {
JNIEnv *env = ThreadAndroid::get_env();
env->CallVoidMethod(net_utils, _multicast_lock_release);
}
}
NetSocket *NetSocketAndroid::_create_func() {
return memnew(NetSocketAndroid);
}
void NetSocketAndroid::make_default() {
_create = _create_func;
}
NetSocketAndroid::NetSocketAndroid() :
wants_broadcast(false),
multicast_groups(0) {
}
NetSocketAndroid::~NetSocketAndroid() {
close();
}
void NetSocketAndroid::close() {
NetSocketPosix::close();
if (wants_broadcast)
multicast_lock_release();
if (multicast_groups)
multicast_lock_release();
wants_broadcast = false;
multicast_groups = 0;
}
Error NetSocketAndroid::set_broadcasting_enabled(bool p_enabled) {
Error err = NetSocketPosix::set_broadcasting_enabled(p_enabled);
if (err != OK)
return err;
if (p_enabled != wants_broadcast) {
if (p_enabled) {
multicast_lock_acquire();
} else {
multicast_lock_release();
}
wants_broadcast = p_enabled;
}
return OK;
}
Error NetSocketAndroid::join_multicast_group(const IP_Address &p_multi_address, String p_if_name) {
Error err = NetSocketPosix::join_multicast_group(p_multi_address, p_if_name);
if (err != OK)
return err;
if (!multicast_groups)
multicast_lock_acquire();
multicast_groups++;
return OK;
}
Error NetSocketAndroid::leave_multicast_group(const IP_Address &p_multi_address, String p_if_name) {
Error err = NetSocketPosix::leave_multicast_group(p_multi_address, p_if_name);
if (err != OK)
return err;
ERR_FAIL_COND_V(multicast_groups == 0, ERR_BUG);
multicast_groups--;
if (!multicast_groups)
multicast_lock_release();
return OK;
}

View file

@ -0,0 +1,78 @@
/*************************************************************************/
/* net_socket_android.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#ifndef NET_SOCKET_ANDROID_H
#define NET_SOCKET_ANDROID_H
#include "drivers/unix/net_socket_posix.h"
#include <jni.h>
/**
* Specialized NetSocket implementation for Android.
*
* Some devices requires Android-specific code to acquire a MulticastLock
* before sockets are allowed to receive broadcast and multicast packets.
* This implementation calls into Java code and automatically acquire/release
* the lock when broadcasting is enabled/disabled on a socket, or that socket
* joins/leaves a multicast group.
*/
class NetSocketAndroid : public NetSocketPosix {
private:
static jobject net_utils;
static jclass cls;
static jmethodID _multicast_lock_acquire;
static jmethodID _multicast_lock_release;
bool wants_broadcast;
int multicast_groups;
static void multicast_lock_acquire();
static void multicast_lock_release();
protected:
static NetSocket *_create_func();
public:
static void make_default();
static void setup(jobject p_net_utils);
virtual void close();
virtual Error set_broadcasting_enabled(bool p_enabled);
virtual Error join_multicast_group(const IP_Address &p_multi_address, String p_if_name);
virtual Error leave_multicast_group(const IP_Address &p_multi_address, String p_if_name);
NetSocketAndroid();
~NetSocketAndroid();
};
#endif

View file

@ -43,6 +43,7 @@
#include "dir_access_jandroid.h"
#include "file_access_jandroid.h"
#include "net_socket_android.h"
#include <dlfcn.h>
@ -106,6 +107,8 @@ void OS_Android::initialize_core() {
DirAccess::make_default<DirAccessJAndroid>(DirAccess::ACCESS_RESOURCES);
DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_USERDATA);
DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_FILESYSTEM);
NetSocketAndroid::make_default();
}
void OS_Android::set_opengl_extensions(const char *p_gl_extensions) {