|
@@ -1,3 +1,5 @@
|
|
|
|
|
+#include <boost/bind.hpp>
|
|
|
|
|
+#include <boost/asio.hpp>
|
|
|
#include "Rpc/RpcCommunicator.h"
|
|
#include "Rpc/RpcCommunicator.h"
|
|
|
|
|
|
|
|
namespace Egametang {
|
|
namespace Egametang {
|
|
@@ -6,12 +8,16 @@ RPCCommunicator::RPCCommunicator()
|
|
|
{
|
|
{
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void RPCCommunicator::RecvMessegeSize()
|
|
|
|
|
|
|
+boost::asio::ip::tcp::socket& RPCCommunicator::Socket()
|
|
|
|
|
+{
|
|
|
|
|
+ return socket_;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void RPCCommunicator::RecvSize()
|
|
|
{
|
|
{
|
|
|
IntPtr size(new int);
|
|
IntPtr size(new int);
|
|
|
boost::asio::async_read(socket_,
|
|
boost::asio::async_read(socket_,
|
|
|
- boost::asio::buffer(
|
|
|
|
|
- reinterpret_cast<char*>(size.get()), sizeof(int)),
|
|
|
|
|
|
|
+ boost::asio::buffer(reinterpret_cast<char*>(size.get()), sizeof(int)),
|
|
|
boost::bind(&RPCCommunicator::RecvMessage, this, size,
|
|
boost::bind(&RPCCommunicator::RecvMessage, this, size,
|
|
|
boost::asio::placeholders::error));
|
|
boost::asio::placeholders::error));
|
|
|
}
|
|
}
|
|
@@ -20,39 +26,53 @@ void RPCCommunicator::RecvMessage(IntPtr size, const boost::system::error_code&
|
|
|
{
|
|
{
|
|
|
if (err)
|
|
if (err)
|
|
|
{
|
|
{
|
|
|
- LOG(ERROR) << "receive response size failed";
|
|
|
|
|
|
|
+ LOG(ERROR) << "receive message size failed: " << err.message();
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
StringPtr ss;
|
|
StringPtr ss;
|
|
|
boost::asio::async_read(socket_,
|
|
boost::asio::async_read(socket_,
|
|
|
boost::asio::buffer(*ss, *size),
|
|
boost::asio::buffer(*ss, *size),
|
|
|
- boost::bind(&RPCCommunicator::OnRecvMessage, this, ss,
|
|
|
|
|
|
|
+ boost::bind(&RPCCommunicator::RecvDone, this, ss,
|
|
|
boost::asio::placeholders::error));
|
|
boost::asio::placeholders::error));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
-void RPCCommunicator::SendMessage(const RpcRequestPtr request,
|
|
|
|
|
- RpcHandlerPtr handler, const boost::system::error_code& err)
|
|
|
|
|
|
|
+void RPCCommunicator::RecvDone(StringPtr ss, const boost::system::error_code& err)
|
|
|
{
|
|
{
|
|
|
if (err)
|
|
if (err)
|
|
|
{
|
|
{
|
|
|
- LOG(ERROR) << "SendRequestSize error:";
|
|
|
|
|
|
|
+ LOG(ERROR) << "receive message failed: " << err.message();
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
- std::string ss = request->SerializeAsString();
|
|
|
|
|
- boost::asio::async_write(socket_, boost::asio::buffer(ss),
|
|
|
|
|
- boost::bind(&RPCCommunicator::OnSendMessage, this, request->id(),
|
|
|
|
|
- handler, boost::asio::placeholders::error));
|
|
|
|
|
|
|
+ OnRecvMessage(ss);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void RPCCommunicator::SendMessageSize(
|
|
|
|
|
- const RpcRequestPtr request, RpcHandlerPtr handler)
|
|
|
|
|
|
|
+void RPCCommunicator::SendSize(int size, std::string message)
|
|
|
{
|
|
{
|
|
|
- int size = request->ByteSize();
|
|
|
|
|
- std::string ss = boost::lexical_cast(size);
|
|
|
|
|
- boost::asio::async_write(socket_, boost::asio::buffer(ss),
|
|
|
|
|
- boost::bind(&RPCCommunicator::SendMessage, this, request,
|
|
|
|
|
|
|
+ std::string ssize = boost::lexical_cast(size);
|
|
|
|
|
+ boost::asio::async_write(socket_, boost::asio::buffer(ssize),
|
|
|
|
|
+ boost::bind(&RPCCommunicator::SendMessage, this, message,
|
|
|
handler, boost::asio::placeholders::error));
|
|
handler, boost::asio::placeholders::error));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+void RPCCommunicator::SendMessage(std::string message, const boost::system::error_code& err)
|
|
|
|
|
+{
|
|
|
|
|
+ if (err)
|
|
|
|
|
+ {
|
|
|
|
|
+ LOG(ERROR) << "send message size failed: " << err.message();
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ boost::asio::async_write(socket_, boost::asio::buffer(message),
|
|
|
|
|
+ boost::bind(&RPCCommunicator::SendDone, this, boost::asio::placeholders::error));
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void RPCCommunicator::SendDone(const boost::system::error_code& err)
|
|
|
|
|
+{
|
|
|
|
|
+ if (err)
|
|
|
|
|
+ {
|
|
|
|
|
+ LOG(ERROR) << "send message failed: " << err.message();
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ OnSendMessage();
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
} // namespace Egametang
|
|
} // namespace Egametang
|