2019-02-22 06:24:53 +01:00
|
|
|
/*
|
|
|
|
* ws_transfer.cpp
|
|
|
|
* Author: Benjamin Sergeant
|
|
|
|
* Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <ixwebsocket/IXWebSocketServer.h>
|
2019-09-23 19:25:23 +02:00
|
|
|
#include <sstream>
|
2019-02-22 06:24:53 +01:00
|
|
|
|
|
|
|
namespace ix
|
|
|
|
{
|
2019-03-14 07:09:45 +01:00
|
|
|
int ws_transfer_main(int port, const std::string& hostname)
|
2019-02-22 06:24:53 +01:00
|
|
|
{
|
2019-09-30 02:21:52 +02:00
|
|
|
std::cout << "ws_transfer: Listening on " << hostname << ":" << port << std::endl;
|
2019-02-22 06:24:53 +01:00
|
|
|
|
2019-03-14 07:09:45 +01:00
|
|
|
ix::WebSocketServer server(port, hostname);
|
2019-02-22 06:24:53 +01:00
|
|
|
|
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 WebSocketMessagePtr& msg) {
|
|
|
|
if (msg->type == ix::WebSocketMessageType::Open)
|
|
|
|
{
|
2019-09-30 02:21:52 +02:00
|
|
|
std::cerr << "ws_transfer: New connection" << std::endl;
|
2019-09-23 19:25:23 +02:00
|
|
|
std::cerr << "id: " << connectionState->getId() << std::endl;
|
|
|
|
std::cerr << "Uri: " << msg->openInfo.uri << std::endl;
|
|
|
|
std::cerr << "Headers:" << std::endl;
|
|
|
|
for (auto it : msg->openInfo.headers)
|
2019-02-22 06:24:53 +01:00
|
|
|
{
|
2019-09-23 19:25:23 +02:00
|
|
|
std::cerr << it.first << ": " << it.second << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (msg->type == ix::WebSocketMessageType::Close)
|
|
|
|
{
|
2019-09-30 02:21:52 +02:00
|
|
|
std::cerr << "ws_transfer: [client " << connectionState->getId()
|
|
|
|
<< "]: Closed connection, code " << msg->closeInfo.code << " reason "
|
2019-09-23 19:25:23 +02:00
|
|
|
<< msg->closeInfo.reason << std::endl;
|
2019-09-30 02:21:52 +02:00
|
|
|
auto remaining = server.getClients().erase(webSocket);
|
|
|
|
std::cerr << "ws_transfer: " << remaining << " remaining clients " << std::endl;
|
2019-09-23 19:25:23 +02:00
|
|
|
}
|
|
|
|
else if (msg->type == ix::WebSocketMessageType::Error)
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
2019-09-30 02:21:52 +02:00
|
|
|
ss << "ws_transfer: Connection error: " << msg->errorInfo.reason << std::endl;
|
2019-09-23 19:25:23 +02:00
|
|
|
ss << "#retries: " << msg->errorInfo.retries << std::endl;
|
|
|
|
ss << "Wait time(ms): " << msg->errorInfo.wait_time << std::endl;
|
|
|
|
ss << "HTTP Status: " << msg->errorInfo.http_status << std::endl;
|
|
|
|
std::cerr << ss.str();
|
|
|
|
}
|
|
|
|
else if (msg->type == ix::WebSocketMessageType::Fragment)
|
|
|
|
{
|
2019-09-30 02:21:52 +02:00
|
|
|
std::cerr << "ws_transfer: Received message fragment " << std::endl;
|
2019-09-23 19:25:23 +02:00
|
|
|
}
|
|
|
|
else if (msg->type == ix::WebSocketMessageType::Message)
|
|
|
|
{
|
2019-09-30 02:21:52 +02:00
|
|
|
std::cerr << "ws_transfer: Received " << msg->wireSize << " bytes" << std::endl;
|
|
|
|
size_t receivers = 0;
|
2019-09-23 19:25:23 +02:00
|
|
|
for (auto&& client : server.getClients())
|
|
|
|
{
|
|
|
|
if (client != webSocket)
|
2019-02-22 06:24:53 +01:00
|
|
|
{
|
2019-09-30 02:21:52 +02:00
|
|
|
auto readyState = client->getReadyState();
|
|
|
|
if (readyState == ReadyState::Open)
|
|
|
|
{
|
|
|
|
++receivers;
|
|
|
|
client->send(msg->str,
|
|
|
|
msg->binary,
|
|
|
|
[id = connectionState->getId()](int current,
|
|
|
|
int total) -> bool {
|
|
|
|
std::cerr << "ws_transfer: [client " << id
|
|
|
|
<< "]: Step " << current << " out of "
|
|
|
|
<< total << std::endl;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
size_t bufferedAmount = client->bufferedAmount();
|
|
|
|
std::cerr << "ws_transfer: [client " << connectionState->getId()
|
|
|
|
<< "]: " << bufferedAmount
|
|
|
|
<< " bytes left to be sent, " << std::endl;
|
|
|
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
2019-03-14 07:09:45 +01:00
|
|
|
|
2019-09-30 02:21:52 +02:00
|
|
|
} while (client->bufferedAmount() != 0 &&
|
|
|
|
client->getReadyState() == ReadyState::Open);
|
|
|
|
}
|
|
|
|
else
|
2019-09-23 19:25:23 +02:00
|
|
|
{
|
2019-09-30 02:21:52 +02:00
|
|
|
std::string readyStateString =
|
|
|
|
readyState == ReadyState::Connecting
|
|
|
|
? "Connecting"
|
|
|
|
: readyState == ReadyState::Closing ? "Closing" : "Closed";
|
2019-09-23 19:25:23 +02:00
|
|
|
size_t bufferedAmount = client->bufferedAmount();
|
2019-09-30 02:21:52 +02:00
|
|
|
std::cerr << "ws_transfer: [client " << connectionState->getId()
|
|
|
|
<< "]: has readystate '" << readyStateString << "' and "
|
|
|
|
<< bufferedAmount << " bytes left to be sent, "
|
|
|
|
<< std::endl;
|
|
|
|
}
|
2019-02-22 06:24:53 +01:00
|
|
|
}
|
|
|
|
}
|
2019-09-30 02:21:52 +02:00
|
|
|
if (!receivers)
|
|
|
|
{
|
|
|
|
std::cerr << "ws_transfer: no remaining receivers" << std::endl;
|
|
|
|
}
|
2019-09-23 19:25:23 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2019-02-22 06:24:53 +01:00
|
|
|
|
|
|
|
auto res = server.listen();
|
|
|
|
if (!res.first)
|
|
|
|
{
|
|
|
|
std::cerr << res.second << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
server.start();
|
|
|
|
server.wait();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2019-09-23 19:25:23 +02:00
|
|
|
} // namespace ix
|