From 065fc54f733203ee94d64a68a0cb7bc4b2f99def Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Tue, 7 Apr 2020 14:05:31 -0500 Subject: [PATCH] IL3Reactor --- src/lib/orderman/actors/ilink3/IL3Reactor.cpp | 142 ++++++++++++++++++ src/lib/orderman/actors/ilink3/IL3Reactor.h | 33 ++++ .../actors/ilink3/test/IL3TestExchange.h | 49 ++++++ .../actors/ilink3/test/ILinkGWTests.cpp | 18 ++- 4 files changed, 238 insertions(+), 4 deletions(-) create mode 100644 src/lib/orderman/actors/ilink3/IL3Reactor.cpp create mode 100644 src/lib/orderman/actors/ilink3/IL3Reactor.h create mode 100644 src/lib/orderman/actors/ilink3/test/IL3TestExchange.h diff --git a/src/lib/orderman/actors/ilink3/IL3Reactor.cpp b/src/lib/orderman/actors/ilink3/IL3Reactor.cpp new file mode 100644 index 000000000..0195ca52c --- /dev/null +++ b/src/lib/orderman/actors/ilink3/IL3Reactor.cpp @@ -0,0 +1,142 @@ +#include "IL3Reactor.h" + +#include "lib/utils/bts_assert.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace bts { +namespace orderman { +namespace ilink3 { + + +struct Reactor::Connection : public IL3Connection { + int epoll_fd; + int fd; + bool is_connected; + IL3ConnectionHandler* handler; + std::queue end_points; + + Connection(int epoll_fd, const std::vector& end_points): + epoll_fd(epoll_fd), fd(0), is_connected(false), handler(nullptr) { + end_points(std::deque { end_points.begin(), end_points.end() }); + } + ~Connection() { + if (is_connected) disconnect(); + } + + void connect(IL3ConnectionHandler& h) override { + bts_assert(!is_connected); + handler = &h; + + fd = socket(AF_INET, SOCK_STREAM, 0); + int flag = 1; + auto result = ::setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char*)&flag, sizeof(int)); + bts_assert(result == 0); + bts_assert(fd >= 0); + + struct sockaddr_in serv_addr; + memset(&serv_addr, '0', sizeof(serv_addr)); + + auto end_point = current_end_point(); + auto host = end_point.host; + auto port = end_point.port; + + serv_addr.sin_family = AF_INET; + serv_addr.sin_port = htons(port); + result = inet_pton(AF_INET, host.c_str(), &serv_addr.sin_addr); + bts_assert(result > 0); + + result = ::connect(fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)); + if (result < 0) { + LOG(OrderMan, Error, "Failed to connect to {}:{}", host, port); + disconnect(); + } + else { + int flag = 1; + result = ::setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, + sizeof(int)); + bts_assert(result == 0); + + epoll_event ev; + ev.events = EPOLLIN | EPOLLHUP | EPOLLERR | EPOLLRDHUP; + ev.data.ptr = this; + auto result = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev); + bts_assert_msg(result == 0, "epoll"); + is_connected = true; + handler->on_connect_success(); + } + } + + void disconnect() override { + if (fd) close(fd); + fd = 0; + is_connected = false; + handler->on_disconnect(); + } + + void send(const std::string_view data) override { + if (!is_connected) { + LOG(OrderMan, Error, "Attempted to send on closed connection. {}:{}", current_end_point().host, current_end_point().port); + return; + } + iovec iv; + iv.iov_base = (void)data.data(); + iv.iov_len = data.size(); + auto result = writ + } + + void failover() override { + bts_assert(!is_connected); + } + + TCPEndpoint current_end_point() { + } + +}; + +struct Reactor::PImpl { + int epoll_fd = -1; + std::vector> connections; +}; + +Reactor::Reactor() : me(new PImpl()) { + me->epoll_fd = epoll_create(100); + bts_assert(me->epoll_fd >= 0); +} + +IL3Connection& Reactor::register_connection(const std::vector& end_points) { + me->connections.emplace_back( + std::make_unique(me->epoll_fd, end_points)); + return *me->connections.back(); +} + +void Reactor::do_work() { + std::array ev_list; + auto ready = epoll_wait(me->epoll_fd, ev_list.data(), ev_list.size(), 0); + if (ready == -1) { + if (errno == EINTR) return; + else bts_assert(false); + } + + for (int i = 0; i < ready; ++i) { + auto& ev = ev_list[i]; + auto handler = (Connection*)(ev.data.ptr); + if (ev.events & EPOLLIN) { + char buffer[4096]; + // auto sz = read(handler + } + } +} + +}}} //namespace diff --git a/src/lib/orderman/actors/ilink3/IL3Reactor.h b/src/lib/orderman/actors/ilink3/IL3Reactor.h new file mode 100644 index 000000000..2e94222a7 --- /dev/null +++ b/src/lib/orderman/actors/ilink3/IL3Reactor.h @@ -0,0 +1,33 @@ +#pragma once + +#include "IL3Connection.h" +#include "IL3ConnectionHandler.h" + +#include +#include + +namespace bts { +namespace orderman { +namespace ilink3 { + +struct TCPEndpoint { + std::string host; + uint16_t port; +}; + +class Reactor { + struct PImpl; + struct Connection; + + std::unique_ptr me; + +public: + Reactor(); + ~Reactor(); + + IL3Connection& register_connection(const std::vector&); + + void do_work(); +}; + +}}} //namespace diff --git a/src/lib/orderman/actors/ilink3/test/IL3TestExchange.h b/src/lib/orderman/actors/ilink3/test/IL3TestExchange.h new file mode 100644 index 000000000..1324858a8 --- /dev/null +++ b/src/lib/orderman/actors/ilink3/test/IL3TestExchange.h @@ -0,0 +1,49 @@ +#pragma once + +namespace bts { +namespace orderman { +namespace ilink3 { +namespace test { + +struct IL3TestExchange; +struct TestExchangeConnection : public IL3Connection { + bool connected; + IL3TestExchange& owner; + IL3ConnectionHandler* handler; + TestExchangeConnection(IL3TestExchange& te) : + connected(false), + owner(te), + handler(nullptr) + {} + + void connect(IL3ConnectionHandler& h) override { + REQUIRE(connected == false); + handler = &h; + connected = true; + handler->on_connect_success(); + } + void disconnect() override { + REQUIRE(connected); + connected = false; + if (handler) handler->on_disconnect(); + } + void failover() override {} + void send(const std::string_view data) { + REQUIRE(connected); + } +}; + +struct IL3TestExchange { + // What do you mean the exchange was supposed to handle multiple connections? That wasn't in the spec. + TestExchangeConnection connection; + IL3TestExchange() : connection( *this ) {} + + IL3Connection& connect() { + return connection; + } + +}; + + + +}}}}//namespace \ No newline at end of file diff --git a/src/lib/orderman/actors/ilink3/test/ILinkGWTests.cpp b/src/lib/orderman/actors/ilink3/test/ILinkGWTests.cpp index dc3f4328a..f5d6b3523 100644 --- a/src/lib/orderman/actors/ilink3/test/ILinkGWTests.cpp +++ b/src/lib/orderman/actors/ilink3/test/ILinkGWTests.cpp @@ -6,6 +6,7 @@ #include "../generated/PartyDetailsDefinitionRequest518.h" #include "../generated/Sequence506.h" #include "../ILink3Utils.h" +#include "IL3TestExchange.h" #include #include @@ -52,6 +53,7 @@ struct TestConnection : public IL3Connection { } }; + const iLinkBinary::MessageHeader wrap_msg_header(const char* data, uint16_t message_size) { iLinkBinary::MessageHeader mheader { (char*)data, @@ -131,11 +133,13 @@ TEST_CASE("ilink3 message framer", "[ilink3][execution]") { } +// +// TODO: +// Add certification tests here +// https://www.cmegroup.com/tools-information/webhelp/autocert-ilink/Content/ACPiLinkUserManual.pdf +// + TEST_CASE("ilink3 login", "[ilink3][execution]") { - TestConnection con { [](const std::string_view) { - }}; - Session session { 100, 0, 0 }; - Config config { std::nullopt, "NGG", "004", false }; SECTION("New Session") { // Simplest scenario. Log in with no messages to replay } @@ -153,6 +157,12 @@ TEST_CASE("ilink3 login", "[ilink3][execution]") { TEST_CASE("ilink3 send order", "[ilink3][execution]") { } +TEST_CASE("OutrightVanillaOrder", "[ilink3][execution]") { + IL3TestExchange exchange; + // simulated socket transport + auto connection = exchange.connect(); +} +