| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*  message_queue.cpp                                                    */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*                       This file is part of:                           */ | 
					
						
							|  |  |  | /*                           GODOT ENGINE                                */ | 
					
						
							| 
									
										
										
										
											2017-08-27 14:16:55 +02:00
										 |  |  | /*                      https://godotengine.org                          */ | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2019-01-01 12:53:14 +01:00
										 |  |  | /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur.                 */ | 
					
						
							|  |  |  | /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md)    */ | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* 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.                */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2018-01-05 00:50:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | #include "message_queue.h"
 | 
					
						
							| 
									
										
										
										
											2017-01-16 08:04:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 18:13:45 +02:00
										 |  |  | #include "core/project_settings.h"
 | 
					
						
							|  |  |  | #include "core/script_language.h"
 | 
					
						
							| 
									
										
										
										
											2017-01-16 08:04:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | MessageQueue *MessageQueue::singleton = NULL; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | MessageQueue *MessageQueue::get_singleton() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return singleton; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | Error MessageQueue::push_call(ObjectID p_id, const StringName &p_method, const Variant **p_args, int p_argcount, bool p_show_error) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	_THREAD_SAFE_METHOD_ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	int room_needed = sizeof(Message) + sizeof(Variant) * p_argcount; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	if ((buffer_end + room_needed) >= buffer_size) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		String type; | 
					
						
							|  |  |  | 		if (ObjectDB::get_instance(p_id)) | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			type = ObjectDB::get_instance(p_id)->get_class(); | 
					
						
							| 
									
										
										
										
											2018-08-24 09:35:07 +02:00
										 |  |  | 		print_line("Failed method: " + type + ":" + p_method + " target ID: " + itos(p_id)); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		statistics(); | 
					
						
							| 
									
										
										
										
											2019-08-14 20:57:49 -06:00
										 |  |  | 		ERR_FAIL_V_MSG(ERR_OUT_OF_MEMORY, "Message queue out of memory. Try increasing 'message_queue_size_kb' in project settings."); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-01-04 09:35:21 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	Message *msg = memnew_placement(&buffer[buffer_end], Message); | 
					
						
							|  |  |  | 	msg->args = p_argcount; | 
					
						
							| 
									
										
										
										
											2019-05-09 05:21:49 -04:00
										 |  |  | 	msg->instance_id = p_id; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	msg->target = p_method; | 
					
						
							|  |  |  | 	msg->type = TYPE_CALL; | 
					
						
							| 
									
										
										
										
											2016-01-04 09:35:21 -03:00
										 |  |  | 	if (p_show_error) | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		msg->type |= FLAG_SHOW_ERROR; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	buffer_end += sizeof(Message); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	for (int i = 0; i < p_argcount; i++) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		Variant *v = memnew_placement(&buffer[buffer_end], Variant); | 
					
						
							|  |  |  | 		buffer_end += sizeof(Variant); | 
					
						
							|  |  |  | 		*v = *p_args[i]; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-04 09:35:21 -03:00
										 |  |  | 	return OK; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | Error MessageQueue::push_call(ObjectID p_id, const StringName &p_method, VARIANT_ARG_DECLARE) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-04 09:35:21 -03:00
										 |  |  | 	VARIANT_ARGPTRS; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	int argc = 0; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	for (int i = 0; i < VARIANT_ARG_MAX; i++) { | 
					
						
							|  |  |  | 		if (argptr[i]->get_type() == Variant::NIL) | 
					
						
							| 
									
										
										
										
											2016-01-04 09:35:21 -03:00
										 |  |  | 			break; | 
					
						
							|  |  |  | 		argc++; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	return push_call(p_id, p_method, argptr, argc, false); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | Error MessageQueue::push_set(ObjectID p_id, const StringName &p_prop, const Variant &p_value) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	_THREAD_SAFE_METHOD_ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	uint8_t room_needed = sizeof(Message) + sizeof(Variant); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	if ((buffer_end + room_needed) >= buffer_size) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		String type; | 
					
						
							|  |  |  | 		if (ObjectDB::get_instance(p_id)) | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			type = ObjectDB::get_instance(p_id)->get_class(); | 
					
						
							| 
									
										
										
										
											2018-08-24 09:35:07 +02:00
										 |  |  | 		print_line("Failed set: " + type + ":" + p_prop + " target ID: " + itos(p_id)); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		statistics(); | 
					
						
							| 
									
										
										
										
											2019-08-14 20:57:49 -06:00
										 |  |  | 		ERR_FAIL_V_MSG(ERR_OUT_OF_MEMORY, "Message queue out of memory. Try increasing 'message_queue_size_kb' in project settings."); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	Message *msg = memnew_placement(&buffer[buffer_end], Message); | 
					
						
							|  |  |  | 	msg->args = 1; | 
					
						
							| 
									
										
										
										
											2019-05-09 05:21:49 -04:00
										 |  |  | 	msg->instance_id = p_id; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	msg->target = p_prop; | 
					
						
							|  |  |  | 	msg->type = TYPE_SET; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	buffer_end += sizeof(Message); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	Variant *v = memnew_placement(&buffer[buffer_end], Variant); | 
					
						
							|  |  |  | 	buffer_end += sizeof(Variant); | 
					
						
							|  |  |  | 	*v = p_value; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Error MessageQueue::push_notification(ObjectID p_id, int p_notification) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	_THREAD_SAFE_METHOD_ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ERR_FAIL_COND_V(p_notification < 0, ERR_INVALID_PARAMETER); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	uint8_t room_needed = sizeof(Message); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	if ((buffer_end + room_needed) >= buffer_size) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		String type; | 
					
						
							|  |  |  | 		if (ObjectDB::get_instance(p_id)) | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			type = ObjectDB::get_instance(p_id)->get_class(); | 
					
						
							| 
									
										
										
										
											2018-08-24 09:35:07 +02:00
										 |  |  | 		print_line("Failed notification: " + itos(p_notification) + " target ID: " + itos(p_id)); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		statistics(); | 
					
						
							| 
									
										
										
										
											2019-08-14 20:57:49 -06:00
										 |  |  | 		ERR_FAIL_V_MSG(ERR_OUT_OF_MEMORY, "Message queue out of memory. Try increasing 'message_queue_size_kb' in project settings."); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	Message *msg = memnew_placement(&buffer[buffer_end], Message); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	msg->type = TYPE_NOTIFICATION; | 
					
						
							| 
									
										
										
										
											2019-05-09 05:21:49 -04:00
										 |  |  | 	msg->instance_id = p_id; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	//msg->target;
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	msg->notification = p_notification; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	buffer_end += sizeof(Message); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | Error MessageQueue::push_call(Object *p_object, const StringName &p_method, VARIANT_ARG_DECLARE) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-07 17:17:31 +07:00
										 |  |  | 	return push_call(p_object->get_instance_id(), p_method, VARIANT_ARG_PASS); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Error MessageQueue::push_notification(Object *p_object, int p_notification) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-07 17:17:31 +07:00
										 |  |  | 	return push_notification(p_object->get_instance_id(), p_notification); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | Error MessageQueue::push_set(Object *p_object, const StringName &p_prop, const Variant &p_value) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-07 17:17:31 +07:00
										 |  |  | 	return push_set(p_object->get_instance_id(), p_prop, p_value); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void MessageQueue::statistics() { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	Map<StringName, int> set_count; | 
					
						
							|  |  |  | 	Map<int, int> notify_count; | 
					
						
							|  |  |  | 	Map<StringName, int> call_count; | 
					
						
							|  |  |  | 	int null_count = 0; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	uint32_t read_pos = 0; | 
					
						
							|  |  |  | 	while (read_pos < buffer_end) { | 
					
						
							|  |  |  | 		Message *message = (Message *)&buffer[read_pos]; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-09 05:21:49 -04:00
										 |  |  | 		Object *target = ObjectDB::get_instance(message->instance_id); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		if (target != NULL) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			switch (message->type & FLAG_MASK) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				case TYPE_CALL: { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					if (!call_count.has(message->target)) | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 						call_count[message->target] = 0; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 					call_count[message->target]++; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				} break; | 
					
						
							|  |  |  | 				case TYPE_NOTIFICATION: { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					if (!notify_count.has(message->notification)) | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 						notify_count[message->notification] = 0; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 					notify_count[message->notification]++; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				} break; | 
					
						
							|  |  |  | 				case TYPE_SET: { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					if (!set_count.has(message->target)) | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 						set_count[message->target] = 0; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 					set_count[message->target]++; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				} break; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-06 17:02:40 +02:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 			//object was deleted
 | 
					
						
							| 
									
										
										
										
											2018-08-24 09:35:07 +02:00
										 |  |  | 			print_line("Object was deleted while awaiting a callback"); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			null_count++; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		read_pos += sizeof(Message); | 
					
						
							|  |  |  | 		if ((message->type & FLAG_MASK) != TYPE_NOTIFICATION) | 
					
						
							|  |  |  | 			read_pos += sizeof(Variant) * message->args; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	print_line("TOTAL BYTES: " + itos(buffer_end)); | 
					
						
							|  |  |  | 	print_line("NULL count: " + itos(null_count)); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	for (Map<StringName, int>::Element *E = set_count.front(); E; E = E->next()) { | 
					
						
							|  |  |  | 		print_line("SET " + E->key() + ": " + itos(E->get())); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	for (Map<StringName, int>::Element *E = call_count.front(); E; E = E->next()) { | 
					
						
							|  |  |  | 		print_line("CALL " + E->key() + ": " + itos(E->get())); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	for (Map<int, int>::Element *E = notify_count.front(); E; E = E->next()) { | 
					
						
							|  |  |  | 		print_line("NOTIFY " + itos(E->key()) + ": " + itos(E->get())); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int MessageQueue::get_max_buffer_usage() const { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return buffer_max_used; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | void MessageQueue::_call_function(Object *p_target, const StringName &p_func, const Variant *p_args, int p_argcount, bool p_show_error) { | 
					
						
							| 
									
										
										
										
											2016-01-04 09:35:21 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	const Variant **argptrs = NULL; | 
					
						
							| 
									
										
										
										
											2016-01-04 09:35:21 -03:00
										 |  |  | 	if (p_argcount) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		argptrs = (const Variant **)alloca(sizeof(Variant *) * p_argcount); | 
					
						
							|  |  |  | 		for (int i = 0; i < p_argcount; i++) { | 
					
						
							|  |  |  | 			argptrs[i] = &p_args[i]; | 
					
						
							| 
									
										
										
										
											2016-01-04 09:35:21 -03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Variant::CallError ce; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	p_target->call(p_func, argptrs, p_argcount, ce); | 
					
						
							|  |  |  | 	if (p_show_error && ce.error != Variant::CallError::CALL_OK) { | 
					
						
							| 
									
										
										
										
											2016-01-04 09:35:21 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-14 20:57:49 -06:00
										 |  |  | 		ERR_PRINTS("Error calling deferred method: " + Variant::get_call_error_text(p_target, p_func, argptrs, p_argcount, ce) + "."); | 
					
						
							| 
									
										
										
										
											2016-01-04 09:35:21 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | void MessageQueue::flush() { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-27 19:07:58 -03:00
										 |  |  | 	if (buffer_end > buffer_max_used) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		buffer_max_used = buffer_end; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	uint32_t read_pos = 0; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-06 22:06:58 -03:00
										 |  |  | 	//using reverse locking strategy
 | 
					
						
							|  |  |  | 	_THREAD_SAFE_LOCK_ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-14 11:24:00 -03:00
										 |  |  | 	ERR_FAIL_COND(flushing); //already flushing, you did something odd
 | 
					
						
							| 
									
										
										
										
											2019-01-14 10:59:28 -03:00
										 |  |  | 	flushing = true; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	while (read_pos < buffer_end) { | 
					
						
							| 
									
										
										
										
											2015-06-06 22:06:58 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-21 11:30:55 -05:00
										 |  |  | 		//lock on each iteration, so a call can re-add itself to the message queue
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		Message *message = (Message *)&buffer[read_pos]; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-18 13:01:12 +02:00
										 |  |  | 		uint32_t advance = sizeof(Message); | 
					
						
							|  |  |  | 		if ((message->type & FLAG_MASK) != TYPE_NOTIFICATION) | 
					
						
							|  |  |  | 			advance += sizeof(Variant) * message->args; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		//pre-advance so this function is reentrant
 | 
					
						
							|  |  |  | 		read_pos += advance; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		_THREAD_SAFE_UNLOCK_ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-09 05:21:49 -04:00
										 |  |  | 		Object *target = ObjectDB::get_instance(message->instance_id); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		if (target != NULL) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			switch (message->type & FLAG_MASK) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 				case TYPE_CALL: { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 					Variant *args = (Variant *)(message + 1); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 					// messages don't expect a return value
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 					_call_function(target, message->target, args, message->args, message->type & FLAG_SHOW_ERROR); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				} break; | 
					
						
							|  |  |  | 				case TYPE_NOTIFICATION: { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					// messages don't expect a return value
 | 
					
						
							|  |  |  | 					target->notification(message->notification); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				} break; | 
					
						
							|  |  |  | 				case TYPE_SET: { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 					Variant *arg = (Variant *)(message + 1); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 					// messages don't expect a return value
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 					target->set(message->target, *arg); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				} break; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-19 14:05:15 +01:00
										 |  |  | 		if ((message->type & FLAG_MASK) != TYPE_NOTIFICATION) { | 
					
						
							|  |  |  | 			Variant *args = (Variant *)(message + 1); | 
					
						
							|  |  |  | 			for (int i = 0; i < message->args; i++) { | 
					
						
							|  |  |  | 				args[i].~Variant(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-05 12:39:30 -03:00
										 |  |  | 		message->~Message(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-06 22:06:58 -03:00
										 |  |  | 		_THREAD_SAFE_LOCK_ | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	buffer_end = 0; // reset buffer
 | 
					
						
							| 
									
										
										
										
											2019-01-14 10:59:28 -03:00
										 |  |  | 	flushing = false; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	_THREAD_SAFE_UNLOCK_ | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-14 10:59:28 -03:00
										 |  |  | bool MessageQueue::is_flushing() const { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return flushing; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | MessageQueue::MessageQueue() { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-25 10:28:50 +02:00
										 |  |  | 	ERR_FAIL_COND_MSG(singleton != NULL, "MessageQueue singleton already exist."); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	singleton = this; | 
					
						
							| 
									
										
										
										
											2019-01-14 10:59:28 -03:00
										 |  |  | 	flushing = false; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	buffer_end = 0; | 
					
						
							|  |  |  | 	buffer_max_used = 0; | 
					
						
							| 
									
										
										
										
											2018-07-19 18:58:15 -03:00
										 |  |  | 	buffer_size = GLOBAL_DEF_RST("memory/limits/message_queue/max_size_kb", DEFAULT_QUEUE_SIZE_KB); | 
					
						
							| 
									
										
										
										
											2018-10-05 11:43:53 -05:00
										 |  |  | 	ProjectSettings::get_singleton()->set_custom_property_info("memory/limits/message_queue/max_size_kb", PropertyInfo(Variant::INT, "memory/limits/message_queue/max_size_kb", PROPERTY_HINT_RANGE, "0,2048,1,or_greater")); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	buffer_size *= 1024; | 
					
						
							|  |  |  | 	buffer = memnew_arr(uint8_t, buffer_size); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | MessageQueue::~MessageQueue() { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	uint32_t read_pos = 0; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	while (read_pos < buffer_end) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		Message *message = (Message *)&buffer[read_pos]; | 
					
						
							|  |  |  | 		Variant *args = (Variant *)(message + 1); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		int argc = message->args; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		if ((message->type & FLAG_MASK) != TYPE_NOTIFICATION) { | 
					
						
							|  |  |  | 			for (int i = 0; i < argc; i++) | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 				args[i].~Variant(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		message->~Message(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		read_pos += sizeof(Message); | 
					
						
							|  |  |  | 		if ((message->type & FLAG_MASK) != TYPE_NOTIFICATION) | 
					
						
							|  |  |  | 			read_pos += sizeof(Variant) * message->args; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	singleton = NULL; | 
					
						
							|  |  |  | 	memdelete_arr(buffer); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } |