2018-09-27 23:56:48 +02:00
|
|
|
/*
|
|
|
|
* IXWebSocketTransport.h
|
|
|
|
* Author: Benjamin Sergeant
|
|
|
|
* Copyright (c) 2017-2018 Machine Zone, Inc. All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
//
|
|
|
|
// Adapted from https://github.com/dhbaird/easywsclient
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
2018-10-01 23:46:11 +02:00
|
|
|
#include <atomic>
|
2018-09-27 23:56:48 +02:00
|
|
|
|
2018-11-10 03:23:49 +01:00
|
|
|
#include "IXWebSocketSendInfo.h"
|
2018-11-07 23:54:44 +01:00
|
|
|
#include "IXWebSocketPerMessageDeflate.h"
|
2018-11-10 03:23:49 +01:00
|
|
|
#include "IXWebSocketPerMessageDeflateOptions.h"
|
|
|
|
#include "IXWebSocketHttpHeaders.h"
|
2018-11-07 23:54:44 +01:00
|
|
|
|
2018-09-27 23:56:48 +02:00
|
|
|
namespace ix
|
|
|
|
{
|
|
|
|
class Socket;
|
|
|
|
|
|
|
|
struct WebSocketInitResult
|
|
|
|
{
|
|
|
|
bool success;
|
|
|
|
int http_status;
|
|
|
|
std::string errorStr;
|
2018-11-10 03:23:49 +01:00
|
|
|
WebSocketHttpHeaders headers;
|
2018-09-27 23:56:48 +02:00
|
|
|
|
2018-11-15 00:52:28 +01:00
|
|
|
WebSocketInitResult(bool s = false,
|
|
|
|
int status = 0,
|
|
|
|
const std::string& e = std::string(),
|
2018-11-10 03:23:49 +01:00
|
|
|
WebSocketHttpHeaders h = WebSocketHttpHeaders())
|
2018-09-27 23:56:48 +02:00
|
|
|
{
|
|
|
|
success = s;
|
2018-11-10 03:23:49 +01:00
|
|
|
http_status = status;
|
2018-09-27 23:56:48 +02:00
|
|
|
errorStr = e;
|
2018-11-10 03:23:49 +01:00
|
|
|
headers = h;
|
2018-09-27 23:56:48 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class WebSocketTransport
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum ReadyStateValues
|
|
|
|
{
|
|
|
|
CLOSING,
|
|
|
|
CLOSED,
|
|
|
|
CONNECTING,
|
|
|
|
OPEN
|
|
|
|
};
|
|
|
|
|
2018-10-25 21:01:47 +02:00
|
|
|
enum MessageKind
|
|
|
|
{
|
|
|
|
MSG,
|
|
|
|
PING,
|
|
|
|
PONG
|
|
|
|
};
|
|
|
|
|
|
|
|
using OnMessageCallback = std::function<void(const std::string&,
|
2018-11-10 03:23:49 +01:00
|
|
|
size_t,
|
2018-11-15 00:52:28 +01:00
|
|
|
bool,
|
2018-10-25 21:01:47 +02:00
|
|
|
MessageKind)>;
|
2018-10-26 03:51:19 +02:00
|
|
|
using OnCloseCallback = std::function<void(uint16_t,
|
2018-11-10 03:23:49 +01:00
|
|
|
const std::string&,
|
|
|
|
size_t)>;
|
2018-09-27 23:56:48 +02:00
|
|
|
|
|
|
|
WebSocketTransport();
|
|
|
|
~WebSocketTransport();
|
|
|
|
|
2018-12-30 06:53:33 +01:00
|
|
|
// Client
|
2018-11-10 03:23:49 +01:00
|
|
|
void configure(const std::string& url,
|
|
|
|
const WebSocketPerMessageDeflateOptions& perMessageDeflateOptions);
|
2018-09-27 23:56:48 +02:00
|
|
|
WebSocketInitResult init();
|
|
|
|
|
2018-12-30 06:53:33 +01:00
|
|
|
// Server
|
|
|
|
WebSocketInitResult initFromSocket(int fd);
|
|
|
|
|
2018-09-27 23:56:48 +02:00
|
|
|
void poll();
|
2018-11-10 03:23:49 +01:00
|
|
|
WebSocketSendInfo sendBinary(const std::string& message);
|
|
|
|
WebSocketSendInfo sendPing(const std::string& message);
|
2018-09-27 23:56:48 +02:00
|
|
|
void close();
|
|
|
|
ReadyStateValues getReadyState() const;
|
|
|
|
void setReadyState(ReadyStateValues readyStateValue);
|
2018-10-26 03:51:19 +02:00
|
|
|
void setOnCloseCallback(const OnCloseCallback& onCloseCallback);
|
2018-09-27 23:56:48 +02:00
|
|
|
void dispatch(const OnMessageCallback& onMessageCallback);
|
|
|
|
|
|
|
|
static void printUrl(const std::string& url);
|
|
|
|
static bool parseUrl(const std::string& url,
|
|
|
|
std::string& protocol,
|
|
|
|
std::string& host,
|
|
|
|
std::string& path,
|
|
|
|
std::string& query,
|
|
|
|
int& port);
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string _url;
|
|
|
|
std::string _origin;
|
|
|
|
|
|
|
|
struct wsheader_type {
|
|
|
|
unsigned header_size;
|
|
|
|
bool fin;
|
2018-11-07 20:45:17 +01:00
|
|
|
bool rsv1;
|
2018-09-27 23:56:48 +02:00
|
|
|
bool mask;
|
|
|
|
enum opcode_type {
|
|
|
|
CONTINUATION = 0x0,
|
|
|
|
TEXT_FRAME = 0x1,
|
|
|
|
BINARY_FRAME = 0x2,
|
|
|
|
CLOSE = 8,
|
|
|
|
PING = 9,
|
|
|
|
PONG = 0xa,
|
|
|
|
} opcode;
|
|
|
|
int N0;
|
|
|
|
uint64_t N;
|
|
|
|
uint8_t masking_key[4];
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<uint8_t> _rxbuf;
|
|
|
|
std::vector<uint8_t> _txbuf;
|
|
|
|
mutable std::mutex _txbufMutex;
|
|
|
|
std::vector<uint8_t> _receivedData;
|
|
|
|
|
|
|
|
std::shared_ptr<Socket> _socket;
|
|
|
|
|
|
|
|
std::atomic<ReadyStateValues> _readyState;
|
|
|
|
|
2018-10-26 03:51:19 +02:00
|
|
|
OnCloseCallback _onCloseCallback;
|
|
|
|
uint16_t _closeCode;
|
|
|
|
std::string _closeReason;
|
2018-11-10 03:23:49 +01:00
|
|
|
size_t _closeWireSize;
|
2018-10-26 03:51:19 +02:00
|
|
|
mutable std::mutex _closeDataMutex;
|
2018-09-27 23:56:48 +02:00
|
|
|
|
2018-11-07 23:54:44 +01:00
|
|
|
WebSocketPerMessageDeflate _perMessageDeflate;
|
2018-11-10 03:23:49 +01:00
|
|
|
WebSocketPerMessageDeflateOptions _perMessageDeflateOptions;
|
|
|
|
std::atomic<bool> _enablePerMessageDeflate;
|
2018-11-07 23:54:44 +01:00
|
|
|
|
2018-12-15 01:28:11 +01:00
|
|
|
// Used to cancel dns lookup + socket connect + http upgrade
|
|
|
|
std::atomic<bool> _requestInitCancellation;
|
|
|
|
|
2018-09-27 23:56:48 +02:00
|
|
|
void sendOnSocket();
|
2018-11-10 03:23:49 +01:00
|
|
|
WebSocketSendInfo sendData(wsheader_type::opcode_type type,
|
2018-11-12 18:00:55 +01:00
|
|
|
const std::string& message,
|
|
|
|
bool compress);
|
2018-11-10 03:23:49 +01:00
|
|
|
|
|
|
|
void emitMessage(MessageKind messageKind,
|
|
|
|
const std::string& message,
|
|
|
|
const wsheader_type& ws,
|
|
|
|
const OnMessageCallback& onMessageCallback);
|
2018-09-27 23:56:48 +02:00
|
|
|
|
|
|
|
bool isSendBufferEmpty() const;
|
|
|
|
void appendToSendBuffer(const std::vector<uint8_t>& header,
|
|
|
|
std::string::const_iterator begin,
|
|
|
|
std::string::const_iterator end,
|
|
|
|
uint64_t message_size,
|
|
|
|
uint8_t masking_key[4]);
|
|
|
|
void appendToSendBuffer(const std::vector<uint8_t>& buffer);
|
|
|
|
|
|
|
|
unsigned getRandomUnsigned();
|
2018-10-26 03:51:19 +02:00
|
|
|
void unmaskReceiveBuffer(const wsheader_type& ws);
|
2018-10-27 19:24:48 +02:00
|
|
|
std::string genRandomString(const int len);
|
2018-12-09 23:07:40 +01:00
|
|
|
|
2018-12-15 01:28:11 +01:00
|
|
|
// Non blocking versions of read/write, used during http upgrade
|
2018-12-09 23:07:40 +01:00
|
|
|
bool readByte(void* buffer);
|
2018-12-15 01:28:11 +01:00
|
|
|
bool writeBytes(const std::string& str);
|
2018-12-30 06:53:33 +01:00
|
|
|
|
|
|
|
// Parse HTTP headers
|
|
|
|
std::pair<bool, WebSocketHttpHeaders> parseHttpHeaders();
|
2018-09-27 23:56:48 +02:00
|
|
|
};
|
|
|
|
}
|