forked from paullouisageneau/libdatachannel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.cpp
68 lines (57 loc) · 1.88 KB
/
message.cpp
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
/**
* Copyright (c) 2019-2020 Paul-Louis Ageneau
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "message.hpp"
namespace rtc {
message_ptr make_message(size_t size, Message::Type type, unsigned int stream,
shared_ptr<Reliability> reliability) {
auto message = std::make_shared<Message>(size, type);
message->stream = stream;
message->reliability = reliability;
return message;
}
message_ptr make_message(binary &&data, Message::Type type, unsigned int stream,
shared_ptr<Reliability> reliability) {
auto message = std::make_shared<Message>(std::move(data), type);
message->stream = stream;
message->reliability = reliability;
return message;
}
message_ptr make_message(message_variant data) {
return std::visit( //
overloaded{
[&](binary data) { return make_message(std::move(data), Message::Binary); },
[&](string data) {
auto b = reinterpret_cast<const byte *>(data.data());
return make_message(b, b + data.size(), Message::String);
},
},
std::move(data));
}
#if RTC_ENABLE_MEDIA
message_ptr make_message_from_opaque_ptr(rtcMessage *&&message) {
auto ptr = std::unique_ptr<Message>(reinterpret_cast<Message *>(message));
return message_ptr(std::move(ptr));
}
#endif
message_variant to_variant(Message &&message) {
switch (message.type) {
case Message::String:
return string(reinterpret_cast<const char *>(message.data()), message.size());
default:
return std::move(message);
}
}
message_variant to_variant(const Message &message) {
switch (message.type) {
case Message::String:
return string(reinterpret_cast<const char *>(message.data()), message.size());
default:
return message;
}
}
} // namespace rtc