Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
217d0650f4 | ||
|
|
45d7bb34d7 | ||
|
|
2e32319236 | ||
|
|
8eb0d0b7c3 | ||
|
|
f18f04c0ee | ||
|
|
193da820b2 | ||
|
|
c6198305d4 | ||
|
|
c77d6ae3f5 | ||
|
|
c72b2dbd6b | ||
|
|
835523f77b | ||
|
|
ec8a35b587 | ||
|
|
aca18995d1 | ||
|
|
f9178f58aa | ||
|
|
2477946e68 | ||
|
|
7c4d040384 |
12
.pre-commit-config.yaml
Normal file
12
.pre-commit-config.yaml
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
repos:
|
||||||
|
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||||
|
rev: v2.3.0
|
||||||
|
hooks:
|
||||||
|
- id: check-yaml
|
||||||
|
- id: end-of-file-fixer
|
||||||
|
- id: trailing-whitespace
|
||||||
|
|
||||||
|
- repo: https://github.com/pocc/pre-commit-hooks
|
||||||
|
rev: ''
|
||||||
|
hooks:
|
||||||
|
- id: clang-format
|
||||||
20
.travis.yml
20
.travis.yml
@@ -15,15 +15,17 @@ matrix:
|
|||||||
- python test/run.py
|
- python test/run.py
|
||||||
- make ws
|
- make ws
|
||||||
|
|
||||||
# # Linux
|
# Linux
|
||||||
# - os: linux
|
- os: linux
|
||||||
# dist: xenial
|
dist: bionic
|
||||||
# script:
|
before_install:
|
||||||
# - python test/run.py
|
- sudo apt-get install -y libmbedtls-dev
|
||||||
# - make ws
|
script:
|
||||||
# env:
|
- python test/run.py
|
||||||
# - CC=gcc
|
- make ws
|
||||||
# - CXX=g++
|
env:
|
||||||
|
- CC=gcc
|
||||||
|
- CXX=g++
|
||||||
|
|
||||||
# Clang + Linux disabled for now
|
# Clang + Linux disabled for now
|
||||||
# - os: linux
|
# - os: linux
|
||||||
|
|||||||
21
CHANGELOG.md
21
CHANGELOG.md
@@ -1,6 +1,27 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
All notable changes to this project will be documented in this file.
|
All notable changes to this project will be documented in this file.
|
||||||
|
|
||||||
|
## [5.0.7] - 2019-08-23
|
||||||
|
- WebSocket: add new option to pass in extra HTTP headers when connecting.
|
||||||
|
- `ws connect` add new option (-H, works like [curl](https://stackoverflow.com/questions/356705/how-to-send-a-header-using-a-http-request-through-a-curl-call)) to pass in extra HTTP headers when connecting
|
||||||
|
|
||||||
|
If you run against `ws echo_server` you will see the headers being received printed in the terminal.
|
||||||
|
```
|
||||||
|
ws connect -H "foo: bar" -H "baz: buz" ws://127.0.0.1:8008
|
||||||
|
```
|
||||||
|
|
||||||
|
- CobraConnection: sets a unique id field for all messages sent to [cobra](https://github.com/machinezone/cobra).
|
||||||
|
- CobraConnection: sets a counter as a field for each event published.
|
||||||
|
|
||||||
|
## [5.0.6] - 2019-08-22
|
||||||
|
- Windows: silly compile error (poll should be in the global namespace)
|
||||||
|
|
||||||
|
## [5.0.5] - 2019-08-22
|
||||||
|
- Windows: use select instead of WSAPoll, through a poll wrapper
|
||||||
|
|
||||||
|
## [5.0.4] - 2019-08-20
|
||||||
|
- Windows build fixes (there was a problem with the use of ::poll that has a different name on Windows (WSAPoll))
|
||||||
|
|
||||||
## [5.0.3] - 2019-08-14
|
## [5.0.3] - 2019-08-14
|
||||||
- CobraMetricThreadedPublisher _enable flag is an atomic, and CobraMetricsPublisher is enabled by default
|
- CobraMetricThreadedPublisher _enable flag is an atomic, and CobraMetricsPublisher is enabled by default
|
||||||
|
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
5.0.3
|
5.0.7
|
||||||
|
|||||||
43
README.md
43
README.md
@@ -16,9 +16,32 @@
|
|||||||
|
|
||||||
The [*ws*](https://github.com/machinezone/IXWebSocket/tree/master/ws) folder countains many interactive programs for chat, [file transfers](https://github.com/machinezone/IXWebSocket/blob/master/ws/ws_send.cpp), [curl like](https://github.com/machinezone/IXWebSocket/blob/master/ws/ws_http_client.cpp) http clients, demonstrating client and server usage.
|
The [*ws*](https://github.com/machinezone/IXWebSocket/tree/master/ws) folder countains many interactive programs for chat, [file transfers](https://github.com/machinezone/IXWebSocket/blob/master/ws/ws_send.cpp), [curl like](https://github.com/machinezone/IXWebSocket/blob/master/ws/ws_http_client.cpp) http clients, demonstrating client and server usage.
|
||||||
|
|
||||||
|
### Windows note
|
||||||
|
|
||||||
|
To use the network system on Windows, you need to initialize it once with *WSAStartup()* and clean it up with *WSACleanup()*. We have helpers for that which you can use, see below. This init would typically take place in your main function.
|
||||||
|
|
||||||
|
```
|
||||||
|
#include <ixwebsocket/IXNetSystem.h>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
ix::initNetSystem();
|
||||||
|
|
||||||
|
...
|
||||||
|
|
||||||
|
ix::uninitNetSystem();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### WebSocket client API
|
### WebSocket client API
|
||||||
|
|
||||||
```
|
```
|
||||||
|
#include <ixwebsocket/IXWebSocket.h>
|
||||||
|
|
||||||
|
...
|
||||||
|
|
||||||
|
# Our websocket object
|
||||||
ix::WebSocket webSocket;
|
ix::WebSocket webSocket;
|
||||||
|
|
||||||
std::string url("ws://localhost:8080/");
|
std::string url("ws://localhost:8080/");
|
||||||
@@ -59,6 +82,10 @@ webSocket.stop()
|
|||||||
### WebSocket server API
|
### WebSocket server API
|
||||||
|
|
||||||
```
|
```
|
||||||
|
#include <ixwebsocket/IXWebSocketServer.h>
|
||||||
|
|
||||||
|
...
|
||||||
|
|
||||||
// Run a server on localhost at a given port.
|
// Run a server on localhost at a given port.
|
||||||
// Bound host name, max connections and listen backlog can also be passed in as parameters.
|
// Bound host name, max connections and listen backlog can also be passed in as parameters.
|
||||||
ix::WebSocketServer server(port);
|
ix::WebSocketServer server(port);
|
||||||
@@ -120,6 +147,10 @@ server.wait();
|
|||||||
### HTTP client API
|
### HTTP client API
|
||||||
|
|
||||||
```
|
```
|
||||||
|
#include <ixwebsocket/IXHttpClient.h>
|
||||||
|
|
||||||
|
...
|
||||||
|
|
||||||
//
|
//
|
||||||
// Preparation
|
// Preparation
|
||||||
//
|
//
|
||||||
@@ -199,6 +230,8 @@ bool ok = httpClient.performRequest(args, [](const HttpResponsePtr& response)
|
|||||||
### HTTP server API
|
### HTTP server API
|
||||||
|
|
||||||
```
|
```
|
||||||
|
#include <ixwebsocket/IXHttpServer.h>
|
||||||
|
|
||||||
ix::HttpServer server(port, hostname);
|
ix::HttpServer server(port, hostname);
|
||||||
|
|
||||||
auto res = server.listen();
|
auto res = server.listen();
|
||||||
@@ -477,3 +510,13 @@ idle connection.
|
|||||||
```
|
```
|
||||||
webSocket.setHeartBeatPeriod(45);
|
webSocket.setHeartBeatPeriod(45);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Supply extra HTTP headers.
|
||||||
|
|
||||||
|
You can set extra HTTP headers to be sent during the WebSocket handshake.
|
||||||
|
|
||||||
|
```
|
||||||
|
WebSocketHttpHeaders headers;
|
||||||
|
headers["foo"] = "bar";
|
||||||
|
webSocket.setExtraHeaders(headers);
|
||||||
|
```
|
||||||
|
|||||||
@@ -15,9 +15,8 @@ namespace ix
|
|||||||
WSADATA wsaData;
|
WSADATA wsaData;
|
||||||
int err;
|
int err;
|
||||||
|
|
||||||
/* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */
|
// Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h
|
||||||
wVersionRequested = MAKEWORD(2, 2);
|
wVersionRequested = MAKEWORD(2, 2);
|
||||||
|
|
||||||
err = WSAStartup(wVersionRequested, &wsaData);
|
err = WSAStartup(wVersionRequested, &wsaData);
|
||||||
|
|
||||||
return err == 0;
|
return err == 0;
|
||||||
@@ -30,10 +29,83 @@ namespace ix
|
|||||||
{
|
{
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
int err = WSACleanup();
|
int err = WSACleanup();
|
||||||
|
|
||||||
return err == 0;
|
return err == 0;
|
||||||
#else
|
#else
|
||||||
return true;
|
return true;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This function should be in the global namespace
|
||||||
|
#ifdef _WIN32
|
||||||
|
//
|
||||||
|
// That function could 'return WSAPoll(pfd, nfds, timeout);'
|
||||||
|
// but WSAPoll is said to have weird behaviors on the internet
|
||||||
|
// (the curl folks have had problems with it).
|
||||||
|
//
|
||||||
|
// So we make it a select wrapper
|
||||||
|
//
|
||||||
|
int poll(struct pollfd *fds, nfds_t nfds, int timeout)
|
||||||
|
{
|
||||||
|
int maxfd = 0;
|
||||||
|
fd_set readfds, writefds, errorfds;
|
||||||
|
FD_ZERO(&readfds);
|
||||||
|
FD_ZERO(&writefds);
|
||||||
|
FD_ZERO(&errorfds);
|
||||||
|
|
||||||
|
for (nfds_t i = 0; i < nfds; ++i)
|
||||||
|
{
|
||||||
|
struct pollfd *fd = &fds[i];
|
||||||
|
|
||||||
|
if (fd->fd > maxfd)
|
||||||
|
{
|
||||||
|
maxfd = fd->fd;
|
||||||
|
}
|
||||||
|
if ((fd->events & POLLIN))
|
||||||
|
{
|
||||||
|
FD_SET(fd->fd, &readfds);
|
||||||
|
}
|
||||||
|
if ((fd->events & POLLOUT))
|
||||||
|
{
|
||||||
|
FD_SET(fd->fd, &writefds);
|
||||||
|
}
|
||||||
|
if ((fd->events & POLLERR))
|
||||||
|
{
|
||||||
|
FD_SET(fd->fd, &errorfds);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct timeval tv;
|
||||||
|
tv.tv_sec = timeout / 1000;
|
||||||
|
tv.tv_usec = (timeout % 1000) * 1000;
|
||||||
|
|
||||||
|
int ret = select(maxfd + 1, &readfds, &writefds, &errorfds,
|
||||||
|
timeout != -1 ? &tv : NULL);
|
||||||
|
|
||||||
|
if (ret < 0)
|
||||||
|
{
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (nfds_t i = 0; i < nfds; ++i)
|
||||||
|
{
|
||||||
|
struct pollfd *fd = &fds[i];
|
||||||
|
fd->revents = 0;
|
||||||
|
|
||||||
|
if (FD_ISSET(fd->fd, &readfds))
|
||||||
|
{
|
||||||
|
fd->revents |= POLLIN;
|
||||||
|
}
|
||||||
|
if (FD_ISSET(fd->fd, &writefds))
|
||||||
|
{
|
||||||
|
fd->revents |= POLLOUT;
|
||||||
|
}
|
||||||
|
if (FD_ISSET(fd->fd, &errorfds))
|
||||||
|
{
|
||||||
|
fd->revents |= POLLERR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|||||||
@@ -12,11 +12,18 @@
|
|||||||
#include <basetsd.h>
|
#include <basetsd.h>
|
||||||
#include <io.h>
|
#include <io.h>
|
||||||
#include <ws2def.h>
|
#include <ws2def.h>
|
||||||
|
|
||||||
|
// Define our own poll on Windows
|
||||||
|
typedef unsigned long int nfds_t;
|
||||||
|
|
||||||
|
int poll(struct pollfd* fds, nfds_t nfds, int timeout);
|
||||||
|
|
||||||
#else
|
#else
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
#include <netinet/tcp.h>
|
#include <netinet/tcp.h>
|
||||||
|
#include <poll.h>
|
||||||
#include <sys/select.h>
|
#include <sys/select.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
|||||||
@@ -17,7 +17,6 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <poll.h>
|
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
@@ -59,7 +58,7 @@ namespace ix
|
|||||||
// shim to fallback to select on those platforms.
|
// shim to fallback to select on those platforms.
|
||||||
// See https://github.com/mpv-player/mpv/pull/5203/files for such a select wrapper.
|
// See https://github.com/mpv-player/mpv/pull/5203/files for such a select wrapper.
|
||||||
//
|
//
|
||||||
int nfds = 1;
|
nfds_t nfds = 1;
|
||||||
struct pollfd fds[2];
|
struct pollfd fds[2];
|
||||||
|
|
||||||
fds[0].fd = sockfd;
|
fds[0].fd = sockfd;
|
||||||
|
|||||||
@@ -70,6 +70,11 @@ namespace ix
|
|||||||
std::lock_guard<std::mutex> lock(_configMutex);
|
std::lock_guard<std::mutex> lock(_configMutex);
|
||||||
_url = url;
|
_url = url;
|
||||||
}
|
}
|
||||||
|
void WebSocket::setExtraHeaders(const WebSocketHttpHeaders& headers)
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(_configMutex);
|
||||||
|
_extraHeaders = headers;
|
||||||
|
}
|
||||||
|
|
||||||
const std::string& WebSocket::getUrl() const
|
const std::string& WebSocket::getUrl() const
|
||||||
{
|
{
|
||||||
@@ -176,7 +181,7 @@ namespace ix
|
|||||||
_pingTimeoutSecs);
|
_pingTimeoutSecs);
|
||||||
}
|
}
|
||||||
|
|
||||||
WebSocketInitResult status = _ws.connectToUrl(_url, timeoutSecs);
|
WebSocketInitResult status = _ws.connectToUrl(_url, _extraHeaders, timeoutSecs);
|
||||||
if (!status.success)
|
if (!status.success)
|
||||||
{
|
{
|
||||||
return status;
|
return status;
|
||||||
|
|||||||
@@ -44,6 +44,9 @@ namespace ix
|
|||||||
~WebSocket();
|
~WebSocket();
|
||||||
|
|
||||||
void setUrl(const std::string& url);
|
void setUrl(const std::string& url);
|
||||||
|
|
||||||
|
// send extra headers in client handshake request
|
||||||
|
void setExtraHeaders(const WebSocketHttpHeaders& headers);
|
||||||
void setPerMessageDeflateOptions(
|
void setPerMessageDeflateOptions(
|
||||||
const WebSocketPerMessageDeflateOptions& perMessageDeflateOptions);
|
const WebSocketPerMessageDeflateOptions& perMessageDeflateOptions);
|
||||||
void setHeartBeatPeriod(int heartBeatPeriodSecs);
|
void setHeartBeatPeriod(int heartBeatPeriodSecs);
|
||||||
@@ -111,6 +114,8 @@ namespace ix
|
|||||||
WebSocketTransport _ws;
|
WebSocketTransport _ws;
|
||||||
|
|
||||||
std::string _url;
|
std::string _url;
|
||||||
|
WebSocketHttpHeaders _extraHeaders;
|
||||||
|
|
||||||
WebSocketPerMessageDeflateOptions _perMessageDeflateOptions;
|
WebSocketPerMessageDeflateOptions _perMessageDeflateOptions;
|
||||||
mutable std::mutex _configMutex; // protect all config variables access
|
mutable std::mutex _configMutex; // protect all config variables access
|
||||||
|
|
||||||
|
|||||||
@@ -88,6 +88,7 @@ namespace ix
|
|||||||
}
|
}
|
||||||
|
|
||||||
WebSocketInitResult WebSocketHandshake::clientHandshake(const std::string& url,
|
WebSocketInitResult WebSocketHandshake::clientHandshake(const std::string& url,
|
||||||
|
const WebSocketHttpHeaders& extraHeaders,
|
||||||
const std::string& host,
|
const std::string& host,
|
||||||
const std::string& path,
|
const std::string& path,
|
||||||
int port,
|
int port,
|
||||||
@@ -127,6 +128,9 @@ namespace ix
|
|||||||
ss << "Sec-WebSocket-Version: 13\r\n";
|
ss << "Sec-WebSocket-Version: 13\r\n";
|
||||||
ss << "Sec-WebSocket-Key: " << secWebSocketKey << "\r\n";
|
ss << "Sec-WebSocket-Key: " << secWebSocketKey << "\r\n";
|
||||||
|
|
||||||
|
for (auto& it : extraHeaders) {
|
||||||
|
ss << it.first << ":" << it.second << "\r\n";
|
||||||
|
}
|
||||||
if (_enablePerMessageDeflate)
|
if (_enablePerMessageDeflate)
|
||||||
{
|
{
|
||||||
ss << _perMessageDeflateOptions.generateHeader();
|
ss << _perMessageDeflateOptions.generateHeader();
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
{
|
{
|
||||||
@@ -50,11 +51,13 @@ namespace ix
|
|||||||
WebSocketPerMessageDeflateOptions& perMessageDeflateOptions,
|
WebSocketPerMessageDeflateOptions& perMessageDeflateOptions,
|
||||||
std::atomic<bool>& enablePerMessageDeflate);
|
std::atomic<bool>& enablePerMessageDeflate);
|
||||||
|
|
||||||
WebSocketInitResult clientHandshake(const std::string& url,
|
WebSocketInitResult clientHandshake(
|
||||||
const std::string& host,
|
const std::string& url,
|
||||||
const std::string& path,
|
const WebSocketHttpHeaders& extraHeaders,
|
||||||
int port,
|
const std::string& host,
|
||||||
int timeoutSecs);
|
const std::string& path,
|
||||||
|
int port,
|
||||||
|
int timeoutSecs);
|
||||||
|
|
||||||
WebSocketInitResult serverHandshake(int fd, int timeoutSecs);
|
WebSocketInitResult serverHandshake(int fd, int timeoutSecs);
|
||||||
|
|
||||||
|
|||||||
@@ -127,8 +127,10 @@ namespace ix
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Client
|
// Client
|
||||||
WebSocketInitResult WebSocketTransport::connectToUrl(const std::string& url,
|
WebSocketInitResult WebSocketTransport::connectToUrl(
|
||||||
int timeoutSecs)
|
const std::string& url,
|
||||||
|
const WebSocketHttpHeaders& headers,
|
||||||
|
int timeoutSecs)
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(_socketMutex);
|
std::lock_guard<std::mutex> lock(_socketMutex);
|
||||||
|
|
||||||
@@ -156,8 +158,8 @@ namespace ix
|
|||||||
_perMessageDeflateOptions,
|
_perMessageDeflateOptions,
|
||||||
_enablePerMessageDeflate);
|
_enablePerMessageDeflate);
|
||||||
|
|
||||||
auto result = webSocketHandshake.clientHandshake(url, host, path, port,
|
auto result = webSocketHandshake.clientHandshake(url, headers, host, path,
|
||||||
timeoutSecs);
|
port, timeoutSecs);
|
||||||
if (result.success)
|
if (result.success)
|
||||||
{
|
{
|
||||||
setReadyState(ReadyState::OPEN);
|
setReadyState(ReadyState::OPEN);
|
||||||
|
|||||||
@@ -24,6 +24,7 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
@@ -75,8 +76,10 @@ namespace ix
|
|||||||
int pingIntervalSecs,
|
int pingIntervalSecs,
|
||||||
int pingTimeoutSecs);
|
int pingTimeoutSecs);
|
||||||
|
|
||||||
WebSocketInitResult connectToUrl(const std::string& url, // Client
|
WebSocketInitResult connectToUrl( // Client
|
||||||
int timeoutSecs);
|
const std::string& url,
|
||||||
|
const WebSocketHttpHeaders& headers,
|
||||||
|
int timeoutSecs);
|
||||||
WebSocketInitResult connectToSocket(int fd, // Server
|
WebSocketInitResult connectToSocket(int fd, // Server
|
||||||
int timeoutSecs);
|
int timeoutSecs);
|
||||||
|
|
||||||
|
|||||||
3
makefile
3
makefile
@@ -17,6 +17,9 @@ ws:
|
|||||||
uninstall:
|
uninstall:
|
||||||
xargs rm -fv < build/install_manifest.txt
|
xargs rm -fv < build/install_manifest.txt
|
||||||
|
|
||||||
|
tag:
|
||||||
|
git tag v"`cat DOCKER_VERSION`"
|
||||||
|
|
||||||
.PHONY: docker
|
.PHONY: docker
|
||||||
|
|
||||||
NAME := bsergean/ws
|
NAME := bsergean/ws
|
||||||
|
|||||||
@@ -24,7 +24,8 @@ namespace ix
|
|||||||
_webSocket(new WebSocket()),
|
_webSocket(new WebSocket()),
|
||||||
_publishMode(CobraConnection_PublishMode_Immediate),
|
_publishMode(CobraConnection_PublishMode_Immediate),
|
||||||
_authenticated(false),
|
_authenticated(false),
|
||||||
_eventCallback(nullptr)
|
_eventCallback(nullptr),
|
||||||
|
_id(0)
|
||||||
{
|
{
|
||||||
_pdu["action"] = "rtm/publish";
|
_pdu["action"] = "rtm/publish";
|
||||||
|
|
||||||
@@ -244,6 +245,7 @@ namespace ix
|
|||||||
Json::Value pdu;
|
Json::Value pdu;
|
||||||
pdu["action"] = "auth/handshake";
|
pdu["action"] = "auth/handshake";
|
||||||
pdu["body"] = body;
|
pdu["body"] = body;
|
||||||
|
pdu["id"] = _id++;
|
||||||
|
|
||||||
std::string serializedJson = serializeJson(pdu);
|
std::string serializedJson = serializeJson(pdu);
|
||||||
CobraConnection::invokeTrafficTrackerCallback(serializedJson.size(), false);
|
CobraConnection::invokeTrafficTrackerCallback(serializedJson.size(), false);
|
||||||
@@ -306,6 +308,7 @@ namespace ix
|
|||||||
Json::Value pdu;
|
Json::Value pdu;
|
||||||
pdu["action"] = "auth/authenticate";
|
pdu["action"] = "auth/authenticate";
|
||||||
pdu["body"] = body;
|
pdu["body"] = body;
|
||||||
|
pdu["id"] = _id++;
|
||||||
|
|
||||||
std::string serializedJson = serializeJson(pdu);
|
std::string serializedJson = serializeJson(pdu);
|
||||||
CobraConnection::invokeTrafficTrackerCallback(serializedJson.size(), false);
|
CobraConnection::invokeTrafficTrackerCallback(serializedJson.size(), false);
|
||||||
@@ -402,6 +405,7 @@ namespace ix
|
|||||||
_body["channels"] = channels;
|
_body["channels"] = channels;
|
||||||
_body["message"] = msg;
|
_body["message"] = msg;
|
||||||
_pdu["body"] = _body;
|
_pdu["body"] = _body;
|
||||||
|
_pdu["id"] = _id++;
|
||||||
|
|
||||||
std::string serializedJson = serializeJson(_pdu);
|
std::string serializedJson = serializeJson(_pdu);
|
||||||
|
|
||||||
@@ -444,6 +448,7 @@ namespace ix
|
|||||||
Json::Value pdu;
|
Json::Value pdu;
|
||||||
pdu["action"] = "rtm/subscribe";
|
pdu["action"] = "rtm/subscribe";
|
||||||
pdu["body"] = body;
|
pdu["body"] = body;
|
||||||
|
pdu["id"] = _id++;
|
||||||
|
|
||||||
_webSocket->send(pdu.toStyledString());
|
_webSocket->send(pdu.toStyledString());
|
||||||
|
|
||||||
@@ -469,6 +474,7 @@ namespace ix
|
|||||||
Json::Value pdu;
|
Json::Value pdu;
|
||||||
pdu["action"] = "rtm/unsubscribe";
|
pdu["action"] = "rtm/unsubscribe";
|
||||||
pdu["body"] = body;
|
pdu["body"] = body;
|
||||||
|
pdu["id"] = _id++;
|
||||||
|
|
||||||
_webSocket->send(pdu.toStyledString());
|
_webSocket->send(pdu.toStyledString());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -168,6 +168,9 @@ namespace ix
|
|||||||
|
|
||||||
// Cap the queue size (100 elems so far -> ~100k)
|
// Cap the queue size (100 elems so far -> ~100k)
|
||||||
static constexpr size_t kQueueMaxSize = 256;
|
static constexpr size_t kQueueMaxSize = 256;
|
||||||
|
|
||||||
|
// Each pdu sent should have an incremental unique id
|
||||||
|
std::atomic<uint64_t> _id;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace ix
|
} // namespace ix
|
||||||
|
|||||||
@@ -191,6 +191,19 @@ namespace ix
|
|||||||
msg["device"] = _device;
|
msg["device"] = _device;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Bump a counter for each id
|
||||||
|
// This is used to make sure that we are not
|
||||||
|
// dropping messages, by checking that all the ids is the list of
|
||||||
|
// all natural numbers until the last value sent (0, 1, 2, ..., N)
|
||||||
|
//
|
||||||
|
std::lock_guard<std::mutex> lock(_device_mutex);
|
||||||
|
auto it = _counters.emplace(id, 0);
|
||||||
|
msg["per_id_counter"] = it.first->second;
|
||||||
|
it.first->second += 1;
|
||||||
|
}
|
||||||
|
|
||||||
// Now actually enqueue the task
|
// Now actually enqueue the task
|
||||||
_cobra_metrics_theaded_publisher.push(msg);
|
_cobra_metrics_theaded_publisher.push(msg);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,10 +7,10 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "IXCobraMetricsThreadedPublisher.h"
|
#include "IXCobraMetricsThreadedPublisher.h"
|
||||||
|
#include <atomic>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <jsoncpp/json/json.h>
|
#include <jsoncpp/json/json.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <atomic>
|
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
@@ -151,6 +151,10 @@ namespace ix
|
|||||||
_last_update;
|
_last_update;
|
||||||
mutable std::mutex _last_update_mutex; // protect access to _last_update
|
mutable std::mutex _last_update_mutex; // protect access to _last_update
|
||||||
|
|
||||||
|
/// Bump a counter for each metric type
|
||||||
|
std::unordered_map<std::string, int> _counters;
|
||||||
|
mutable std::mutex _counters_mutex; // protect access to _counters
|
||||||
|
|
||||||
// const strings for internal ids
|
// const strings for internal ids
|
||||||
static const std::string kSetRateControlId;
|
static const std::string kSetRateControlId;
|
||||||
static const std::string kSetBlacklistId;
|
static const std::string kSetBlacklistId;
|
||||||
|
|||||||
@@ -109,6 +109,7 @@ int main(int argc, char** argv)
|
|||||||
|
|
||||||
CLI::App* connectApp = app.add_subcommand("connect", "Connect to a remote server");
|
CLI::App* connectApp = app.add_subcommand("connect", "Connect to a remote server");
|
||||||
connectApp->add_option("url", url, "Connection url")->required();
|
connectApp->add_option("url", url, "Connection url")->required();
|
||||||
|
connectApp->add_option("-H", headers, "Header")->join();
|
||||||
connectApp->add_flag("-d", disableAutomaticReconnection, "Disable Automatic Reconnection");
|
connectApp->add_flag("-d", disableAutomaticReconnection, "Disable Automatic Reconnection");
|
||||||
connectApp->add_flag("-x", disablePerMessageDeflate, "Disable per message deflate");
|
connectApp->add_flag("-x", disablePerMessageDeflate, "Disable per message deflate");
|
||||||
connectApp->add_flag("-b", binaryMode, "Send in binary mode");
|
connectApp->add_flag("-b", binaryMode, "Send in binary mode");
|
||||||
@@ -252,7 +253,7 @@ int main(int argc, char** argv)
|
|||||||
}
|
}
|
||||||
else if (app.got_subcommand("connect"))
|
else if (app.got_subcommand("connect"))
|
||||||
{
|
{
|
||||||
ret = ix::ws_connect_main(url, disableAutomaticReconnection,
|
ret = ix::ws_connect_main(url, headers, disableAutomaticReconnection,
|
||||||
disablePerMessageDeflate, binaryMode);
|
disablePerMessageDeflate, binaryMode);
|
||||||
}
|
}
|
||||||
else if (app.got_subcommand("chat"))
|
else if (app.got_subcommand("chat"))
|
||||||
|
|||||||
1
ws/ws.h
1
ws/ws.h
@@ -31,6 +31,7 @@ namespace ix
|
|||||||
int ws_chat_main(const std::string& url, const std::string& user);
|
int ws_chat_main(const std::string& url, const std::string& user);
|
||||||
|
|
||||||
int ws_connect_main(const std::string& url,
|
int ws_connect_main(const std::string& url,
|
||||||
|
const std::string& headers,
|
||||||
bool disableAutomaticReconnection,
|
bool disableAutomaticReconnection,
|
||||||
bool disablePerMessageDeflate,
|
bool disablePerMessageDeflate,
|
||||||
bool binaryMode);
|
bool binaryMode);
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ namespace ix
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
WebSocketConnect(const std::string& _url,
|
WebSocketConnect(const std::string& _url,
|
||||||
|
const std::string& headers,
|
||||||
bool disableAutomaticReconnection,
|
bool disableAutomaticReconnection,
|
||||||
bool disablePerMessageDeflate,
|
bool disablePerMessageDeflate,
|
||||||
bool binaryMode);
|
bool binaryMode);
|
||||||
@@ -30,14 +31,17 @@ namespace ix
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
std::string _url;
|
std::string _url;
|
||||||
|
WebSocketHttpHeaders _headers;
|
||||||
ix::WebSocket _webSocket;
|
ix::WebSocket _webSocket;
|
||||||
bool _disablePerMessageDeflate;
|
bool _disablePerMessageDeflate;
|
||||||
bool _binaryMode;
|
bool _binaryMode;
|
||||||
|
|
||||||
void log(const std::string& msg);
|
void log(const std::string& msg);
|
||||||
|
WebSocketHttpHeaders parseHeaders(const std::string& data);
|
||||||
};
|
};
|
||||||
|
|
||||||
WebSocketConnect::WebSocketConnect(const std::string& url,
|
WebSocketConnect::WebSocketConnect(const std::string& url,
|
||||||
|
const std::string& headers,
|
||||||
bool disableAutomaticReconnection,
|
bool disableAutomaticReconnection,
|
||||||
bool disablePerMessageDeflate,
|
bool disablePerMessageDeflate,
|
||||||
bool binaryMode) :
|
bool binaryMode) :
|
||||||
@@ -49,6 +53,8 @@ namespace ix
|
|||||||
{
|
{
|
||||||
_webSocket.disableAutomaticReconnection();
|
_webSocket.disableAutomaticReconnection();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_headers = parseHeaders(headers);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebSocketConnect::log(const std::string& msg)
|
void WebSocketConnect::log(const std::string& msg)
|
||||||
@@ -56,6 +62,31 @@ namespace ix
|
|||||||
std::cout << msg << std::endl;
|
std::cout << msg << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
WebSocketHttpHeaders WebSocketConnect::parseHeaders(const std::string& data)
|
||||||
|
{
|
||||||
|
WebSocketHttpHeaders headers;
|
||||||
|
|
||||||
|
// Split by \n
|
||||||
|
std::string token;
|
||||||
|
std::stringstream tokenStream(data);
|
||||||
|
|
||||||
|
while (std::getline(tokenStream, token))
|
||||||
|
{
|
||||||
|
std::size_t pos = token.rfind(':');
|
||||||
|
|
||||||
|
// Bail out if last '.' is found
|
||||||
|
if (pos == std::string::npos) continue;
|
||||||
|
|
||||||
|
auto key = token.substr(0, pos);
|
||||||
|
auto val = token.substr(pos+1);
|
||||||
|
|
||||||
|
std::cerr << key << ": " << val << std::endl;
|
||||||
|
headers[key] = val;
|
||||||
|
}
|
||||||
|
|
||||||
|
return headers;
|
||||||
|
}
|
||||||
|
|
||||||
void WebSocketConnect::stop()
|
void WebSocketConnect::stop()
|
||||||
{
|
{
|
||||||
_webSocket.stop();
|
_webSocket.stop();
|
||||||
@@ -64,6 +95,7 @@ namespace ix
|
|||||||
void WebSocketConnect::start()
|
void WebSocketConnect::start()
|
||||||
{
|
{
|
||||||
_webSocket.setUrl(_url);
|
_webSocket.setUrl(_url);
|
||||||
|
_webSocket.setExtraHeaders(_headers);
|
||||||
|
|
||||||
if (_disablePerMessageDeflate)
|
if (_disablePerMessageDeflate)
|
||||||
{
|
{
|
||||||
@@ -151,12 +183,14 @@ namespace ix
|
|||||||
}
|
}
|
||||||
|
|
||||||
int ws_connect_main(const std::string& url,
|
int ws_connect_main(const std::string& url,
|
||||||
|
const std::string& headers,
|
||||||
bool disableAutomaticReconnection,
|
bool disableAutomaticReconnection,
|
||||||
bool disablePerMessageDeflate,
|
bool disablePerMessageDeflate,
|
||||||
bool binaryMode)
|
bool binaryMode)
|
||||||
{
|
{
|
||||||
std::cout << "Type Ctrl-D to exit prompt..." << std::endl;
|
std::cout << "Type Ctrl-D to exit prompt..." << std::endl;
|
||||||
WebSocketConnect webSocketChat(url,
|
WebSocketConnect webSocketChat(url,
|
||||||
|
headers,
|
||||||
disableAutomaticReconnection,
|
disableAutomaticReconnection,
|
||||||
disablePerMessageDeflate,
|
disablePerMessageDeflate,
|
||||||
binaryMode);
|
binaryMode);
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ namespace ix
|
|||||||
if (pos == std::string::npos) continue;
|
if (pos == std::string::npos) continue;
|
||||||
|
|
||||||
auto key = token.substr(0, pos);
|
auto key = token.substr(0, pos);
|
||||||
auto val = token.substr(pos+2);
|
auto val = token.substr(pos+1);
|
||||||
|
|
||||||
std::cerr << key << ": " << val << std::endl;
|
std::cerr << key << ": " << val << std::endl;
|
||||||
headers[key] = val;
|
headers[key] = val;
|
||||||
|
|||||||
Reference in New Issue
Block a user