1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| #include "protobuf/dispatcher.h" #include "protobuf/codec.h"
#include "muduo/include/muduo/base/Mutex.h" #include "muduo/include/muduo/base/Logging.h" #include "muduo/include/muduo/net/EventLoop.h" #include "muduo/include/muduo/net/TcpClient.h" #include "muduo/include/muduo/net/EventLoopThread.h" #include "muduo/include/muduo/base/CountDownLatch.h"
#include "Request.pb.h" #include "../logs/Xulog.h"
#include <iostream> #include <unistd.h>
class Client { public: using MessagePtr = std::shared_ptr<google::protobuf::Message>; using TranslateResponsePtr = std::shared_ptr<Xu::TranslateResponse>; using AddResponsePtr = std::shared_ptr<Xu::AddResponse>; Client(const std::string &sip, int sport) : _latch(1), _client(_loopthread.startLoop(), muduo::net::InetAddress(sip, sport), "Client"), _dispatcher(std::bind(&Client::onUnknowMessage, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)), _codec(std::bind(&ProtobufDispatcher::onProtobufMessage, &_dispatcher, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)) { _dispatcher.registerMessageCallback<Xu::TranslateResponse>(std::bind(&Client::onTranslate, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); _dispatcher.registerMessageCallback<Xu::AddResponse>(std::bind(&Client::onAdd, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); _client.setMessageCallback(std::bind(&ProtobufCodec::onMessage, &_codec, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); _client.setConnectionCallback(std::bind(&Client::onConnection, this, std::placeholders::_1)); } void connect() { _client.connect(); _latch.wait(); } void Translate(const std::string &str) { Xu::TranslateRequest req; req.set_msg(str);
send(&req); } void Add(int num1, int num2) { Xu::AddRequest req; req.set_num1(num1); req.set_num2(num2);
send(&req); }
private: bool send(const google::protobuf::Message *msg) { if (_conn->connected()) { _codec.send(_conn, *msg); return true; } return false; }
void onAdd(const muduo::net::TcpConnectionPtr &conn, const AddResponsePtr message, muduo::Timestamp) { INFO("加法结果是: %d", message->result()); } void onTranslate(const muduo::net::TcpConnectionPtr &conn, const TranslateResponsePtr message, muduo::Timestamp) { INFO("翻译结果是: %s", message->msg()); } void onConnection(const muduo::net::TcpConnectionPtr &conn) { if (conn->connected()) { _latch.countDown(); _conn = conn; } else { _conn.reset(); } }
void onUnknowMessage(const muduo::net::TcpConnectionPtr &conn, const Client::MessagePtr message, muduo::Timestamp) { INFO("onUnknowMessage: %s", message->GetTypeName()); conn->shutdown(); }
private: muduo::CountDownLatch _latch; muduo::net::EventLoopThread _loopthread; muduo::net::TcpConnectionPtr _conn; muduo::net::TcpClient _client; ProtobufDispatcher _dispatcher; ProtobufCodec _codec; };
int main() { Client client("127.0.0.1", 8085); client.connect(); client.Translate("Hello"); client.Add(1, 6); sleep(1); return 0; }
|