Erixonich commented on code in PR #7607: URL: https://github.com/apache/ignite-3/pull/7607#discussion_r2882968743
########## modules/platforms/cpp/tests/fake_server/proxy/asio_proxy.h: ########## @@ -0,0 +1,195 @@ +// 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 <iostream> +#include <queue> +#include <tuple> + +#include <asio.hpp> +#include <asio/ts/buffer.hpp> +#include <asio/ts/internet.hpp> + +namespace ignite::proxy { + +using asio::ip::tcp; + +struct message { + char *m_arr; + size_t m_size; + + message(char *arr, size_t size) + : m_arr(nullptr) + , m_size(size) { + m_arr = new char[m_size]; + std::memcpy(m_arr, arr, size); + } + + ~message() { delete[] m_arr; } +}; + +class session : public std::enable_shared_from_this<session> { +public: + session(tcp::socket in_sock, tcp::socket out_sock) + : m_in_sock(std::move(in_sock)) + , m_out_sock(std::move(out_sock)) { } + + void start() { do_serve(); } + + tcp::socket &get_out_sock() { return m_out_sock; } + + void set_writable(bool writable) { + m_in_to_out_writable = writable; + m_out_to_in_writable = writable; + } + + enum direction { forward, reverse }; + +private: + void do_serve() { + do_read(forward); + do_read(reverse); + } + + void do_read(direction direction) { + auto tup = get_sockets_and_queue(direction); + tcp::socket &src = std::get<0>(tup); + std::queue<message> &queue = std::get<2>(tup); + bool &writable = std::get<3>(tup); + + src.async_read_some(asio::buffer(buf, BUFF_SIZE), + [this, &queue, direction, &writable](asio::error_code ec, size_t len) { + if (ec) { + throw std::runtime_error("Error while reading from socket " + ec.message()); + } + + // we have one-threaded executor no synchronization is needed + queue.emplace(buf, len); + + if (writable) { // there are pending write operation on this socket + do_write(direction); + } + + do_read(direction); + }); + } + + void do_write(direction direction) { + auto tup = get_sockets_and_queue(direction); + tcp::socket &dst = std::get<1>(tup); + std::queue<message> &queue = std::get<2>(tup); + bool &writable = std::get<3>(tup); + + writable = false; // protects from writing same buffer twice (from head of queue). + + if (!queue.empty()) { + message &msg = queue.front(); + + asio::async_write( + dst, asio::buffer(msg.m_arr, msg.m_size), [this, &queue, direction, &writable](asio::error_code ec, size_t) { + if (ec) { + throw std::runtime_error("Error while writing to socket " + ec.message()); + } + + queue.pop(); + + if (!queue.empty()) { + // makes writes on the same socket strictly ordered + do_write(direction); + } else { + writable = true; // now read operation can initiate writes + } + }); Review Comment: fixed -- 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]
