Skip to content

Commit 7dbdcea

Browse files
committed
client.cpp
1 parent e3a9110 commit 7dbdcea

2 files changed

Lines changed: 46 additions & 15 deletions

File tree

section5/SalesData.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ class SalesData final
7979
return sbuf;
8080
}
8181

82+
SalesData(const msgpack::sbuffer& sbuf)
83+
{
84+
auto obj = msgpack::unpack(
85+
sbuf.data(), sbuf.size()).get();
86+
obj.convert(*this);
87+
}
88+
8289
public:
8390
void inc_sold(uint_type s) noexcept
8491
{

section5/client.cpp

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
// you should put json.hpp in ../common
1515
#include "json.hpp"
1616

17+
USING_NAMESPACE(std);
18+
USING_NAMESPACE(cpp_study);
19+
1720
static
1821
auto debug_print = [](auto& b)
1922
{
@@ -29,39 +32,60 @@ auto debug_print = [](auto& b)
2932
std::cout << j.dump(2) << std::endl;
3033
};
3134

32-
int main()
33-
try
35+
// sales data
36+
static
37+
auto make_sales = [=](const auto& id, auto s, auto r)
38+
//-> msgpack::sbuffer
3439
{
35-
USING_NAMESPACE(std);
36-
USING_NAMESPACE(cpp_study);
37-
38-
cout << "hello cpp_study" << endl;
40+
return SalesData(id, s, r).pack();
3941

40-
// sales data
42+
#if 0
43+
SalesData book(id);
4144

42-
SalesData book1("isbn001");
45+
book.inc_sold(s);
46+
book.inc_revenue(r);
4347

44-
book1.inc_sold(10);
45-
book1.inc_revenue(100);
48+
debug_print(book);
4649

47-
debug_print(book1);
48-
49-
auto buf = book1.pack();
50+
auto buf = book.pack();
5051
cout << buf.size() << endl;
5152

52-
// zmq send
53+
//SalesData book2 {buf};
54+
//assert(book.id() == book2.id());
55+
//debug_print(book2);
56+
57+
return buf;
58+
#endif
59+
};
5360

61+
// zmq send
62+
static
63+
auto send_sales = [](const auto& addr, const auto& buf)
64+
{
5465
using zmq_ctx = ZmqContext<1>;
5566

5667
auto sock = zmq_ctx::send_sock();
5768

58-
sock.connect("tcp://127.0.0.1:5555");
69+
sock.connect(addr);
5970
assert(sock.connected());
6071

6172
auto len = sock.send(buf.data(), buf.size());
6273
assert(len == buf.size());
6374

6475
cout << "send len = " << len << endl;
76+
};
77+
78+
int main()
79+
try
80+
{
81+
cout << "hello cpp_study client" << endl;
82+
83+
//auto buf = make_sales("001", 10, 100);
84+
//send_sales("tcp://127.0.0.1:5555", buf);
85+
86+
send_sales("tcp://127.0.0.1:5555",
87+
make_sales("001", 10, 100));
88+
6589
}
6690
catch(std::exception& e)
6791
{

0 commit comments

Comments
 (0)