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

复杂类型改成使用 c++ 11 auto关键字进行推导

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

+ 2 - 2
Cpp/Platform/Log/Log.cc

@@ -29,14 +29,14 @@ std::string FileName(const char* s)
 
 BoostLogInit::BoostLogInit(const char* fileName)
 {
-	boost::shared_ptr<boost::log::core> core = boost::log::core::get();
+	auto core = boost::log::core::get();
 	core->add_global_attribute("TimeStamp", boost::make_shared<attrs::local_clock>());
 	core->add_global_attribute("ThreadId", boost::make_shared<attrs::current_thread_id>());
 
 	pSink = boost::make_shared<text_sink>();
 
 	std::string logFileName = std::string(fileName) + ".log";
-	boost::shared_ptr<std::ostream> logStream = boost::make_shared<std::ofstream>(
+	auto logStream = boost::make_shared<std::ofstream>(
 	        logFileName.c_str());
 	if (!logStream->good())
 	{

+ 1 - 1
Cpp/Platform/Orm/DbHelper.h

@@ -34,7 +34,7 @@ public:
 		std::string sql = select.ToString();
 		VLOG(2) << "execute sql: " << sql;
 		ResultSetPtr resultSet(statement->executeQuery(sql));
-		DbResultPtr dbResult = boost::make_shared<DbResult>(resultSet);
+		auto dbResult = boost::make_shared<DbResult>(resultSet);
 		return dbResult;
 	}
 };

+ 1 - 1
Cpp/Platform/Orm/DbResultTest.cc

@@ -28,7 +28,7 @@ TEST_F(DbResultTest, One)
 				Where(Column("age") > 10)
 			);
 
-		boost::shared_ptr<Person> person = boost::make_shared<Person>();
+		auto person = boost::make_shared<Person>();
 		result->One(person);
 		ASSERT_EQ(26, person->age());
 	}

+ 1 - 1
Cpp/Platform/Python/PythonInterpreterTest.cc

@@ -72,7 +72,7 @@ TEST_F(PythonInterpreterTest, EnterPythonScript)
 	interpreter.ImportPath("../../../Cpp/Platform/Python/");
 	interpreter.ImportModule("PythonInterpreterTest");
 
-	PersonTestPtr person = boost::make_shared<PersonTestPtr>();
+	auto person = boost::make_shared<PersonTestPtr>();
 	interpreter.RegisterObjectPtr("person", person);
 
 	ASSERT_EQ(0, person->Guid());

+ 2 - 2
Cpp/Platform/Rpc/ResponseHandler.cc

@@ -37,8 +37,8 @@ google::protobuf::Message* ResponseHandler::Response()
 
 void ResponseHandler::Run()
 {
-	RpcMetaPtr meta = boost::make_shared<RpcMeta>();
-	StringPtr message = boost::make_shared<std::string>();
+	auto meta = boost::make_shared<RpcMeta>();
+	auto message = boost::make_shared<std::string>();
 	response->SerializeToString(message.get());
 	meta->id = id;
 	meta->size = message->size();

+ 6 - 6
Cpp/Platform/Rpc/RpcClient.cc

@@ -32,8 +32,8 @@ void RpcClient::OnAsyncConnect(const boost::system::error_code& err)
 	{
 		return;
 	}
-	RpcMetaPtr recvMeta = boost::make_shared<RpcMeta>();
-	StringPtr recvMessage = boost::make_shared<std::string>();
+	auto recvMeta = boost::make_shared<RpcMeta>();
+	auto recvMessage = boost::make_shared<std::string>();
 	RecvMeta(recvMeta, recvMessage);
 }
 
@@ -47,7 +47,7 @@ void RpcClient::OnRecvMessage(RpcMetaPtr meta, StringPtr message)
 	}
 	else
 	{
-		RequestHandlerPtr requestHandler = requestHandlers[meta->id];
+		auto requestHandler = requestHandlers[meta->id];
 		requestHandlers.erase(meta->id);
 		requestHandler->Response()->ParseFromString(*message);
 		// meta和message可以循环利用
@@ -72,13 +72,13 @@ void RpcClient::CallMethod(
 {
 	if (done)
 	{
-		RequestHandlerPtr request_handler = boost::make_shared<RequestHandler>(response, done);
+		auto request_handler = boost::make_shared<RequestHandler>(response, done);
 		requestHandlers[++id] = request_handler;
 	}
 	boost::hash<std::string> stringHash;
-	StringPtr message = boost::make_shared<std::string>();
+	auto message = boost::make_shared<std::string>();
 	request->SerializePartialToString(message.get());
-	RpcMetaPtr meta = boost::make_shared<RpcMeta>();
+	auto meta = boost::make_shared<RpcMeta>();
 	meta->size = message->size();
 	meta->id = id;
 	meta->method = stringHash(method->full_name());

+ 5 - 5
Cpp/Platform/Rpc/RpcClientTest.cc

@@ -39,8 +39,8 @@ public:
 		{
 			return;
 		}
-		RpcMetaPtr meta = boost::make_shared<RpcMeta>();
-		StringPtr message = boost::make_shared<std::string>();
+		auto meta = boost::make_shared<RpcMeta>();
+		auto message = boost::make_shared<std::string>();
 		RecvMeta(meta, message);
 	}
 
@@ -60,9 +60,9 @@ public:
 		EchoResponse response;
 		response.set_num(num);
 
-		StringPtr responseMessage = boost::make_shared<std::string>();
+		auto responseMessage = boost::make_shared<std::string>();
 		response.SerializeToString(responseMessage.get());
-		RpcMetaPtr responseMeta = boost::make_shared<RpcMeta>();
+		auto responseMeta = boost::make_shared<RpcMeta>();
 		responseMeta->id = meta->id;
 		responseMeta->size = responseMessage->size();
 		SendMeta(responseMeta, responseMessage);
@@ -99,7 +99,7 @@ TEST_F(RpcClientTest, Echo)
 
 	CountBarrier barrier(2);
 	RpcServerTest server(ioServer, port, barrier);
-	RpcClientPtr client = boost::make_shared<RpcClient>(ioClient, "127.0.0.1", port);
+	auto client = boost::make_shared<RpcClient>(ioClient, "127.0.0.1", port);
 	EchoService_Stub service(client.get());
 
 	boost::threadpool::fifo_pool threadPool(2);

+ 17 - 9
Cpp/Platform/Rpc/RpcCommunicatorTest.cc

@@ -1,3 +1,4 @@
+#include <WinSock2.h>
 #include <boost/bind.hpp>
 #include <boost/asio.hpp>
 #include <boost/threadpool.hpp>
@@ -41,11 +42,12 @@ public:
 	{
 		if (err)
 		{
+			LOG(FATAL) << err.message();
 			return;
 		}
 
-		RpcMetaPtr meta = boost::make_shared<RpcMeta>();
-		StringPtr message = boost::make_shared<std::string>();
+		auto meta = boost::make_shared<RpcMeta>();
+		auto message = boost::make_shared<std::string>();
 		RecvMeta(meta, message);
 	}
 
@@ -65,8 +67,8 @@ public:
 		recvMessage = *message;
 		recvMeta = *meta;
 
-		RpcMetaPtr responseMeta = boost::make_shared<RpcMeta>();
-		StringPtr response_message = boost::make_shared<std::string>(
+		auto responseMeta = boost::make_shared<RpcMeta>();
+		auto response_message = boost::make_shared<std::string>(
 				"response test rpc communicator string");
 		responseMeta->size = response_message->size();
 		responseMeta->method = 123456;
@@ -94,6 +96,7 @@ public:
 		boost::asio::ip::address address;
 		address.from_string("127.0.0.1");
 		boost::asio::ip::tcp::endpoint endpoint(address, port);
+		VLOG(2) << "port: " << port;
 		socket.async_connect(endpoint,
 				boost::bind(&RpcClientTest::OnAsyncConnect, this,
 						boost::asio::placeholders::error));
@@ -113,18 +116,19 @@ public:
 	{
 		if (err)
 		{
+			LOG(FATAL) << err.message() << err.value();
 			return;
 		}
 
-		RpcMetaPtr sendMeta = boost::make_shared<RpcMeta>();
-		StringPtr sendMessage = boost::make_shared<std::string>(
+		auto sendMeta = boost::make_shared<RpcMeta>();
+		auto sendMessage = boost::make_shared<std::string>(
 				"send test rpc communicator string");
 		sendMeta->size = sendMessage->size();
 		sendMeta->method = 654321;
 		SendMeta(sendMeta, sendMessage);
 
-		RpcMetaPtr meta = boost::make_shared<RpcMeta>();
-		StringPtr message = boost::make_shared<std::string>();
+		auto meta = boost::make_shared<RpcMeta>();
+		auto message = boost::make_shared<std::string>();
 		RecvMeta(meta, message);
 	}
 
@@ -168,7 +172,7 @@ TEST_F(RpcCommunicatorTest, SendAndRecvString)
 	boost::threadpool::fifo_pool threadPool(2);
 	threadPool.schedule(boost::bind(&RpcServerTest::Start, &rpcServer));
 
-	boost::this_thread::sleep(boost::posix_time::milliseconds(100));
+	boost::this_thread::sleep(boost::posix_time::milliseconds(2000));
 	threadPool.schedule(boost::bind(&RpcClientTest::Start, &rpcClient));
 	barrier.Wait();
 	threadPool.wait();
@@ -189,6 +193,10 @@ TEST_F(RpcCommunicatorTest, SendAndRecvString)
 
 int main(int argc, char* argv[])
 {
+	WSADATA wsaData;
+	memset(&wsaData, 0, sizeof(wsaData));
+	int ret = WSAStartup(MAKEWORD(2,0), &wsaData);
+	assert(ret == 0);
 	testing::InitGoogleTest(&argc, argv);
 	google::InitGoogleLogging(argv[0]);
 	google::ParseCommandLineFlags(&argc, &argv, true);

+ 4 - 4
Cpp/Platform/Rpc/RpcServer.cc

@@ -25,7 +25,7 @@ RpcServer::RpcServer(boost::asio::io_service& service, int port):
 	acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
 	acceptor.bind(endpoint);
 	acceptor.listen();
-	RpcSessionPtr new_session = boost::make_shared<RpcSession>(ioService, *this);
+	auto new_session = boost::make_shared<RpcSession>(ioService, *this);
 	acceptor.async_accept(new_session->Socket(),
 			boost::bind(&RpcServer::OnAsyncAccept, this,
 					new_session, boost::asio::placeholders::error));
@@ -43,7 +43,7 @@ void RpcServer::OnAsyncAccept(RpcSessionPtr session, const boost::system::error_
 	}
 	session->Start();
 	sessions.insert(session);
-	RpcSessionPtr newSession = boost::make_shared<RpcSession>(ioService, *this);
+	auto newSession = boost::make_shared<RpcSession>(ioService, *this);
 	acceptor.async_accept(newSession->Socket(),
 			boost::bind(&RpcServer::OnAsyncAccept, this,
 					newSession, boost::asio::placeholders::error));
@@ -68,7 +68,7 @@ void RpcServer::RunService(RpcSessionPtr session, RpcMetaPtr meta,
 {
 	MethodInfoPtr methodInfo = methods[meta->method];
 
-	ResponseHandlerPtr responseHandler =
+	auto responseHandler =
 			boost::make_shared<ResponseHandler>(methodInfo, meta->id, messageHandler);
 	responseHandler->Request()->ParseFromString(*message);
 
@@ -92,7 +92,7 @@ void RpcServer::Register(ProtobufServicePtr service)
 		const google::protobuf::MethodDescriptor* methodDescriptor =
 				serviceDescriptor->method(i);
 		std::size_t methodHash = stringHash(methodDescriptor->full_name());
-		MethodInfoPtr methodInfo = boost::make_shared<MethodInfo>(service, methodDescriptor);
+		auto methodInfo = boost::make_shared<MethodInfo>(service, methodDescriptor);
 		methods[methodHash] = methodInfo;
 	}
 }

+ 3 - 3
Cpp/Platform/Rpc/RpcServerTest.cc

@@ -65,14 +65,14 @@ TEST_F(RpcServerTest, ClientAndServer)
 {
 	boost::threadpool::fifo_pool threadPool(2);
 
-	ProtobufServicePtr echoSevice = boost::make_shared<MyEcho>();
+	auto echoSevice = boost::make_shared<MyEcho>();
 
-	RpcServerPtr server = boost::make_shared<RpcServer>(ioServer, port);
+	auto server = boost::make_shared<RpcServer>(ioServer, port);
 	// 注册service
 	server->Register(echoSevice);
 	ASSERT_EQ(1U, GetMethodMap(server).size());
 
-	RpcClientPtr client = boost::make_shared<RpcClient>(ioClient, "127.0.0.1", port);
+	auto client = boost::make_shared<RpcClient>(ioClient, "127.0.0.1", port);
 	EchoService_Stub service(client.get());
 
 	// 定义消息

+ 2 - 2
Cpp/Platform/Rpc/RpcSession.cc

@@ -30,8 +30,8 @@ void RpcSession::OnSendMessage(RpcMetaPtr meta, StringPtr message)
 
 void RpcSession::Start()
 {
-	RpcMetaPtr meta = boost::make_shared<RpcMeta>();
-	StringPtr message = boost::make_shared<std::string>();
+	auto meta = boost::make_shared<RpcMeta>();
+	auto message = boost::make_shared<std::string>();
 	RecvMeta(meta, message);
 }