Skip to content

Commit e3a9110

Browse files
committed
client.cpp
1 parent 561444a commit e3a9110

4 files changed

Lines changed: 93 additions & 3 deletions

File tree

section5/SalesData.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55

66
#include "cpplang.hpp"
77

8+
#include <msgpack.hpp>
9+
10+
#if MSGPACK_VERSION_MAJOR < 2
11+
# error "msgpack is too old"
12+
#endif
13+
814
BEGIN_NAMESPACE(cpp_study)
915

1016
// demo oop in C++
@@ -62,6 +68,17 @@ class SalesData final
6268
uint_type m_sold = 0;
6369
uint_type m_revenue = 0;
6470

71+
public:
72+
MSGPACK_DEFINE(m_id, m_sold, m_revenue);
73+
74+
msgpack::sbuffer pack() const
75+
{
76+
msgpack::sbuffer sbuf;
77+
msgpack::pack(sbuf, *this);
78+
79+
return sbuf;
80+
}
81+
6582
public:
6683
void inc_sold(uint_type s) noexcept
6784
{

section5/Zmq.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,24 @@ class ZmqContext final
3131
return ctx;
3232
}
3333
public:
34-
zmq_socket_type recv_sock(size_t hwm = 1000) const
34+
static
35+
zmq_socket_type recv_sock(int hwm = 1000)
3536
{
3637
zmq_socket_type sock(context(), ZMQ_PULL);
3738

3839
sock.setsockopt(ZMQ_RCVHWM, hwm);
40+
sock.setsockopt(ZMQ_LINGER, 10); // wait for 10ms
3941

4042
return sock;
4143
}
4244

43-
zmq_socket_type send_sock(size_t hwm = 1000) const
45+
static
46+
zmq_socket_type send_sock(int hwm = 1000)
4447
{
4548
zmq_socket_type sock(context(), ZMQ_PUSH);
4649

4750
sock.setsockopt(ZMQ_SNDHWM, hwm);
51+
sock.setsockopt(ZMQ_LINGER, 10); // wait for 10ms
4852

4953
return sock;
5054
}

section5/client.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright (c) 2020 by Chrono
2+
//
3+
// g++ client.cpp -std=c++14 -I../common -I../common/include -lzmq -lpthread -o c.out;./c.out
4+
// g++ client.cpp -std=c++14 -I../common -I../common/include -lzmq -lpthread -g -O0 -o c.out
5+
// g++ client.cpp -std=c++14 -I../common -I../common/include -lzmq -lpthread -g -O0 -o c.out;./c.out
6+
7+
//#include <iostream>
8+
9+
#include "cpplang.hpp"
10+
#include "SalesData.hpp"
11+
#include "SpinLock.hpp"
12+
#include "Zmq.hpp"
13+
14+
// you should put json.hpp in ../common
15+
#include "json.hpp"
16+
17+
static
18+
auto debug_print = [](auto& b)
19+
{
20+
using json_t = nlohmann::json;
21+
22+
json_t j;
23+
24+
j["id"] = b.id();
25+
j["sold"] = b.sold();
26+
j["revenue"] = b.revenue();
27+
//j["average"] = b.average();
28+
29+
std::cout << j.dump(2) << std::endl;
30+
};
31+
32+
int main()
33+
try
34+
{
35+
USING_NAMESPACE(std);
36+
USING_NAMESPACE(cpp_study);
37+
38+
cout << "hello cpp_study" << endl;
39+
40+
// sales data
41+
42+
SalesData book1("isbn001");
43+
44+
book1.inc_sold(10);
45+
book1.inc_revenue(100);
46+
47+
debug_print(book1);
48+
49+
auto buf = book1.pack();
50+
cout << buf.size() << endl;
51+
52+
// zmq send
53+
54+
using zmq_ctx = ZmqContext<1>;
55+
56+
auto sock = zmq_ctx::send_sock();
57+
58+
sock.connect("tcp://127.0.0.1:5555");
59+
assert(sock.connected());
60+
61+
auto len = sock.send(buf.data(), buf.size());
62+
assert(len == buf.size());
63+
64+
cout << "send len = " << len << endl;
65+
}
66+
catch(std::exception& e)
67+
{
68+
std::cerr << e.what() << std::endl;
69+
}

section5/cpplang.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
// macro for convienient namespace
4444
#define BEGIN_NAMESPACE(x) namespace x {
4545
#define END_NAMESPACE(x) }
46-
#define USING_NAMESPACE(x) using namespace x;
46+
#define USING_NAMESPACE(x) using namespace x
4747

4848
//static_assert(
4949
// __GNUC__ >= 4, "GCC is too old");

0 commit comments

Comments
 (0)