Compare commits
16 Commits
Author | SHA1 | Date | |
---|---|---|---|
193da820b2 | |||
c6198305d4 | |||
c77d6ae3f5 | |||
c72b2dbd6b | |||
835523f77b | |||
ec8a35b587 | |||
aca18995d1 | |||
f9178f58aa | |||
2477946e68 | |||
7c4d040384 | |||
197cf8ed36 | |||
dd0d7c268f | |||
b2bfccac0a | |||
8b8b352e61 | |||
0403dd354b | |||
b78b453504 |
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
|
||||||
|
@ -1,6 +1,15 @@
|
|||||||
# 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.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
|
||||||
|
- CobraMetricThreadedPublisher _enable flag is an atomic, and CobraMetricsPublisher is enabled by default
|
||||||
|
|
||||||
## [5.0.2] - 2019-08-01
|
## [5.0.2] - 2019-08-01
|
||||||
- ws cobra_subscribe has a new -q (quiet) option
|
- ws cobra_subscribe has a new -q (quiet) option
|
||||||
- ws cobra_subscribe knows to and display msg stats (count and # of messages received per second)
|
- ws cobra_subscribe knows to and display msg stats (count and # of messages received per second)
|
||||||
|
@ -1 +1 @@
|
|||||||
5.0.1
|
5.0.4
|
||||||
|
51
README.md
51
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.
|
||||||
|
|
||||||
Here is what the client API looks like.
|
### 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
|
||||||
|
|
||||||
|
```
|
||||||
|
#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/");
|
||||||
@ -56,9 +79,13 @@ webSocket.sendBinary("some serialized binary data");
|
|||||||
webSocket.stop()
|
webSocket.stop()
|
||||||
```
|
```
|
||||||
|
|
||||||
Here is what the server API looks like. Note that server support is very recent and subject to changes.
|
### 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);
|
||||||
@ -117,9 +144,13 @@ server.wait();
|
|||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Here is what the HTTP client API looks like.
|
### HTTP client API
|
||||||
|
|
||||||
```
|
```
|
||||||
|
#include <ixwebsocket/IXHttpClient.h>
|
||||||
|
|
||||||
|
...
|
||||||
|
|
||||||
//
|
//
|
||||||
// Preparation
|
// Preparation
|
||||||
//
|
//
|
||||||
@ -170,7 +201,7 @@ out = httpClient.post(url, std::string("foo=bar"), args);
|
|||||||
//
|
//
|
||||||
// Result
|
// Result
|
||||||
//
|
//
|
||||||
auto errorCode = response->errorCode; // Can be HttpErrorCode::Ok, HttpErrorCode::UrlMalformed, etc...
|
auto statusCode = response->statusCode; // Can be HttpErrorCode::Ok, HttpErrorCode::UrlMalformed, etc...
|
||||||
auto errorCode = response->errorCode; // 200, 404, etc...
|
auto errorCode = response->errorCode; // 200, 404, etc...
|
||||||
auto responseHeaders = response->headers; // All the headers in a special case-insensitive unordered_map of (string, string)
|
auto responseHeaders = response->headers; // All the headers in a special case-insensitive unordered_map of (string, string)
|
||||||
auto payload = response->payload; // All the bytes from the response as an std::string
|
auto payload = response->payload; // All the bytes from the response as an std::string
|
||||||
@ -196,9 +227,11 @@ bool ok = httpClient.performRequest(args, [](const HttpResponsePtr& response)
|
|||||||
// ok will be false if your httpClient is not async
|
// ok will be false if your httpClient is not async
|
||||||
```
|
```
|
||||||
|
|
||||||
Here is what the HTTP server API looks like. Note that HTTP server support is very, very recent and subject to changes.
|
### 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();
|
||||||
@ -243,7 +276,7 @@ CMakefiles for the library and the examples are available. This library has few
|
|||||||
```
|
```
|
||||||
mkdir build # make a build dir so that you can build out of tree.
|
mkdir build # make a build dir so that you can build out of tree.
|
||||||
cd build
|
cd build
|
||||||
cmake ..
|
cmake -DUSE_TLS=1 ..
|
||||||
make -j
|
make -j
|
||||||
make install # will install to /usr/local on Unix, on macOS it is a good idea to sudo chown -R `whoami`:staff /usr/local
|
make install # will install to /usr/local on Unix, on macOS it is a good idea to sudo chown -R `whoami`:staff /usr/local
|
||||||
```
|
```
|
||||||
@ -251,6 +284,12 @@ make install # will install to /usr/local on Unix, on macOS it is a good idea to
|
|||||||
Headers and a static library will be installed to the target dir.
|
Headers and a static library will be installed to the target dir.
|
||||||
There is a unittest which can be executed by typing `make test`.
|
There is a unittest which can be executed by typing `make test`.
|
||||||
|
|
||||||
|
Options for building:
|
||||||
|
|
||||||
|
* `-DUSE_TLS=1` will enable TLS support
|
||||||
|
* `-DUSE_MBED_TLS=1` will use [mbedlts](https://tls.mbed.org/) for the TLS support (default on Windows)
|
||||||
|
* `-DUSE_WS=1` will build the ws interactive command line tool
|
||||||
|
|
||||||
### vcpkg
|
### vcpkg
|
||||||
|
|
||||||
It is possible to get IXWebSocket through Microsoft [vcpkg](https://github.com/microsoft/vcpkg).
|
It is possible to get IXWebSocket through Microsoft [vcpkg](https://github.com/microsoft/vcpkg).
|
||||||
|
@ -12,10 +12,10 @@
|
|||||||
|
|
||||||
#include "IXCancellationRequest.h"
|
#include "IXCancellationRequest.h"
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
|
#include <memory>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <memory>
|
|
||||||
|
|
||||||
struct addrinfo;
|
struct addrinfo;
|
||||||
|
|
||||||
|
@ -6,8 +6,8 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "IXWebSocketHttpHeaders.h"
|
|
||||||
#include "IXProgressCallback.h"
|
#include "IXProgressCallback.h"
|
||||||
|
#include "IXWebSocketHttpHeaders.h"
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
@ -111,10 +111,12 @@ namespace ix
|
|||||||
class Http
|
class Http
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static std::tuple<bool, std::string, HttpRequestPtr> parseRequest(std::shared_ptr<Socket> socket);
|
static std::tuple<bool, std::string, HttpRequestPtr> parseRequest(
|
||||||
|
std::shared_ptr<Socket> socket);
|
||||||
static bool sendResponse(HttpResponsePtr response, std::shared_ptr<Socket> socket);
|
static bool sendResponse(HttpResponsePtr response, std::shared_ptr<Socket> socket);
|
||||||
|
|
||||||
static std::tuple<std::string, std::string, std::string> parseRequestLine(const std::string& line);
|
static std::tuple<std::string, std::string, std::string> parseRequestLine(
|
||||||
|
const std::string& line);
|
||||||
static std::string trim(const std::string& str);
|
static std::string trim(const std::string& str);
|
||||||
};
|
};
|
||||||
}
|
} // namespace ix
|
||||||
|
@ -14,6 +14,7 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
#include <zlib.h>
|
#include <zlib.h>
|
||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
@ -52,6 +53,8 @@ namespace ix
|
|||||||
bool HttpClient::performRequest(HttpRequestArgsPtr args,
|
bool HttpClient::performRequest(HttpRequestArgsPtr args,
|
||||||
const OnResponseCallback& onResponseCallback)
|
const OnResponseCallback& onResponseCallback)
|
||||||
{
|
{
|
||||||
|
assert(_async && "HttpClient needs its async parameter set to true "
|
||||||
|
"in order to call performRequest");
|
||||||
if (!_async) return false;
|
if (!_async) return false;
|
||||||
|
|
||||||
// Enqueue the task
|
// Enqueue the task
|
||||||
|
@ -6,9 +6,9 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "IXHttp.h"
|
||||||
#include "IXSocket.h"
|
#include "IXSocket.h"
|
||||||
#include "IXWebSocketHttpHeaders.h"
|
#include "IXWebSocketHttpHeaders.h"
|
||||||
#include "IXHttp.h"
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <condition_variable>
|
#include <condition_variable>
|
||||||
|
@ -6,9 +6,9 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "IXHttp.h"
|
||||||
#include "IXSocketServer.h"
|
#include "IXSocketServer.h"
|
||||||
#include "IXWebSocket.h"
|
#include "IXWebSocket.h"
|
||||||
#include "IXHttp.h"
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
@ -47,4 +47,3 @@ namespace ix
|
|||||||
void setDefaultConnectionCallback();
|
void setDefaultConnectionCallback();
|
||||||
};
|
};
|
||||||
} // namespace ix
|
} // namespace ix
|
||||||
|
|
||||||
|
@ -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,82 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#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;
|
||||||
@ -128,6 +127,10 @@ namespace ix
|
|||||||
optval != 0)
|
optval != 0)
|
||||||
{
|
{
|
||||||
pollResult = PollResultType::Error;
|
pollResult = PollResultType::Error;
|
||||||
|
|
||||||
|
// set errno to optval so that external callers can have an
|
||||||
|
// appropriate error description when calling strerror
|
||||||
|
errno = optval;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
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
|
||||||
|
@ -6,9 +6,9 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "IXGetFreePort.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <ixwebsocket/IXWebSocketServer.h>
|
#include <ixwebsocket/IXWebSocketServer.h>
|
||||||
#include "IXGetFreePort.h"
|
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <spdlog/spdlog.h>
|
#include <spdlog/spdlog.h>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
@ -17,7 +17,7 @@ namespace ix
|
|||||||
const std::string CobraMetricsPublisher::kSetBlacklistId = "sms_set_blacklist_id";
|
const std::string CobraMetricsPublisher::kSetBlacklistId = "sms_set_blacklist_id";
|
||||||
|
|
||||||
CobraMetricsPublisher::CobraMetricsPublisher() :
|
CobraMetricsPublisher::CobraMetricsPublisher() :
|
||||||
_enabled(false)
|
_enabled(true)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
#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>
|
||||||
@ -132,8 +133,8 @@ namespace ix
|
|||||||
CobraMetricsThreadedPublisher _cobra_metrics_theaded_publisher;
|
CobraMetricsThreadedPublisher _cobra_metrics_theaded_publisher;
|
||||||
|
|
||||||
/// A boolean to enable or disable this system
|
/// A boolean to enable or disable this system
|
||||||
/// push becomes a no-op when _enabled is true
|
/// push becomes a no-op when _enabled is false
|
||||||
bool _enabled;
|
std::atomic<bool> _enabled;
|
||||||
|
|
||||||
/// A uuid used to uniquely identify a session
|
/// A uuid used to uniquely identify a session
|
||||||
std::string _session;
|
std::string _session;
|
||||||
|
Reference in New Issue
Block a user