Просмотр исходного кода

RPCCommunicatorTest编译通过

tanghai 14 лет назад
Родитель
Сommit
c2104dfa3b

+ 2 - 0
Src/CMakeLists.txt

@@ -6,5 +6,7 @@ enable_testing()
 include_directories(Egametang)
 include_directories(ThirdParty)
 
+link_directories(/usr/local/lib)
+
 add_subdirectory(Egametang)
 add_subdirectory(ThirdParty)

+ 1 - 0
Src/Egametang/CMakeLists.txt

@@ -1,2 +1,3 @@
 add_subdirectory(Python)
 add_subdirectory(Thread)
+add_subdirectory(Rpc)

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

@@ -0,0 +1,23 @@
+set(RpcSrc 
+	RPCCommunicator.cc
+	)
+
+add_library(Rpc ${RpcSrc})
+
+add_executable(RPCCommunicatorTest RPCCommunicatorTest.cc)
+
+set(Excutes 
+	RPCCommunicatorTest
+)
+
+foreach(Excute ${Excutes})
+	target_link_libraries(${Excute}
+		Rpc
+		boost_system
+		protobuf
+		glog
+		gtest
+		gmock
+	)
+	add_test(${Excute} ${Excute})
+endforeach()

+ 17 - 6
Src/Egametang/Rpc/RPCCommunicator.cc

@@ -1,10 +1,13 @@
 #include <boost/bind.hpp>
 #include <boost/asio.hpp>
-#include "Rpc/RpcCommunicator.h"
+#include <boost/lexical_cast.hpp>
+#include <glog/logging.h>
+#include "Rpc/RPCCommunicator.h"
 
 namespace Egametang {
 
-RPCCommunicator::RPCCommunicator()
+RPCCommunicator::RPCCommunicator(boost::asio::io_service& io_service):
+		socket_(io_service)
 {
 }
 
@@ -29,9 +32,9 @@ void RPCCommunicator::RecvMessage(IntPtr size, const boost::system::error_code&
 		LOG(ERROR) << "receive message size failed: " << err.message();
 		return;
 	}
-	StringPtr ss;
+	StringPtr ss(new std::string(*size, '\0'));
 	boost::asio::async_read(socket_,
-			boost::asio::buffer(*ss, *size),
+			boost::asio::buffer(reinterpret_cast<char*>(ss->at(0)), *size),
 			boost::bind(&RPCCommunicator::RecvDone, this, ss,
 					boost::asio::placeholders::error));
 }
@@ -48,10 +51,10 @@ void RPCCommunicator::RecvDone(StringPtr ss, const boost::system::error_code& er
 
 void RPCCommunicator::SendSize(int size, std::string message)
 {
-	std::string ssize = boost::lexical_cast(size);
+	std::string ssize = boost::lexical_cast<std::string>(size);
 	boost::asio::async_write(socket_, boost::asio::buffer(ssize),
 			boost::bind(&RPCCommunicator::SendMessage, this, message,
-					handler, boost::asio::placeholders::error));
+					boost::asio::placeholders::error));
 }
 
 void RPCCommunicator::SendMessage(std::string message, const boost::system::error_code& err)
@@ -75,4 +78,12 @@ void RPCCommunicator::SendDone(const boost::system::error_code& err)
 	OnSendMessage();
 }
 
+void RPCCommunicator::OnRecvMessage(StringPtr ss)
+{
+}
+
+void RPCCommunicator::OnSendMessage()
+{
+}
+
 } // namespace Egametang

+ 3 - 3
Src/Egametang/Rpc/RPCCommunicator.h

@@ -16,7 +16,7 @@ protected:
 	boost::asio::ip::tcp::socket socket_;
 
 public:
-	RPCCommunicator();
+	RPCCommunicator(boost::asio::io_service&);
 
 	boost::asio::ip::tcp::socket& Socket();
 
@@ -30,8 +30,8 @@ public:
 	void SendMessage(std::string message, const boost::system::error_code& err);
 	void SendDone(const boost::system::error_code& err);
 
-	virtual void OnRecvMessage(StringPtr ss) = 0;
-	virtual void OnSendMessage() = 0;
+	virtual void OnRecvMessage(StringPtr ss);
+	virtual void OnSendMessage();
 };
 
 } // namespace Egametang

+ 45 - 47
Src/Egametang/Rpc/RPCCommunicatorTest.cc

@@ -1,3 +1,5 @@
+#include <boost/bind.hpp>
+#include <boost/asio.hpp>
 #include <gtest/gtest.h>
 #include <gflags/gflags.h>
 #include <glog/logging.h>
@@ -5,23 +7,19 @@
 
 namespace Egametang {
 
-static int port = 10001;
+static int global_port = 10001;
 
 class RPCServerTest: public RPCCommunicator
 {
 public:
-	StringPtr recv_string_;
-
-	int send_id_;
-	RpcHandlerPtr send_handler_;
-
-	boost::asio::io_service& io_service_;
+	std::string recv_string_;
 	boost::asio::ip::tcp::acceptor acceptor_;
-
-	boost::asio::ip::tcp::socket server_socket;
+	boost::asio::io_service& io_service_;
 
 public:
-	RPCServerTest(boost::asio::io_service& io_service, int port)
+	RPCServerTest(boost::asio::io_service& io_service, int port):
+		io_service_(io_service), acceptor_(io_service_),
+		RPCCommunicator(io_service_)
 	{
 		boost::asio::ip::address address;
 		address.from_string("localhost");
@@ -41,40 +39,31 @@ public:
 
 	void Start()
 	{
-		IntPtr size(new int);
-		boost::asio::async_read(socket_,
-				boost::asio::buffer(reinterpret_cast<char*>(size.get()), sizeof(int)),
-				boost::bind(&RPCCommunicator::RecvMessage, this, size,
-						boost::asio::placeholders::error));
+		RecvSize();
 	}
 
 	virtual void OnRecvMessage(StringPtr ss)
 	{
-		recv_string_ = ss;
+		recv_string_ = *ss;
 	}
-	virtual void OnSendMessage(int32 id, RpcHandlerPtr handler)
+	virtual void OnSendMessage()
 	{
-		send_id_ = id;
-		send_handler_ = handler;
 	}
 };
 
 class RPCClientTest: public RPCCommunicator
 {
 public:
-	StringPtr recv_string_;
-	int send_id_;
-	RpcHandlerPtr send_handler_;
-
+	std::string recv_string_;
 	boost::asio::io_service& io_service_;
 
 public:
-	RPCClientTest(boost::asio::io_service& io_service, std::string& host, int port):
-		io_service_(io_service)
+	RPCClientTest(boost::asio::io_service& io_service, std::string host, int port):
+		io_service_(io_service), RPCCommunicator(io_service_)
 	{
 		boost::asio::ip::address address;
 		address.from_string(host);
-		boost::asio::ip::tcp::endpoint endpoint(address, port_);
+		boost::asio::ip::tcp::endpoint endpoint(address, port);
 		socket_.async_connect(endpoint,
 				boost::bind(&RPCClientTest::OnAsyncConnect, this,
 						boost::asio::placeholders::error));
@@ -85,47 +74,56 @@ public:
 		io_service_.run();
 	}
 
-	void OnAsyncConnect()
-	{
-	}
-
-	void SendString()
+	void OnAsyncConnect(const boost::system::error_code& err)
 	{
-
-		req->set_request(request->SerializeAsString());
-		RpcHandlerPtr handler(new RpcHandler(controller, response, done));
-		SendSize(req, handler);
+		if (err)
+		{
+			LOG(ERROR) << "async connect failed: " << err.message();
+			return;
+		}
+		std::string send_string("send test rpc communicator string");
+		SendSize(send_string.size(), send_string);
 	}
 
 	virtual void OnRecvMessage(StringPtr ss)
 	{
-		recv_string_ = ss;
+		recv_string_ = *ss;
 	}
 
-	virtual void OnSendMessage(int32 id, RpcHandlerPtr handler)
+	virtual void OnSendMessage()
 	{
-		send_id_ = id;
-		send_handler_ = handler;
 	}
 };
 
 class RPCCommunicatorTest: public testing::Test
 {
 protected:
+	boost::asio::io_service io_service_;
 	RPCServerTest rpc_server_;
 	RPCClientTest rpc_client_;
+
+public:
+	RPCCommunicatorTest():
+		io_service_(), rpc_server_(io_service_, global_port),
+		rpc_client_(io_service_, "localhost", global_port)
+	{
+	}
 };
 
 
-TEST_F(RPCCommunicatorTest, CallMethod)
+TEST_F(RPCCommunicatorTest, ClientSendString)
 {
-	RpcServerTest server(io_service_, port);
-	ASSERT_EQ(0, server.size);
-
-	RpcChannel channel(io_service_, "localhost", port);
-	channel.CallMethod(NULL, NULL, request, response_, done_);
-
-	ASSERT_EQ(request.ByteSize(), server.size);
+	int a = 2;
 }
 
 } // namespace Egametang
+
+
+int main(int argc, char* argv[])
+{
+	FLAGS_logtostderr = true;
+	testing::InitGoogleTest(&argc, argv);
+	google::ParseCommandLineFlags(&argc, &argv, true);
+	google::InitGoogleLogging(argv[0]);
+	return RUN_ALL_TESTS();
+}

+ 5 - 0
Src/Egametang/Rpc/RpcServer.cc

@@ -56,6 +56,11 @@ void RpcServer::Stop()
 	sessions_.clear();
 }
 
+boost::asio::io_service& RpcServer::IOService()
+{
+	return io_service_;
+}
+
 void RpcServer::RunService(RpcSessionPtr session, RpcRequestPtr request,
 		boost::function<void (RpcSessionPtr, RpcResponsePtr)> handler)
 {

+ 2 - 0
Src/Egametang/Rpc/RpcServer.h

@@ -8,6 +8,7 @@ namespace Egametang {
 class RpcServer: public boost::enable_shared_from_this<RpcServer>
 {
 private:
+	friend class RpcSession;
 	typedef boost::unordered_set<RpcSessionPtr> RpcSessionSet;
 
 	google::protobuf::Service& service_;
@@ -26,6 +27,7 @@ public:
 	void Start();
 	void Stop();
 
+	boost::asio::io_service& IOService();
 	void RunService(RpcSessionPtr session, RpcRequestPtr request,
 			boost::function<void (RpcSessionPtr, RpcResponsePtr)> handler);
 	void RegisterService(ProtobufServicePtr service);

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

@@ -2,7 +2,8 @@
 
 namespace Egametang {
 
-RpcSession::RpcSession(RpcServer& rpc_server): rpc_server_(rpc_server)
+RpcSession::RpcSession(RpcServer& rpc_server):
+		rpc_server_(rpc_server), RPCCommunicator(rpc_server_.io_service_)
 {
 }