bool redis_connection::tcp_client_receive_handler(network::tcp_client&, const std::vector<char>& buffer) { try { m_builder << std::string(buffer.begin(), buffer.end()); } catch (const redis_error& e) { return false; } while (m_builder.reply_available()) { std::lock_guard<std::mutex> lock(m_reply_callback_mutex); auto reply = m_builder.get_front(); m_builder.pop_front(); if (m_reply_callback) m_reply_callback(*this, reply); } return true; }
void redis_connection::tcp_client_receive_handler(const tcp_client_iface::read_result &result) { if (!result.success) { return; } try { __CPP_REDIS_LOG(debug, "cpp_redis::network::redis_connection receives packet, attempts to build reply"); m_builder << std::string(result.buffer.begin(), result.buffer.end()); } catch (const redis_error &) { __CPP_REDIS_LOG(error, "cpp_redis::network::redis_connection could not build reply (invalid format), disconnecting"); call_disconnection_handler(); return; } while (m_builder.reply_available()) { __CPP_REDIS_LOG(debug, "cpp_redis::network::redis_connection reply fully built"); auto reply = m_builder.get_front(); m_builder.pop_front(); if (m_reply_callback) { __CPP_REDIS_LOG(debug, "cpp_redis::network::redis_connection executes reply callback"); m_reply_callback(*this, reply); } } try { tcp_client_iface::read_request request = {__CPP_REDIS_READ_SIZE, std::bind(&redis_connection::tcp_client_receive_handler, this, std::placeholders::_1)}; m_client->async_read(request); } catch (const std::exception &) { /** * Client disconnected in the meantime */ } }