Erixonich commented on code in PR #7607: URL: https://github.com/apache/ignite-3/pull/7607#discussion_r2933117593
########## modules/platforms/cpp/tests/fake_server/proxy/asio_proxy.h: ########## @@ -0,0 +1,248 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#pragma once + +#include <atomic> +#include <iostream> +#include <map> +#include <memory> +#include <queue> +#include <thread> +#include <vector> + +#include <asio.hpp> +#include <asio/ts/internet.hpp> + +#include "message_listener.h" + +#include <list> + +namespace ignite::proxy { + +using asio::ip::tcp; + +static constexpr size_t BUFF_SIZE = 4096; + +struct configuration { + asio::ip::port_type m_in_port; + std::string m_out_host_and_port; + std::shared_ptr<message_listener> m_listener; + + configuration( + asio::ip::port_type m_in_port, + const std::string &m_out_host_and_port, + std::shared_ptr<message_listener> m_listener) + : m_in_port(m_in_port) + , m_out_host_and_port(m_out_host_and_port) + , m_listener(std::move(m_listener)) { } +}; + +enum class direction { forward, reverse }; + +template<direction DIRECTION> +class session_part: public std::enable_shared_from_this<session_part<DIRECTION>> { +public: + session_part(std::shared_ptr<tcp::socket> m_src, std::shared_ptr<tcp::socket> m_dst, std::shared_ptr<message_listener> listener) + : m_src(std::move(m_src)) + , m_dst(std::move(m_dst)) + , m_listener(std::move(listener)) {} + + void do_read() { + std::cout << "Reading from socket\n"; + + m_src->async_read_some(asio::buffer(m_buf, BUFF_SIZE), + [self = std::move(this->shared_from_this())](const asio::error_code& ec, size_t len) { + if (ec) { + if (ec == asio::error::eof) { + return; + } + throw std::runtime_error("Error while reading from socket " + ec.message()); + } + + message m{self->m_buf.begin(), self->m_buf.begin() + len}; + + if (self->m_listener) { + if constexpr (DIRECTION == direction::forward) { + self->m_listener->register_out_message(m); + } else { + self->m_listener->register_in_message(m); + } + } + + self->do_write(std::move(m)); + }); + } + + void do_write(message&& msg) { + asio::async_write( + *m_dst, asio::buffer(msg.data(), msg.size()), + [self = std::move(this->shared_from_this())](asio::error_code ec, size_t) { + if (ec) { + if (ec == asio::error::eof) { + return; + } + throw std::runtime_error("Error while writing to socket " + ec.message()); + } + + self->do_read(); + }); + } + +private: + std::shared_ptr<tcp::socket> m_src; + std::shared_ptr<tcp::socket> m_dst; + std::array<char, BUFF_SIZE> m_buf{}; + std::shared_ptr<message_listener> m_listener{nullptr}; + +}; + +class session : public std::enable_shared_from_this<session> { +public: + session( + std::shared_ptr<tcp::socket> in_sock, + std::shared_ptr<tcp::socket> out_sock, + std::shared_ptr<message_listener> listener) + : m_in_sock(std::move(in_sock)) + , m_out_sock(std::move(out_sock)) + { + m_forward_part = std::make_shared<session_part<direction::forward>>(m_in_sock, m_out_sock, listener); + m_reverse_part = std::make_shared<session_part<direction::reverse>>(m_out_sock, m_in_sock, listener); Review Comment: I tried to not to use server/client parts, because proxy can be used for other modes e.g server/server connections. We always have incoming connection (someone connected to proxy) and corresponding related outgoing connection (proxy establish connection according to mapping). Would you like more: dirrection::forward => direction::in_to_out dirrection::reverse => direction::out_to_in -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
