void processRequest(const muduo::net::TcpConnectionPtr& conn, const QueenTask& task) { std::list<std::vector<int>> queens; if(task.getPos().size() == 0) { queens = solve_queens(task.getSize()); } else { queens = complete_queens(task.getPos(), task.getSize()); } int number = queens.size(); // solutions <clientId> <id> <subid> <number>\r\n std::string prefix = task.getClientId() + " " + task.getTaskId() + " " + task.getSubTaskId(); std::string res = "solutions " + prefix + " " + std::to_string(number) + "\r\n"; conn->send(res); LOG_INFO << "send " << res.substr(0, res.size()-2) << " to server"; if(task.isComputeSolutions()) { for(auto &queen : queens) { // one_solution <client_id> <id> <subid> <solution>\r\n std::string line = "one_solution " + prefix; for(auto &n : queen) { line += " " + std::to_string(n); } line += "\r\n"; conn->send(line); } } }
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); }
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; } } }
void onConnection(const muduo::net::TcpConnectionPtr& conn) { if(conn->connected()) { LOG_INFO << "connect to " << conn->peerAddress().toIpPort(); conn->setTcpNoDelay(true); conn->send("ready\r\n"); } else { LOG_INFO << conn->peerAddress().toIpPort() << " is down"; client.disconnect(); } }
void QueryServer::onMessage(const muduo::net::TcpConnectionPtr &conn, muduo::net::Buffer *buf, muduo::Timestamp) { muduo::string s(buf->retrieveAllAsString()); LOG_INFO << "receive php msg: " << s; std::string msg(s.c_str()); std::string result = query_.queryPage(msg); conn->send(result.c_str()); }
void MsgManager::broadcast(const muduo::net::TcpConnectionPtr& conn) { /* * { "type" : "new_msg", //消息类型,客户端向其他人发送消息,如梁杰发消息问晚上谁开黑 "data" : [ { "id":"101", "content":"晚上谁开黑", "sender":"liangjie" "receiver":"group202" } ] } * */ conn->send(tempMessage_); LOG_INFO<<"broadcast send:"<<tempMessage_; }
// command: // 1. genNumber n response: ok // 2. average response: number<double> // 3. median response: number<int64_t> // 4. sort response: ok // 5. freq n response: <n1, freq1> <n2, freq2> ... <n, freq> void DataServer::onClientMessage(const muduo::net::TcpConnectionPtr& conn, muduo::net::Buffer* buf, muduo::Timestamp time) { while(buf->findCRLF()) { const char* crlf = buf->findCRLF(); std::string command(buf->peek(), crlf); buf->retrieveUntil(crlf+2); std::vector<std::string> tokens; boost::split(tokens, command, boost::is_any_of(" ")); if(command.find("genNumber") == 0) { if(tokens.size() == 3) { GenNumberExecutor executor(connections); dataExecutor = &executor; int64_t number = std::stol(tokens[1]); char mode = tokens[2][0]; executor.execute(number, mode); conn->send("OK\r\n"); } else { conn->send("usage: genNumber [n] [n/u/z]\r\n"); } } else if(command == "average") { AverageExecutor executor(connections); dataExecutor = &executor; double average = executor.execute(); conn->send("average: " + std::to_string(average) + "\r\n"); } else if(command == "median") { MedianExecutor executor(connections); dataExecutor = &executor; int64_t median = executor.execute(); conn->send("median: " + std::to_string(median) + "\r\n"); } else if(command == "sort") { SortExecutor executor(connections); dataExecutor = &executor; executor.execute(); conn->send("OK\r\n"); } else if(command.find("freq") == 0) { size_t freqNumber = std::stoi(tokens[1]); FreqExecutor executor(connections); dataExecutor = &executor; std::vector<std::pair<int64_t, int64_t>> freqs = executor.execute(freqNumber); std::string response = "freqs:"; for(auto& freq : freqs) { response += " " + std::to_string(freq.first) + " " + std::to_string(freq.second); } response += "\r\n"; conn->send(response); } else { LOG_ERROR << "receive unknown command [" << command << "] from " << conn->peerAddress().toIpPort(); conn->shutdown(); } } }