From 0b7aa02704f6ece97d17fbb118519c5cc62caaba Mon Sep 17 00:00:00 2001
From: Joel Grunbaum <joelgrun@gmail.com>
Date: Wed, 19 Jan 2022 07:45:21 +0000
Subject: [PATCH] Bot prints exchange feed

---
 protocol.cpp |   57 +++++++++++++++++++++++++++++++++++----------------------
 1 files changed, 35 insertions(+), 22 deletions(-)

diff --git a/protocol.cpp b/protocol.cpp
index 0492374..a4b8475 100644
--- a/protocol.cpp
+++ b/protocol.cpp
@@ -22,6 +22,7 @@
 	mapAnnounce;
 std::string server = std::string(HOST) + ":" + std::string(PORT);
 httplib::Client cli("http://" + server);
+std::unique_ptr<easywsclient::WebSocket> ws;
 
 double lastime = 0;
 
@@ -57,32 +58,44 @@
 	return bs;
 }
 
-void catchUp(std::unordered_map<std::string, book::Book>& bs)
+void createWebSocket()
 {
-	static std::unique_ptr<easywsclient::WebSocket> ws(
-		easywsclient::WebSocket::from_url("ws://" + server + "/information"));
-	std::string feed;
-	bool gotMessage = false;
+	ws = std::unique_ptr<easywsclient::WebSocket>(
+		easywsclient::WebSocket::pointer(easywsclient::WebSocket::from_url(
+			"ws://" + server + "/information")));
 	ws->poll();
-	ws->dispatch([gotMessageOut = &gotMessage, messageOut = &feed,
-	              ws = ws.get()](const std::string& message) {
-		*gotMessageOut = true;
-		*messageOut = message;
-	});
-	if (gotMessage) {
-		std::queue<json::Message*> a(json::parse(feed));
-		while (!a.empty()) {
-			if (static_cast<json::FromExchange*>(a.front()) != nullptr ||
-			    static_cast<json::FromExchange*>(a.front())->timestamp >
-			        lastime) {
-				lastime =
-					static_cast<json::FromExchange*>(a.front())->timestamp;
+}
+
+std::deque<json::Message*>
+catchUp(std::unordered_map<std::string, book::Book>& bs)
+{
+	std::string feed;
+	bool gotMessage;
+	std::deque<json::Message*> out;
+	do {
+		gotMessage = false;
+		ws->poll();
+		ws->dispatch([gotMessageOut = &gotMessage, messageOut = &feed,
+		              ws = ws.get()](const std::string& message) {
+			*gotMessageOut = true;
+			*messageOut = message;
+		});
+		if (gotMessage) {
+			std::queue<json::Message*> a(json::parse(feed));
+			while (!a.empty()) {
+				if (static_cast<json::FromExchange*>(a.front()) != nullptr ||
+				    static_cast<json::FromExchange*>(a.front())->timestamp >
+				        lastime) {
+					lastime =
+						static_cast<json::FromExchange*>(a.front())->timestamp;
+				}
+				protocol::handleMessage(bs, a.front());
+				out.push_back(a.front());
+				a.pop();
 			}
-			protocol::handleMessage(bs, a.front());
-			delete a.front();
-			a.pop();
 		}
-	}
+	} while (gotMessage);
+	return out;
 }
 
 json::Message* addOrder(json::AddMessage& order)

--
Gitblit v1.9.3