GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — cc_tutorial/tutorials/tutorial24/src/ClientSession.cpp at master · commschamp/cc_tutorial

Expand file tree

/

ClientSession.cpp

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
161 lines (128 loc) · 4.56 KB
/

ClientSession.cpp

Copy path

File metadata and controls

161 lines (128 loc) · 4.56 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "ClientSession.h"
#include <cassert>
#include <iterator>
#include "comms/process.h"
#include "comms/iterator.h"
namespace cc_tutorial
{
void ClientSession::handle(Msg1& msg)
{
std::cout << "Received message \"" << msg.doName() << "\" with ID=" << (unsigned)msg.doGetId() << '\n';
printIntField(msg.field_f1());
printIntField(msg.field_f2());
std::cout << std::endl;
if (m_currentStage != CommsStage_Msg1) {
std::cerr << "ERROR: Unexpected stage" << std::endl;
return;
}
doNextStage();
}
void ClientSession::handle(Msg2& msg)
{
std::cout << "Received message \"" << msg.doName() << "\" with ID=" << (unsigned)msg.doGetId() << '\n';
printIntField(msg.field_f1());
printIntField(msg.field_f2());
std::cout << std::endl;
if (m_currentStage != CommsStage_Msg2) {
std::cerr << "ERROR: Unexpected stage" << std::endl;
return;
}
doNextStage();
}
void ClientSession::handle(Message& msg)
{
std::cerr << "ERROR: Received unexpected message \"" << msg.name() << " with ID=" << (unsigned)msg.getId() << std::endl;
}
bool ClientSession::startImpl()
{
doNextStage();
return true;
}
std::size_t ClientSession::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 (appropriate handle() member function will be called)
return comms::processAllWithDispatch(buf, bufLen, m_frame, *this);
}
void ClientSession::sendMessage(const Message& msg)
{
std::cout << "Sending message \"" << msg.name() << "\" with ID=" << (unsigned)msg.getId() << std::endl;
// The statement below uses polymorphic message name and ID retrievals.
std::vector<std::uint8_t> output;
// Use polymorphic serialization length calculation to reserve
// needed capacity
output.reserve(m_frame.length(msg));
// Serialize message into the buffer (including framing)
// The serialization uses polymorphic write functionality.
auto writeIter = std::back_inserter(output);
// The frame will use polymorphic message ID retrieval to
// prefix message payload with message ID
auto es = m_frame.write(msg, writeIter, output.max_size());
if (es == comms::ErrorStatus::UpdateRequired) {
auto updateIter = &output[0];
es = m_frame.update(updateIter, output.size());
}
if (es != comms::ErrorStatus::Success) {
assert(!"Write operation failed unexpectedly");
return;
}
// Send serialized message back
sendOutput(&output[0], output.size());
}
void ClientSession::doNextStage()
{
do {
if (CommsStage_NumOfValues <= m_currentStage) {
// Happens in first cycle
m_currentStage = static_cast<decltype(m_currentStage)>(0);
break;
}
m_currentStage =
static_cast<decltype(m_currentStage)>(
static_cast<unsigned>(m_currentStage) + 1);
if (m_currentStage < CommsStage_NumOfValues) {
break;
}
// Client execution is complete
getIo().stop();
return;
} while (false);
using NextSendFunc = void (ClientSession::*)();
static const NextSendFunc Map[] = {
/* CommsStage_Msg1 */ &ClientSession::sendMsg1,
/* CommsStage_Msg2 */ &ClientSession::sendMsg2,
};
static const std::size_t MapSize = std::extent<decltype(Map)>::value;
static_assert(MapSize == CommsStage_NumOfValues, "Invalid Map");
auto func = Map[m_currentStage];
(this->*func)(); // Call appropriate sending function
}
void ClientSession::sendMsg1()
{
Msg1 msg;
assert(msg.doValid());
msg.field_f2().value() = 2;
assert(!msg.doValid());
sendMessage(msg); // Invalid message can be sent, but should be ignored on reception
msg.field_f1().value() = 1;
sendMessage(msg); // Should get received and echoed back
}
void ClientSession::sendMsg2()
{
Msg2 msg;
assert(!msg.doValid()); // F2 is invalid when default constructed
sendMessage(msg); // Invalid message can be sent, but should be ignored on reception
msg.field_f1().value() = 5;
msg.field_f2().value() = 10;
assert(msg.doValid());
sendMessage(msg); // Should get received and echoed back
}
SessionPtr Session::createClient(boost_wrap::io& io)
{
return SessionPtr(new ClientSession(io));
}
} // namespace cc_tutorial
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.