Skip to content

Commit dcd6f3e

Browse files
committed
add src code
1 parent 1ad4e11 commit dcd6f3e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+110301
-0
lines changed

src/MapReduce/.vscode/settings.json

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"makefile.extensionOutputFolder": "./.vscode",
3+
"files.associations": {
4+
"*.cjson": "jsonc",
5+
"*.wxss": "css",
6+
"*.wxs": "javascript",
7+
"ostream": "cpp",
8+
"*.tcc": "cpp",
9+
"fstream": "cpp",
10+
"iosfwd": "cpp",
11+
"istream": "cpp",
12+
"limits": "cpp",
13+
"sstream": "cpp",
14+
"streambuf": "cpp",
15+
"array": "cpp",
16+
"cctype": "cpp",
17+
"clocale": "cpp",
18+
"cmath": "cpp",
19+
"cstdint": "cpp",
20+
"cstdio": "cpp",
21+
"cstdlib": "cpp",
22+
"ctime": "cpp",
23+
"cwchar": "cpp",
24+
"cwctype": "cpp",
25+
"deque": "cpp",
26+
"unordered_map": "cpp",
27+
"vector": "cpp",
28+
"exception": "cpp",
29+
"functional": "cpp",
30+
"initializer_list": "cpp",
31+
"iostream": "cpp",
32+
"new": "cpp",
33+
"optional": "cpp",
34+
"stdexcept": "cpp",
35+
"string_view": "cpp",
36+
"system_error": "cpp",
37+
"type_traits": "cpp",
38+
"tuple": "cpp",
39+
"typeinfo": "cpp",
40+
"utility": "cpp",
41+
"bitset": "cpp",
42+
"regex": "cpp",
43+
"valarray": "cpp",
44+
"string": "cpp"
45+
},
46+
"editor.bracketPairColorization.enabled": false
47+
48+
49+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
2+
# buttonrpc - a simple rpc framework for C++
3+
- ZeroMQ as the network layer.
4+
- c++14版本 [https://github.com/button-chen/buttonrpc_cpp14](https://github.com/button-chen/buttonrpc_cpp14)
5+
6+
## Features
7+
- 轻量级,跨平台,简单易用
8+
- 服务端可以绑定自由函数,类成员函数,std::function对象
9+
- 服务端可以绑定参数是任意自定义类型的函数
10+
- 客户端与服务端自动重连机制
11+
- 客户端调用超时选项
12+
13+
## Example
14+
server:
15+
16+
```c++
17+
#include "buttonrpc.hpp"
18+
19+
int foo(int age, int mm){
20+
return age + mm;
21+
}
22+
23+
int main()
24+
{
25+
buttonrpc server;
26+
server.as_server(5555);
27+
28+
server.bind("foo", foo);
29+
server.run();
30+
31+
return 0;
32+
}
33+
```
34+
35+
client:
36+
37+
```c++
38+
#include <iostream>
39+
#include "buttonrpc.hpp"
40+
41+
int main()
42+
{
43+
buttonrpc client;
44+
client.as_client("127.0.0.1", 5555);
45+
int a = client.call<int>("foo", 2, 3).val();
46+
std::cout << "call foo result: " << a << std::endl;
47+
system("pause");
48+
return 0;
49+
}
50+
51+
// output: call foo result: 5
52+
53+
```
54+
55+
## Dependences
56+
- [ZeroMQ](http://zguide.zeromq.org/page:all)
57+
58+
59+
## Building
60+
- vs2010 或者更高版本 (为了兼容vs2010没有用到可变模板参数)
61+
- gcc/g++ 支持部分c++11特性即可
62+
63+
## Usage
64+
65+
- 1: 更多例子在目录 example/ 下
66+
- 2: 最多支持5个参数的函数,支持任意多个参数函数请使用c++14版本:
67+
[https://github.com/button-chen/buttonrpc_cpp14](https://github.com/button-chen/buttonrpc_cpp14)
68+
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
/**
2+
*
3+
* buttonrpc library
4+
* Copyright 2018-04-28 Button
5+
*
6+
*/
7+
8+
#pragma once
9+
#include <vector>
10+
#include <sstream>
11+
#include <algorithm>
12+
#include <cstdint>
13+
using namespace std;
14+
15+
class StreamBuffer : public vector<char>
16+
{
17+
public:
18+
StreamBuffer(){ m_curpos = 0; }
19+
StreamBuffer(const char* in, size_t len){
20+
m_curpos = 0;
21+
insert(begin(), in, in+len);
22+
}
23+
~StreamBuffer(){ };
24+
25+
void reset(){ m_curpos = 0; }
26+
const char* data(){ return &(*this)[0]; }
27+
const char* current(){ return&(*this)[m_curpos]; }
28+
void offset(int k){ m_curpos += k; }
29+
bool is_eof(){ return (m_curpos >= size()); }
30+
void input( char* in, size_t len){ insert(end(), in, in+len); }
31+
int findc(char c){
32+
iterator itr = find(begin()+m_curpos, end(), c);
33+
if (itr != end())
34+
{
35+
return itr - (begin()+m_curpos);
36+
}
37+
return -1;
38+
}
39+
40+
private:
41+
// 当前字节流位置
42+
unsigned int m_curpos;
43+
};
44+
45+
class Serializer
46+
{
47+
public:
48+
Serializer() { m_byteorder = LittleEndian; };
49+
~Serializer(){ };
50+
51+
Serializer(StreamBuffer dev, int byteorder=LittleEndian){
52+
m_byteorder = byteorder;
53+
m_iodevice = dev;
54+
}
55+
56+
public:
57+
enum ByteOrder {
58+
BigEndian,
59+
LittleEndian
60+
};
61+
62+
public:
63+
void reset(){
64+
m_iodevice.reset();
65+
}
66+
int size(){
67+
return m_iodevice.size();
68+
}
69+
void skip_raw_date(int k){
70+
m_iodevice.offset(k);
71+
}
72+
const char* data(){
73+
return m_iodevice.data();
74+
}
75+
void byte_orser(char* in, int len){
76+
if (m_byteorder == BigEndian){
77+
reverse(in, in+len);
78+
}
79+
}
80+
void write_raw_data(char* in, int len){
81+
m_iodevice.input(in, len);
82+
m_iodevice.offset(len);
83+
}
84+
const char* current(){
85+
return m_iodevice.current();
86+
}
87+
void clear(){
88+
m_iodevice.clear();
89+
reset();
90+
}
91+
92+
template<typename T>
93+
void output_type(T& t);
94+
95+
template<typename T>
96+
void input_type(T t);
97+
98+
// 直接给一个长度, 返回当前位置以后x个字节数据
99+
void get_length_mem(char* p, int len){
100+
memcpy(p, m_iodevice.current(), len);
101+
m_iodevice.offset(len);
102+
}
103+
104+
public:
105+
template<typename T>
106+
Serializer &operator >> (T& i){
107+
output_type(i);
108+
return *this;
109+
}
110+
111+
template<typename T>
112+
Serializer &operator << (T i){
113+
input_type(i);
114+
return *this;
115+
}
116+
117+
private:
118+
int m_byteorder;
119+
StreamBuffer m_iodevice;
120+
};
121+
122+
template<typename T>
123+
inline void Serializer::output_type(T& t)
124+
{
125+
int len = sizeof(T);
126+
char* d = new char[len];
127+
if (!m_iodevice.is_eof()){
128+
memcpy(d, m_iodevice.current(), len);
129+
m_iodevice.offset(len);
130+
byte_orser(d, len);
131+
t = *reinterpret_cast<T*>(&d[0]);
132+
}
133+
delete [] d;
134+
}
135+
136+
template<>
137+
inline void Serializer::output_type(std::string& in)
138+
{
139+
int marklen = sizeof(uint16_t);
140+
char* d = new char[marklen];
141+
memcpy(d, m_iodevice.current(), marklen);
142+
byte_orser(d, marklen);
143+
int len = *reinterpret_cast<uint16_t*>(&d[0]);
144+
m_iodevice.offset(marklen);
145+
delete [] d;
146+
if (len == 0) return;
147+
in.insert(in.begin(), m_iodevice.current(), m_iodevice.current() + len);
148+
m_iodevice.offset(len);
149+
}
150+
151+
template<typename T>
152+
inline void Serializer::input_type(T t)
153+
{
154+
int len = sizeof(T);
155+
char* d = new char[len];
156+
const char* p = reinterpret_cast<const char*>(&t);
157+
memcpy(d, p, len);
158+
byte_orser(d, len);
159+
m_iodevice.input(d, len);
160+
delete [] d;
161+
}
162+
163+
template<>
164+
inline void Serializer::input_type(std::string in)
165+
{
166+
// 先存入字符串长度
167+
uint16_t len = in.size();
168+
char* p = reinterpret_cast< char*>(&len);
169+
byte_orser(p, sizeof(uint16_t));
170+
m_iodevice.input(p, sizeof(uint16_t));
171+
172+
// 存入字符串
173+
if (len == 0) return;
174+
char* d = new char[len];
175+
memcpy(d, in.c_str(), len);
176+
m_iodevice.input(d, len);
177+
delete [] d;
178+
}
179+
180+
template<>
181+
inline void Serializer::input_type(const char* in)
182+
{
183+
input_type<std::string>(std::string(in));
184+
}

0 commit comments

Comments
 (0)