Skip to content

Commit d9dc279

Browse files
Merge pull request #530 from crypto-chassis/misc
support non-ssl in websocket
2 parents ff0050b + 4201dd8 commit d9dc279

8 files changed

+208
-154
lines changed

include/ccapi_cpp/ccapi_fix_connection.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class FixConnection {
6969
std::string url;
7070
Subscription subscription;
7171
Status status{Status::UNKNOWN};
72-
std::shared_ptr<T> streamPtr;
72+
std::shared_ptr<T> streamPtr{nullptr};
7373
};
7474

7575
} /* namespace ccapi */

include/ccapi_cpp/ccapi_http_connection.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class HttpConnection {
3030

3131
std::string host;
3232
std::string port;
33-
std::shared_ptr<beast::ssl_stream<beast::tcp_stream>> streamPtr;
33+
std::shared_ptr<beast::ssl_stream<beast::tcp_stream>> streamPtr{nullptr};
3434
TimePoint lastReceiveDataTp{std::chrono::seconds{0}};
3535

3636
boost::beast::flat_buffer buffer;

include/ccapi_cpp/ccapi_ws_connection.h

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define INCLUDE_CCAPI_CPP_CCAPI_WS_CONNECTION_H_
33

44
#include <string>
5+
#include <variant>
56

67
#include "ccapi_cpp/ccapi_logger.h"
78
#include "ccapi_cpp/ccapi_subscription.h"
@@ -16,9 +17,9 @@ class WsConnection {
1617
WsConnection(const WsConnection&) = delete;
1718
WsConnection& operator=(const WsConnection&) = delete;
1819

19-
WsConnection(std::string url, std::string group, std::vector<Subscription> subscriptionList, std::map<std::string, std::string> credential,
20-
std::shared_ptr<beast::websocket::stream<beast::ssl_stream<beast::tcp_stream>>> streamPtr)
21-
: url(url), group(group), subscriptionList(subscriptionList), credential(credential), streamPtr(streamPtr) {
20+
WsConnection(const std::string& url, const std::string& group, const std::vector<Subscription>& subscriptionList,
21+
const std::map<std::string, std::string>& credential)
22+
: url(url), group(group), subscriptionList(subscriptionList), credential(credential) {
2223
std::map<std::string, std::string> shortCredential;
2324
for (const auto& x : credential) {
2425
shortCredential.insert(std::make_pair(x.first, UtilString::firstNCharacter(x.second, CCAPI_CREDENTIAL_DISPLAY_LENGTH)));
@@ -39,7 +40,15 @@ class WsConnection {
3940
shortCredential.insert(std::make_pair(x.first, UtilString::firstNCharacter(x.second, CCAPI_CREDENTIAL_DISPLAY_LENGTH)));
4041
}
4142
std::ostringstream oss;
42-
oss << streamPtr;
43+
std::visit(
44+
[&oss](auto&& streamSharedPtr) {
45+
if (streamSharedPtr) {
46+
oss << streamSharedPtr.get();
47+
} else {
48+
oss << "nullptr";
49+
}
50+
},
51+
streamPtr);
4352
std::string output = "WsConnection [longId = " + longId + ", id = " + id + ", url = " + url + ", group = " + group +
4453
", subscriptionList = " + ccapi::toString(subscriptionList) + ", credential = " + ccapi::toString(shortCredential) +
4554
", status = " + statusToString(status) + ", headers = " + ccapi::toString(headers) + ", streamPtr = " + oss.str() +
@@ -116,6 +125,9 @@ class WsConnection {
116125
this->port = CCAPI_HTTP_PORT_DEFAULT;
117126
}
118127
}
128+
if (splitted1.at(0) == "https" || splitted1.at(0) == "wss") {
129+
this->isSecure = true;
130+
}
119131
}
120132
}
121133

@@ -133,7 +145,8 @@ class WsConnection {
133145
Status status{Status::UNKNOWN};
134146
std::map<std::string, std::string> headers;
135147
std::map<std::string, std::string> credential;
136-
std::shared_ptr<beast::websocket::stream<beast::ssl_stream<beast::tcp_stream>>> streamPtr;
148+
std::variant<std::shared_ptr<beast::websocket::stream<beast::ssl_stream<beast::tcp_stream>>>, std::shared_ptr<beast::websocket::stream<beast::tcp_stream>>>
149+
streamPtr;
137150
beast::websocket::close_code remoteCloseCode{};
138151
beast::websocket::close_reason remoteCloseReason{};
139152
std::string hostHttpHeaderValue;
@@ -145,6 +158,7 @@ class WsConnection {
145158
std::array<char, CCAPI_WEBSOCKET_WRITE_BUFFER_SIZE> writeMessageBuffer;
146159
size_t writeMessageBufferWrittenLength{};
147160
std::vector<size_t> writeMessageBufferBoundary;
161+
bool isSecure{};
148162
};
149163

150164
} /* namespace ccapi */

include/ccapi_cpp/service/ccapi_execution_management_service.h

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,15 @@ class ExecutionManagementService : public Service {
6161
credential = that->credentialDefault;
6262
}
6363

64-
std::shared_ptr<beast::websocket::stream<beast::ssl_stream<beast::tcp_stream>>> streamPtr(nullptr);
65-
try {
66-
streamPtr = that->createWsStream(that->serviceContextPtr->ioContextPtr, that->serviceContextPtr->sslContextPtr);
67-
} catch (const beast::error_code& ec) {
68-
CCAPI_LOGGER_TRACE("fail");
69-
that->onError(Event::Type::SUBSCRIPTION_STATUS, Message::Type::SUBSCRIPTION_FAILURE, ec, "create stream", {subscription.getCorrelationId()});
70-
return;
71-
}
7264
const auto& fieldSet = subscription.getFieldSet();
7365
if (fieldSet.find(CCAPI_EM_WEBSOCKET_ORDER_ENTRY) != fieldSet.end()) {
74-
std::shared_ptr<WsConnection> wsConnectionPtr(new WsConnection(that->baseUrlWsOrderEntry, "", {subscription}, credential, streamPtr));
66+
auto wsConnectionPtr = std::make_shared<WsConnection>(that->baseUrlWsOrderEntry, "", std::vector<Subscription>{subscription}, credential);
67+
that->setWsConnectionStream(wsConnectionPtr);
7568
CCAPI_LOGGER_WARN("about to subscribe with new wsConnectionPtr " + toString(*wsConnectionPtr));
7669
that->prepareConnect(wsConnectionPtr);
7770
} else {
78-
std::shared_ptr<WsConnection> wsConnectionPtr(new WsConnection(that->baseUrlWs, "", {subscription}, credential, streamPtr));
71+
auto wsConnectionPtr = std::make_shared<WsConnection>(that->baseUrlWs, "", std::vector<Subscription>{subscription}, credential);
72+
that->setWsConnectionStream(wsConnectionPtr);
7973
CCAPI_LOGGER_WARN("about to subscribe with new wsConnectionPtr " + toString(*wsConnectionPtr));
8074
that->prepareConnect(wsConnectionPtr);
8175
}

include/ccapi_cpp/service/ccapi_execution_management_service_ascendex.h

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -363,16 +363,9 @@ class ExecutionManagementServiceAscendex : public ExecutionManagementService {
363363
}
364364
const auto& accountGroup = mapGetWithDefault(credential, that->apiAccountGroupName);
365365

366-
std::shared_ptr<beast::websocket::stream<beast::ssl_stream<beast::tcp_stream>>> streamPtr(nullptr);
367-
try {
368-
streamPtr = that->createWsStream(that->serviceContextPtr->ioContextPtr, that->serviceContextPtr->sslContextPtr);
369-
} catch (const beast::error_code& ec) {
370-
CCAPI_LOGGER_TRACE("fail");
371-
that->onError(Event::Type::SUBSCRIPTION_STATUS, Message::Type::SUBSCRIPTION_FAILURE, ec, "create stream", {subscription.getCorrelationId()});
372-
return;
373-
}
374-
std::shared_ptr<WsConnection> wsConnectionPtr(
375-
new WsConnection(that->baseUrlWs + "/" + accountGroup + "/api/pro/v1/stream", "", {subscription}, credential, streamPtr));
366+
auto wsConnectionPtr = std::make_shared<WsConnection>(that->baseUrlWs + "/" + accountGroup + "/api/pro/v1/stream", "",
367+
std::vector<Subscription>{subscription}, credential);
368+
that->setWsConnectionStream(wsConnectionPtr);
376369
CCAPI_LOGGER_WARN("about to subscribe with new wsConnectionPtr " + toString(*wsConnectionPtr));
377370
that->prepareConnect(wsConnectionPtr);
378371
});

include/ccapi_cpp/service/ccapi_execution_management_service_gateio_perpetual_futures.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,8 @@ class ExecutionManagementServiceGateioPerpetualFutures : public ExecutionManagem
101101
credential = that->credentialDefault;
102102
}
103103

104-
std::shared_ptr<beast::websocket::stream<beast::ssl_stream<beast::tcp_stream>>> streamPtr(nullptr);
105-
try {
106-
streamPtr = that->createWsStream(that->serviceContextPtr->ioContextPtr, that->serviceContextPtr->sslContextPtr);
107-
} catch (const beast::error_code& ec) {
108-
CCAPI_LOGGER_TRACE("fail");
109-
that->onError(Event::Type::SUBSCRIPTION_STATUS, Message::Type::SUBSCRIPTION_FAILURE, ec, "create stream", {subscription.getCorrelationId()});
110-
return;
111-
}
112-
std::shared_ptr<WsConnection> wsConnectionPtr(new WsConnection(that->baseUrlWs + settle, "", {subscription}, credential, streamPtr));
104+
auto wsConnectionPtr = std::make_shared<WsConnection>(that->baseUrlWs + settle, "", std::vector<Subscription>{subscription}, credential);
105+
that->setWsConnectionStream(wsConnectionPtr);
113106
CCAPI_LOGGER_WARN("about to subscribe with new wsConnectionPtr " + toString(*wsConnectionPtr));
114107
that->prepareConnect(wsConnectionPtr);
115108
}

include/ccapi_cpp/service/ccapi_market_data_service.h

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,9 @@ class MarketDataService : public Service {
9292
if (credential.empty()) {
9393
credential = that->credentialDefault;
9494
}
95-
std::shared_ptr<beast::websocket::stream<beast::ssl_stream<beast::tcp_stream>>> streamPtr(nullptr);
96-
try {
97-
streamPtr = that->createWsStream(that->serviceContextPtr->ioContextPtr, that->serviceContextPtr->sslContextPtr);
98-
} catch (const beast::error_code& ec) {
99-
CCAPI_LOGGER_TRACE("fail");
100-
std::vector<std::string> correlationIdList;
101-
correlationIdList.reserve(subscriptionListGivenInstrumentGroup.size());
102-
std::transform(subscriptionListGivenInstrumentGroup.cbegin(), subscriptionListGivenInstrumentGroup.cend(), std::back_inserter(correlationIdList),
103-
[](Subscription subscription) { return subscription.getCorrelationId(); });
104-
that->onError(Event::Type::SUBSCRIPTION_STATUS, Message::Type::SUBSCRIPTION_FAILURE, ec, "create stream", correlationIdList);
105-
return;
106-
}
107-
std::shared_ptr<WsConnection> wsConnectionPtr(new WsConnection(url, instrumentGroup, subscriptionListGivenInstrumentGroup, credential, streamPtr));
95+
96+
auto wsConnectionPtr = std::make_shared<WsConnection>(url, instrumentGroup, subscriptionListGivenInstrumentGroup, credential);
97+
that->setWsConnectionStream(wsConnectionPtr);
10898
CCAPI_LOGGER_WARN("about to subscribe with new wsConnectionPtr " + toString(*wsConnectionPtr));
10999
that->prepareConnect(wsConnectionPtr);
110100
}

0 commit comments

Comments
 (0)