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

修复RpcChannelTest,导致该bug原因是RpcChannel的handle callback需要在
RecvMeta之后调用,否则其它线程可以stop io_service和socket,导致RecvMeta
还未执行完成,socket就断开了

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

+ 9 - 7
Src/Egametang/Rpc/RpcChannel.cc

@@ -37,17 +37,21 @@ void RpcChannel::OnAsyncConnect(const boost::system::error_code& err)
 
 void RpcChannel::OnRecvMessage(RpcMetaPtr meta, StringPtr message)
 {
-	VLOG(2) << "RpcChannel::OnRecvMessage";
 	RpcHandlerPtr handler = handlers_[meta->id];
 	handler->GetResponse()->ParseFromString(*message);
-	if (handler->GetDone() != NULL)
-	{
-		handler->GetDone()->Run();
-	}
+
 	handlers_.erase(meta->id);
 
 	// read size
 	RecvMeta();
+
+	// 回调放在函数最后.如果RecvMeta()放在回调之后,
+	// 另外线程可能让io_service stop,导致RecvMeta还未跑完
+	// 网络就终止了
+	if (handler->GetDone() != NULL)
+	{
+		handler->GetDone()->Run();
+	}
 }
 
 void RpcChannel::OnSendMessage()
@@ -77,8 +81,6 @@ void RpcChannel::CallMethod(
 	VLOG(3) << "send size: " << meta.size;
 	meta.id = id_;
 	meta.method = string_hash(method->full_name());
-	VLOG(3) << "send meta1: " << meta.size << " "
-					<< meta.id << " " << meta.method;
 	SendMeta(meta, message);
 }
 

+ 1 - 1
Src/Egametang/Rpc/RpcChannel.h

@@ -19,7 +19,7 @@ class RpcChannel:
 private:
 	typedef boost::unordered_map<std::size_t, RpcHandlerPtr> RpcCallbackMap;
 
-	int32 id_;
+	std::size_t id_;
 	RpcCallbackMap handlers_;
 
 	void OnAsyncConnect(const boost::system::error_code& err);

+ 1 - 4
Src/Egametang/Rpc/RpcChannelTest.cc

@@ -58,7 +58,6 @@ public:
 		request.ParseFromString(*message);
 
 		num_ = request.num();
-		VLOG(2) << "num: " << num_;
 
 		// 回一个消息
 		EchoResponse response;
@@ -68,13 +67,11 @@ public:
 		RpcMeta response_meta;
 		response_meta.id = meta->id;
 		response_meta.size = send_string.size();
-		VLOG(3) << "send meta: " << response_meta.size << " "
-				<< response_meta.id << " " << response_meta.method;
 		SendMeta(response_meta, send_string);
-		barrier_.Signal();
 	}
 	virtual void OnSendMessage()
 	{
+		barrier_.Signal();
 	}
 };
 

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

@@ -4,6 +4,7 @@
 #include <google/protobuf/service.h>
 #include <boost/unordered_map.hpp>
 #include <boost/asio.hpp>
+#include <boost/format.hpp>
 #include "Base/Marcos.h"
 #include "Base/Typedef.h"
 #include "Rpc/RpcTypedef.h"
@@ -27,7 +28,8 @@ struct RpcMeta
 
 	std::string ToString()
 	{
-		return "";
+		boost::format format("size: %1%, id: %2%, method: %3%\n");
+		return boost::str(format % size % id % method);
 	}
 };