Browse Source

RpcServer RpcSession编译通过

tanghai 14 years ago
parent
commit
7ac1df3dbc

+ 3 - 0
Src/Egametang/Rpc/CMakeLists.txt

@@ -6,7 +6,10 @@ SET(RpcSrc
 	RpcCommunicator.cc
 	RpcController.cc
 	RequestHandler.cc
+	ResponseHandler.cc
 	RpcChannel.cc
+	RpcServer.cc
+	RpcSession.cc
 	${proto_srcs}
 )
 

+ 3 - 2
Src/Egametang/Rpc/MethodInfo.h

@@ -3,6 +3,7 @@
 
 #include <google/protobuf/service.h>
 #include <google/protobuf/message.h>
+#include "Rpc/RpcTypedef.h"
 
 namespace Egametang {
 
@@ -10,8 +11,8 @@ struct MethodInfo
 {
 	RpcServicePtr service;
 	const google::protobuf::MethodDescriptor* method_descriptor;
-	google::protobuf::Message* request_prototype;
-	google::protobuf::Message* response_prototype;
+	const google::protobuf::Message* request_prototype;
+	const google::protobuf::Message* response_prototype;
 
 	MethodInfo(RpcServicePtr service, const google::protobuf::MethodDescriptor* method_descriptor):
 		service(service), method_descriptor(method_descriptor)

+ 2 - 1
Src/Egametang/Rpc/ResponseHandler.cc

@@ -1,5 +1,6 @@
 #include "Rpc/MethodInfo.h"
 #include "Rpc/ResponseHandler.h"
+#include "Rpc/RpcCommunicator.h"
 
 namespace Egametang {
 
@@ -18,7 +19,7 @@ ResponseHandler::~ResponseHandler()
 	delete response;
 }
 
-google::protobuf::MethodDescriptor* ResponseHandler::Method()
+const google::protobuf::MethodDescriptor* ResponseHandler::Method()
 {
 	return method;
 }

+ 6 - 2
Src/Egametang/Rpc/ResponseHandler.h

@@ -1,15 +1,19 @@
 #ifndef RPC_RESPONSEHANDLER_H
 #define RPC_RESPONSEHANDLER_H
 
+#include <boost/function.hpp>
 #include <google/protobuf/service.h>
 #include <google/protobuf/message.h>
+#include "Base/Typedef.h"
+#include "Rpc/RpcTypedef.h"
 
 namespace Egametang {
 
 class ResponseHandler
 {
 private:
-	google::protobuf::MethodDescriptor* method;
+
+	const google::protobuf::MethodDescriptor* method;
 	google::protobuf::Message* request;
 	google::protobuf::Message* response;
 	std::size_t id;
@@ -20,7 +24,7 @@ public:
 
 	~ResponseHandler();
 
-	google::protobuf::MethodDescriptor* Method();
+	const google::protobuf::MethodDescriptor* Method();
 
 	google::protobuf::Message* Request();
 

+ 2 - 1
Src/Egametang/Rpc/RpcCommunicator.h

@@ -36,10 +36,11 @@ struct RpcMeta
 
 class RpcCommunicator: public boost::noncopyable
 {
-protected:
+private:
 	boost::asio::io_service& io_service;
 	boost::asio::ip::tcp::socket socket;
 
+public:
 	explicit RpcCommunicator(boost::asio::io_service& io_service);
 	virtual ~RpcCommunicator();
 	boost::asio::ip::tcp::socket& Socket();

+ 9 - 8
Src/Egametang/Rpc/RpcServer.cc

@@ -1,6 +1,8 @@
 #include <boost/asio.hpp>
 #include <boost/foreach.hpp>
+#include <boost/bind.hpp>
 #include <google/protobuf/service.h>
+#include <google/protobuf/descriptor.h>
 #include <glog/logging.h>
 #include "Base/Marcos.h"
 #include "Rpc/RpcTypedef.h"
@@ -8,15 +10,14 @@
 #include "Rpc/RpcSession.h"
 #include "Rpc/ResponseHandler.h"
 #include "Rpc/MethodInfo.h"
-#include "Thread/ThreadPool.h"
 
 namespace Egametang {
 
-RpcServer::RpcServer(boost::asio::io_service& io_service, int port):
-		io_service(io_service), thread_pool()
+RpcServer::RpcServer(boost::asio::io_service& service, int port):
+		io_service(service), acceptor(io_service), thread_pool()
 {
 	boost::asio::ip::address address;
-	address.from_string("localhost");
+	address.from_string("127.0.0.1");
 	boost::asio::ip::tcp::endpoint endpoint(address, port);
 	acceptor.open(endpoint.protocol());
 	acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
@@ -45,7 +46,7 @@ void RpcServer::OnAsyncAccept(RpcSessionPtr session, const boost::system::error_
 	RpcSessionPtr new_session(new RpcSession(*this));
 	acceptor.async_accept(new_session->Socket(),
 			boost::bind(&RpcServer::OnAsyncAccept, this,
-					boost::asio::placeholders::error));
+					new_session, boost::asio::placeholders::error));
 }
 
 void RpcServer::OnCallMethod(RpcSessionPtr session, ResponseHandlerPtr response_handler)
@@ -76,12 +77,12 @@ void RpcServer::RunService(RpcSessionPtr session, RpcMetaPtr meta,
 	response_handler->Request()->ParseFromString(*message);
 
 	google::protobuf::Closure* done = google::protobuf::NewCallback(
-			shared_from_this(), &RpcServer::OnCallMethod,
+			this, &RpcServer::OnCallMethod,
 			session, response_handler);
 
 	thread_pool.Schedule(
-			boost::bind(&google::protobuf::Service::CallMethod, this,
-					response_handler->Method(), NULL,
+			boost::bind(&google::protobuf::Service::CallMethod, method_info->service,
+					response_handler->Method(), (google::protobuf::RpcController*)(NULL),
 					response_handler->Request(), response_handler->Response(),
 					done));
 }

+ 5 - 1
Src/Egametang/Rpc/RpcServer.h

@@ -2,11 +2,15 @@
 #define RPC_RPCSERVER_H
 #include <boost/asio.hpp>
 #include <boost/function.hpp>
+#include <boost/unordered_set.hpp>
+#include <boost/unordered_map.hpp>
+#include <google/protobuf/service.h>
+#include "Thread/ThreadPool.h"
 #include "Rpc/RpcTypedef.h"
 
 namespace Egametang {
 
-class RpcServer: public google::protobuf::Service, boost::enable_shared_from_this<RpcServer>
+class RpcServer: public boost::noncopyable
 {
 private:
 	typedef boost::unordered_set<RpcSessionPtr> RpcSessionSet;

+ 6 - 3
Src/Egametang/Rpc/RpcSession.cc

@@ -1,5 +1,6 @@
 #include <boost/bind.hpp>
 #include "Rpc/RpcSession.h"
+#include "Rpc/RpcServer.h"
 
 namespace Egametang {
 
@@ -10,8 +11,9 @@ RpcSession::RpcSession(RpcServer& server):
 
 void RpcSession::OnRecvMessage(RpcMetaPtr meta, StringPtr message)
 {
-	rpc_server.RunService(shared_from_this(), meta, message,
-			boost::bind(&RpcSession::SendMeta, shared_from_this(), _1, _2));
+	RpcSessionPtr session = shared_from_this();
+	rpc_server.RunService(session, meta, message,
+			boost::bind(&RpcSession::SendMeta, session, _1, _2));
 
 	// 可以循环利用
 	RecvMeta(meta, message);
@@ -31,7 +33,8 @@ void RpcSession::Start()
 void RpcSession::Stop()
 {
 	RpcCommunicator::Stop();
-	rpc_server.RemoveSession(shared_from_this());
+	RpcSessionPtr session = shared_from_this();
+	rpc_server.RemoveSession(session);
 }
 
 } // namespace Egametang

+ 2 - 5
Src/Egametang/Rpc/RpcSession.h

@@ -2,18 +2,15 @@
 #define RPC_RPCSESSION_H
 
 #include <boost/asio.hpp>
-#include <boost/noncopyable.hpp>
 #include <boost/enable_shared_from_this.hpp>
 #include "Rpc/RpcTypedef.h"
+#include "Rpc/RpcCommunicator.h"
 
 namespace Egametang {
 
 class RpcServer;
 
-class RpcSession:
-		private boost::noncopyable,
-		public RpcCommunicator,
-		public boost::enable_shared_from_this<RpcSession>
+class RpcSession: public RpcCommunicator, public boost::enable_shared_from_this<RpcSession>
 {
 private:
 	RpcServer& rpc_server;

+ 1 - 0
Src/Egametang/Rpc/RpcTypedef.h

@@ -3,6 +3,7 @@
 #include <boost/shared_ptr.hpp>
 #include <boost/function.hpp>
 #include <google/protobuf/service.h>
+#include "Base/Typedef.h"
 
 namespace Egametang {
 

+ 1 - 0
Src/Egametang/Thread/ThreadPool.h

@@ -4,6 +4,7 @@
 #include <list>
 #include <boost/thread.hpp>
 #include <boost/function.hpp>
+#include <boost/noncopyable.hpp>
 #include <boost/detail/atomic_count.hpp>
 #include "Thread/ThreadTypedef.h"
 #include "Base/Marcos.h"

+ 1 - 1
Src/Egametang/Thread/ThreadPoolTest.cc

@@ -41,7 +41,7 @@ TEST_F(ThreadPoolTest, Test1)
 	pool.Wait();
 	for (int i = 0; i < 100; ++i)
 	{
-		ASSERT_EQ(9, z[i]) << "i = " << i;
+		ASSERT_EQ(9, z[i]);
 	}
 }