IL3Reactor
This commit is contained in:
1 parent
95fb13f58b
commit
065fc54f73
4 files changed
+238
-4
No files matched your search
@@ -0,0 +1,142 @@
|
||||
#include "IL3Reactor.h"
|
||||
|
||||
#include "lib/utils/bts_assert.h"
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <fcntl.h>
|
||||
#include <fstream>
|
||||
#include <netinet/tcp.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/epoll.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/uio.h>
|
||||
#include <unistd.h>
|
||||
#include <array>
|
||||
#include <queue>
|
||||
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
namespace ilink3 {
|
||||
|
||||
|
||||
struct Reactor::Connection : public IL3Connection {
|
||||
int epoll_fd;
|
||||
int fd;
|
||||
bool is_connected;
|
||||
IL3ConnectionHandler* handler;
|
||||
std::queue<TCPEndpoint> end_points;
|
||||
|
||||
Connection(int epoll_fd, const std::vector<TCPEndpoint>& 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<std::unique_ptr<Connection>> 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<TCPEndpoint>& end_points) {
|
||||
me->connections.emplace_back(
|
||||
std::make_unique<Connection>(me->epoll_fd, end_points));
|
||||
return *me->connections.back();
|
||||
}
|
||||
|
||||
void Reactor::do_work() {
|
||||
std::array<epoll_event, 100> 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
|
||||
@@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
|
||||
#include "IL3Connection.h"
|
||||
#include "IL3ConnectionHandler.h"
|
||||
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
namespace ilink3 {
|
||||
|
||||
struct TCPEndpoint {
|
||||
std::string host;
|
||||
uint16_t port;
|
||||
};
|
||||
|
||||
class Reactor {
|
||||
struct PImpl;
|
||||
struct Connection;
|
||||
|
||||
std::unique_ptr<PImpl> me;
|
||||
|
||||
public:
|
||||
Reactor();
|
||||
~Reactor();
|
||||
|
||||
IL3Connection& register_connection(const std::vector<TCPEndpoint>&);
|
||||
|
||||
void do_work();
|
||||
};
|
||||
|
||||
}}} //namespace
|
||||
@@ -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
|
||||
@@ -6,6 +6,7 @@
|
||||
#include "../generated/PartyDetailsDefinitionRequest518.h"
|
||||
#include "../generated/Sequence506.h"
|
||||
#include "../ILink3Utils.h"
|
||||
#include "IL3TestExchange.h"
|
||||
|
||||
#include <optional>
|
||||
#include <variant>
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user