/*! \brief 构造函数 JSocketBase对象内部会管理一个QTcpSocket对象。 */ JSocketBase::JSocketBase(QTcpSocket* socket,QObject* parent) :QObject(parent), m_socket(socket) { m_type=-1; m_size=0; m_session = new JSession(this); m_socketStrategy=NULL; connect(m_socket,SIGNAL(readyRead()),SLOT(on_socket_readyRead())); connect(m_socket,SIGNAL(error(QAbstractSocket::SocketError)),SLOT(on_socket_error(QAbstractSocket::SocketError))); connect(m_socket,SIGNAL(disconnected()),SIGNAL(disconnected())); }
void session::do_write(const char* reply, size_t size){ auto self(shared_from_this()); boost::asio::async_write(socket_, boost::asio::buffer(reply, size), [this, self](boost::system::error_code ec, std::size_t /*length*/) { if (ec) { //fail to reply //todo : add log on_socket_error(); } }); }
void session::do_read() { auto self(shared_from_this()); socket_.async_read_some(boost::asio::buffer(buffer_, max_length), [this, self](boost::system::error_code ec, std::size_t bytes_transferred) { if (!ec) { RemoteUserAction::Parse_Result result = user_action_.Parse( buffer_, buffer_ + bytes_transferred); if (result == RemoteUserAction::Parse_Result::indeterminate){ //do nothing } else{ std::string immediatelyReply; if (result == RemoteUserAction::Parse_Result::Good) { immediatelyReply = "Valid Action is received, responsing..."; //send (UserAction Object) to fifo queue for handling... GetFIFOActionQueue().Push_back(user_action_); } else if (result == RemoteUserAction::Parse_Result::Bad){ immediatelyReply = "Invalid Action is received, ignored..."; } do_write(immediatelyReply.c_str(), immediatelyReply.length()); user_action_.Reset();//start to receive new Action Request } do_read(); } else{ assert(false); on_socket_error(); } }); }