O2 2.0
A communication protocol for interactive music and media applications.
message.h
1/* message.h -- message creation */
2
3/* Roger B. Dannenberg
4 * April 2020
5 */
6
7#ifndef message_h
8#define message_h
9
10extern thread_local O2_context *o2_ctx;
11
12// prevent infinite tap loops using a "time too live" algorithm:
13// The value of 3 allows using a tap to implement "publish/subscribe"
14// and then a debugger tapping the subscriber and then one more tap
15// indirection for good measure. The problem with these arbitrary limits
16// is anticipating and allowing for all applications. Since this value is
17// stored in a char, the value can be as high as 127, but the risk of high
18// values is that a cycle will actually cause messages to be forwarded
19// all O2_MAX_TAP_FORWARDING times.
20#define O2_MAX_TAP_FORWARDING 3
21
22// you can OR these together to make message flags
23#define O2_UDP_FLAG 0 // UDP, not TCP
24#define O2_TCP_FLAG 1 // TCP, not UDP
25#define O2_TAP_FLAG 2 // this is a message to a tap
26
27#define MAX_SERVICE_LEN 64
28
29#ifdef WIN32
30#define ssize_t long long
31#endif
32
33
34/* MESSAGE CONSTRUCTION */
35
36#ifndef O2_NO_BUNDLES
37int o2_add_bundle_head(int64_t time);
38#endif
39
40int32_t *o2_msg_len_ptr(void);
41
42int o2_set_msg_length(int32_t *msg_len_ptr);
43
44int o2_add_raw_bytes(int32_t len, char *bytes);
45
46char *o2_msg_data_get(int32_t *len_ptr);
47
48void o2_msg_data_print_2(o2_msg_data_ptr msg);
49
50
51/* GENERAL MESSAGE FUNCIONS */
52
53#ifndef O2_NO_BUNDLES
54#define IS_BUNDLE(msg)((msg)->address[0] == '#')
55
56// Iterate over elements of a bundle. msg is an o2_msg_data_ptr, and
57// code is the code to execute. When code is entered, embedded is an
58// o2_msg_data_ptr pointing to each element of msg. code MUST assign
59// the length of embedded to len. (This is not built-in because
60// embedded may be byte-swapped.) Generally, code will include a
61// recursive call to process embedded, which may itself be a bundle.
62//
63#define FOR_EACH_EMBEDDED(msg, code) \
64 char *end_of_msg = O2_MSG_DATA_END(msg); \
65 o2_msg_data_ptr embedded = (o2_msg_data_ptr) (o2_msg_data_types(msg) - 1); \
66 while (PTR(embedded) < end_of_msg) { int32_t len; \
67 code; \
68 embedded = (o2_msg_data_ptr) (PTR(embedded) + len + sizeof(int32_t)); }
69#endif
70
71/* allocate message structure with at least size bytes in the data portion */
72#define o2_message_new(size) ((O2message_ptr) O2netmsg_new(size))
73
74void o2_message_list_free(O2message_ptr *msg);
75
83O2err o2_msg_swap_endian(o2_msg_data_ptr msg, int is_host_order);
84
85O2err o2_message_build(O2message_ptr *msg, O2time timestamp,
86 const char *service_name,
87 const char *path, const char *typestring,
88 bool tcp_flag, va_list ap);
89
90#endif /* message_h */
double O2time
O2 timestamps are doubles representing seconds since the approximate start time of the ensemble.
Definition: o2.h:625
O2err
return values used generally by O2 functions
Definition: o2.h:329
an O2 message container
Definition: o2.h:690
data part of an O2 message
Definition: o2.h:638