|
|
@@ -1,39 +1,54 @@
|
|
|
+#include <boost/asio.hpp>
|
|
|
+#include <boost/make_shared.hpp>
|
|
|
#include <google/protobuf/message.h>
|
|
|
-#include "net/rpc_channel.h"
|
|
|
+#include "Net/RpcChannel.h"
|
|
|
+#include "Net/RpcCommunicator.h"
|
|
|
|
|
|
namespace Hainan {
|
|
|
|
|
|
-google::protobuf::Closure* to_closure();
|
|
|
-
|
|
|
-rpc_channel::rpc_channel(std::string& host, int port):
|
|
|
+RpcChannel::RpcChannel(std::string& host, int port):
|
|
|
id(0)
|
|
|
{
|
|
|
+ // socket.async_connect(endpoint, );
|
|
|
+}
|
|
|
+
|
|
|
+void RpcCommunicator::SendRequestHandler(int32 id, RpcHandlerPtr handler,
|
|
|
+ const boost::system::error_code& err)
|
|
|
+{
|
|
|
+ if (err)
|
|
|
+ {
|
|
|
+ handler->GetController()->SetFailed("failed");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ handlers[id] = handler;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-//void client_channel::register_service(const google::protobuf::Service service) {
|
|
|
-// const google::protobuf::ServiceDescriptor* service_descriptor =
|
|
|
-// service->GetDescriptor();
|
|
|
-// for (int i = 0; i < service_descriptor; ++i)
|
|
|
-// {
|
|
|
-// const google::protobuf::MethodDescriptor* method =
|
|
|
-// service_descriptor->method(i);
|
|
|
-// std::string method_name(method->full_name());
|
|
|
-// service_map_[method_name] = service;
|
|
|
-// }
|
|
|
-//}
|
|
|
+void RpcChannel::SendRequest(const RpcRequest& request, RpcHandlerPtr handler)
|
|
|
+{
|
|
|
+ int size = request.ByteSize();
|
|
|
+ std::stringstream ss;
|
|
|
+ ss << size;
|
|
|
+ ss << request.SerializeAsString();
|
|
|
+ communicator.AsyncWrite(boost::asio::buffer(ss),
|
|
|
+ boost::bind(&RpcChannel::SendRequestHandler, this, request.id(),
|
|
|
+ handler, boost::asio::placeholders::error));
|
|
|
+}
|
|
|
|
|
|
-void rpc_channel::CallMethod(
|
|
|
+void RpcChannel::CallMethod(
|
|
|
const google::protobuf::MethodDescriptor* method,
|
|
|
google::protobuf::RpcController* controller,
|
|
|
const google::protobuf::Message* request,
|
|
|
google::protobuf::Message* response,
|
|
|
google::protobuf::Closure* done) {
|
|
|
- rpc_request req;
|
|
|
+ RpcRequest req;
|
|
|
req.set_id(++id);
|
|
|
req.set_method(method->full_name());
|
|
|
req.set_request(request->SerializeAsString());
|
|
|
- rpc_callback callback(controller, response, done);
|
|
|
- communicator.send_message(req, callback);
|
|
|
+ RpcHandlerPtr handler = boost::make_shared<RpcHandler>(
|
|
|
+ controller, response, done);
|
|
|
+ SendRequest(req, handler);
|
|
|
}
|
|
|
|
|
|
} // namespace Hainan
|