diff --git a/linux/include/ccapi_cpp/ccapi_decimal.h b/linux/include/ccapi_cpp/ccapi_decimal.h index 3113b5f45..4bd5e733a 100644 --- a/linux/include/ccapi_cpp/ccapi_decimal.h +++ b/linux/include/ccapi_cpp/ccapi_decimal.h @@ -4,7 +4,10 @@ #include "ccapi_cpp/ccapi_util_private.h" namespace ccapi { -// minimalistic just for the purpose of being used as the key of a map +/** + * This class provides a numeric type for representing an arbitrary precision decimal number. It is minimalistic for the purpose of high performance. + * Furthermore, unlike double, it is suitable for being used as the key of a map. + */ class Decimal CCAPI_FINAL { public: Decimal() {} diff --git a/linux/include/ccapi_cpp/ccapi_element.h b/linux/include/ccapi_cpp/ccapi_element.h index 00480ea5a..8286d575f 100644 --- a/linux/include/ccapi_cpp/ccapi_element.h +++ b/linux/include/ccapi_cpp/ccapi_element.h @@ -5,6 +5,10 @@ #include "ccapi_cpp/ccapi_util_private.h" namespace ccapi { +/** + * Element represents an item in a message. The value(s) in an Element can be queried in a number of ways. Use the getValue() functions to retrieve a single + * value. Use the getNameValueMap() function (or getTagValueMap() function for FIX API) to retrieve all the values. + */ class Element CCAPI_FINAL { public: explicit Element(bool isFix = false) : isFix(isFix) {} diff --git a/linux/include/ccapi_cpp/ccapi_event.h b/linux/include/ccapi_cpp/ccapi_event.h index 7314e1f21..c8dd33c01 100644 --- a/linux/include/ccapi_cpp/ccapi_event.h +++ b/linux/include/ccapi_cpp/ccapi_event.h @@ -5,6 +5,12 @@ #include "ccapi_cpp/ccapi_logger.h" #include "ccapi_cpp/ccapi_message.h" namespace ccapi { +/** +** A single event resulting from a subscription or a request. Event objects are created by the API and passed to the application either through a registered +*EventHandler or EventQueue. Event objects contain Message objects which can be accessed using the getMessageList() function. The Event object is a handle to an +*event. The event is the basic unit of work provided to applications. Each Event object consists of an Type attribute and zero or more Message objects. +*/ + class Event CCAPI_FINAL { public: enum class Type { diff --git a/linux/include/ccapi_cpp/ccapi_event_dispatcher.h b/linux/include/ccapi_cpp/ccapi_event_dispatcher.h index f5001670c..d4e63cafe 100644 --- a/linux/include/ccapi_cpp/ccapi_event_dispatcher.h +++ b/linux/include/ccapi_cpp/ccapi_event_dispatcher.h @@ -13,6 +13,12 @@ #include "ccapi_cpp/ccapi_logger.h" #include "ccapi_cpp/ccapi_util_private.h" namespace ccapi { +/** + * Dispatches events from one or more Sessions through callbacks. EventDispatcher objects are optionally specified when Session objects are constructed. A + * single EventDispatcher can be shared by multiple Session objects. The EventDispatcher provides an event-driven interface, generating callbacks from one or + * more internal threads for one or more sessions. + */ + class EventDispatcher CCAPI_FINAL { public: explicit EventDispatcher(const int numDispatcherThreads = 1) : numDispatcherThreads(numDispatcherThreads) { diff --git a/linux/include/ccapi_cpp/ccapi_event_handler.h b/linux/include/ccapi_cpp/ccapi_event_handler.h index c3824fae3..5b81f9140 100644 --- a/linux/include/ccapi_cpp/ccapi_event_handler.h +++ b/linux/include/ccapi_cpp/ccapi_event_handler.h @@ -3,6 +3,10 @@ #include "ccapi_cpp/ccapi_event.h" namespace ccapi { class Session; +/** + * Defines the callback supplied by the application to process Events. Applications which use event driven programming supply instances of their own subclass of + * this class to the Session to receive callbacks when an Event is available. + */ class EventHandler { public: virtual ~EventHandler() {} diff --git a/linux/include/ccapi_cpp/ccapi_fix_connection.h b/linux/include/ccapi_cpp/ccapi_fix_connection.h index ed63778b4..32d10ae27 100644 --- a/linux/include/ccapi_cpp/ccapi_fix_connection.h +++ b/linux/include/ccapi_cpp/ccapi_fix_connection.h @@ -8,6 +8,9 @@ namespace beast = boost::beast; namespace ccapi { template class FixConnection CCAPI_FINAL { + /** + * This class represents a TCP socket connection for the FIX API. + */ public: FixConnection(std::string host, std::string port, Subscription subscription, std::shared_ptr streamPtr) : host(host), port(port), subscription(subscription), streamPtr(streamPtr) { diff --git a/linux/include/ccapi_cpp/ccapi_http_connection.h b/linux/include/ccapi_cpp/ccapi_http_connection.h index 536ae8c03..c8dfde833 100644 --- a/linux/include/ccapi_cpp/ccapi_http_connection.h +++ b/linux/include/ccapi_cpp/ccapi_http_connection.h @@ -6,6 +6,9 @@ namespace beast = boost::beast; namespace ccapi { class HttpConnection CCAPI_FINAL { + /** + * This class represents a TCP socket connection for the REST API. + */ public: HttpConnection(std::string host, std::string port, std::shared_ptr > streamPtr) : host(host), port(port), streamPtr(streamPtr) {} diff --git a/linux/include/ccapi_cpp/ccapi_http_retry.h b/linux/include/ccapi_cpp/ccapi_http_retry.h index a14bad7a6..bc8eb559c 100644 --- a/linux/include/ccapi_cpp/ccapi_http_retry.h +++ b/linux/include/ccapi_cpp/ccapi_http_retry.h @@ -2,6 +2,9 @@ #define INCLUDE_CCAPI_CPP_CCAPI_HTTP_RETRY_H_ #include namespace ccapi { +/** + * This class is used for retrying http requests for the REST API. + */ class HttpRetry CCAPI_FINAL { public: explicit HttpRetry(int numRetry = 0, int numRedirect = 0, std::string redirectUrlStr = "", diff --git a/linux/include/ccapi_cpp/ccapi_jwt.h b/linux/include/ccapi_cpp/ccapi_jwt.h index 566371f29..0df2a7419 100644 --- a/linux/include/ccapi_cpp/ccapi_jwt.h +++ b/linux/include/ccapi_cpp/ccapi_jwt.h @@ -4,6 +4,9 @@ #include "ccapi_cpp/ccapi_macro.h" #include "ccapi_cpp/ccapi_util_private.h" namespace ccapi { +/** + * This class is used for handling jwt tokens. + */ class Jwt CCAPI_FINAL { public: static std::string generate(const Hmac::ShaVersion shaVersion, const std::string& secret, const std::string& payload) { diff --git a/linux/include/ccapi_cpp/ccapi_logger.h b/linux/include/ccapi_cpp/ccapi_logger.h index c01dbaaf4..676ddde25 100644 --- a/linux/include/ccapi_cpp/ccapi_logger.h +++ b/linux/include/ccapi_cpp/ccapi_logger.h @@ -75,6 +75,9 @@ #include "ccapi_cpp/ccapi_macro.h" #include "ccapi_cpp/ccapi_util.h" namespace ccapi { +/** + * This class is used for library logging. + */ class Logger { public: std::string LOG_SEVERITY_FATAL = "FATAL"; diff --git a/linux/include/ccapi_cpp/ccapi_macro.h b/linux/include/ccapi_cpp/ccapi_macro.h index 11e7dbb74..76a5f850b 100644 --- a/linux/include/ccapi_cpp/ccapi_macro.h +++ b/linux/include/ccapi_cpp/ccapi_macro.h @@ -261,6 +261,7 @@ #define CCAPI_WEBSOCKET_KUCOIN_FUTURES_CHANNEL_MARKET_LEVEL2DEPTH50 "/contractMarket/level2Depth50" #define CCAPI_WEBSOCKET_FTX_BASE_CHANNEL_TRADES "trades" #define CCAPI_WEBSOCKET_FTX_BASE_CHANNEL_ORDERBOOKS "orderbook" +#define CCAPI_WEBSOCKET_FTX_BASE_CHANNEL_TICKER "ticker" #define CCAPI_WEBSOCKET_DERIBIT_CHANNEL_TRADES "trades.{instrument_name}.{interval}" #define CCAPI_WEBSOCKET_DERIBIT_CHANNEL_BOOK "book.{instrument_name}.{group}.{depth}.{interval}" #define CCAPI_WEBSOCKET_DERIBIT_CHANNEL_BOOK_TBT "book.{instrument_name}.{interval}" @@ -474,7 +475,7 @@ // start: exchange REST urls #ifndef CCAPI_COINBASE_URL_REST_BASE -#define CCAPI_COINBASE_URL_REST_BASE "https://api.pro.coinbase.com" +#define CCAPI_COINBASE_URL_REST_BASE "https://api.exchange.coinbase.com" #endif #ifndef CCAPI_GEMINI_URL_REST_BASE #define CCAPI_GEMINI_URL_REST_BASE "https://api.gemini.com" @@ -639,7 +640,7 @@ // start: exchange WS urls #ifndef CCAPI_COINBASE_URL_WS_BASE -#define CCAPI_COINBASE_URL_WS_BASE "wss://ws-feed.pro.coinbase.com" +#define CCAPI_COINBASE_URL_WS_BASE "wss://ws-feed.exchange.coinbase.com" #endif #ifndef CCAPI_GEMINI_URL_WS_BASE #define CCAPI_GEMINI_URL_WS_BASE "wss://api.gemini.com" @@ -744,7 +745,7 @@ // start: exchange FIX urls #ifndef CCAPI_COINBASE_URL_FIX_BASE -#define CCAPI_COINBASE_URL_FIX_BASE "tcp+ssl://fix.pro.coinbase.com:4198" +#define CCAPI_COINBASE_URL_FIX_BASE "tcp+ssl://fix.exchange.coinbase.com:4198" #endif #ifndef CCAPI_GEMINI_URL_FIX_HOST #define CCAPI_GEMINI_URL_FIX_HOST "" diff --git a/linux/include/ccapi_cpp/ccapi_market_data_message.h b/linux/include/ccapi_cpp/ccapi_market_data_message.h index f08572ba0..f9a8a12dd 100644 --- a/linux/include/ccapi_cpp/ccapi_market_data_message.h +++ b/linux/include/ccapi_cpp/ccapi_market_data_message.h @@ -5,6 +5,11 @@ // #include "ccapi_message.h" namespace ccapi { class MarketDataMessage CCAPI_FINAL { + /** + * A handle to a single market data message. Each MarketDataMessage is associated with one 'exchangeSubscriptionId' value. The MarketDataMessage contents are + * represented in its 'data' attribute. Each MarketDataMessage object consists of an Type attribute and a RecapType attribute. The exchange timestamp (if any) + * associated with the MarketDataMessage object is represented in its 'tp' attribute. + */ public: enum class Type { UNKNOWN, diff --git a/linux/include/ccapi_cpp/ccapi_message.h b/linux/include/ccapi_cpp/ccapi_message.h index 5eedf2c67..cd2f21bfa 100644 --- a/linux/include/ccapi_cpp/ccapi_message.h +++ b/linux/include/ccapi_cpp/ccapi_message.h @@ -6,6 +6,12 @@ #include "ccapi_cpp/ccapi_element.h" #include "ccapi_cpp/ccapi_logger.h" namespace ccapi { +/** + * A handle to a single message. Message objects are obtained from the getMessageList() function of the Event object. Each Message is associated with one or + * more correlation id values. The Message contents are represented as Elements and can be accessed via the getElementList() function. Each Message object + * consists of an Type attribute and a RecapType attribute. The exchange timestamp (if any) associated with the Messsage object can be retrieved via the + * getTime() function. The library timestamp can be retrieved via the getTimeReceived() function. + */ class Message CCAPI_FINAL { public: enum class RecapType { diff --git a/linux/include/ccapi_cpp/ccapi_queue.h b/linux/include/ccapi_cpp/ccapi_queue.h index 1b7702304..bbcf5810e 100644 --- a/linux/include/ccapi_cpp/ccapi_queue.h +++ b/linux/include/ccapi_cpp/ccapi_queue.h @@ -7,13 +7,18 @@ #include "ccapi_cpp/ccapi_logger.h" namespace ccapi { template +/** + * This class represents a generic FIFO queue. + */ class Queue { public: std::string EXCEPTION_QUEUE_FULL = "queue is full"; std::string EXCEPTION_QUEUE_EMPTY = "queue is empty"; explicit Queue(const size_t maxSize = 0) : maxSize(maxSize) {} void pushBack(T&& t) { +#ifndef CCAPI_USE_SINGLE_THREAD std::lock_guard lock(this->m); +#endif if (this->maxSize <= 0 || this->queue.size() < this->maxSize) { CCAPI_LOGGER_TRACE("this->queue.size() = " + size_tToString(this->queue.size())); this->queue.push_back(t); @@ -22,7 +27,9 @@ class Queue { } } T popBack() { +#ifndef CCAPI_USE_SINGLE_THREAD std::lock_guard lock(this->m); +#endif if (this->queue.empty()) { throw std::runtime_error(EXCEPTION_QUEUE_EMPTY); } else { @@ -32,13 +39,17 @@ class Queue { } } std::vector purge() { +#ifndef CCAPI_USE_SINGLE_THREAD std::lock_guard lock(this->m); +#endif std::vector p; std::swap(p, this->queue); return p; } void removeAll(std::vector& c) { +#ifndef CCAPI_USE_SINGLE_THREAD std::lock_guard lock(this->m); +#endif if (c.empty()) { c = std::move(this->queue); } else { @@ -48,11 +59,15 @@ class Queue { this->queue.clear(); } size_t size() const { +#ifndef CCAPI_USE_SINGLE_THREAD std::lock_guard lock(this->m); +#endif return this->queue.size(); } bool empty() const { +#ifndef CCAPI_USE_SINGLE_THREAD std::lock_guard lock(this->m); +#endif return this->queue.empty(); } #ifndef CCAPI_EXPOSE_INTERNAL @@ -60,7 +75,9 @@ class Queue { private: #endif std::vector queue; +#ifndef CCAPI_USE_SINGLE_THREAD mutable std::mutex m; +#endif size_t maxSize{}; }; } /* namespace ccapi */ diff --git a/linux/include/ccapi_cpp/ccapi_request.h b/linux/include/ccapi_cpp/ccapi_request.h index 60ca6f93c..9f7118d5f 100644 --- a/linux/include/ccapi_cpp/ccapi_request.h +++ b/linux/include/ccapi_cpp/ccapi_request.h @@ -8,6 +8,12 @@ #include "ccapi_cpp/ccapi_macro.h" #include "ccapi_cpp/ccapi_util_private.h" namespace ccapi { +/** + * A single request. Request objects are created using Request constructors. They are used with Session::sendRequest() or Session::sendRequestByWebsocket() or + * Session::sendRequestByFix(). The Request object contains the parameters for a single request. Once a Request has been created its fields can be further + * modified using the convenience functions appendParam() or appendParamFix() or setParamList() or setParamListFix(). A correlation id can be used as the unique + * identifier to tag all data associated with this request. + */ class Request CCAPI_FINAL { public: static constexpr int operationTypeCustom = 0x100; @@ -130,6 +136,9 @@ class Request CCAPI_FINAL { const std::string& getServiceName() const { return serviceName; } void appendParam(const std::map& param) { this->paramList.push_back(param); } void appendParamFix(const std::vector >& param) { this->paramListFix.push_back(param); } + void appendParamListFix(const std::vector > >& paramList) { + this->paramListFix.insert(std::end(this->paramListFix), std::begin(paramList), std::end(paramList)); + } void setParamListFix(const std::vector > >& paramListFix) { this->paramListFix = paramListFix; } Operation getOperation() const { return operation; } const std::vector >& getParamList() const { return paramList; } diff --git a/linux/include/ccapi_cpp/ccapi_session.h b/linux/include/ccapi_cpp/ccapi_session.h index 451fb579f..43e31989e 100644 --- a/linux/include/ccapi_cpp/ccapi_session.h +++ b/linux/include/ccapi_cpp/ccapi_session.h @@ -203,6 +203,12 @@ #include "ccapi_cpp/service/ccapi_service_context.h" using steady_timer = boost::asio::steady_timer; namespace ccapi { +/** + * This class provides a consumer session for making requests and subscriptions for services. Sessions manage access to services either by requests and + * responses or subscriptions. A Session can dispatch events and replies in either an immediate or batching mode. The mode of a Session is determined when it is + * constructed and cannot be changed subsequently. A Session is immediate if an EventHandler object is supplied when it is constructed. All incoming events are + * delivered to the EventHandler supplied on construction. A Session is batching if an EventHandler object is not supplied when it is constructed. + */ class Session { public: Session(const Session&) = delete; @@ -614,6 +620,18 @@ class Session { } else { if (this->eventHandler) { CCAPI_LOGGER_TRACE("handle event in immediate mode"); +#ifdef CCAPI_USE_SINGLE_THREAD + bool shouldContinue = true; + try { + shouldContinue = this->eventHandler->processEvent(event, this); + } catch (const std::runtime_error& e) { + CCAPI_LOGGER_ERROR(e.what()); + } + if (!shouldContinue) { + CCAPI_LOGGER_DEBUG("about to pause the event dispatcher"); + this->eventDispatcher->pause(); + } +#else this->eventDispatcher->dispatch([that = this, event = std::move(event)] { bool shouldContinue = true; try { @@ -626,6 +644,7 @@ class Session { that->eventDispatcher->pause(); } }); +#endif } else { CCAPI_LOGGER_TRACE("handle event in batching mode"); this->eventQueue.pushBack(std::move(event)); diff --git a/linux/include/ccapi_cpp/ccapi_session_configs.h b/linux/include/ccapi_cpp/ccapi_session_configs.h index dc6a74f54..97f116e88 100644 --- a/linux/include/ccapi_cpp/ccapi_session_configs.h +++ b/linux/include/ccapi_cpp/ccapi_session_configs.h @@ -9,6 +9,10 @@ #include "ccapi_cpp/ccapi_macro.h" #include "ccapi_cpp/ccapi_util_private.h" namespace ccapi { +/** + * This class contains the configs when creating a session. It loads and maintains exchange information. Most of the values shouldn't be changed. To use + * non-default configs on a Session, create a SessionConfigs instance and set the required configs and then supply it when creating a Session. + */ class SessionConfigs CCAPI_FINAL { public: explicit SessionConfigs(std::map credential = {}) : credential(credential) { diff --git a/linux/include/ccapi_cpp/ccapi_session_options.h b/linux/include/ccapi_cpp/ccapi_session_options.h index 112f2849b..bb27345ec 100644 --- a/linux/include/ccapi_cpp/ccapi_session_options.h +++ b/linux/include/ccapi_cpp/ccapi_session_options.h @@ -5,6 +5,10 @@ #include "ccapi_cpp/ccapi_macro.h" #include "ccapi_cpp/ccapi_util_private.h" namespace ccapi { +/** + * This class contains the options which the user can specify when creating a session. To use non-default options on a Session, create a SessionOptions instance + * and set the required options and then supply it when creating a Session. + */ class SessionOptions CCAPI_FINAL { public: std::string toString() const { diff --git a/linux/include/ccapi_cpp/ccapi_subscription.h b/linux/include/ccapi_cpp/ccapi_subscription.h index ee14e0447..269ddbb47 100644 --- a/linux/include/ccapi_cpp/ccapi_subscription.h +++ b/linux/include/ccapi_cpp/ccapi_subscription.h @@ -6,6 +6,10 @@ #include "ccapi_cpp/ccapi_macro.h" #include "ccapi_cpp/ccapi_util_private.h" namespace ccapi { +/** + * A single subscription. A 'Subscription' is used when calling 'Session::subscribe()' or 'Session::subscribeByFix'. Subscription objects are created using + * Subscription constructors. A correlation id can be used as the unique identifier to tag all data associated with this subscription. + */ class Subscription CCAPI_FINAL { public: Subscription() {} diff --git a/linux/include/ccapi_cpp/ccapi_url.h b/linux/include/ccapi_cpp/ccapi_url.h index 5c7108000..12f6e588a 100644 --- a/linux/include/ccapi_cpp/ccapi_url.h +++ b/linux/include/ccapi_cpp/ccapi_url.h @@ -6,6 +6,9 @@ #include "ccapi_cpp/ccapi_macro.h" #include "ccapi_cpp/ccapi_util_private.h" namespace ccapi { +/** + * This class represents a url. + */ class Url CCAPI_FINAL { public: explicit Url(std::string urlStr) { diff --git a/linux/include/ccapi_cpp/ccapi_util_private.h b/linux/include/ccapi_cpp/ccapi_util_private.h index 4e7ab5279..85491b4e7 100644 --- a/linux/include/ccapi_cpp/ccapi_util_private.h +++ b/linux/include/ccapi_cpp/ccapi_util_private.h @@ -26,6 +26,9 @@ #include "ccapi_cpp/ccapi_util.h" #include "openssl/evp.h" namespace ccapi { +/** + * Utilities. + */ class UtilString CCAPI_FINAL { public: static std::string roundInputBySignificantFigure(double input, int numSignificantFigure, int roundDirection) { diff --git a/linux/include/ccapi_cpp/ccapi_ws_connection.h b/linux/include/ccapi_cpp/ccapi_ws_connection.h index 65e932ea8..f21071bf1 100644 --- a/linux/include/ccapi_cpp/ccapi_ws_connection.h +++ b/linux/include/ccapi_cpp/ccapi_ws_connection.h @@ -6,6 +6,9 @@ #include "ccapi_cpp/ccapi_subscription.h" namespace wspp = websocketpp; namespace ccapi { +/** + * This class represents a TCP socket connection for the websocket API. + */ class WsConnection CCAPI_FINAL { public: WsConnection(std::string url, std::string group, std::vector subscriptionList) : url(url), group(group), subscriptionList(subscriptionList) { diff --git a/linux/include/ccapi_cpp/service/ccapi_execution_management_service.h b/linux/include/ccapi_cpp/service/ccapi_execution_management_service.h index 94d7734bd..c07c02213 100644 --- a/linux/include/ccapi_cpp/service/ccapi_execution_management_service.h +++ b/linux/include/ccapi_cpp/service/ccapi_execution_management_service.h @@ -15,6 +15,10 @@ #include "ccapi_cpp/ccapi_macro.h" #include "ccapi_cpp/service/ccapi_service.h" namespace ccapi { +/** + * The ExecutionManagementService class inherits from the Service class and provides implemenations more specific to execution management such as order + * submission, order cancellation, etc.. + */ class ExecutionManagementService : public Service { public: enum class JsonDataType { diff --git a/linux/include/ccapi_cpp/service/ccapi_execution_management_service_ftx_base.h b/linux/include/ccapi_cpp/service/ccapi_execution_management_service_ftx_base.h index e4cee9a86..e755567e8 100644 --- a/linux/include/ccapi_cpp/service/ccapi_execution_management_service_ftx_base.h +++ b/linux/include/ccapi_cpp/service/ccapi_execution_management_service_ftx_base.h @@ -357,6 +357,7 @@ class ExecutionManagementServiceFtxBase : public ExecutionManagementService { message.setElementList(elementList); messageList.emplace_back(std::move(message)); } else if (channel == "orders" && fieldSet.find(CCAPI_EM_ORDER_UPDATE) != fieldSet.end()) { + message.setTime(timeReceived); message.setType(Message::Type::EXECUTION_MANAGEMENT_EVENTS_ORDER_UPDATE); const std::map >& extractionFieldNameMap = { {CCAPI_EM_ORDER_ID, std::make_pair("id", JsonDataType::STRING)}, diff --git a/linux/include/ccapi_cpp/service/ccapi_execution_management_service_huobi_base.h b/linux/include/ccapi_cpp/service/ccapi_execution_management_service_huobi_base.h index 6a52f9b10..2ee2764c0 100644 --- a/linux/include/ccapi_cpp/service/ccapi_execution_management_service_huobi_base.h +++ b/linux/include/ccapi_cpp/service/ccapi_execution_management_service_huobi_base.h @@ -79,7 +79,10 @@ class ExecutionManagementServiceHuobiBase : public ExecutionManagementService { } } else { if (key == "type") { - value = (value == CCAPI_EM_ORDER_SIDE_BUY || value == "buy-limit") ? "buy-limit" : "sell-limit"; + if (value == CCAPI_EM_ORDER_SIDE_BUY) + value = "buy-limit"; + else if (value == CCAPI_EM_ORDER_SIDE_SELL) + value = "sell-limit"; } } document.AddMember(rj::Value(key.c_str(), allocator).Move(), rj::Value(value.c_str(), allocator).Move(), allocator); diff --git a/linux/include/ccapi_cpp/service/ccapi_fix_service.h b/linux/include/ccapi_cpp/service/ccapi_fix_service.h index a4bec3bcb..9eaaa5f76 100644 --- a/linux/include/ccapi_cpp/service/ccapi_fix_service.h +++ b/linux/include/ccapi_cpp/service/ccapi_fix_service.h @@ -1,11 +1,21 @@ #ifndef INCLUDE_CCAPI_CPP_SERVICE_CCAPI_FIX_SERVICE_H_ #define INCLUDE_CCAPI_CPP_SERVICE_CCAPI_FIX_SERVICE_H_ #ifdef CCAPI_ENABLE_SERVICE_FIX +#ifndef CCAPI_FIX_READ_BUFFER_SIZE +#define CCAPI_FIX_READ_BUFFER_SIZE 1 << 20 +#endif +#ifndef CCAPI_FIX_WRITE_BUFFER_SIZE +#define CCAPI_FIX_WRITE_BUFFER_SIZE 1 << 20 +#endif #include "ccapi_cpp/service/ccapi_service.h" #include "hffix.hpp" namespace hff = hffix; namespace ccapi { template +/** + * Defines a service which provides access to exchange API and normalizes them. This is a base class that implements generic functionalities for dealing with + * exchange FIX APIs. The Session object is responsible for routing requests and subscriptions to the desired concrete service. + */ class FixService : public Service { public: FixService(std::function*)> eventHandler, SessionOptions sessionOptions, SessionConfigs sessionConfigs, @@ -374,7 +384,7 @@ class FixService : public Service { auto& connectionId = fixConnectionPtr->id; auto& writeMessageBuffer = this->writeMessageBufferByConnectionIdMap[connectionId]; auto& writeMessageBufferWrittenLength = this->writeMessageBufferWrittenLengthByConnectionIdMap[connectionId]; - size_t n = 0; + size_t n = writeMessageBufferWrittenLength; for (const auto& param : paramList) { auto commonParam = this->createCommonParam(connectionId, nowFixTimeStr); hff::message_writer messageWriter(writeMessageBuffer.data() + n, writeMessageBuffer.data() + writeMessageBuffer.size()); @@ -395,11 +405,13 @@ class FixService : public Service { n += messageWriter.message_end() - messageWriter.message_begin(); } CCAPI_LOGGER_DEBUG("about to send " + printableString(writeMessageBuffer.data(), n)); + CCAPI_LOGGER_TRACE("writeMessageBufferWrittenLength = " + toString(writeMessageBufferWrittenLength)); if (writeMessageBufferWrittenLength == 0) { CCAPI_LOGGER_TRACE("about to start write"); this->startWrite_3(fixConnectionPtr, writeMessageBuffer.data(), n); } - writeMessageBufferWrittenLength += n; + writeMessageBufferWrittenLength = n; + CCAPI_LOGGER_TRACE("writeMessageBufferWrittenLength = " + toString(writeMessageBufferWrittenLength)); } void onPongByMethod(PingPongMethod method, std::shared_ptr> fixConnectionPtr, const TimePoint& timeReceived) { CCAPI_LOGGER_FUNCTION_ENTER; @@ -517,9 +529,9 @@ class FixService : public Service { return {}; } const size_t readMessageChunkSize = CCAPI_HFFIX_READ_MESSAGE_CHUNK_SIZE; - std::map> readMessageBufferByConnectionIdMap; + std::map> readMessageBufferByConnectionIdMap; std::map readMessageBufferReadLengthByConnectionIdMap; - std::map> writeMessageBufferByConnectionIdMap; + std::map> writeMessageBufferByConnectionIdMap; std::map writeMessageBufferWrittenLengthByConnectionIdMap; std::map>> fixConnectionPtrByIdMap; std::map sequenceSentByConnectionIdMap; diff --git a/linux/include/ccapi_cpp/service/ccapi_market_data_service.h b/linux/include/ccapi_cpp/service/ccapi_market_data_service.h index 0a059a4d9..fcd5f15cd 100644 --- a/linux/include/ccapi_cpp/service/ccapi_market_data_service.h +++ b/linux/include/ccapi_cpp/service/ccapi_market_data_service.h @@ -10,6 +10,9 @@ #include "ccapi_cpp/ccapi_util_private.h" #include "ccapi_cpp/service/ccapi_service.h" namespace ccapi { +/** + * The MarketDataService class inherits from the Service class and provides implemenations more specific to market data such as order book, trades, etc.. + */ class MarketDataService : public Service { public: MarketDataService(std::function*)> eventHandler, SessionOptions sessionOptions, SessionConfigs sessionConfigs, diff --git a/linux/include/ccapi_cpp/service/ccapi_market_data_service_ftx_base.h b/linux/include/ccapi_cpp/service/ccapi_market_data_service_ftx_base.h index a1f23790e..db5795f90 100644 --- a/linux/include/ccapi_cpp/service/ccapi_market_data_service_ftx_base.h +++ b/linux/include/ccapi_cpp/service/ccapi_market_data_service_ftx_base.h @@ -24,7 +24,12 @@ class MarketDataServiceFtxBase : public MarketDataService { const std::map optionMap) override { auto marketDepthRequested = std::stoi(optionMap.at(CCAPI_MARKET_DEPTH_MAX)); if (field == CCAPI_MARKET_DEPTH) { - this->marketDepthSubscribedToExchangeByConnectionIdChannelIdSymbolIdMap[wsConnection.id][channelId][symbolId] = 100; + if (marketDepthRequested == 1) { + channelId = CCAPI_WEBSOCKET_FTX_BASE_CHANNEL_TICKER; + this->shouldAlignSnapshot = false; + } else { + this->marketDepthSubscribedToExchangeByConnectionIdChannelIdSymbolIdMap[wsConnection.id][channelId][symbolId] = 100; + } } } void pingOnApplicationLevel(wspp::connection_hdl hdl, ErrorCode& ec) override { this->send(hdl, R"({"op":"ping"})", wspp::frame::opcode::text, ec); } @@ -33,14 +38,15 @@ class MarketDataServiceFtxBase : public MarketDataService { for (const auto& subscriptionListByChannelIdSymbolId : this->subscriptionListByConnectionIdChannelIdSymbolIdMap.at(wsConnection.id)) { auto channelId = subscriptionListByChannelIdSymbolId.first; rj::Value channel(rj::kObjectType); - rj::Value symbolIds(rj::kArrayType); for (const auto& subscriptionListBySymbolId : subscriptionListByChannelIdSymbolId.second) { rj::Document document; document.SetObject(); rj::Document::AllocatorType& allocator = document.GetAllocator(); document.AddMember("op", rj::Value("subscribe").Move(), allocator); std::string symbolId = subscriptionListBySymbolId.first; - symbolIds.PushBack(rj::Value(symbolId.c_str(), allocator).Move(), allocator); + if (channelId == CCAPI_WEBSOCKET_FTX_BASE_CHANNEL_TICKER) { + this->l2UpdateIsReplaceByConnectionIdChannelIdSymbolIdMap[wsConnection.id][channelId][symbolId] = true; + } std::string exchangeSubscriptionId = channelId + "|" + symbolId; std::string market = symbolId; std::string channelIdString = channelId; @@ -89,7 +95,33 @@ class MarketDataServiceFtxBase : public MarketDataService { auto symbolId = std::string(document["market"].GetString()); auto channel = std::string(document["channel"].GetString()); auto exchangeSubscriptionId = channel + "|" + symbolId; - if (channel == CCAPI_WEBSOCKET_FTX_BASE_CHANNEL_ORDERBOOKS) { + if (channel == CCAPI_WEBSOCKET_FTX_BASE_CHANNEL_TICKER) { + MarketDataMessage marketDataMessage; + marketDataMessage.type = MarketDataMessage::Type::MARKET_DATA_EVENTS_MARKET_DEPTH; + marketDataMessage.exchangeSubscriptionId = exchangeSubscriptionId; + auto timePair = UtilTime::divide(data["time"].GetString()); + auto tp = TimePoint(std::chrono::duration(timePair.first)); + tp += std::chrono::nanoseconds(timePair.second); + marketDataMessage.tp = tp; + if (this->processedInitialSnapshotByConnectionIdChannelIdSymbolIdMap[wsConnection.id][channel][symbolId]) { + marketDataMessage.recapType = MarketDataMessage::RecapType::NONE; + } else { + marketDataMessage.recapType = MarketDataMessage::RecapType::SOLICITED; + } + { + MarketDataMessage::TypeForDataPoint dataPoint; + dataPoint.insert({MarketDataMessage::DataFieldType::PRICE, UtilString::normalizeDecimalString(data["bid"].GetString())}); + dataPoint.insert({MarketDataMessage::DataFieldType::SIZE, UtilString::normalizeDecimalString(data["bidSize"].GetString())}); + marketDataMessage.data[MarketDataMessage::DataType::BID].emplace_back(std::move(dataPoint)); + } + { + MarketDataMessage::TypeForDataPoint dataPoint; + dataPoint.insert({MarketDataMessage::DataFieldType::PRICE, UtilString::normalizeDecimalString(data["ask"].GetString())}); + dataPoint.insert({MarketDataMessage::DataFieldType::SIZE, UtilString::normalizeDecimalString(data["askSize"].GetString())}); + marketDataMessage.data[MarketDataMessage::DataType::ASK].emplace_back(std::move(dataPoint)); + } + marketDataMessageList.emplace_back(std::move(marketDataMessage)); + } else if (channel == CCAPI_WEBSOCKET_FTX_BASE_CHANNEL_ORDERBOOKS) { MarketDataMessage marketDataMessage; marketDataMessage.type = MarketDataMessage::Type::MARKET_DATA_EVENTS_MARKET_DEPTH; marketDataMessage.exchangeSubscriptionId = exchangeSubscriptionId; diff --git a/linux/include/ccapi_cpp/service/ccapi_service.h b/linux/include/ccapi_cpp/service/ccapi_service.h index 96fe0a80a..874424361 100644 --- a/linux/include/ccapi_cpp/service/ccapi_service.h +++ b/linux/include/ccapi_cpp/service/ccapi_service.h @@ -60,6 +60,10 @@ using tcp = boost::asio::ip::tcp; namespace wspp = websocketpp; namespace rj = rapidjson; namespace ccapi { +/** + * Defines a service which provides access to exchange API and normalizes them. This is a base class that implements generic functionalities for dealing with + * exchange REST and Websocket APIs. The Session object is responsible for routing requests and subscriptions to the desired concrete service. + */ class Service : public std::enable_shared_from_this { public: typedef wspp::lib::shared_ptr ServiceContextPtr; @@ -361,6 +365,9 @@ class Service : public std::enable_shared_from_this { } CCAPI_LOGGER_TRACE("connected"); beast::ssl_stream& stream = *httpConnectionPtr->streamPtr; + // #ifdef CCAPI_DISABLE_NAGLE_ALGORITHM + beast::get_lowest_layer(stream).socket().set_option(tcp::no_delay(true)); + // #endif CCAPI_LOGGER_TRACE("before async_handshake"); stream.async_handshake(ssl::stream_base::client, beast::bind_front_handler(&Service::onHandshake, shared_from_this(), httpConnectionPtr, req, errorHandler, responseHandler)); @@ -467,6 +474,9 @@ class Service : public std::enable_shared_from_this { } CCAPI_LOGGER_TRACE("connected"); beast::ssl_stream& stream = *httpConnectionPtr->streamPtr; + // #ifdef CCAPI_DISABLE_NAGLE_ALGORITHM + beast::get_lowest_layer(stream).socket().set_option(tcp::no_delay(true)); + // #endif CCAPI_LOGGER_TRACE("before async_handshake"); stream.async_handshake(ssl::stream_base::client, beast::bind_front_handler(&Service::onHandshake_2, shared_from_this(), httpConnectionPtr, request, req, retry, eventQueuePtr)); diff --git a/linux/include/ccapi_cpp/service/ccapi_service_context.h b/linux/include/ccapi_cpp/service/ccapi_service_context.h index bf97616b4..37ccd6f5a 100644 --- a/linux/include/ccapi_cpp/service/ccapi_service_context.h +++ b/linux/include/ccapi_cpp/service/ccapi_service_context.h @@ -7,18 +7,23 @@ #include "websocketpp/config/boost_config.hpp" namespace wspp = websocketpp; namespace ccapi { - +/** + * Defines the service that the service depends on. + */ class ServiceContext CCAPI_FINAL { public: typedef wspp::lib::asio::io_service IoContext; typedef wspp::lib::shared_ptr IoContextPtr; struct CustomClientConfig : public wspp::config::asio_tls_client { -#ifdef WEBSOCKETPP_ENABLE_SINGLE_THREADING +#ifdef CCAPI_USE_SINGLE_THREAD typedef wspp::config::asio_tls_client base; static bool const enable_multithreading = false; struct transport_config : public base::transport_config { static bool const enable_multithreading = false; }; +#endif +#ifdef CCAPI_WEBSOCKET_READ_BUFFER_SIZE +static const size_t connection_read_buffer_size = CCAPI_WEBSOCKET_READ_BUFFER_SIZE; #endif static const wspp::log::level alog_level = wspp::log::alevel::none; static const wspp::log::level elog_level = wspp::log::elevel::none; diff --git a/src/ccapi/install_ccapi.sh b/src/ccapi/install_ccapi.sh index a3e6e6ac0..d94b93a7c 100755 --- a/src/ccapi/install_ccapi.sh +++ b/src/ccapi/install_ccapi.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash -ccapi_tar="v5.29.3.tar.gz" -ccapi="ccapi-5.29.3" +ccapi_tar="v5.31.2.tar.gz" +ccapi="ccapi-5.31.2" this_dir=$(realpath .) root=$(realpath ../..) src=$root/src diff --git a/src/ccapi/v5.31.2.tar.gz b/src/ccapi/v5.31.2.tar.gz new file mode 100644 index 000000000..61af9715b Binary files /dev/null and b/src/ccapi/v5.31.2.tar.gz differ