-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage_pack_rpc.h
148 lines (121 loc) · 6.04 KB
/
message_pack_rpc.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*************************************************************************/
/* message_pack_rpc.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 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 MESSAGE_PACK_RPC_H
#define MESSAGE_PACK_RPC_H
#include "core/io/stream_peer_tcp.h"
#include "core/object/ref_counted.h"
#include "core/os/mutex.h"
#include "core/os/thread.h"
#include "core/string/ustring.h"
#include "core/templates/vector.h"
#include "core/variant/array.h"
#include "core/variant/dictionary.h"
#include "core/variant/typed_array.h"
#include "message_pack.h"
// Max message buf size: 8MiB, should be way more than enough.
#define _MSG_BUF_MAX_SIZE (1 << 23)
// Max message queue size
#define _MSG_QUEUE_MAX_SIZE 2048
class MessagePackRPC : public Object {
GDCLASS(MessagePackRPC, Object);
MessagePack msg_pack;
Mutex mutex;
Thread thread;
bool running = false;
bool connected = false;
Ref<StreamPeerTCP> tcp_stream;
List<Array> msg_queue;
PackedByteArray out_buf;
int out_tail = 0;
int out_head = 0;
PackedByteArray in_buf;
int in_tail = 0;
int in_head = 0;
HashMap<String, Callable> request_map;
HashMap<String, Callable> notify_map;
uint64_t msgid = 0;
bool sync_started = false;
bool sync_responded;
int sync_msgid;
Array sync_result;
void _error_handle(Error p_err, const String p_err_msg);
Error _message_handle(const Variant &p_message);
void _write_out();
void _read_in();
Error _try_connect(const IPAddress &p_ip, int p_port);
void _start_stream(int p_msgs_max = _MSG_MAX_SIZE);
Error _try_parse_stream();
Array _sync_call(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
Error _async_call(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
Error _notify(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
static size_t _stream_reader(mpack_tree_t *p_tree, char *r_buffer, size_t p_count);
protected:
static void _bind_methods();
public:
enum MessageType {
REQUEST = 0,
RESPONSE,
NOTIFICATION,
};
static PackedByteArray make_message_byte_array(const Array &p_message);
static PackedByteArray make_request(int p_msgid, const String &p_method, const Array &p_params = Array());
static PackedByteArray make_response(int p_msgid, const Variant &p_result, const Variant &p_error = Variant());
static PackedByteArray make_notification(const String &p_method, const Array &p_params = Array());
static void _thread_func(void *p_user_data);
Error connect_to_host(const IPAddress &p_ip, int p_port, bool p_big_endian = false);
Error takeover_connection(Ref<StreamPeerTCP> p_peer);
#if MPACK_EXTENSIONS
void register_extension_type(int8_t p_ext_type, const Callable &p_decoder);
#endif
Error register_request(const String &p_method, const Callable &p_callable, bool p_rewrite = false);
Error unregister_request(const String &p_method);
Error register_notification(const String &p_method, const Callable &p_callable, bool p_rewrite = false);
Error unregister_notification(const String &p_method);
void poll();
void close();
void _got_error(Error p_err, const String &p_err_msg);
void _message_received(const Variant &p_message);
void _request_received(int p_msgid, const String &p_method, const Array &p_params);
void _response_received(int p_msgid, const Variant &p_error, const Variant &p_result);
void _notification_received(const String &p_method, const Array &p_params);
inline uint64_t get_next_msgid() const { return msgid; }
inline void set_next_msgid(int p_msgid) { msgid = p_msgid; }
bool is_rpc_connected();
Error _put_message(const Array &p_msg);
Array sync_callv(const String &p_method, uint64_t p_timeout_msec = 100, const Array &p_params = Array());
Error async_callv(const String &p_method, const Array &p_params = Array());
Error response(uint64_t p_msgid, const Variant &p_result);
Error response_error(uint64_t p_msgid, const Variant &p_error);
Error notifyv(const String &p_method, const Array &p_params = Array());
MessagePackRPC(Ref<StreamPeerTCP> p_stream = Ref<StreamPeerTCP>());
~MessagePackRPC();
};
VARIANT_ENUM_CAST(MessagePackRPC::MessageType);
#endif // MESSAGE_PACK_RPC_H