Building Low Latency Applications with C++ by Sourav Ghosh

Building Low Latency Applications with C++ by Sourav Ghosh

Author:Sourav Ghosh
Language: eng
Format: epub
Publisher: Packt Publishing Pvt Ltd
Published: 2023-07-17T00:00:00+00:00


Consuming from and publishing to the order gateway queue

First, we will start with the implementation of processClientRequest() in the MatchingEngine class in the matching_engine.h header file. This implementation simply checks for the type of the MEClientRequest and forwards it to the limit order book for the corresponding instrument. It finds the correct order book instance that this MEClientRequest is meant for by accessing the ticker_order_book_ container, using the ticker_id_ field in MEClientRequest:

auto processClientRequest(const MEClientRequest *client_request) noexcept { auto order_book = ticker_order_book_[client_request ->ticker_id_];

For client requests that try to add a new order (ClientRequestType::NEW), we call the MEOrderBook::add() method and let it service that request:

switch (client_request->type_) { case ClientRequestType::NEW: { order_book->add(client_request->client_id_, client_request->order_id_, client_request->ticker_id_, client_request->side_, client_request->price_, client_request->qty_); } break;

Similarly, client requests that try to cancel an existing order (ClientRequestType::CANCEL) are forwarded to the MEOrderBook::cancel() method:

case ClientRequestType::CANCEL: { order_book->cancel(client_request->client_id_, client_request->order_id_, client_request->ticker_id_); } break; default: { FATAL("Received invalid client-request-type:" + clientRequestTypeToString(client_request->type_)); } break; } }

We will also define a method in the same class that the limit order book will use to publish order responses through MEClientResponse messages. This simply writes the response to the outgoing_ogw_responses_ lock-free queue and advances the writer index. It does that by finding the next valid index to write the MEClientResponse message to by calling the LFQueue::getNextToWriteTo() method, moving the data into that slot, and updating the next write index by calling the LFQueue::updateWriteIndex() method:

auto sendClientResponse(const MEClientResponse *client_response) noexcept { logger_.log("%:% %() % Sending %
", __FILE__, __LINE__, __FUNCTION__, Common::getCurrentTimeStr(&time_str_), client_response->toString()); auto next_write = outgoing_ogw_responses_ ->getNextToWriteTo(); *next_write = std::move(*client_response); outgoing_ogw_responses_->updateWriteIndex(); }

Now, we will look at some code that is similar to what we just saw, except it is used to publish market data updates.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.