void Connection::Start () { auto conn = shared_from_this (); boost::asio::async_read_until (Socket_, Buf_, std::string { "\r\n\r\n" }, Strand_.wrap ([conn] (const boost::system::error_code& ec, ulong transferred) { conn->HandleHeader (ec, transferred); })); }
void SocketHandler::handleNewData() { //qDebug() << "handleNewData()"; QByteArray new_data = socket_.readAll(); buffer_.append(new_data); switch (reading_state_) { case ReadingState::HEADER: HandleHeader(); break; case ReadingState::BODY: HandleBody(); break; } }
void SocketHandler::HandleBody() { if (buffer_.size() < HEADER_SIZE + message_size_) { return; } Message2 new_message; new_message.type = message_type_; QByteArray loc = buffer_.mid(HEADER_SIZE, message_size_); new_message.json = net_codec_->toUnicode(loc); //qDebug() << new_message.json; network_->PushMessage(new_message); if (buffer_.size() == HEADER_SIZE + message_size_) { buffer_.clear(); } else { buffer_ = buffer_.mid(HEADER_SIZE + message_size_); } reading_state_ = ReadingState::HEADER; if (is_first_message_) { qDebug() << "First message"; emit firstMessage(); is_first_message_ = false; } HandleHeader(); }
int SharedPortState::Handle(Stream *s) { HandlerResult result = CONTINUE; while (result == CONTINUE || (!m_non_blocking && (result == WAIT))) { switch (m_state) { case UNBOUND: result = HandleUnbound(s); break; case SEND_HEADER: result = HandleHeader(s); break; case SEND_FD: result = HandleFD(s); break; case RECV_RESP: result = HandleResp(s); break; default: result = FAILED; } } if (result == WAIT && !daemonCore->SocketIsRegistered(s)) { int reg_rc = daemonCore->Register_Socket( s, m_requested_by.c_str(), (SocketHandlercpp)&SharedPortState::Handle, "Shared Port state handler", this, ALLOW); if(reg_rc < 0) { dprintf(D_ALWAYS, "Socket passing to %s failed because " "Register_Socket returned %d.", m_requested_by.c_str(), reg_rc); result = FAILED; } } // Update result statistics if (result == DONE) { SharedPortClient::m_successPassSocketCalls++; } if (result == FAILED) { SharedPortClient::m_failPassSocketCalls++; } // If we are done, clean up and dellocate if (result == DONE || result == FAILED) { if ((s) && (m_state != RECV_RESP || !m_non_blocking || !daemonCore->SocketIsRegistered(s))) { delete s; } delete this; } if (result == WAIT) { // set flag to dealloc m_sock in the destructor... we need to do this because by // returning KEEP_STREAM, the shared_port_server will tell daemonCore to not close // the socket to pass (m_sock), and yet we do not have m_sock registered with daemonCore, // we only have the unix domain socket (s) registered. So after we called back // from daemonCore when we get a response on the domain socket, it will be up to this // class to close the client copy of the m_sock that got passed. m_dealloc_sock = true; return KEEP_STREAM; } else { return result; } }