-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathSimpleWebSocketMessagePlatformAdapter.cpp
More file actions
85 lines (64 loc) · 1.99 KB
/
Copy pathSimpleWebSocketMessagePlatformAdapter.cpp
File metadata and controls
85 lines (64 loc) · 1.99 KB
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
// Copyright © 2022 Michael Thornburgh
// SPDX-License-Identifier: MIT
#include "SimpleWebSocketMessagePlatformAdapter.hpp"
using namespace com::zenomt::websock;
namespace com { namespace zenomt { namespace rtws {
SimpleWebSocketMessagePlatformAdapter::SimpleWebSocketMessagePlatformAdapter(std::shared_ptr<IStreamPlatformAdapter> platform) :
m_platform(platform)
{}
bool SimpleWebSocketMessagePlatformAdapter::init(std::shared_ptr<SimpleWebSocket> websock)
{
if(m_websock)
return false;
auto myself = share_ref(this);
m_websock = websock;
m_websock->onOpen = [this, myself] { (void)myself; onWebsockOpen(); };
m_websock->onError = [this, myself] { (void)myself; onWebsockError(); };
m_websock->init();
return true;
}
Time SimpleWebSocketMessagePlatformAdapter::getCurrentTime()
{
return m_platform->getCurrentTime();
}
void SimpleWebSocketMessagePlatformAdapter::notifyWhenWritable(const onwritable_f &onwritable)
{
m_websock->notifyWhenWritable(onwritable);
}
void SimpleWebSocketMessagePlatformAdapter::setOnReceiveBytesCallback(const onreceivebytes_f &onreceivebytes)
{
m_websock->onBinaryMessage = [onreceivebytes] (const uint8_t *bytes, size_t len) { onreceivebytes(bytes, len); return true; };
}
void SimpleWebSocketMessagePlatformAdapter::setOnStreamDidCloseCallback(const Task &onstreamdidclose)
{
m_onstreamdidclose = onstreamdidclose;
}
void SimpleWebSocketMessagePlatformAdapter::doLater(const Task &task)
{
m_platform->doLater(task);
}
bool SimpleWebSocketMessagePlatformAdapter::writeBytes(const void *bytes, size_t len)
{
if(not m_websock)
return false;
m_websock->sendBinaryMessage(bytes, len);
return true;
}
void SimpleWebSocketMessagePlatformAdapter::onClientClosed()
{
onOpen = nullptr;
if(m_websock)
m_websock->close();
}
// ---
void SimpleWebSocketMessagePlatformAdapter::onWebsockOpen()
{
if(onOpen)
onOpen();
}
void SimpleWebSocketMessagePlatformAdapter::onWebsockError()
{
if(m_onstreamdidclose)
m_onstreamdidclose();
}
} } } // namespace com::zenomt::rtws