2019-01-05 02:28:13 +01:00
|
|
|
/*
|
|
|
|
* IXWebSocketPerMessageDeflateCodec.h
|
|
|
|
* Author: Benjamin Sergeant
|
|
|
|
* Copyright (c) 2018-2019 Machine Zone, Inc. All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-08-01 07:54:57 +02:00
|
|
|
#ifdef IXWEBSOCKET_USE_ZLIB
|
2019-01-05 02:28:13 +01:00
|
|
|
#include "zlib.h"
|
2020-08-01 07:54:57 +02:00
|
|
|
#endif
|
2019-01-05 02:28:13 +01:00
|
|
|
#include <memory>
|
2019-05-30 17:46:50 +02:00
|
|
|
#include <string>
|
2020-07-08 03:17:44 +02:00
|
|
|
#include <vector>
|
2019-01-05 02:28:13 +01:00
|
|
|
|
2019-02-21 03:59:07 +01:00
|
|
|
namespace ix
|
2019-01-05 02:28:13 +01:00
|
|
|
{
|
|
|
|
class WebSocketPerMessageDeflateCompressor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
WebSocketPerMessageDeflateCompressor();
|
|
|
|
~WebSocketPerMessageDeflateCompressor();
|
|
|
|
|
|
|
|
bool init(uint8_t deflateBits, bool clientNoContextTakeOver);
|
|
|
|
bool compress(const std::string& in, std::string& out);
|
2020-07-08 03:17:44 +02:00
|
|
|
bool compress(const std::string& in, std::vector<uint8_t>& out);
|
|
|
|
bool compress(const std::vector<uint8_t>& in, std::string& out);
|
|
|
|
bool compress(const std::vector<uint8_t>& in, std::vector<uint8_t>& out);
|
2019-01-05 02:28:13 +01:00
|
|
|
|
|
|
|
private:
|
2020-07-08 19:39:46 +02:00
|
|
|
template<typename T, typename S>
|
|
|
|
bool compressData(const T& in, S& out);
|
|
|
|
template<typename T>
|
|
|
|
bool endsWithEmptyUnCompressedBlock(const T& value);
|
2019-01-05 02:28:13 +01:00
|
|
|
|
|
|
|
int _flush;
|
|
|
|
size_t _compressBufferSize;
|
|
|
|
std::unique_ptr<unsigned char[]> _compressBuffer;
|
2020-08-01 07:54:57 +02:00
|
|
|
|
|
|
|
#ifdef IXWEBSOCKET_USE_ZLIB
|
2019-01-05 02:28:13 +01:00
|
|
|
z_stream _deflateState;
|
2020-08-01 07:54:57 +02:00
|
|
|
#endif
|
2019-01-05 02:28:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class WebSocketPerMessageDeflateDecompressor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
WebSocketPerMessageDeflateDecompressor();
|
|
|
|
~WebSocketPerMessageDeflateDecompressor();
|
|
|
|
|
|
|
|
bool init(uint8_t inflateBits, bool clientNoContextTakeOver);
|
|
|
|
bool decompress(const std::string& in, std::string& out);
|
|
|
|
|
|
|
|
private:
|
|
|
|
int _flush;
|
|
|
|
size_t _compressBufferSize;
|
|
|
|
std::unique_ptr<unsigned char[]> _compressBuffer;
|
2020-08-01 07:54:57 +02:00
|
|
|
|
|
|
|
#ifdef IXWEBSOCKET_USE_ZLIB
|
2019-01-05 02:28:13 +01:00
|
|
|
z_stream _inflateState;
|
2020-08-01 07:54:57 +02:00
|
|
|
#endif
|
2019-01-05 02:28:13 +01:00
|
|
|
};
|
|
|
|
|
2019-05-30 17:46:50 +02:00
|
|
|
} // namespace ix
|