Преглед изворни кода

增加了rpc几个类,还未完成……

tanghai пре 15 година
родитељ
комит
2e3cbca8f0

+ 10 - 0
src/base/typedefs.h

@@ -2,7 +2,17 @@
 #define BASE_TYPEDEFS_H
 #include <boost/smart_ptr.hpp>
 
+typedef boost::int8_t   int8;
+typedef boost::int16_t  int16;
+typedef boost::int32_t  int32;
+typedef boost::int64_t  int64;
+typedef boost::uint8_t  uint8;
+typedef boost::uint16_t uint16;
+typedef boost::uint32_t uint32;
+typedef boost::uint64_t uint64;
+
 // smart_ptr typedef
 typedef boost::shared_ptr<boost::thread> thread_ptr;
+typedef boost::shared_ptr<google::protobuf::Service> protobuf_service_ptr;
 
 #endif // BASE_TYPEDEFS_H

+ 1 - 1
src/net/asyn_server.h

@@ -25,6 +25,6 @@ public:
 	void stop();
 };
 
-} // hainan
+} // namespace hainan
 
 #endif // NET_ASYNSERVER_H

+ 21 - 0
src/net/protobuf_rpc.proto

@@ -0,0 +1,21 @@
+package hainan;
+
+enum response_type
+{
+	RESPONSE_TYPE_OK = 1;
+	RESPONSE_TYPE_ERROR = 2;
+}
+
+message rpc_request
+{
+	required uint32 id = 1;
+	required string method = 2;
+	optional bytes request = 3;
+}
+
+message rpc_response
+{
+	required uint32 id = 1;
+	required response_type type = 2;
+	optional bytes response = 3;
+}

+ 29 - 0
src/net/rpc_callback.cc

@@ -0,0 +1,29 @@
+#include <google/protobuf/service.h>
+#include <google/protobuf/message.h>
+#include "net/rpc_callback.h"
+
+namespace hainan {
+
+rpc_callback::rpc_callback(google::protobuf::RpcController* controller,
+		google::protobuf::Message* response,
+		google::protobuf::Closure* done):
+		controller_(controller), response_(response), done_(done)
+{
+}
+
+google::protobuf::RpcController* rpc_callback::controller() const
+{
+    return controller_;
+}
+
+google::protobuf::Closure* rpc_callback::done() const
+{
+    return done_;
+}
+
+google::protobuf::Message* rpc_callback::response() const
+{
+    return response_;
+}
+
+} // hainan

+ 29 - 0
src/net/rpc_callback.h

@@ -0,0 +1,29 @@
+#ifndef NET_RPC_CALLBACK_H
+#define NET_RPC_CALLBACK_H
+
+namespace hainan {
+
+class google::protobuf::RpcController;
+class google::protobuf::Message;
+class google::protobuf::Closure;
+
+class rpc_callback
+{
+private:
+	google::protobuf::RpcController* controller_;
+	google::protobuf::Message* response_;
+	google::protobuf::Closure* done_;
+
+public:
+	rpc_callback(google::protobuf::RpcController* controller,
+			google::protobuf::Message* response,
+			google::protobuf::Closure* done);
+	~rpc_callback();
+    google::protobuf::RpcController*controller() const;
+    google::protobuf::Closure* done() const;
+    google::protobuf::Message* response() const;
+};
+
+} // hainan
+
+#endif // NET_RPC_CALLBACK_H

+ 39 - 0
src/net/rpc_channel.cc

@@ -0,0 +1,39 @@
+#include <google/protobuf/message.h>
+#include "net/rpc_channel.h"
+
+namespace hainan {
+
+google::protobuf::Closure* to_closure();
+
+rpc_channel::rpc_channel(std::string& host, int port):
+		id_(0)
+{
+}
+
+//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 rpc_channel::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;
+	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);
+}
+
+} // namespace hainan

+ 30 - 0
src/net/rpc_channel.h

@@ -0,0 +1,30 @@
+#ifndef NET_RPC_CHANNEL_H
+#define NET_RPC_CHANNEL_H
+
+#include <google/protobuf/service.h>
+#include <boost/unordered_map.hpp>
+#include <boost/asio.hpp>
+#include "base/base.h"
+#include "net/rpc_communicator.h"
+
+namespace hainan {
+
+class rpc_channel: public google::protobuf::RpcChannel
+{
+private:
+	int32 id_;
+	rpc_communicator communicator_;
+public:
+	rpc_channel(std::string& host, int port);
+	~rpc_channel();
+	virtual void CallMethod(
+			const google::protobuf::MethodDescriptor* method,
+			google::protobuf::RpcController* controller,
+			const google::protobuf::Message* request,
+			google::protobuf::Message* response,
+			google::protobuf::Closure* done);
+};
+
+} // namespace hainan
+
+#endif // NET_RPC_CHANNEL_H

+ 11 - 0
src/net/rpc_communicator.cc

@@ -0,0 +1,11 @@
+#include "net/rpc_communicator.h"
+
+namespace hainan {
+
+void rpc_communicator::send_message(
+		const rpc_request& req, rpc_callback& callback)
+{
+
+}
+
+} // namespace hainan

+ 23 - 0
src/net/rpc_communicator.h

@@ -0,0 +1,23 @@
+#ifndef NET_RPC_COMMUNICATOR_H
+#define NET_RPC_COMMUNICATOR_H
+
+#include <boost/asio.hpp>
+
+namespace hainan {
+
+class rpc_request;
+class rpc_callback;
+
+class rpc_communicator
+{
+private:
+	boost::asio::ip::tcp::socket socket_;
+public:
+	rpc_communicator(boost::asio::ip::tcp::endpoint& endpoint);
+	~rpc_communicator();
+	void send_message(const rpc_request& req, rpc_callback& callback);
+};
+
+}
+
+#endif // NET_RPC_COMMUNICATOR_H

+ 1 - 0
src/net/rpc_controller.cc

@@ -0,0 +1 @@
+#include "net/rpc_controller.h"

+ 16 - 0
src/net/rpc_controller.h

@@ -0,0 +1,16 @@
+#ifndef NET_RPC_CONTROLLER_H
+#define NET_RPC_CONTROLLER_H
+
+#include <google/protobuf/service.h>
+
+namespace hainan {
+
+class rpc_controller: public google::protobuf::RpcController
+{
+public:
+
+};
+
+} // namespace hainan
+
+#endif // NET_RPC_CONTROLLER_H