Skip to content

Commit 5bca830

Browse files
Merge pull request #544 from crypto-chassis/add-example-use-multiple-sessions
add example use multiple sessions
2 parents f9a9a8c + cf5b12c commit 5bca830

4 files changed

Lines changed: 118 additions & 48 deletions

File tree

README.md

Lines changed: 57 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,51 @@
11
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
2-
2+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
33
**Table of Contents** *generated with [DocToc](https://github.com/ktechhub/doctoc)*
44

5-
<!---toc start-->
6-
7-
* [ccapi](#ccapi)
8-
* [Branches](#branches)
9-
* [Build](#build)
10-
* [C++](#c)
11-
* [non-C++](#non-c)
12-
* [Constants](#constants)
13-
* [Examples](#examples)
14-
* [Documentations](#documentations)
15-
* [Simple Market Data](#simple-market-data)
16-
* [Advanced Market Data](#advanced-market-data)
17-
* [Complex request parameters](#complex-request-parameters)
18-
* [Specify subscription market depth](#specify-subscription-market-depth)
19-
* [Specify correlation id](#specify-correlation-id)
20-
* [Multiple exchanges and/or instruments](#multiple-exchanges-andor-instruments)
21-
* [Receive subscription events at periodic intervals](#receive-subscription-events-at-periodic-intervals)
22-
* [Receive subscription events at periodic intervals including when the market depth snapshot hasn't changed](#receive-subscription-events-at-periodic-intervals-including-when-the-market-depth-snapshot-hasnt-changed)
23-
* [Receive subscription market depth updates](#receive-subscription-market-depth-updates)
24-
* [Receive subscription trade events](#receive-subscription-trade-events)
25-
* [Receive subscription calculated-candlestick events at periodic intervals](#receive-subscription-calculated-candlestick-events-at-periodic-intervals)
26-
* [Receive subscription exchange-provided-candlestick events at periodic intervals](#receive-subscription-exchange-provided-candlestick-events-at-periodic-intervals)
27-
* [Send generic public requests](#send-generic-public-requests)
28-
* [Make generic public subscriptions](#make-generic-public-subscriptions)
29-
* [Send generic private requests](#send-generic-private-requests)
30-
* [Simple Execution Management](#simple-execution-management)
31-
* [Advanced Execution Management](#advanced-execution-management)
32-
* [Specify correlation id](#specify-correlation-id-1)
33-
* [Multiple exchanges and/or instruments](#multiple-exchanges-andor-instruments-1)
34-
* [Multiple subscription fields](#multiple-subscription-fields)
35-
* [Make Session::sendRequest blocking](#make-sessionsendrequest-blocking)
36-
* [Provide API credentials for an exchange](#provide-api-credentials-for-an-exchange)
37-
* [Override exchange urls](#override-exchange-urls)
38-
* [Complex request parameters](#complex-request-parameters-1)
39-
* [Send request by Websocket API](#send-request-by-websocket-api)
40-
* [Specify instrument type](#specify-instrument-type)
41-
* [FIX API](#fix-api)
42-
* [More Advanced Topics](#more-advanced-topics)
43-
* [Handle events in "immediate" vs. "batching" mode](#handle-events-in-immediate-vs-batching-mode)
44-
* [Thread safety](#thread-safety)
45-
* [Enable library logging](#enable-library-logging)
46-
* [Set timer](#set-timer)
47-
* [Heartbeat](#heartbeat)
48-
* [Performance Tuning](#performance-tuning)
49-
* [Known Issues and Workarounds](#known-issues-and-workarounds)
50-
51-
<!---toc end-->
5+
- [ccapi](#ccapi)
6+
- [Branches](#branches)
7+
- [Build](#build)
8+
- [C++](#c)
9+
- [non-C++](#non-c)
10+
- [Constants](#constants)
11+
- [Examples](#examples)
12+
- [Documentations](#documentations)
13+
- [Simple Market Data](#simple-market-data)
14+
- [Advanced Market Data](#advanced-market-data)
15+
- [Complex request parameters](#complex-request-parameters)
16+
- [Specify subscription market depth](#specify-subscription-market-depth)
17+
- [Specify correlation id](#specify-correlation-id)
18+
- [Multiple exchanges and/or instruments](#multiple-exchanges-andor-instruments)
19+
- [Receive subscription events at periodic intervals](#receive-subscription-events-at-periodic-intervals)
20+
- [Receive subscription events at periodic intervals including when the market depth snapshot hasn't changed](#receive-subscription-events-at-periodic-intervals-including-when-the-market-depth-snapshot-hasnt-changed)
21+
- [Receive subscription market depth updates](#receive-subscription-market-depth-updates)
22+
- [Receive subscription trade events](#receive-subscription-trade-events)
23+
- [Receive subscription calculated-candlestick events at periodic intervals](#receive-subscription-calculated-candlestick-events-at-periodic-intervals)
24+
- [Receive subscription exchange-provided-candlestick events at periodic intervals](#receive-subscription-exchange-provided-candlestick-events-at-periodic-intervals)
25+
- [Send generic public requests](#send-generic-public-requests)
26+
- [Make generic public subscriptions](#make-generic-public-subscriptions)
27+
- [Send generic private requests](#send-generic-private-requests)
28+
- [Simple Execution Management](#simple-execution-management)
29+
- [Advanced Execution Management](#advanced-execution-management)
30+
- [Specify correlation id](#specify-correlation-id-1)
31+
- [Multiple exchanges and/or instruments](#multiple-exchanges-andor-instruments-1)
32+
- [Multiple subscription fields](#multiple-subscription-fields)
33+
- [Make Session::sendRequest blocking](#make-sessionsendrequest-blocking)
34+
- [Provide API credentials for an exchange](#provide-api-credentials-for-an-exchange)
35+
- [Override exchange urls](#override-exchange-urls)
36+
- [Complex request parameters](#complex-request-parameters-1)
37+
- [Send request by Websocket API](#send-request-by-websocket-api)
38+
- [Specify instrument type](#specify-instrument-type)
39+
- [FIX API](#fix-api)
40+
- [More Advanced Topics](#more-advanced-topics)
41+
- [Handle events in "immediate" vs. "batching" mode](#handle-events-in-immediate-vs-batching-mode)
42+
- [Thread safety](#thread-safety)
43+
- [Enable library logging](#enable-library-logging)
44+
- [Set timer](#set-timer)
45+
- [Heartbeat](#heartbeat)
46+
- [Use multiple sessions](#use-multiple-sessions)
47+
- [Performance Tuning](#performance-tuning)
48+
- [Known Issues and Workarounds](#known-issues-and-workarounds)
5249

5350
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
5451

@@ -1067,6 +1064,18 @@ Subscription subscription("", "", "HEARTBEAT", "HEARTBEAT_INTERVAL_MILLISECONDS=
10671064
session.subscribe(subscription);
10681065
```
10691066

1067+
#### Use multiple sessions
1068+
1069+
[C++](example/src/use_multiple_sessions/main.cpp)
1070+
1071+
Multiple `session` instances, each with their own `SessionOptions` and `SessionConfigs`, can share a common `EventHandler`. If no `EventDispatcher` is provided, thread safety must be maintained by using a shared `ServiceContext`.
1072+
```
1073+
Subscription subscription("", "", "HEARTBEAT", "HEARTBEAT_INTERVAL_MILLISECONDS=1000");
1074+
session.subscribe(subscription);
1075+
```
1076+
1077+
1078+
10701079
## Performance Tuning
10711080
* Turn on compiler optimization flags (e.g. `cmake -DCMAKE_BUILD_TYPE=Release ...`).
10721081
* Enable link time optimization (e.g. in CMakeLists.txt `set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)` before a target is created). Note that link time optimization is only applicable to static linking.

example/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,4 @@ add_subdirectory(src/heartbeat)
8686
add_subdirectory(src/override_exchange_url_at_runtime)
8787
add_subdirectory(src/test_order_latency)
8888
add_subdirectory(src/test_cpu_usage)
89+
add_subdirectory(src/use_multiple_sessions)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
set(NAME use_multiple_sessions)
2+
project(${NAME})
3+
add_compile_definitions(CCAPI_ENABLE_SERVICE_MARKET_DATA)
4+
add_compile_definitions(CCAPI_ENABLE_EXCHANGE_OKX)
5+
add_executable(${NAME} main.cpp)
6+
add_dependencies(${NAME} boost rapidjson)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include "ccapi_cpp/ccapi_session.h"
2+
3+
namespace ccapi {
4+
5+
Logger* Logger::logger = nullptr; // This line is needed.
6+
7+
class MyEventHandler : public EventHandler {
8+
public:
9+
void processEvent(const Event& event, Session* sessionPtr) override {
10+
std::cout << "Received an event from session " << this->sessionPtrs.at(sessionPtr) << " on thread " << std::this_thread::get_id() << std::endl;
11+
}
12+
13+
void setSessionPtrs(const std::map<Session*, std::string>& sessionPtrs) { this->sessionPtrs = sessionPtrs; }
14+
15+
private:
16+
std::map<Session*, std::string> sessionPtrs;
17+
};
18+
19+
} /* namespace ccapi */
20+
21+
using ::ccapi::MyEventHandler;
22+
using ::ccapi::ServiceContext;
23+
using ::ccapi::Session;
24+
using ::ccapi::SessionConfigs;
25+
using ::ccapi::SessionOptions;
26+
using ::ccapi::Subscription;
27+
using ::ccapi::toString;
28+
29+
int main(int argc, char** argv) {
30+
SessionOptions sessionOptions_1;
31+
SessionOptions sessionOptions_2;
32+
SessionConfigs sessionConfigs_1;
33+
SessionConfigs sessionConfigs_2;
34+
MyEventHandler eventHandler;
35+
ServiceContext serviceContext;
36+
serviceContext.start();
37+
38+
Session session_1(sessionOptions_1, sessionConfigs_1, &eventHandler, nullptr, &serviceContext);
39+
Session session_2(sessionOptions_2, sessionConfigs_2, &eventHandler, nullptr, &serviceContext);
40+
eventHandler.setSessionPtrs({
41+
{&session_1, "1"},
42+
{&session_2, "2"},
43+
});
44+
Subscription subscription("okx", "BTC-USDT", "MARKET_DEPTH");
45+
session_1.subscribe(subscription);
46+
session_2.subscribe(subscription);
47+
48+
std::this_thread::sleep_for(std::chrono::seconds(10));
49+
session_1.stop();
50+
session_2.stop();
51+
serviceContext.stop();
52+
std::cout << "Bye" << std::endl;
53+
return EXIT_SUCCESS;
54+
}

0 commit comments

Comments
 (0)