Пример #1
0
int main(int argc, char* argv[])
{
    signal(SIGINT, signalFun);
    if (argc == 2 && 
        (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "/?") == 0))
    {
        cout << "please input like example:" << endl;
        cout << "tcpTest remoteIP remotePort startType" << endl;
        cout << "./tcpTest 0.0.0.0 8081 0" << endl;
        cout << "startType: 0 server, 1 client" << endl;
        return 0;
    }

    if (argc > 1) g_remoteIP = argv[1];
    if (argc > 2) g_remotePort = atoi(argv[2]);
    if (argc > 3) g_startType = atoi(argv[3]);


    
    if (g_startType == 0) ILog4zManager::getPtr()->config("server.cfg");
    else ILog4zManager::getPtr()->config("client.cfg");
    ILog4zManager::getPtr()->start();
    LOGI("g_remoteIP=" << g_remoteIP << ", g_remotePort=" << g_remotePort << ", g_startType=" << g_startType );

    summer = std::shared_ptr<EventLoop>(new EventLoop);
    summer->initialize();

    if (g_startType == 0)
    {
        accepter = std::shared_ptr<TcpAccept>(new TcpAccept());
        accepter->initialize(summer);
        ts = std::shared_ptr<TcpSocket>(new TcpSocket);
        if (!accepter->openAccept(g_remoteIP.c_str(), g_remotePort)) return 0;
        accepter->doAccept(ts, std::bind(OnAcceptSocket, std::placeholders::_1, std::placeholders::_2));
    }
    else
    {
        usedSocket = std::shared_ptr<TcpSocket>(new TcpSocket);
        usedSocket->initialize(summer);
        usedSocket->doConnect(g_remoteIP.c_str(), g_remotePort, std::bind(onConnect, std::placeholders::_1));
    }

    std::function<void()> moniter = [&moniter]()
    {
        cout << "echo=" << sendCount / 5 << endl;
        sendCount = 0;
        summer->createTimer(5000, std::bind(moniter));
    };
    summer->createTimer(5000, std::bind(moniter));

    while (g_runing) summer->runOnce();

    ts.reset();
    accepter.reset();
    usedSocket.reset();

    return 0;
}
Пример #2
0
int main(int argc, char* argv[])
{
    signal(SIGINT, signalFun);
    if (argc == 2 && 
        (strcmp(argv[1], "--help") == 0 
        || strcmp(argv[1], "/?") == 0))
    {
        cout << "please input like example:" << endl;
        cout << "tcpTest remoteIP remotePort startType" << endl;
        cout << "./tcpTest 0.0.0.0 8081 0" << endl;
        cout << "startType: 0 server, 1 client" << endl;
        return 0;
    }
    if (argc > 1)
    {
        g_remoteIP = argv[1];
    }
    if (argc > 2)
    {
        g_remotePort = atoi(argv[2]);
    }
    if (argc > 3)
    {
        g_startType = atoi(argv[3]);
    }


    
    if (g_startType == 0)
    {
        ILog4zManager::getPtr()->config("server.cfg");
        ILog4zManager::getPtr()->start();
    }
    else
    {
        ILog4zManager::getPtr()->config("client.cfg");
        ILog4zManager::getPtr()->start();
    }

    LOGI("g_remoteIP=" << g_remoteIP << ", g_remotePort=" << g_remotePort << ", g_startType=" << g_startType );


    

    summer = std::shared_ptr<EventLoop>(new EventLoop);
    summer->initialize();

    if (g_startType == 0)
    {
        accepter = std::shared_ptr<TcpAccept>(new TcpAccept());
        accepter->initialize(summer);
        ts = std::shared_ptr<TcpSocket>(new TcpSocket);
        if (!accepter->openAccept(g_remoteIP.c_str(), g_remotePort))
        {
            return 0;
        }
        
        accepter->doAccept(ts, std::bind(OnAcceptSocket, std::placeholders::_1, std::placeholders::_2));
    }
    else
    {
        usedSocket = std::shared_ptr<TcpSocket>(new TcpSocket);
        usedSocket->initialize(summer);
        usedSocket->doConnect(g_remoteIP.c_str(), g_remotePort, std::bind(onConnect, std::placeholders::_1));
    }
    
    while (g_runing)
    {
        summer->runOnce();
    }
    ts.reset();
    accepter.reset();
    usedSocket.reset();
    //warning: be safe close need:  1.close all established socket. 2. wait all socket recv callback. 3 stop runonce and safe exit.
    return 0;
}