Skip to content

Commit afef2e8

Browse files
committed
Merge branch 'dev' of git.tigerbrokers.net:server/openapi/openapi-cpp-sdk into dev
2 parents 5eacc9e + f67afda commit afef2e8

33 files changed

+628
-127
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,6 @@ output/Mac/Release/*.dylib
7979
output/Mac/Release/*.a
8080

8181
output/Linux/Debug/
82-
output/Linux/Release/
82+
output/Linux/Release/
83+
84+
*.properties

CMakeLists.txt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.0)
1+
cmake_minimum_required(VERSION 3.0)
22

33

44
# Find the source file that contains the version information
@@ -17,13 +17,15 @@ project(tigerapi VERSION ${PROJECT_VERSION})
1717
# Configure required libraries ...
1818
if(UNIX) # Darwing or Linux
1919

20-
find_package(Boost REQUIRED COMPONENTS system thread log program_options chrono)
20+
find_package(Boost REQUIRED COMPONENTS system thread log program_options chrono filesystem)
2121
find_package(Threads REQUIRED)
2222
find_package(absl REQUIRED)
2323

2424
# cpprest
25-
find_path(CPPREST_INCLUDE_DIR NAMES cpprest/http_client.h cpprest/http_msg.h)
26-
find_library(CPPREST_LIBRARY NAMES cpprest)
25+
# find_path(CPPREST_INCLUDE_DIR NAMES cpprest/http_client.h cpprest/http_msg.h)
26+
set(CPPREST_INCLUDE_DIR "/usr/local/opt/cpprest/include")
27+
set(CPPREST_LIBRARY "/usr/local/opt/cpprest/lib/libcpprest.dylib")
28+
# find_library(CPPREST_LIBRARY NAMES cpprest)
2729
message(CPPREST_INCLUDE_DIR " ${CPPREST_INCLUDE_DIR}")
2830
message(CPPREST_LIBRARY " ${CPPREST_LIBRARY}")
2931

README.md

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ brew install boost
2626
```
2727
tar --bzip2 -xf boost_1_81_0.tar.bz2
2828
```
29-
3. 编译
29+
3. 编译 (注意权限, 如有报错可前面加sudo重试)
3030
```shell
3131
cd /usr/local/boost_1_81_0
3232
./bootstrap.sh
@@ -43,10 +43,25 @@ cd cpprestsdk
4343
git submodule update --init
4444
mkdir build
4545
cd build
46-
cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug ..
46+
cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug-DBOOST_ROOT=/usr/local/boost_1_81_0 -DCMAKE_INSTALL_PREFIX=/usr/local/opt/cpprest ..
4747
make -j 8
4848
make install
4949
```
50+
如果找不到openssl或boost 或其他错误,可尝试指定路径,关闭某些模块 (如果有报错最好删除build目录重新创建):
51+
```
52+
cmake .. \
53+
-DCMAKE_BUILD_TYPE=Debug \
54+
-DCMAKE_INSTALL_PREFIX=/usr/local/opt/cpprest \
55+
-DBUILD_SHARED_LIBS=ON \
56+
-DBUILD_SAMPLES=OFF \
57+
-DBUILD_TESTS=OFF \
58+
-DOPENSSL_ROOT_DIR=/opt/homebrew/opt/openssl@3 \
59+
-DOPENSSL_CRYPTO_LIBRARY=/opt/homebrew/opt/openssl@3/lib/libcrypto.dylib \
60+
-DOPENSSL_SSL_LIBRARY=/opt/homebrew/opt/openssl@3/lib/libssl.dylib \
61+
-DOPENSSL_INCLUDE_DIR=/opt/homebrew/opt/openssl@3/include \
62+
-DBOOST_ROOT=/usr/local/boost_1_81_0 \
63+
-DCMAKE_CXX_FLAGS="-Wno-error=null-pointer-subtraction"
64+
```
5065
5166
**说明**
5267
[参考文档](https://github.com/Microsoft/cpprestsdk/wiki/Getting-Started-Tutorial)
@@ -61,7 +76,7 @@ cd protobuf
6176
git checkout v3.1.0
6277
mkdir cmake_build
6378
cd cmake_build
64-
cmake ../cmake \
79+
cmake .. \
6580
-DCMAKE_BUILD_TYPE=Release \
6681
-DCMAKE_INSTALL_PREFIX=/usr/local/opt/protobuf \
6782
-Dprotobuf_BUILD_TESTS=OFF \
@@ -82,7 +97,8 @@ make install
8297
```
8398
3. 编译
8499
```
85-
cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug ..
100+
cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug \
101+
-DBOOST_ROOT=/usr/local/boost_1_81_0 ..
86102
make -j 6
87103
make install
88104
```
@@ -94,6 +110,15 @@ make install
94110
95111
### 验证测试
96112
编译运行demo项目
113+
```
114+
cd demo
115+
mkdir build
116+
cd build
117+
cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug \
118+
-DBOOST_ROOT=/usr/local/boost_1_81_0 ..
119+
make -j 8
120+
./openapi_cpp_test
121+
```
97122
98123
99124
@@ -166,5 +191,24 @@ ln -s libicuuc.dylib libicuuc.70.dylib
166191
export LDFLAGS="-L/usr/local/opt/icu4c/lib"
167192
export CPPFLAGS="-I/usr/local/opt/icu4c/include"
168193
```
169-
194+
2. mac 系统编译 cpprestsdk 时,报错:
195+
```
196+
CMake Error at Release/cmake/cpprest_find_openssl.cmake:40 (list):
197+
list GET given empty list
198+
Call Stack (most recent call first):
199+
Release/cmake/cpprest_find_websocketpp.cmake:18 (cpprest_find_openssl)
200+
Release/src/CMakeLists.txt:68 (cpprest_find_websocketpp)
201+
```
202+
Fix 指定openssl路径:
203+
```
204+
cmake .. \
205+
-DCMAKE_BUILD_TYPE=Release \
206+
-DBUILD_SHARED_LIBS=ON \
207+
-DCMAKE_INSTALL_PREFIX=/usr/local \
208+
-DOPENSSL_ROOT_DIR=/opt/homebrew/opt/openssl@3 \
209+
-DOPENSSL_CRYPTO_LIBRARY=/opt/homebrew/opt/openssl@3/lib/libcrypto.dylib \
210+
-DOPENSSL_SSL_LIBRARY=/opt/homebrew/opt/openssl@3/lib/libssl.dylib \
211+
-DOPENSSL_INCLUDE_DIR=/opt/homebrew/opt/openssl@3/include \
212+
-DBOOST_ROOT=/usr/local/boost_1_81_0
213+
```
170214

demo/CMakeLists.txt

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ project(openapi_cpp_test)
66

77
# Configure required libraries ...
88
if(UNIX) # Darwing or Linux
9-
9+
1010
find_package(Boost REQUIRED COMPONENTS system thread log program_options chrono)
1111
find_package(Threads REQUIRED)
1212

1313
# cpprest
14-
find_path(CPPREST_INCLUDE_DIR NAMES cpprest/http_client.h cpprest/http_msg.h)
15-
find_library(CPPREST_LIBRARY NAMES cpprest)
14+
# find_path(CPPREST_INCLUDE_DIR NAMES cpprest/http_client.h cpprest/http_msg.h)
15+
# find_library(CPPREST_LIBRARY NAMES cpprest)
16+
set(CPPREST_INCLUDE_DIR "/usr/local/opt/cpprest/include")
17+
set(CPPREST_LIBRARY "/usr/local/opt/cpprest/lib/libcpprest.dylib")
1618
message(CPPREST_INCLUDE_DIR " ${CPPREST_INCLUDE_DIR}")
1719
message(CPPREST_LIBRARY " ${CPPREST_LIBRARY}")
1820

@@ -26,6 +28,7 @@ if(UNIX) # Darwing or Linux
2628
set(Protobuf_LIBRARY "/usr/local/opt/protobuf/lib/libprotobuf.dylib")
2729
# find_path(Protobuf_INCLUDE_DIR NAMES google/protobuf/service.h)
2830
# find_library(Protobuf_LIBRARY NAMES protobuf)
31+
# find_package(Protobuf REQUIRED)
2932
message(STATUS "Protobuf version: ${Protobuf_VERSION}")
3033
message(STATUS "Protobuf include dir: ${Protobuf_INCLUDE_DIR}")
3134
message(STATUS "Protobuf libraries: ${Protobuf_LIBRARY}")
@@ -72,6 +75,15 @@ add_executable(${PROJECT_NAME} openapi_cpp_test/openapi_cpp_test.cpp)
7275
# headers search paths ...
7376
set(HEADER_SEARCH_PATHS ${CPPREST_INCLUDE_DIR} ${Boost_INCLUDE_DIR} ${OPENSSL_INCLUDE_DIR} ${TIGERAPI_INCLUDE_DIR} ${Protobuf_INCLUDE_DIR})
7477

78+
# 在 HEADER_SEARCH_PATHS 如果有 /opt/homebrew/include, 则将其移到最后(因为其中的protobuf是homebrew的, 会优先于自定义的protobuf)
79+
if(HEADER_SEARCH_PATHS MATCHES "/opt/homebrew/include")
80+
list(REMOVE_ITEM HEADER_SEARCH_PATHS "/opt/homebrew/include")
81+
list(APPEND HEADER_SEARCH_PATHS "/opt/homebrew/include")
82+
endif()
83+
message(HEADER_SEARCH_PATHS " ${HEADER_SEARCH_PATHS}")
84+
85+
86+
7587
# library search paths ...
7688
if(APPLE)
7789
set(OPENSSL_LIBS "${OPENSSL_LIBRARIES}")

demo/openapi_cpp_test/openapi_cpp_test.cpp

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
#include "tigerapi/utils.h"
1111
#include "cpprest/details/basic_types.h"
1212
#include "tigerapi/price_util.h"
13-
#include "tigerapi/easylogging++.h"
1413
#include <chrono>
1514
#include <thread>
15+
#include "tigerapi/logger.h"
1616

1717
using namespace std;
1818
using namespace web;
@@ -406,7 +406,7 @@ class TestQuoteClient {
406406

407407

408408
static void test_quote(const std::shared_ptr<QuoteClient> quote_client) {
409-
TestQuoteClient::test_get_kline_quota(quote_client);
409+
TestQuoteClient::test_get_quote_delay(quote_client);
410410
}
411411
};
412412

@@ -469,6 +469,19 @@ class TestPushClient {
469469
push_client->subscribe_tick(symbols);
470470
}
471471

472+
void error_callback(const tigeropen::push::pb::Response& data) {
473+
ucout << "Error callback: " << std::endl;
474+
ucout << "- code: " << data.code() << std::endl;
475+
ucout << "- msg: " << utility::conversions::to_string_t(data.msg()) << std::endl;
476+
}
477+
478+
void kickout_callback(const tigeropen::push::pb::Response& data) {
479+
ucout << "Kickout callback: " << std::endl;
480+
ucout << "- code: " << data.code() << std::endl;
481+
ucout << "- msg: " << utility::conversions::to_string_t(data.msg()) << std::endl;
482+
483+
}
484+
472485
void position_changed_callback(const tigeropen::push::pb::PositionData& data) {
473486
ucout << "Position changed:" << std::endl;
474487
ucout << "- symbol: " << utility::conversions::to_string_t(data.symbol()) << std::endl;
@@ -542,6 +555,8 @@ class TestPushClient {
542555

543556
void start_test(ClientConfig config) {
544557
push_client->set_connected_callback(std::bind(&TestPushClient::connected_callback, this));
558+
push_client->set_error_callback(std::bind(&TestPushClient::error_callback, this, std::placeholders::_1));
559+
push_client->set_kickout_callback(std::bind(&TestPushClient::kickout_callback, this, std::placeholders::_1));
545560
push_client->set_position_changed_callback(std::bind(&TestPushClient::position_changed_callback, this, std::placeholders::_1));
546561
push_client->set_order_changed_callback(std::bind(&TestPushClient::order_changed_callback, this, std::placeholders::_1));
547562
push_client->set_asset_changed_callback(std::bind(&TestPushClient::asset_changed_callback, this, std::placeholders::_1));
@@ -579,27 +594,31 @@ class TestPushClient {
579594
}
580595
};
581596

582-
int main()
583-
{
597+
int main(int argc, char* argv[]) {
598+
LoggerConfig::set_log_level(el::Level::Debug);
584599
//Set Tiger OpenAPI SDK configuration
585600
bool sand_box = false;
586-
ClientConfig config = ClientConfig(sand_box);
587-
config.private_key = U("");
588-
config.tiger_id = U("");
589-
config.account = U("");
601+
ClientConfig config = ClientConfig(false, U("../openapi_cpp_test/"));
602+
//config.set_server_url(U("http://127.0.0.1:8085/gateway"));
603+
//config.set_server_public_key(SANDBOX_TIGER_PUBLIC_KEY);
604+
//ClientConfig config = ClientConfig(false);
605+
606+
// config.private_key = U("");
607+
// config.tiger_id = U("");
608+
// config.account = U("");
590609
config.use_full_tick = true;
591610
//config.lang = U("en_US");
592611

593612
//Create a push client instance
594-
auto push_client = IPushClient::create_push_client(config);
613+
// auto push_client = IPushClient::create_push_client(config);
595614
//Run some push test cases
596-
TestPushClient::test_push_client(push_client, config);
615+
// TestPushClient::test_push_client(push_client, config);
597616

598617
/**
599618
* QuoteClient
600619
*/
601-
//std::shared_ptr<QuoteClient> quote_client = std::make_shared<QuoteClient>(config);
602-
//TestQuoteClient::test_quote(quote_client);
620+
std::shared_ptr<QuoteClient> quote_client = std::make_shared<QuoteClient>(config);
621+
TestQuoteClient::test_quote(quote_client);
603622

604623
/**
605624
* TradeClient

demo/openapi_cpp_test/openapi_cpp_test.vcxproj

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@
160160
<ClCompile>
161161
<WarningLevel>Level3</WarningLevel>
162162
<SDLCheck>true</SDLCheck>
163-
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;WIN32_LEAN_AND_MEAN;%(PreprocessorDefinitions)</PreprocessorDefinitions>
163+
<PreprocessorDefinitions>WIN32;_CRT_SECURE_NO_WARNINGS;_DEBUG;_CONSOLE;WIN32_LEAN_AND_MEAN;%(PreprocessorDefinitions)</PreprocessorDefinitions>
164164
<ConformanceMode>true</ConformanceMode>
165165
<AdditionalIncludeDirectories>..\..\include</AdditionalIncludeDirectories>
166166
</ClCompile>
@@ -175,7 +175,7 @@
175175
<ClCompile>
176176
<WarningLevel>Level3</WarningLevel>
177177
<SDLCheck>true</SDLCheck>
178-
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;WIN32_LEAN_AND_MEAN;%(PreprocessorDefinitions)</PreprocessorDefinitions>
178+
<PreprocessorDefinitions>WIN32;_CRT_SECURE_NO_WARNINGS;_DEBUG;_CONSOLE;WIN32_LEAN_AND_MEAN;%(PreprocessorDefinitions)</PreprocessorDefinitions>
179179
<ConformanceMode>true</ConformanceMode>
180180
<AdditionalIncludeDirectories>..\..\include</AdditionalIncludeDirectories>
181181
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
@@ -191,7 +191,7 @@
191191
<ClCompile>
192192
<WarningLevel>Level3</WarningLevel>
193193
<SDLCheck>true</SDLCheck>
194-
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
194+
<PreprocessorDefinitions>WIN32;_CRT_SECURE_NO_WARNINGS;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
195195
<ConformanceMode>true</ConformanceMode>
196196
<AdditionalIncludeDirectories>..\..\include</AdditionalIncludeDirectories>
197197
</ClCompile>
@@ -206,7 +206,7 @@
206206
<ClCompile>
207207
<WarningLevel>Level3</WarningLevel>
208208
<SDLCheck>true</SDLCheck>
209-
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
209+
<PreprocessorDefinitions>WIN32;_CRT_SECURE_NO_WARNINGS;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
210210
<ConformanceMode>true</ConformanceMode>
211211
<AdditionalIncludeDirectories>..\..\include</AdditionalIncludeDirectories>
212212
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
@@ -224,7 +224,7 @@
224224
<FunctionLevelLinking>true</FunctionLevelLinking>
225225
<IntrinsicFunctions>true</IntrinsicFunctions>
226226
<SDLCheck>true</SDLCheck>
227-
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
227+
<PreprocessorDefinitions>WIN32;_CRT_SECURE_NO_WARNINGS;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
228228
<ConformanceMode>true</ConformanceMode>
229229
<AdditionalIncludeDirectories>..\..\include</AdditionalIncludeDirectories>
230230
</ClCompile>
@@ -243,7 +243,7 @@
243243
<FunctionLevelLinking>true</FunctionLevelLinking>
244244
<IntrinsicFunctions>true</IntrinsicFunctions>
245245
<SDLCheck>true</SDLCheck>
246-
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
246+
<PreprocessorDefinitions>WIN32;_CRT_SECURE_NO_WARNINGS;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
247247
<ConformanceMode>true</ConformanceMode>
248248
<AdditionalIncludeDirectories>..\..\include</AdditionalIncludeDirectories>
249249
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
@@ -263,7 +263,7 @@
263263
<FunctionLevelLinking>true</FunctionLevelLinking>
264264
<IntrinsicFunctions>true</IntrinsicFunctions>
265265
<SDLCheck>true</SDLCheck>
266-
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
266+
<PreprocessorDefinitions>WIN32;_CRT_SECURE_NO_WARNINGS;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
267267
<ConformanceMode>true</ConformanceMode>
268268
<AdditionalIncludeDirectories>..\..\include</AdditionalIncludeDirectories>
269269
</ClCompile>
@@ -282,7 +282,7 @@
282282
<FunctionLevelLinking>true</FunctionLevelLinking>
283283
<IntrinsicFunctions>true</IntrinsicFunctions>
284284
<SDLCheck>true</SDLCheck>
285-
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
285+
<PreprocessorDefinitions>WIN32;_CRT_SECURE_NO_WARNINGS;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
286286
<ConformanceMode>true</ConformanceMode>
287287
<AdditionalIncludeDirectories>..\..\include</AdditionalIncludeDirectories>
288288
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>

0 commit comments

Comments
 (0)