This repository has been archived on 2024-12-15. You can view files and clone it, but cannot push or open issues or pull requests.
ProtoBufTest/Server/Server.cpp

88 lines
1.7 KiB
C++
Raw Normal View History

2015-02-19 13:59:22 +00:00
#include <iostream>
#include <string>
2015-04-09 14:23:47 +00:00
#include <memory>
#include <zmq.hpp>
2015-04-09 14:23:47 +00:00
#include "proto/Messages.pb.h"
class MyPingService : public Messages::PingService
{
public:
void Ping(
::google::protobuf::RpcController* controller,
const ::Messages::PingRequest* request,
::Messages::PingReply* response,
::google::protobuf::Closure* done) override
{
}
void Ping2(
::google::protobuf::RpcController* controller,
const ::Messages::PingRequest* request,
::Messages::PingReply* response,
::google::protobuf::Closure* done) override
{
}
};
class MyRpcServer
{
private:
std::shared_ptr<zmq::context_t> m_zmqContext;
std::shared_ptr<zmq::socket_t> m_zmqSocket;
MyPingService m_pingService;
public:
MyRpcServer(const std::string &bindString)
{
m_zmqContext = std::make_shared<zmq::context_t>(1);
m_zmqSocket = std::make_shared<zmq::socket_t>(*m_zmqContext, ZMQ_REQ);
m_zmqSocket->bind(bindString.c_str());
}
void Run()
{
while (true)
{
}
}
};
2015-02-19 13:59:22 +00:00
int main(int argc, char **argv)
{
std::cout << "Server running" << std::endl;
2015-04-09 14:23:47 +00:00
MyRpcServer server("tcp://*:5555");
server.Run();
zmq::context_t context(1);
zmq::socket_t socket(context, ZMQ_REP);
socket.bind("tcp://*:5555");
while (true)
{
zmq::message_t request;
socket.recv(&request);
2015-04-09 14:23:47 +00:00
Messages::PingRequest requestMessage;
requestMessage.ParseFromArray(request.data(), request.size());
std::cout << "Received request: " << requestMessage.text() << std::endl;
2015-04-09 14:23:47 +00:00
Messages::PingReply replyMessage;
replyMessage.set_text("Hello Client");
zmq::message_t reply(replyMessage.ByteSize());
replyMessage.SerializeToArray(reply.data(), reply.size());
socket.send(reply);
}
2015-02-19 13:59:22 +00:00
return 0;
2015-04-09 14:23:47 +00:00
}