コード例 #1
0
int main(int argc, char **argv)
{
    int thread_num = atoi(argv[1]);
    int port_num = atoi(argv[2]);
    int num = atoi(argv[3]);
    int packet_len = atoi(argv[4]);

    std::string tmp(packet_len, 'a');

    WrapServer::PTR server = std::make_shared<WrapServer>();

    server->startWorkThread(thread_num);

    for (int i = 0; i < num; i++)
    {
        sock fd = ox_socket_connect("127.0.0.1", port_num);
        server->addSession(fd, [&](TCPSession::PTR session){
            session->setCloseCallback(onSessionClose);
            session->setDataCallback(onSessionMsg);
            session->send(tmp.c_str(), tmp.size());
        });
    }

    std::cin.get();
}
コード例 #2
0
int main()
{
    if (readConfig())
    {
        ox_dir_create("logs");
        ox_dir_create("logs/ConnectionServer");

        gDailyLogger = std::make_shared<WrapLog>();
        gDailyLogger->setFile("", "logs/ConnectionServer/daily");

        startServer();

        std::map<string, string> etcdKV;

        WrapJsonValue serverJson(rapidjson::kObjectType);
        serverJson.AddMember("ID", gSelfID);
        serverJson.AddMember("IP", selfIP);
        serverJson.AddMember("portForClient", portForClient);
        serverJson.AddMember("portForLogicServer", portForLogicServer);

        etcdKV["value"] = serverJson.toString();
        etcdKV["ttl"] = std::to_string(15); /*存活时间为15秒*/

        while (true)
        {
            if (app_kbhit())
            {
                string input;
                std::getline(std::cin, input);
                gDailyLogger->warn("console input {}", input);

                if (input == "quit")
                {
                    break;
                }
            }

            for (auto& etcd : etcdServers)
            {
                if (!etcdSet(std::get<0>(etcd), std::get<1>(etcd), string("ConnectionServerList/") + std::to_string(gSelfID), etcdKV, 5000).getBody().empty())
                {
                    break;
                }
            }

            std::this_thread::sleep_for(std::chrono::milliseconds(5000));   /*5s 重设一次*/
        }

        gListenClient->closeListenThread();
        gListenLogic->closeListenThread();
        gServer->getService()->closeListenThread();
        gServer->getService()->stopWorkerThread();
        gDailyLogger->stop();
    }

    return 0;
}
コード例 #3
0
int main(int argc, char **argv)
{
    if (argc != 6)
    {
        fprintf(stderr, "Usage: <host> <port> <net work thread num> <session num> <packet size> \n");
        exit(-1);
    }

    std::string serverip = argv[1];
    int port_num = atoi(argv[2]);
    int thread_num = atoi(argv[3]);
    int num = atoi(argv[4]);
    int packet_len = atoi(argv[5]);

    std::string tmp(packet_len, 'a');

    WrapServer::PTR server = std::make_shared<WrapServer>();

    server->startWorkThread(thread_num);
    std::vector<TCPSession::PTR> slist;
    std::mutex slinuxLock;

    for (int i = 0; i < num; i++)
    {
        sock fd = ox_socket_connect(serverip.c_str(), port_num);
        server->addSession(fd, [&](TCPSession::PTR session){
            session->setDataCallback(onSessionMsg);
            session->send(tmp.c_str(), tmp.size());
            slinuxLock.lock();
            slist.push_back(session);
            slinuxLock.unlock();
        }, false, 1024 * 1024);
    }

    std::cin.get();

    slinuxLock.lock();
    for (auto& s : slist)
    {
        s->postClose();
    }
    slinuxLock.unlock();

    std::this_thread::sleep_for(std::chrono::seconds(5));
}
コード例 #4
0
static void startServer()
{
    gServer = std::make_shared<WrapServer>();
    gServer->startWorkThread(ox_getcpunum(), [](EventLoop&){
    });

    /*开启对外客户端端口*/
    gListenClient = std::make_shared<ListenThread>();
    gListenClient->startListen(false, "127.0.0.1", portForClient, nullptr, nullptr, [&](int fd){
        WrapAddNetSession(gServer, fd, std::make_shared<ConnectionClientSession>(), -1, 32 * 1024);
    });

    /*开启对内逻辑服务器端口*/
    gListenLogic = std::make_shared<ListenThread>();
    gListenLogic->startListen(false, "127.0.0.1", portForLogicServer, nullptr, nullptr, [&](int fd){
        WrapAddNetSession(gServer, fd, std::make_shared<LogicServerSession>(), 10000, 32 * 1024 * 1024);
    });
}
コード例 #5
0
ファイル: DBProxyServer.cpp プロジェクト: charliexp/DBProxy
int main()
{
    {
        srand(static_cast<unsigned int>(time(nullptr)));
        struct lua_State* L = nullptr;
        int listenPort;         /*代理服务器的监听端口*/
        ox_socket_init();
        std::vector<std::tuple<int, string, int>> backendConfigs;

        try
        {
            struct msvalue_s config(true);
            L = luaL_newstate();
            luaopen_base(L);
            luaL_openlibs(L);
            /*TODO::由启动参数指定配置路径*/
            if (lua_tinker::dofile(L, "Config.lua"))
            {
                aux_readluatable_byname(L, "ProxyConfig", &config);
            }
            else
            {
                throw std::runtime_error("not found lua file");
            }

            map<string, msvalue_s*>& allconfig = *config._map;
            listenPort = atoi(map_at(allconfig, string("listenPort"))->_str.c_str());
            sharding_function = map_at(allconfig, string("sharding_function"))->_str;

            map<string, msvalue_s*>& backends = *map_at(allconfig, string("backends"))->_map;
            
            cout << "listen port:" << listenPort << endl;

            for (auto& v : backends)
            {
                map<string, msvalue_s*>& oneBackend = *(v.second)->_map;
                int id = atoi(map_at(oneBackend, string("id"))->_str.c_str());
                string dbServerIP = map_at(oneBackend, string("ip"))->_str;
                int port = atoi(map_at(oneBackend, string("port"))->_str.c_str());
                backendConfigs.push_back(std::make_tuple(id, dbServerIP, port));

                cout << "backend :" << id << ", ip:" << dbServerIP << ", port:" << port << endl;
            }
        }
        catch (const std::exception& e)
        {
            cout << "exception:" << e.what() << endl;
            cin.get();
            exit(-1);
        }

        //WrapLog::PTR gDailyLogger = std::make_shared<WrapLog>();

        spdlog::set_level(spdlog::level::info);

        ox_dir_create("logs");
        ox_dir_create("logs/DBProxyServer");
        //gDailyLogger->setFile("", "logs/DBProxyServer/daily");

        EventLoop mainLoop;

        WrapServer::PTR server = std::make_shared<WrapServer>();
        ListenThread::PTR listenThread = std::make_shared<ListenThread>();

        int netWorkerThreadNum = ox_getcpunum();
        /*开启网络线程*/
        server->startWorkThread(netWorkerThreadNum, nullptr);

        /*链接数据库服务器*/
        for (auto& v : backendConfigs)
        {
            int id = std::get<0>(v);
            string ip = std::get<1>(v);
            int port = std::get<2>(v);

            //gDailyLogger->info("connec db server id:{}, address: {}:{}", id, ip, port);
            sock fd = ox_socket_connect(ip.c_str(), port);
            auto bserver = std::make_shared<BackendSession>();
            bserver->setID(id);
            WrapAddNetSession(server, fd, bserver, -1, 32*1024*1024);
        }

       // gDailyLogger->info("listen proxy port:{}", listenPort);
        /*开启代理服务器监听*/
        listenThread->startListen(listenPort, nullptr, nullptr, [&](int fd){
            WrapAddNetSession(server, fd, make_shared<ClientSession>(), -1, 32 * 1024 * 1024);
        });

        //gDailyLogger->warn("db proxy server start!");

        while (true)
        {
            if (app_kbhit())
            {
                break;
            }

            mainLoop.loop(1);
        }

        listenThread->closeListenThread();
        server->getService()->closeService();
        lua_close(L);
        L = nullptr;
    }

    return 0;
}
コード例 #6
0
ファイル: DBProxyServer.cpp プロジェクト: shitfSign/DBProxy
int main()
{
    int listenPort;         /*代理服务器的监听端口*/
    
    std::vector<std::tuple<int, string, int>> backendConfigs;
    {
        struct msvalue_s config(true);
        L = luaL_newstate();
        luaopen_base(L);
        luaL_openlibs(L);
        /*TODO::由启动参数指定配置路径*/
        lua_tinker::dofile(L, "Config.lua");
        aux_readluatable_byname(L, "ProxyConfig", &config);

        map<string, msvalue_s*>& allconfig = *config._map;
        listenPort = atoi(allconfig["listenPort"]->_str.c_str());
        sharding_function = allconfig["sharding_function"]->_str;

        map<string, msvalue_s*>& backends = *allconfig["backends"]->_map;

        for (auto& v : backends)
        {
            map<string, msvalue_s*>& oneBackend = *(v.second)->_map;
            int id = atoi(oneBackend["id"]->_str.c_str());
            string dbServerIP = oneBackend["ip"]->_str;
            int port = atoi(oneBackend["port"]->_str.c_str());
            backendConfigs.push_back(std::make_tuple(id, dbServerIP, port));
        }
    }

    gDailyLogger = std::make_shared<WrapLog>();

    spdlog::set_level(spdlog::level::info);

    ox_dir_create("logs");
    ox_dir_create("logs/DBProxyServer");
    gDailyLogger->setFile("", "logs/DBProxyServer/daily");

    EventLoop mainLoop;

    WrapServer::PTR server = std::make_shared<WrapServer>();

    /*开启网络线程*/
    server->startWorkThread(1, [&](EventLoop&){
        syncNet2LogicMsgList(mainLoop);
    });

    /*链接数据库服务器*/
    for (auto& v : backendConfigs)
    {
        int id = std::get<0>(v);
        string ip = std::get<1>(v);
        int port = std::get<2>(v);

        gDailyLogger->info("connec db server id:{}, address: {}:{}", id, ip, port);
        sock fd = ox_socket_connect(ip.c_str(), port);
        auto bserver = std::make_shared<BackendLogicSession>();
        bserver->setID(id);
        WrapAddNetSession(server, fd, make_shared<BackendExtNetSession>(bserver), -1);
    }

    gDailyLogger->info("listen proxy port:{}", listenPort);
    /*开启代理服务器监听*/
    server->getListenThread().startListen(listenPort, nullptr, nullptr, [&](int fd){
        WrapAddNetSession(server, fd, make_shared<ClientExtNetSession>(std::make_shared<ClientLogicSession>()), -1);
    });

    gDailyLogger->warn("db proxy server start!");

    while (true)
    {
        mainLoop.loop(1);
        /*  处理网络线程投递过来的消息 */
        procNet2LogicMsgList();
    }

    std::cin.get();

    lua_close(L);
    L = nullptr;

    return 0;
}