-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
109 changed files
with
112,109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
{ | ||
"makefile.extensionOutputFolder": "./.vscode", | ||
"files.associations": { | ||
"*.cjson": "jsonc", | ||
"*.wxss": "css", | ||
"*.wxs": "javascript", | ||
"ostream": "cpp", | ||
"*.tcc": "cpp", | ||
"fstream": "cpp", | ||
"iosfwd": "cpp", | ||
"istream": "cpp", | ||
"limits": "cpp", | ||
"sstream": "cpp", | ||
"streambuf": "cpp", | ||
"array": "cpp", | ||
"cctype": "cpp", | ||
"clocale": "cpp", | ||
"cmath": "cpp", | ||
"cstdint": "cpp", | ||
"cstdio": "cpp", | ||
"cstdlib": "cpp", | ||
"ctime": "cpp", | ||
"cwchar": "cpp", | ||
"cwctype": "cpp", | ||
"deque": "cpp", | ||
"unordered_map": "cpp", | ||
"vector": "cpp", | ||
"exception": "cpp", | ||
"functional": "cpp", | ||
"initializer_list": "cpp", | ||
"iostream": "cpp", | ||
"new": "cpp", | ||
"optional": "cpp", | ||
"stdexcept": "cpp", | ||
"string_view": "cpp", | ||
"system_error": "cpp", | ||
"type_traits": "cpp", | ||
"tuple": "cpp", | ||
"typeinfo": "cpp", | ||
"utility": "cpp", | ||
"bitset": "cpp", | ||
"regex": "cpp", | ||
"valarray": "cpp", | ||
"string": "cpp" | ||
}, | ||
"editor.bracketPairColorization.enabled": false | ||
|
||
|
||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
|
||
# buttonrpc - a simple rpc framework for C++ | ||
- ZeroMQ as the network layer. | ||
- c++14版本 [https://github.com/button-chen/buttonrpc_cpp14](https://github.com/button-chen/buttonrpc_cpp14) | ||
|
||
## Features | ||
- 轻量级,跨平台,简单易用 | ||
- 服务端可以绑定自由函数,类成员函数,std::function对象 | ||
- 服务端可以绑定参数是任意自定义类型的函数 | ||
- 客户端与服务端自动重连机制 | ||
- 客户端调用超时选项 | ||
|
||
## Example | ||
server: | ||
|
||
```c++ | ||
#include "buttonrpc.hpp" | ||
|
||
int foo(int age, int mm){ | ||
return age + mm; | ||
} | ||
|
||
int main() | ||
{ | ||
buttonrpc server; | ||
server.as_server(5555); | ||
|
||
server.bind("foo", foo); | ||
server.run(); | ||
|
||
return 0; | ||
} | ||
``` | ||
client: | ||
```c++ | ||
#include <iostream> | ||
#include "buttonrpc.hpp" | ||
int main() | ||
{ | ||
buttonrpc client; | ||
client.as_client("127.0.0.1", 5555); | ||
int a = client.call<int>("foo", 2, 3).val(); | ||
std::cout << "call foo result: " << a << std::endl; | ||
system("pause"); | ||
return 0; | ||
} | ||
// output: call foo result: 5 | ||
``` | ||
|
||
## Dependences | ||
- [ZeroMQ](http://zguide.zeromq.org/page:all) | ||
|
||
|
||
## Building | ||
- vs2010 或者更高版本 (为了兼容vs2010没有用到可变模板参数) | ||
- gcc/g++ 支持部分c++11特性即可 | ||
|
||
## Usage | ||
|
||
- 1: 更多例子在目录 example/ 下 | ||
- 2: 最多支持5个参数的函数,支持任意多个参数函数请使用c++14版本: | ||
[https://github.com/button-chen/buttonrpc_cpp14](https://github.com/button-chen/buttonrpc_cpp14) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
/** | ||
* | ||
* buttonrpc library | ||
* Copyright 2018-04-28 Button | ||
* | ||
*/ | ||
|
||
#pragma once | ||
#include <vector> | ||
#include <sstream> | ||
#include <algorithm> | ||
#include <cstdint> | ||
using namespace std; | ||
|
||
class StreamBuffer : public vector<char> | ||
{ | ||
public: | ||
StreamBuffer(){ m_curpos = 0; } | ||
StreamBuffer(const char* in, size_t len){ | ||
m_curpos = 0; | ||
insert(begin(), in, in+len); | ||
} | ||
~StreamBuffer(){ }; | ||
|
||
void reset(){ m_curpos = 0; } | ||
const char* data(){ return &(*this)[0]; } | ||
const char* current(){ return&(*this)[m_curpos]; } | ||
void offset(int k){ m_curpos += k; } | ||
bool is_eof(){ return (m_curpos >= size()); } | ||
void input( char* in, size_t len){ insert(end(), in, in+len); } | ||
int findc(char c){ | ||
iterator itr = find(begin()+m_curpos, end(), c); | ||
if (itr != end()) | ||
{ | ||
return itr - (begin()+m_curpos); | ||
} | ||
return -1; | ||
} | ||
|
||
private: | ||
// 当前字节流位置 | ||
unsigned int m_curpos; | ||
}; | ||
|
||
class Serializer | ||
{ | ||
public: | ||
Serializer() { m_byteorder = LittleEndian; }; | ||
~Serializer(){ }; | ||
|
||
Serializer(StreamBuffer dev, int byteorder=LittleEndian){ | ||
m_byteorder = byteorder; | ||
m_iodevice = dev; | ||
} | ||
|
||
public: | ||
enum ByteOrder { | ||
BigEndian, | ||
LittleEndian | ||
}; | ||
|
||
public: | ||
void reset(){ | ||
m_iodevice.reset(); | ||
} | ||
int size(){ | ||
return m_iodevice.size(); | ||
} | ||
void skip_raw_date(int k){ | ||
m_iodevice.offset(k); | ||
} | ||
const char* data(){ | ||
return m_iodevice.data(); | ||
} | ||
void byte_orser(char* in, int len){ | ||
if (m_byteorder == BigEndian){ | ||
reverse(in, in+len); | ||
} | ||
} | ||
void write_raw_data(char* in, int len){ | ||
m_iodevice.input(in, len); | ||
m_iodevice.offset(len); | ||
} | ||
const char* current(){ | ||
return m_iodevice.current(); | ||
} | ||
void clear(){ | ||
m_iodevice.clear(); | ||
reset(); | ||
} | ||
|
||
template<typename T> | ||
void output_type(T& t); | ||
|
||
template<typename T> | ||
void input_type(T t); | ||
|
||
// 直接给一个长度, 返回当前位置以后x个字节数据 | ||
void get_length_mem(char* p, int len){ | ||
memcpy(p, m_iodevice.current(), len); | ||
m_iodevice.offset(len); | ||
} | ||
|
||
public: | ||
template<typename T> | ||
Serializer &operator >> (T& i){ | ||
output_type(i); | ||
return *this; | ||
} | ||
|
||
template<typename T> | ||
Serializer &operator << (T i){ | ||
input_type(i); | ||
return *this; | ||
} | ||
|
||
private: | ||
int m_byteorder; | ||
StreamBuffer m_iodevice; | ||
}; | ||
|
||
template<typename T> | ||
inline void Serializer::output_type(T& t) | ||
{ | ||
int len = sizeof(T); | ||
char* d = new char[len]; | ||
if (!m_iodevice.is_eof()){ | ||
memcpy(d, m_iodevice.current(), len); | ||
m_iodevice.offset(len); | ||
byte_orser(d, len); | ||
t = *reinterpret_cast<T*>(&d[0]); | ||
} | ||
delete [] d; | ||
} | ||
|
||
template<> | ||
inline void Serializer::output_type(std::string& in) | ||
{ | ||
int marklen = sizeof(uint16_t); | ||
char* d = new char[marklen]; | ||
memcpy(d, m_iodevice.current(), marklen); | ||
byte_orser(d, marklen); | ||
int len = *reinterpret_cast<uint16_t*>(&d[0]); | ||
m_iodevice.offset(marklen); | ||
delete [] d; | ||
if (len == 0) return; | ||
in.insert(in.begin(), m_iodevice.current(), m_iodevice.current() + len); | ||
m_iodevice.offset(len); | ||
} | ||
|
||
template<typename T> | ||
inline void Serializer::input_type(T t) | ||
{ | ||
int len = sizeof(T); | ||
char* d = new char[len]; | ||
const char* p = reinterpret_cast<const char*>(&t); | ||
memcpy(d, p, len); | ||
byte_orser(d, len); | ||
m_iodevice.input(d, len); | ||
delete [] d; | ||
} | ||
|
||
template<> | ||
inline void Serializer::input_type(std::string in) | ||
{ | ||
// 先存入字符串长度 | ||
uint16_t len = in.size(); | ||
char* p = reinterpret_cast< char*>(&len); | ||
byte_orser(p, sizeof(uint16_t)); | ||
m_iodevice.input(p, sizeof(uint16_t)); | ||
|
||
// 存入字符串 | ||
if (len == 0) return; | ||
char* d = new char[len]; | ||
memcpy(d, in.c_str(), len); | ||
m_iodevice.input(d, len); | ||
delete [] d; | ||
} | ||
|
||
template<> | ||
inline void Serializer::input_type(const char* in) | ||
{ | ||
input_type<std::string>(std::string(in)); | ||
} |
Oops, something went wrong.