Skip to content

Commit 85a0404

Browse files
committed
Merge branch 'pmalhaire-socket_stream_example'
2 parents 61c053d + ce67c8e commit 85a0404

File tree

4 files changed

+161
-7
lines changed

4 files changed

+161
-7
lines changed

.travis.yml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ install:
1818
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo apt-get install -y bzip2; fi
1919
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo apt-get install -y libc6-dbg; fi
2020
- wget https://github.com/google/googletest/archive/release-1.7.0.zip -O googletest-release-1.7.0.zip
21-
- wget http://valgrind.org/downloads/valgrind-3.10.1.tar.bz2 && tar xjf valgrind-3.10.1.tar.bz2 && cd valgrind-3.10.1 && ./configure && make && sudo make install && cd ..
21+
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then wget http://valgrind.org/downloads/valgrind-3.12.0.tar.bz2 && tar xjf valgrind-3.12.0.tar.bz2 && cd valgrind-3.12.0 && ./configure && make && sudo make install && cd ..; fi
2222

2323
- if [ "$TRAVIS_OS_NAME" == "osx" ]; then brew unlink boost; fi
24-
- if [ "$BOOST" == "boost" ]; then wget http://sourceforge.net/projects/boost/files/boost/1.58.0/boost_1_58_0.zip && unzip -q boost_1_58_0.zip && cd boost_1_58_0 && ./bootstrap.sh && ./b2 --with-timer --with-chrono address-model="$ARCH" > /dev/null && sudo ./b2 --with-timer --with-chrono address-model="$ARCH" install > /dev/null && cd ..; fi
24+
- if [ "$BOOST" == "boost" ]; then wget http://sourceforge.net/projects/boost/files/boost/1.62.0/boost_1_62_0.zip && unzip -q boost_1_62_0.zip && cd boost_1_62_0 && ./bootstrap.sh && ./b2 --with-timer --with-chrono address-model="$ARCH" > /dev/null && sudo ./b2 --with-timer --with-chrono address-model="$ARCH" install > /dev/null && cd ..; fi
2525

2626
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then unzip -q googletest-release-1.7.0.zip && cd googletest-release-1.7.0 && sudo cp -r include/gtest /usr/local/include && g++ src/gtest-all.cc -I. -Iinclude -c && g++ src/gtest_main.cc -I. -Iinclude -c && ar -rv libgtest.a gtest-all.o && ar -rv libgtest_main.a gtest_main.o && sudo mv *.a /usr/local/lib && g++ -m32 src/gtest-all.cc -I. -Iinclude -c && g++ -m32 src/gtest_main.cc -I. -Iinclude -c && ar -rv libgtest.a gtest-all.o && ar -rv libgtest_main.a gtest_main.o && sudo mkdir /usr/local/lib32 && sudo mv *.a /usr/local/lib32 && cd ..; fi
2727
- if [ "$TRAVIS_OS_NAME" == "osx" ]; then unzip -q googletest-release-1.7.0.zip && cd googletest-release-1.7.0 && sudo cp -r include/gtest /usr/local/include && clang++ src/gtest-all.cc -I. -Iinclude -c && g++ src/gtest_main.cc -I. -Iinclude -c && ar -rv libgtest.a gtest-all.o && ar -rv libgtest_main.a gtest_main.o && sudo mv *.a /usr/local/lib && cd ..; fi
@@ -32,19 +32,15 @@ before_script:
3232
matrix:
3333
include:
3434
- os: osx
35-
osx_image: beta-xcode6.2
3635
compiler: clang
3736
env: ACTION="ci/build_cmake.sh" VERSION="cpp03" ARCH="64" LIBPATH="/usr/local/lib" BOOST="boost" BOOST_INC="/usr/local/boost" CHAR_SIGN="unsigned" API_VERSION="1"
3837
- os: osx
39-
osx_image: beta-xcode6.2
4038
compiler: clang
4139
env: ACTION="ci/build_cmake.sh" VERSION="cpp11" ARCH="64" LIBPATH="/usr/local/lib" BOOST="boost" BOOST_INC="/usr/local/boost" CHAR_SIGN="signed" API_VERSION="2"
4240
- os: osx
43-
osx_image: beta-xcode6.2
4441
compiler: clang
4542
env: ACTION="ci/build_cmake.sh" VERSION="cpp11" ARCH="64" LIBPATH="/usr/local/lib" BOOST="" BOOST_INC="" CHAR_SIGN="signed" API_VERSION="2"
4643
- os: osx
47-
osx_image: beta-xcode6.2
4844
compiler: clang
4945
env: ACTION="ci/build_cmake.sh" VERSION="cpp03" ARCH="64" LIBPATH="/usr/local/lib" BOOST="" BOOST_INC="" CHAR_SIGN="unsigned" API_VERSION="2"
5046
- os: linux

ci/build_cmake.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ then
8383
exit $ret
8484
fi
8585

86-
if [ "$2" != "32" ]
86+
if [ "$2" != "32" ] && [ `uname` = "Linux" ]
8787
then
8888
ctest -T memcheck | tee memcheck.log
8989

example/cpp11/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ IF (MSGPACK_CXX11)
66
LIST (APPEND exec_PROGRAMS
77
container.cpp
88
non_def_con_class.cpp
9+
socket_stream_example.cpp
910
)
1011

1112
FOREACH (source_file ${exec_PROGRAMS})
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
#include <iostream>
2+
#include <sstream>
3+
#include <msgpack.hpp>
4+
5+
struct json_like_visitor : msgpack::v2::null_visitor {
6+
json_like_visitor(std::string& s):m_s(s), m_ref(false) {} // m_ref is false by default
7+
8+
bool visit_nil() {
9+
m_s += "null";
10+
return true;
11+
}
12+
bool visit_boolean(bool v) {
13+
if (v) m_s += "true";
14+
else m_s += "false";
15+
return true;
16+
}
17+
bool visit_positive_integer(uint64_t v) {
18+
std::stringstream ss;
19+
ss << v;
20+
m_s += ss.str();
21+
return true;
22+
}
23+
bool visit_negative_integer(int64_t v) {
24+
std::stringstream ss;
25+
ss << v;
26+
m_s += ss.str();
27+
return true;
28+
}
29+
bool visit_str(const char* v, uint32_t size) {
30+
// I omit escape process.
31+
m_s += '"' + std::string(v, size) + '"';
32+
return true;
33+
}
34+
bool start_array(uint32_t /*num_elements*/) {
35+
m_s += "[";
36+
return true;
37+
}
38+
bool end_array_item() {
39+
m_s += ",";
40+
return true;
41+
}
42+
bool end_array() {
43+
m_s.erase(m_s.size() - 1, 1); // remove the last ','
44+
m_s += "]";
45+
return true;
46+
}
47+
bool start_map(uint32_t /*num_kv_pairs*/) {
48+
m_s += "{";
49+
return true;
50+
}
51+
bool end_map_key() {
52+
m_s += ":";
53+
return true;
54+
}
55+
bool end_map_value() {
56+
m_s += ",";
57+
return true;
58+
}
59+
bool end_map() {
60+
m_s.erase(m_s.size() - 1, 1); // remove the last ','
61+
m_s += "}";
62+
return true;
63+
}
64+
void parse_error(size_t /*parsed_offset*/, size_t /*error_offset*/) {
65+
std::cerr << "parse error"<<std::endl;
66+
}
67+
void insufficient_bytes(size_t /*parsed_offset*/, size_t /*error_offset*/) {
68+
std::cout << "insufficient bytes"<<std::endl;
69+
}
70+
std::string& m_s;
71+
72+
// These two functions are required by parser.
73+
void set_referenced(bool ref) { m_ref = ref; }
74+
bool referenced() const { return m_ref; }
75+
bool m_ref;
76+
};
77+
78+
struct do_nothing {
79+
void operator()(char* /*buffer*/) {
80+
}
81+
};
82+
83+
class json_like_printer : public msgpack::parser<json_like_printer, do_nothing>,
84+
public json_like_visitor {
85+
typedef parser<json_like_printer, do_nothing> parser_t;
86+
public:
87+
json_like_printer(std::size_t initial_buffer_size = MSGPACK_UNPACKER_INIT_BUFFER_SIZE)
88+
:parser_t(do_nothing_, initial_buffer_size),
89+
json_like_visitor(json_str_) {
90+
}
91+
92+
json_like_visitor& visitor() { return *this; }
93+
void print() { std::cout << json_str_ << std::endl; json_str_.clear();}
94+
private:
95+
do_nothing do_nothing_;
96+
std::string json_str_;
97+
};
98+
99+
template <typename T>
100+
struct ref_buffer {
101+
ref_buffer(T& t):t(t) {}
102+
void write(char const* ptr, std::size_t len) {
103+
if (len > t.buffer_capacity()) {
104+
t.reserve_buffer(len - t.buffer_capacity());
105+
}
106+
std::memcpy(t.buffer(), ptr, len);
107+
t.buffer_consumed(len);
108+
}
109+
T& t;
110+
};
111+
112+
#define BUFFERING_SIZE_MAX 100
113+
114+
//simulates streamed content (a socket for example)
115+
bool produce( std::stringstream & ss, char* buff, std::size_t& size)
116+
{
117+
ss.read(buff, BUFFERING_SIZE_MAX);
118+
size = static_cast<std::size_t>(ss.gcount());
119+
return (size > 0);
120+
}
121+
122+
//shows how you can treat data
123+
void consume( const char* buff, const std::size_t size,
124+
ref_buffer<json_like_printer> & rb,
125+
json_like_printer & jp
126+
)
127+
{
128+
rb.write(buff,size);
129+
while( jp.next() )
130+
{
131+
//here we print the data, you could do any wanted processing
132+
jp.print();
133+
}
134+
}
135+
136+
int main() {
137+
138+
std::vector<std::vector<int>> vvi1 { { 1,2,3,4,5}, { 6,7,8,9,10} };
139+
std::vector<std::vector<int>> vvi2 { { 11,12,13,14,15}, { 16,17,18,19,20} };
140+
141+
std::stringstream ss;
142+
143+
msgpack::pack(ss, vvi1);
144+
msgpack::pack(ss, vvi2);
145+
146+
char buffer[BUFFERING_SIZE_MAX];
147+
std::size_t size = 0;
148+
149+
json_like_printer jp(1); // set initial buffer size explicitly
150+
ref_buffer<json_like_printer> rb(jp);
151+
152+
while( produce(ss,buffer,size) )
153+
{
154+
consume(buffer, size, rb, jp);
155+
}
156+
157+
}

0 commit comments

Comments
 (0)