File size: 9,823 Bytes
f86fb3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include "../../include/gateway/tcp_order_gateway.hpp"
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <unistd.h>
#include <cerrno>
#include <cstring>
#include <iostream>

namespace quant::gateway {

static bool make_socket_nonblocking(int fd) {
    int flags = fcntl(fd, F_GETFL, 0);
    if (flags < 0) return false;
    return fcntl(fd, F_SETFL, flags | O_NONBLOCK) == 0;
}

static bool tune_hft_tcp_socket(int fd) {
    int opt = 1;
    setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
#if defined(SO_REUSEPORT)
    setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt));
#endif
    setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));

    int buf_size = 4 * 1024 * 1024; // 4MB socket buffer
    setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &buf_size, sizeof(buf_size));
    setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &buf_size, sizeof(buf_size));

    return make_socket_nonblocking(fd);
}

TCPOrderGateway::TCPOrderGateway(IOReactor& reactor, uint16_t port)
    : reactor_(reactor), port_(port) {}

TCPOrderGateway::~TCPOrderGateway() {
    stop();
}

bool TCPOrderGateway::start() {
    listen_fd_ = socket(AF_INET, SOCK_STREAM, 0);
    if (listen_fd_ < 0) {
        return false;
    }

    if (!tune_hft_tcp_socket(listen_fd_)) {
        close(listen_fd_);
        listen_fd_ = -1;
        return false;
    }

    sockaddr_in addr{};
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = INADDR_ANY;
    addr.sin_port = htons(port_);

    if (bind(listen_fd_, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) < 0) {
        close(listen_fd_);
        listen_fd_ = -1;
        return false;
    }

    if (listen(listen_fd_, 1024) < 0) {
        close(listen_fd_);
        listen_fd_ = -1;
        return false;
    }

    return reactor_.add_socket(
        listen_fd_,
        IOEvent::READ,
        [this](int fd, IOEvent ev) { handle_listen_event(fd, ev); }
    );
}

void TCPOrderGateway::stop() {
    if (listen_fd_ >= 0) {
        reactor_.remove_socket(listen_fd_);
        close(listen_fd_);
        listen_fd_ = -1;
    }

    auto client_fds = std::move(sessions_);
    for (auto& [fd, session] : client_fds) {
        reactor_.remove_socket(fd);
        close(fd);
    }
    sessions_.clear();
    client_to_fd_.clear();
}

void TCPOrderGateway::handle_listen_event(int fd, IOEvent events) {
    if (!(events & IOEvent::READ)) return;

    for (;;) {
        sockaddr_in client_addr{};
        socklen_t addr_len = sizeof(client_addr);
        int client_fd = accept(fd, reinterpret_cast<sockaddr*>(&client_addr), &addr_len);
        if (client_fd < 0) {
            if (errno == EAGAIN || errno == EWOULDBLOCK) {
                break; // Drained all incoming connections
            }
            break;
        }

        tune_hft_tcp_socket(client_fd);

        ClientSession session;
        session.fd = client_fd;
        session.last_heartbeat_ns = core::TimeUtils::now_ns();
        sessions_[client_fd] = std::move(session);

        reactor_.add_socket(
            client_fd,
            IOEvent::READ,
            [this](int cfd, IOEvent cev) { handle_client_event(cfd, cev); }
        );
    }
}

void TCPOrderGateway::handle_client_event(int fd, IOEvent events) {
    auto it = sessions_.find(fd);
    if (it == sessions_.end()) return;

    ClientSession& session = it->second;

    if (events & (IOEvent::HANGUP | IOEvent::ERROR)) {
        close_client(fd);
        return;
    }

    if (events & IOEvent::READ) {
        std::array<uint8_t, 4096> read_chunk;
        for (;;) {
            ssize_t n = recv(fd, read_chunk.data(), read_chunk.size(), 0);
            if (n > 0) {
                session.rx_buffer.insert(session.rx_buffer.end(), read_chunk.begin(), read_chunk.begin() + n);
            } else if (n == 0) {
                close_client(fd);
                return;
            } else {
                if (errno == EAGAIN || errno == EWOULDBLOCK) {
                    break;
                }
                close_client(fd);
                return;
            }
        }
        process_client_rx(session);
    }

    if (events & IOEvent::WRITE) {
        flush_client_tx(session);
    }
}

void TCPOrderGateway::process_client_rx(ClientSession& session) {
    size_t offset = 0;
    while (session.rx_buffer.size() - offset >= sizeof(protocol::FrameHeader)) {
        std::span<const uint8_t> remaining(session.rx_buffer.data() + offset, session.rx_buffer.size() - offset);
        auto hdr_opt = protocol::BinaryProtocol::parse_header(remaining);
        if (!hdr_opt) {
            // Corrupt or desynchronized stream, discard 1 byte and realign
            ++offset;
            continue;
        }

        const auto& hdr = *hdr_opt;
        size_t total_msg_size = sizeof(protocol::FrameHeader) + hdr.body_len;
        if (remaining.size() < total_msg_size) {
            // Need more data from socket
            break;
        }

        session.last_heartbeat_ns = core::TimeUtils::now_ns();
        auto msg_type = static_cast<protocol::MessageType>(hdr.msg_type);

        switch (msg_type) {
            case protocol::MessageType::NEW_ORDER_SINGLE: {
                const auto* payload = protocol::BinaryProtocol::parse_payload<protocol::NewOrderSinglePayload>(remaining);
                if (payload) {
                    session.client_id = payload->client_id;
                    client_to_fd_[payload->client_id] = session.fd;
                    if (on_new_order_) {
                        on_new_order_(*payload, hdr.seq_num, hdr.timestamp_ns);
                    }
                }
                break;
            }
            case protocol::MessageType::ORDER_CANCEL_REQ: {
                const auto* payload = protocol::BinaryProtocol::parse_payload<protocol::OrderCancelReqPayload>(remaining);
                if (payload) {
                    session.client_id = payload->client_id;
                    client_to_fd_[payload->client_id] = session.fd;
                    if (on_cancel_) {
                        on_cancel_(*payload, hdr.seq_num, hdr.timestamp_ns);
                    }
                }
                break;
            }
            case protocol::MessageType::HEARTBEAT: {
                // Pong heartbeat back
                protocol::HeartbeatPayload pong{core::TimeUtils::now_ns()};
                std::array<uint8_t, protocol::MAX_FRAME_SIZE> frame;
                size_t len = protocol::BinaryProtocol::serialize_message(
                    std::span<uint8_t>(frame),
                    protocol::MessageType::HEARTBEAT,
                    hdr.seq_num,
                    core::TimeUtils::now_ns(),
                    pong
                );
                if (len > 0) {
                    send_raw(session, frame.data(), len);
                }
                break;
            }
            default:
                break;
        }

        offset += total_msg_size;
    }

    if (offset > 0) {
        session.rx_buffer.erase(session.rx_buffer.begin(), session.rx_buffer.begin() + offset);
    }
}

bool TCPOrderGateway::send_execution_report(const protocol::ExecutionReportPayload& report, uint64_t seq_num) {
    auto it = client_to_fd_.find(report.client_id);
    if (it == client_to_fd_.end()) return false;

    auto sess_it = sessions_.find(it->second);
    if (sess_it == sessions_.end()) return false;

    std::array<uint8_t, protocol::MAX_FRAME_SIZE> frame;
    size_t len = protocol::BinaryProtocol::serialize_message(
        std::span<uint8_t>(frame),
        protocol::MessageType::EXECUTION_REPORT,
        seq_num,
        core::TimeUtils::now_ns(),
        report
    );
    if (len == 0) return false;

    return send_raw(sess_it->second, frame.data(), len);
}

bool TCPOrderGateway::send_raw(ClientSession& session, const void* data, size_t len) {
    if (session.fd < 0 || len == 0) return false;

    const uint8_t* byte_ptr = static_cast<const uint8_t*>(data);

    // If there's already pending backpressure data, maintain FIFO order in buffer
    if (!session.tx_buffer.empty()) {
        session.tx_buffer.insert(session.tx_buffer.end(), byte_ptr, byte_ptr + len);
        return true;
    }

    ssize_t sent = send(session.fd, data, len, 0);
    if (sent == static_cast<ssize_t>(len)) {
        return true; // Sent completely
    }

    if (sent < 0) {
        if (errno == EAGAIN || errno == EWOULDBLOCK) {
            sent = 0;
        } else {
            close_client(session.fd);
            return false;
        }
    }

    // Queue unsent remainder and arm reactor for WRITE
    session.tx_buffer.insert(session.tx_buffer.end(), byte_ptr + sent, byte_ptr + len);
    reactor_.modify_socket(session.fd, IOEvent::READ | IOEvent::WRITE);
    return true;
}

void TCPOrderGateway::flush_client_tx(ClientSession& session) {
    while (!session.tx_buffer.empty()) {
        ssize_t sent = send(session.fd, session.tx_buffer.data(), session.tx_buffer.size(), 0);
        if (sent > 0) {
            session.tx_buffer.erase(session.tx_buffer.begin(), session.tx_buffer.begin() + sent);
        } else if (sent < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
            break; // Socket still saturated
        } else {
            close_client(session.fd);
            return;
        }
    }

    // When buffer is drained, remove WRITE event to prevent spinloop
    if (session.tx_buffer.empty()) {
        reactor_.modify_socket(session.fd, IOEvent::READ);
    }
}

void TCPOrderGateway::close_client(int fd) {
    reactor_.remove_socket(fd);
    close(fd);

    auto it = sessions_.find(fd);
    if (it != sessions_.end()) {
        client_to_fd_.erase(it->second.client_id);
        sessions_.erase(it);
    }
}

} // namespace quant::gateway