IXWebSocket/test/IXTest.cpp

182 lines
5.1 KiB
C++
Raw Normal View History

2018-12-30 03:33:15 +01:00
/*
* IXTest.cpp
* Author: Benjamin Sergeant
* Copyright (c) 2018 Machine Zone. All rights reserved.
*/
#include "IXTest.h"
#include <chrono>
#include <fstream>
2019-03-22 23:33:04 +01:00
#include <iomanip>
2019-09-23 19:25:23 +02:00
#include <iostream>
#include <ixwebsocket/IXNetSystem.h>
#include <ixwebsocket/IXWebSocket.h>
#include <mutex>
#include <random>
2019-09-23 19:25:23 +02:00
#include <stack>
#include <stdlib.h>
#include <string>
#include <thread>
2018-12-30 03:33:15 +01:00
namespace ix
{
std::atomic<size_t> incomingBytes(0);
std::atomic<size_t> outgoingBytes(0);
2020-03-12 17:07:01 +01:00
std::mutex TLogger::_mutex;
std::stack<int> freePorts;
2018-12-30 03:33:15 +01:00
void setupWebSocketTrafficTrackerCallback()
{
2019-09-23 19:25:23 +02:00
ix::WebSocket::setTrafficTrackerCallback([](size_t size, bool incoming) {
if (incoming)
{
incomingBytes += size;
}
else
2018-12-30 03:33:15 +01:00
{
2019-09-23 19:25:23 +02:00
outgoingBytes += size;
2018-12-30 03:33:15 +01:00
}
2019-09-23 19:25:23 +02:00
});
2018-12-30 03:33:15 +01:00
}
void reportWebSocketTraffic()
{
2020-03-12 17:07:01 +01:00
TLogger() << incomingBytes;
TLogger() << "Incoming bytes: " << incomingBytes;
TLogger() << "Outgoing bytes: " << outgoingBytes;
2018-12-30 03:33:15 +01:00
}
void msleep(int ms)
{
std::chrono::duration<double, std::milli> duration(ms);
std::this_thread::sleep_for(duration);
}
std::string generateSessionId()
{
auto now = std::chrono::system_clock::now();
auto seconds =
2019-09-23 19:25:23 +02:00
std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch()).count();
2018-12-30 03:33:15 +01:00
return std::to_string(seconds);
}
2019-01-08 03:04:28 +01:00
void log(const std::string& msg)
{
2020-03-12 17:07:01 +01:00
TLogger() << msg;
2019-01-08 03:04:28 +01:00
}
2019-09-23 19:25:23 +02:00
void hexDump(const std::string& prefix, const std::string& s)
2019-03-22 23:33:04 +01:00
{
std::ostringstream ss;
bool upper_case = false;
for (std::string::size_type i = 0; i < s.length(); ++i)
{
2019-09-23 19:25:23 +02:00
ss << std::hex << std::setfill('0') << std::setw(2)
<< (upper_case ? std::uppercase : std::nouppercase) << (int) s[i];
2019-03-22 23:33:04 +01:00
}
std::cout << prefix << ": " << s << " => " << ss.str() << std::endl;
}
bool startWebSocketEchoServer(ix::WebSocketServer& server)
{
2019-09-23 19:25:23 +02:00
server.setOnConnectionCallback([&server](std::shared_ptr<ix::WebSocket> webSocket,
std::shared_ptr<ConnectionState> connectionState) {
webSocket->setOnMessageCallback(
[webSocket, connectionState, &server](const ix::WebSocketMessagePtr& msg) {
if (msg->type == ix::WebSocketMessageType::Open)
{
2020-03-12 17:07:01 +01:00
TLogger() << "New connection";
TLogger() << "Uri: " << msg->openInfo.uri;
TLogger() << "Headers:";
2019-09-23 19:25:23 +02:00
for (auto it : msg->openInfo.headers)
{
2020-03-12 17:07:01 +01:00
TLogger() << it.first << ": " << it.second;
}
2019-09-23 19:25:23 +02:00
}
else if (msg->type == ix::WebSocketMessageType::Close)
{
2020-03-12 17:07:01 +01:00
TLogger() << "Closed connection";
2019-09-23 19:25:23 +02:00
}
else if (msg->type == ix::WebSocketMessageType::Message)
{
for (auto&& client : server.getClients())
{
2019-09-23 19:25:23 +02:00
if (client != webSocket)
{
2019-09-23 19:25:23 +02:00
client->send(msg->str, msg->binary);
}
}
}
2019-09-23 19:25:23 +02:00
});
});
auto res = server.listen();
if (!res.first)
{
2020-03-12 17:07:01 +01:00
TLogger() << res.second;
return false;
}
server.start();
return true;
}
std::vector<uint8_t> load(const std::string& path)
{
std::vector<uint8_t> memblock;
std::ifstream file(path);
if (!file.is_open()) return memblock;
file.seekg(0, file.end);
std::streamoff size = file.tellg();
file.seekg(0, file.beg);
memblock.resize((size_t) size);
2019-09-23 19:25:23 +02:00
file.read((char*) &memblock.front(), static_cast<std::streamsize>(size));
return memblock;
}
std::string readAsString(const std::string& path)
{
auto vec = load(path);
return std::string(vec.begin(), vec.end());
}
snake::AppConfig makeSnakeServerConfig(int port)
{
snake::AppConfig appConfig;
appConfig.port = port;
appConfig.hostname = "127.0.0.1";
appConfig.verbose = true;
appConfig.redisPort = getFreePort();
appConfig.redisPassword = "";
appConfig.redisHosts.push_back("localhost"); // only one host supported now
std::string appsConfigPath("appsConfig.json");
// Parse config file
auto str = readAsString(appsConfigPath);
if (str.empty())
{
std::cout << "Cannot read content of " << appsConfigPath << std::endl;
return appConfig;
}
std::cout << str << std::endl;
auto apps = nlohmann::json::parse(str);
appConfig.apps = apps["apps"];
// Display config on the terminal for debugging
dumpConfig(appConfig);
return appConfig;
}
2019-09-23 19:25:23 +02:00
} // namespace ix