Ejemplo n.º 1
0
void EchoServer::onMessage(const muduo::net::TcpConnectionPtr &conn,
        muduo::net::Buffer *buf, muduo::Timestamp time)
{
    muduo::string msg(buf->retrieveAllAsString());
    LOG_INFO << conn->name() << " echo " << msg.size() << " bytes, "
        << " data received at " << time.toString();
    conn->send(msg);
}
Ejemplo n.º 2
0
void SudoKuServer::onMessage(const muduo::net::TcpConnectionPtr& conn,
                             muduo::net::Buffer* buf, muduo::Timestamp time)
{
    LOG_DEBUG << conn->name();
    size_t len = buf->readableBytes();
    while (len >= kCell + 2)
    {
        const char* crlf = buf->findCRLF();
        if(crlf)
        {
            string request(buf->peek(), crlf);
            string id;
            buf->retrieveUntil(crlf + 2);
            string::iterator colon = find(request.begin(), request.end(), ':');
            if (colon != request.end())
            {
                id.assign(request.begin(), colon);
                request.erase(request.begin(), colon + 1);
            }
            if (request.size() == boost::implicit_cast<size_t>(kCell))
            {
                string result = solveSudoku(request);
                if (id.empty())
                {
                    conn->send(result + "\r\n");
                }
                else
                {
                    conn->send(id + ":" + result + "\r\n");
                }
            }
            else
            {
                conn->send("Bad Request!\r\n");
            }
        }
        else
        {
            break;
        }

    }
}