当前位置: 首页>>代码示例>>C++>>正文


C++ StreamSocket::read方法代码示例

本文整理汇总了C++中StreamSocket::read方法的典型用法代码示例。如果您正苦于以下问题:C++ StreamSocket::read方法的具体用法?C++ StreamSocket::read怎么用?C++ StreamSocket::read使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在StreamSocket的用法示例。


在下文中一共展示了StreamSocket::read方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: startMercury

int startMercury(LogContext& m_log, MercuryConfig& config) {
    StreamServerSocket sock;
    SocketAddress* bind_addr = config.bind_ip;

    int error_pipe[2];
    if(pipe(error_pipe)) {
        perror("Error on pipe()\n");
        return 1;
    }

    /* Redirect stderr to the error_pipe. This will make it
     * so we can still see stderr messages in the log */
    dup2(error_pipe[1], STDERR_FILENO);

    /* Start the service that is only resposible for
     * logging the stderr of the this process and
     * its other children */
    start_logging_service(error_pipe[0]);

    /*
     * Start the monitor. This will be used
     * to retrieve statistics about all the
     * network interfaces.
     */
    MonitorProxy* monitor_proxy;
    monitor_proxy = start_monitor(config);

    m_log.printfln(INFO, "Binding to address: %s", bind_addr->toString().c_str());
    sock.bind(*bind_addr);
    sock.listen(1);

    while(true) {
        StreamSocket* client = sock.accept();
        m_log.printfln(INFO, "Connection accepted");

        /* simply get 64 bytes from the client and
         * then close the connection */
        byte recieve[64];
        size_t bytes_read;
        bytes_read = client->read(recieve, sizeof(recieve));
        delete client;

        /* make sure the cookie is equal to what we expect */
        if(bytes_read == 64 && std::equal(recieve, recieve + 32, mercury_magic_cookie)) {
            ScopedLock __sl(*g_mutex);
            /* the cookie is equal to what we expect
             * so continue with the fork() */
            m_log.printfln(INFO, "Magic cookie accepted, forking new process");
            pid_t child;

            child = start_child(recieve, config, monitor_proxy);

            /* parent */
            m_log.printfln(INFO, "Child started pid=%d", child);
            int res = 0;
            pid_t pid;

            /* Wait for the child to exit. this
             * condition will be signaled by the
             * sigchld handler */
            if(!g_cond->timedwait(*g_mutex, 300 SECS)) {
                /* The child is taking too long to return
                 * kill it */
                m_log.printfln(WARN, "Child timeout, sending SIGTERM");
                kill(child, SIGTERM);
                if(!g_cond->timedwait(*g_mutex, 10 SECS)) {
                    /* The child still isn't dead */
                    m_log.printfln(WARN, "Child hard timeout, sending SIGKILL");
                    kill(child, SIGKILL);
                }
            }

            if((pid = waitpid(child, &res, WNOHANG))) {
                if(pid == -1) {
                    m_log.printfln(ERROR, "Error in waitpid %s", strerror(errno));
                } else {
                    m_log.printfln(INFO, "Reap child (%d)\n", (int)pid);
                }
            }

            if(WIFSIGNALED(res)) {
                m_log.printfln(WARN, "Child was signaled with %d", WTERMSIG(res));
            }

            if(WEXITSTATUS(res)) {
                m_log.printfln(WARN, "Child returned abnormal status: %d", WEXITSTATUS(res));
            }

            if(WIFSTOPPED(res)) {
                m_log.printfln(WARN, "Child timed out and was killed by parent");
            }
        } else {
            m_log.printfln(ERROR, "Bad magic cookie received. Timeout for 5 seconds");
            sleep(5);
            m_log.printfln(INFO, "Timeout complete");
        }
    }

    return 0;
}
开发者ID:ZachAnders,项目名称:OPLabs,代码行数:100,代码来源:main.cpp


注:本文中的StreamSocket::read方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。