1 #ifndef __PROTOBUFRPC_H__
2 #define __PROTOBUFRPC_H__
5 #include <boost/shared_ptr.hpp>
6 #include <boost/enable_shared_from_this.hpp>
7 #include <boost/thread.hpp>
8 #include <boost/function.hpp>
11 #include "socket_pool.h"
14 #include <google/protobuf/stubs/common.h>
15 #include <google/protobuf/generated_message_reflection.h>
16 #include <google/protobuf/repeated_field.h>
17 #include <google/protobuf/extension_set.h>
18 #include <google/protobuf/service.h>
21 using namespace google::protobuf;
22 using namespace boost;
27 class ProtoBufRpcService;
29 class ProtoBufRpcConnection
30 : public enable_shared_from_this<ProtoBufRpcConnection>,
31 private boost::noncopyable
34 explicit ProtoBufRpcConnection(asio::io_service& io_service,
37 asio::ip::tcp::socket& socket();
41 void writeResponse(Message *msg);
45 void handle_read(const error_code& e,
46 std::size_t bytes_transferred);
48 void handle_write(const error_code& e,
49 std::size_t bytes_transferred);
54 asio::io_service::strand _strand;
58 asio::streambuf _buffer;
65 STATE_HAVE_ID_AND_LEN,
66 STATE_WAITING_FOR_DATA,
72 class ProtoBufRpcServer
77 bool registerService(uint16_t port,
78 shared_ptr< Service> service);
80 // So we can call this as a thread.
88 class RegisteredService
92 shared_ptr<asio::io_service> io_service,
94 shared_ptr<Service> service
97 void handle_accept(const error_code& e);
101 shared_ptr<asio::io_service> _io_service;
103 shared_ptr<Service> _service;
104 tcp::endpoint _endpoint;
105 tcp::acceptor _acceptor;
106 shared_ptr<ProtoBufRpcConnection> _new_connection;
109 list<shared_ptr<RegisteredService> > _services;
110 shared_ptr<asio::io_service> _io_service;
113 class ProtoBufRpcController : public RpcController
116 ProtoBufRpcController();
117 virtual ~ProtoBufRpcController();
119 virtual void Reset();
120 virtual bool Failed() const;
121 virtual string ErrorText() const;
122 virtual void StartCancel();
124 virtual void SetFailed(const string &reason);
125 virtual bool IsCanceled() const;
126 virtual void NotifyOnCancel(Closure *callback);
129 class ProtoBufRpcChannel
131 public enable_shared_from_this<ProtoBufRpcChannel>,
132 private boost::noncopyable
135 ProtoBufRpcChannel(const string &remotehost, const string &port);
137 virtual ~ProtoBufRpcChannel();
139 virtual void CallMethod(
140 const MethodDescriptor * method,
141 RpcController * controller,
142 const Message * request,
147 shared_ptr<tcp::socket> getSocket();
148 void putSocket(shared_ptr<tcp::socket>);
157 asio::io_service _io_service;
158 tcp::resolver _resolver;
159 // This exists to keep the io service running
160 tcp::acceptor _acceptor;
164 asio::ip::tcp::socket _lame_socket;
165 void lame_handle_accept(const error_code &err);
167 shared_ptr<boost::thread> _thread;
171 } // namespace bicker