Need help with shineframe?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

shineframe
275 Stars 63 Forks Apache License 2.0 79 Commits 2 Opened issues

Description

高性能超轻量级C++开发库及服务器编程框架

Services available

!
?

Need anything else?

Contributors list

# 131,162
C
mysql-c...
C++
raft-pr...
37 commits
# 429,948
C
mysql-c...
C++
raft-pr...
1 commit

shineframe : 高性能超轻量级C++开发库及服务器编程框架

Open Source Love stableBuild Status

一、简述 ##

shineframe是使用c++11编写,支持(linux/windows)平台。用户通过它可以非常方便地开发非阻塞式高并发服务器程序,同时提供了一些功能库,使开发的过程变得简单高效。

shineframe使用时只需要包含.hpp头文件即可,编译时需要添加 -std=c++11选项。目前处于不断开发的过程中,功能库将会逐步丰富及持续改进。

github:https://github.com/shineframe/shineframe

技术交流群:805424758

二、库组成

shine serial 媲美protobuf的强大序列化/反序列化工具

支持c++原生对象的序列化与反序列化,是网络自定义协议格式应用的开发利器。

shine serial编解效率均高于google protobuf,提供与protobuf相似的序列化特性,如:数值压缩编码,类似于varint,序列化后体积极小(小于protobuf)。serial支持比protobuf更丰富强大的数据类型,基本的数据类型及主要STL标准容器类型字段均可进行序列化(vector, deque, list, forwardlist, map, unorderedmap, set, unorderedset),支持结构嵌套(注:嵌套的结构体一定也要以SHINESERIAL宏修饰,否则不支持,编译不通过)

shine_serial操作示例(一行代码实现c++原生对象的序列化与反序列化):

#include 
#include "shine_serial.hpp"

struct B { int a; double b; std::string c; //将类型名称及需要序列化的字段用SHINE_SERIAL包裹 SHINE_SERIAL(B, a, b, c); };

struct A{ int a; double b; std::string c;

//此处嵌套上方的结构体B
std::map<int b> d;

std::list<int> e;
std::vector<float> f;
std::deque<double> g;
std::forward_list<long> h;
std::set<:string> i;

SHINE_SERIAL(A, a, b, c, d, e, f, g, h, i);

};

int main(){

A a;
a.a = 123;
a.b = 345.567;
a.c = "hello world!";

B b;

b.a = 666;
b.b = 777.7777;
b.c = "999999!";

a.d.emplace(999, b);

a.e.emplace_back(123);
a.e.emplace_back(345);

a.f.emplace_back((float)12.34);
a.f.emplace_back((float)45.67);

a.g.emplace_back((double)456.789);
a.g.emplace_back((double)78.9);

a.h.emplace_front(666);
a.h.emplace_front(555);

a.i.emplace("A");
a.i.emplace("B");
a.i.emplace("C");

//将对象a序列化成字节流
auto data = a.shine_serial_encode();

//将字节流反序列化成对象,反序列化后a2与a中数据相同
A a2;
a2.shine_serial_decode(data);

//确定结果
std::cout &lt;&lt; ((a == a2) ? "success" : "failed") &lt;&lt; std::endl;

return 0;

} </:string>

执行后输出:success

json 强大的json工具

json解析类,支持字符串与json对象的互转,另外支持json字符串与c++原生对象的互转,是json协议格式应用的开发利器。

json_model操作示例(一行代码实现json字符串与c++原生对象的互转):

#include 
#include "util/json.hpp"

using namespace shine;

struct B { int a; double b; std::string c; SHINE_JSON_MODEL(B, a, b, c); };

struct A{ int a; double b; std::string c;

std::map<int b> d;
std::list<int> e;
std::vector<float> f;
std::deque<double> g;
std::forward_list<long> h;
std::set<:string> i;

SHINE_JSON_MODEL(A, a, b, c, d, e, f, g, h, i);

};

int main(){

A a;
a.a = 123;
a.b = 345.567;
a.c = "hello world!";

B b;

b.a = 666;
b.b = 777.7777;
b.c = "999999!";

a.d.emplace(999, b);

a.e.emplace_back(123);
a.e.emplace_back(345);

a.f.emplace_back((float)12.34);
a.f.emplace_back((float)45.67);

a.g.emplace_back((double)456.789);
a.g.emplace_back((double)78.9);

a.h.emplace_front(666);
a.h.emplace_front(555);

a.i.emplace("A");
a.i.emplace("B");
a.i.emplace("C");

//将对象a编码成json字符串
auto a_str = a.json_encode();

//将json字符串解码成对象,解码后a2与a中数据相同
A a2;
a2.json_decode(a_str);

return 0;

} </:string>

json基本操作示例:

#include 
#include "../json.hpp"

using namespace shine;

int json_base_main(){

shine::string json_str = "{\
    \"name\": \"\\\"BeJson\\\"\",\
    \"url\": \"http://www.bejson.com\",\
    \"page\": 33.1288,\
    \"isNonProfit\": true,\
    \"address\": {\
    \"street\": \"科技园路.\",\
    \"city\": \"江苏苏州\",\
    \"country\": \"中国\"\

},
"links": [
{
"name": "Google",
"url": "http://www.google.com\"\ },
{
"name": "Baidu",
"url": "http://www.baidu.com\"\ },
{
"name": "SoSo",
"url": "http://www.SoSo.com\"\ }
]
}";

shine::json json;
json.decode(json_str);

shine::string json_str_encode = json.encode();

shine::json json2;

json_node_t node1;
node1.set_key("node1");
node1.set_number("-123.345");
json2.get_root().insert_kv_child(node1);

auto json2_str = json2.encode();

shine::json json3;
json_node_t arr;
arr.set_key("arr");

for (int i = 0; i &lt; 5; i++)
{
    json_node_t tmp;
    tmp.set_string(shine::string(i));
    arr.push_back_array_child(tmp);
}

json3.get_root().insert_kv_child(arr);
auto json3_str = json3.encode();


return 0;

}

string 字符串封装

log 简单的日志实现

timer 定时器实现

pool 简单的对象池实现

redis redis客户端封装

目前只实现了同步式请求,异步式请求与请求/发布功能待完善。

简单同步客户端代码示例:

#include 
#include 
#include 
#include 
#include "../redis.hpp"

using namespace shine; using namespace shine::net;

int redis_main(){ shine::redis::sync redis; redis.set_addr("127.0.0.1:6379"); redis.set_recv_timeout(3000); redis.set_auth("password");

shine::string str;
std::vector<:string> arr;
std::set<:string> set;
std::map<:string shine::string> map;

redis.SET("library", "redis");
redis.GET("library", str);
std::cout &lt;&lt; str &lt;&lt; std::endl;


redis.SADD("names", "a", "b", "c", "e");
redis.SMEMBERS("names", set);
for (auto &amp;iter : set){
    std::cout &lt;&lt; iter &lt;&lt; std::endl;
}

redis.KEYS("*", arr);
for (auto &amp;iter : arr){
    std::cout &lt;&lt; iter &lt;&lt; std::endl;
}

redis.HSET("members", "a", "A");
redis.HSET("members", "b", "B");
redis.HSET("members", "c", "C");
redis.HGETALL("members", map);
for (auto &amp;iter : map){
    std::cout &lt;&lt; iter.first &lt;&lt; ":" &lt;&lt; iter.second &lt;&lt; std::endl;
}

return 0;

} </:string></:string></:string>

net 网络封装

主要封装了socket操作,提供proactor风格非阻塞套接字操作。

echo服务端示例:

#include 
#include "../proactor_engine.hpp"

using namespace shine; using namespace shine::net;

int echo_server_main(){

std::cout &lt;&lt; "bind address: ";
shine::string addr;
std::cin &gt;&gt; addr;

proactor_engine engine;
bool rc = engine.add_acceptor("echo_server", addr, [&amp;engine](bool status, connection *conn)-&gt;bool{
    if (status)
    {
        conn-&gt;set_recv_timeout(0);

        conn-&gt;register_recv_callback([](const int8 *data, shine::size_t len, connection *conn)-&gt;bool{
            conn-&gt;async_send(data, len);
            return true;
        });

        conn-&gt;async_recv();
    }

    return true;
});

if (rc)
{
    std::cout &lt;&lt; "bind " &lt;&lt; addr &lt;&lt; "success." &lt;&lt; endl;
    engine.run();
}
else
{
    std::cout &lt;&lt; "bind " &lt;&lt; addr &lt;&lt; "failed." &lt;&lt; endl;
}

return 0;

}

echo客户端示例:

#include 
#include "../proactor_engine.hpp"

using namespace shine; using namespace shine::net;

int echo_client_main(){ std::cout << "connect address: "; shine::string addr; std::cin >> addr;

proactor_engine engine;

bool rc = engine.add_connector("client", addr, [](bool ok, connector *conn){
    std::cout &lt;&lt; "connect:" &lt;&lt; ok &lt;&lt; endl;
    if (ok)
    {
        conn-&gt;set_recv_timeout(10000);

        conn-&gt;register_recv_callback([](const int8 *data, shine::size_t len, connection *conn)-&gt;bool{
            std::cout &lt;&lt; "recv_callback len:" &lt;&lt; len &lt;&lt; " data:" &lt;&lt; data &lt;&lt; std::endl;
            conn-&gt;get_timer_manager()-&gt;set_timer(5000, [conn]()-&gt;bool{
                shine::string str = "hello world!";
                conn-&gt;async_send(str.data(), str.size());                 
                return false;
            });
            return true;
        });

        conn-&gt;register_send_callback([](shine::size_t len, connection *conn)-&gt;bool{
            return true;
        });

        conn-&gt;register_recv_timeout_callback([](connection *conn)-&gt;bool{
            std::cout &lt;&lt; "recv_timeout_callback:" &lt;&lt; endl;
            return true;
        });

        conn-&gt;register_send_timeout_callback([](connection *conn)-&gt;bool{
            std::cout &lt;&lt; "send_timeout_callback:" &lt;&lt; endl;
            return false;
        });

        conn-&gt;register_close_callback([](connection *conn){
            std::cout &lt;&lt; "close_callback:" &lt;&lt; endl;
        });

        shine::string data = "hello world";
        conn-&gt;async_send(data.data(), data.size());
        conn-&gt;async_recv();
    }
});

if (rc)
{
    std::cout &lt;&lt; "connect " &lt;&lt; addr &lt;&lt; "success." &lt;&lt; endl;
    engine.run();
}
else
{
    std::cout &lt;&lt; "connect " &lt;&lt; addr &lt;&lt; "failed." &lt;&lt; endl;
}

return 0;

}

http http服务端/客户端封装

httpbaseservermultithread示例: 在多数情况下,http服务器是需要将客户端请求分配至多个工作线组中处理的,shineframe支持这种处理方式,示例如下:

#include 
#include "http/http_server.hpp"

using namespace shine; using namespace shine::net; using namespace http;

int main(){ proactor_engine engine;

shine::string addr = "0.0.0.0:38300";

http::server_peer_multi_thread::setup(engine);
bool rc = engine.add_acceptor("http_base_server", addr, [&amp;engine](bool status, connection *conn)-&gt;bool{
    if (status)
    {
        http::server_peer_multi_thread *conn_handle = new http::server_peer_multi_thread;

            conn_handle-&gt;set_recv_timeout(15000);
            conn_handle-&gt;register_url_handle("/", [](const http::request &amp;request, http::response &amp;response)-&gt;bool{
                http::entry_t allow;
                allow.set_key("Access-Control-Allow-Origin");
                allow.set_value("*");
                response.add_entry(allow);
                response.set_version(request.get_version());
                response.set_status_code(200);
                response.set_body("hello shineframe!");
                std::cout &lt;&lt; "handle thread: " &lt;&lt; std::this_thread::get_id() &lt;&lt; std::endl;
                return true;
            });

            conn_handle-&gt;register_url_handle("/api/hello", [](const http::request &amp;request, http::response &amp;response)-&gt;bool{
                response.set_version(request.get_version());
                response.set_status_code(200);

                shine::string body = "hello api!\r\n\r\nparameters:\r\n";
                for (auto pa : request.get_url_parameters())
                {
                    body += pa.first + "=" + pa.second + "\r\n";
                }

                response.set_body(body);
                return true;
            });

            conn_handle-&gt;run(conn);
     }

    return true;
});

if (rc)
{
    std::cout &lt;&lt; "bind " &lt;&lt; addr &lt;&lt; "success." &lt;&lt; endl;
    engine.run();
}
else
{
    std::cout &lt;&lt; "bind " &lt;&lt; addr &lt;&lt; "failed." &lt;&lt; endl;
}

return 0;

}

httpbaseserver示例:

#include 
#include "../http_server.hpp"

using namespace shine; using namespace shine::net; using namespace http;

int base_server_main(){ proactor_engine engine;

shine::string addr = "0.0.0.0:8300";
bool rc = engine.add_acceptor("http_base_server", addr, [&amp;engine](bool status, connection *conn)-&gt;bool{
    if (status)
    {
        http::server_peer *conn_handle = new http::server_peer;

            conn_handle-&gt;set_recv_timeout(15000);
            conn_handle-&gt;register_url_handle("/", [](const http::request &amp;request, http::response &amp;response)-&gt;bool{
                response.set_version(request.get_version());
                response.set_status_code(200);
                response.set_body("hello shineframe!");
                return true;
            });

            conn_handle-&gt;register_url_handle("/api/*", [](const http::request &amp;request, http::response &amp;response)-&gt;bool{
                response.set_version(request.get_version());
                response.set_status_code(200);

                shine::string body = "hello api!\r\n\r\nparameters:\r\n";
                for (auto pa : request.get_url_parameters())
                {
                    body += pa.first + "=" + pa.second + "\r\n";
                }

                response.set_body(body);
                return true;
            });

            conn_handle-&gt;run(conn);
     }

    return true;
});

if (rc)
{
    std::cout &lt;&lt; "bind " &lt;&lt; addr &lt;&lt; "success." &lt;&lt; endl;
    engine.run();
}
else
{
    std::cout &lt;&lt; "bind " &lt;&lt; addr &lt;&lt; "failed." &lt;&lt; endl;
}

return 0;

}

httpbaseclient抓取页面示例:

#include 
#include "../http_client.hpp"

using namespace shine; using namespace shine::net; using namespace http;

int base_client_main(){ sync_client clinet; clinet.set_recv_timeout(3000); clinet.get_request().set_host("www.baidu.com"); clinet.get_request().set_method(http::method::get); clinet.get_request().set_url("/");

if (clinet.call())
{
    shine::string tmp;
    clinet.get_response().encode(tmp);
    std::cout &lt;&lt; tmp &lt;&lt; std::endl;
}
else
{
    std::cout &lt;&lt; "" &lt;&lt; std::endl;
}

return 0;

}

mysql封装

封装了mysql c api,提供常用的数据库访问接口。

#include 
#include "db/mysql.hpp"

using namespace shine; using namespace shine::db;

int main(){ db::mysql_connect_info connect_info; connect_info.set_addr("172.10.4.19:3306"); connect_info.set_user("root"); connect_info.set_password("root"); connect_info.set_database("tmp"); shine::db::mysql_pool pool(connect_info); auto conn = pool.get();

conn-&gt;execute("DROP TABLE IF EXISTS students");
conn-&gt;execute("CREATE TABLE `students` (\
    `id`  int(11) NOT NULL AUTO_INCREMENT,\
    `name`  varchar(32) NOT NULL,\
    `age`  int(4) NOT NULL,\
    PRIMARY KEY(`id`)\
    )");

shine::string sql = "INSERT INTO `students` (`id`, `name`, `age`) VALUES ";
for (int i = 1; i &lt;= 100; i++)
{
    if (i &gt; 1)
        sql.append(",");

    sql.format_append("(%d, 'name_%d', %d) ", i, i, i);
}

conn-&gt;execute(sql);

db::mysql_result result;
conn-&gt;select("SELECT * FROM students", &amp;result);

result.foreach_colmuns([](shine::size_t index, const shine::string &amp;name){
    std::cout &lt;&lt; name &lt;&lt; "    ";
});

std::cout &lt;&lt; std::endl;

shine::size_t row_num = -1;
result.foreach_rows([&amp;row_num](shine::size_t row, shine::size_t column_num, const shine::string &amp;column_name, const shine::string &amp;value){
    if (row_num != row)
    {
        row_num = row;
        std::cout &lt;&lt; std::endl;
    }

    std::cout&lt;&lt; value &lt;&lt; "    ";
});
return 0;

}

websocket服务端封装

echo服务端代码示例:

#include 
#include "websocket/websocket_server.hpp"

using namespace shine; using namespace shine::net; using namespace websocket;

int main(){ proactor_engine engine;

shine::string addr = "0.0.0.0:8300";
bool rc = engine.add_acceptor("websocket_echo_server", addr, [&amp;engine](bool status, connection *conn)-&gt;bool{
    if (status)
    {
        websocket::server_peer *conn_handle = new websocket::server_peer;
        conn_handle-&gt;set_recv_timeout(0);

        conn_handle-&gt;register_recv_callback([](frame_type type, const int8 *data, shine::size_t len, net::connection *conn)-&gt;bool{

            if (type == websocket::e_text)
            {
                shine::string response = parser::encode(type, data, len);
                conn-&gt;async_send(response.data(), response.size());
            }
            else if (type == websocket::e_binary)
            {
                shine::string response = parser::encode(type, data, len);
                conn-&gt;async_send(response.data(), response.size());
            }

            return true;            
        });

        conn_handle-&gt;register_close_callback([](net::connection *conn){
            std::cout &lt;&lt; "websocket_close_callback:" &lt;&lt; conn &lt;&lt; std::endl;
        });

        conn_handle-&gt;run(conn);
     }

    return true;
});

if (rc)
{
    std::cout &lt;&lt; "bind " &lt;&lt; addr &lt;&lt; "success." &lt;&lt; endl;
    engine.run();
}
else
{
    std::cout &lt;&lt; "bind " &lt;&lt; addr &lt;&lt; "failed." &lt;&lt; endl;
}

return 0;

}

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.