-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathServerSession.cpp
More file actions
61 lines (47 loc) · 1.87 KB
/
Copy pathServerSession.cpp
File metadata and controls
61 lines (47 loc) · 1.87 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
#include "ServerSession.h"
#include <cassert>
#include "comms/process.h"
#include "comms/iterator.h"
namespace cc_tutorial
{
void ServerSession::handle(Message& msg)
{
std::cout << "Received message \"" << msg.name() <<
"\" with ID=" << (unsigned)msg.getId() << std::endl;
sendMessage(msg);
}
std::size_t ServerSession::processInputImpl(const std::uint8_t* buf, std::size_t bufLen)
{
std::cout << "Processing input: " << std::hex;
std::copy_n(buf, bufLen, std::ostream_iterator<unsigned>(std::cout, " "));
std::cout << std::dec << std::endl;
// Process reported input, create relevant message objects and
// dispatch all the created messages
// to this object for handling (handle() member function will be called)
return comms::processAllWithDispatch(buf, bufLen, m_frame, *this);
}
void ServerSession::sendMessage(const Message& msg)
{
std::cout << "Sending message \"" << msg.name() << "\" with ID=" << (unsigned)msg.getId() << std::endl;
std::vector<std::uint8_t> output;
// Use polymorphic serialization length calculation to create
// buffer of the requires size
output.resize(m_frame.length(msg));
// Serialize message into the buffer (including framing)
// The serialization uses polymorphic write functionality.
auto writeIter = &output[0];
auto es = m_frame.write(msg, writeIter, output.size());
if (es != comms::ErrorStatus::Success) {
assert(!"Write operation failed unexpectedly");
return;
}
// writeIter has been advanced, check that it reached end of the allocated buffer.
assert(output.size() == static_cast<std::size_t>(std::distance(&output[0], writeIter)));
// Send (re)serialized message back
sendOutput(&output[0], output.size());
}
SessionPtr Session::createServer(boost_wrap::io& io)
{
return SessionPtr(new ServerSession(io));
}
} // namespace cc_tutorial