Feature/ws cli (#15)
* New command line tool for transfering files / still very beta. * add readme * use cli11 for argument parsing * json -> msgpack * stop using base64 and use binary which can be stored in message pack
This commit is contained in:
		
				
					committed by
					
						 GitHub
						GitHub
					
				
			
			
				
	
			
			
			
						parent
						
							262de49c3c
						
					
				
				
					commit
					73d7280723
				
			| @@ -47,6 +47,7 @@ int main(int argc, char** argv) | ||||
|                     } | ||||
|                     else if (messageType == ix::WebSocket_MessageType_Message) | ||||
|                     { | ||||
|                         std::cerr << "Received " << wireSize << " bytes" << std::endl; | ||||
|                         for (auto&& client : server.getClients()) | ||||
|                         { | ||||
|                             if (client != webSocket) | ||||
|   | ||||
| @@ -1,135 +0,0 @@ | ||||
| /* | ||||
|  base64.cpp and base64.h | ||||
|  | ||||
|  Copyright (C) 2004-2008 René Nyffenegger | ||||
|  | ||||
|  This source code is provided 'as-is', without any express or implied | ||||
|  warranty. In no event will the author be held liable for any damages | ||||
|  arising from the use of this software. | ||||
|  | ||||
|  Permission is granted to anyone to use this software for any purpose, | ||||
|  including commercial applications, and to alter it and redistribute it | ||||
|  freely, subject to the following restrictions: | ||||
|  | ||||
|  1. The origin of this source code must not be misrepresented; you must not | ||||
|  claim that you wrote the original source code. If you use this source code | ||||
|  in a product, an acknowledgment in the product documentation would be | ||||
|  appreciated but is not required. | ||||
|  | ||||
|  2. Altered source versions must be plainly marked as such, and must not be | ||||
|  misrepresented as being the original source code. | ||||
|  | ||||
|  3. This notice may not be removed or altered from any source distribution. | ||||
|  | ||||
|  René Nyffenegger rene.nyffenegger@adp-gmbh.ch | ||||
|  | ||||
|  */ | ||||
|  | ||||
| #include "IXBase64.h" | ||||
|  | ||||
| namespace ix | ||||
| { | ||||
|     static const std::string base64_chars = | ||||
|     "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | ||||
|     "abcdefghijklmnopqrstuvwxyz" | ||||
|     "0123456789+/"; | ||||
|  | ||||
|     std::string base64_encode(const std::string& data, size_t len) | ||||
|     { | ||||
|         std::string ret; | ||||
|         int i = 0; | ||||
|         int j = 0; | ||||
|         unsigned char char_array_3[3]; | ||||
|         unsigned char char_array_4[4]; | ||||
|  | ||||
|         const char* bytes_to_encode = data.c_str(); | ||||
|  | ||||
|         while(len--) | ||||
|         { | ||||
|             char_array_3[i++] = *(bytes_to_encode++); | ||||
|             if(i == 3) | ||||
|             { | ||||
|                 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; | ||||
|                 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); | ||||
|                 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); | ||||
|                 char_array_4[3] = char_array_3[2] & 0x3f; | ||||
|  | ||||
|                 for(i = 0; (i <4) ; i++) | ||||
|                     ret += base64_chars[char_array_4[i]]; | ||||
|  | ||||
|                 i = 0; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         if(i) | ||||
|         { | ||||
|             for(j = i; j < 3; j++) | ||||
|                 char_array_3[j] = '\0'; | ||||
|  | ||||
|             char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; | ||||
|             char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); | ||||
|             char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); | ||||
|             char_array_4[3] = char_array_3[2] & 0x3f; | ||||
|  | ||||
|             for(j = 0; (j < i + 1); j++) | ||||
|                 ret += base64_chars[char_array_4[j]]; | ||||
|  | ||||
|             while((i++ < 3)) | ||||
|                 ret += '='; | ||||
|  | ||||
|         } | ||||
|  | ||||
|         return ret; | ||||
|     } | ||||
|  | ||||
|     static inline bool is_base64(unsigned char c) | ||||
|     { | ||||
|         return (isalnum(c) || (c == '+') || (c == '/')); | ||||
|     } | ||||
|  | ||||
|     std::string base64_decode(const std::string& encoded_string) | ||||
|     { | ||||
|         int in_len = (int)encoded_string.size(); | ||||
|         int i = 0; | ||||
|         int j = 0; | ||||
|         int in_ = 0; | ||||
|         unsigned char char_array_4[4], char_array_3[3]; | ||||
|         std::string ret; | ||||
|  | ||||
|         while(in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) | ||||
|         { | ||||
|             char_array_4[i++] = encoded_string[in_]; in_++; | ||||
|             if(i ==4) | ||||
|             { | ||||
|                 for(i = 0; i <4; i++) | ||||
|                     char_array_4[i] = base64_chars.find(char_array_4[i]); | ||||
|  | ||||
|                 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); | ||||
|                 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); | ||||
|                 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; | ||||
|  | ||||
|                 for(i = 0; (i < 3); i++) | ||||
|                     ret += char_array_3[i]; | ||||
|  | ||||
|                 i = 0; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         if(i) | ||||
|         { | ||||
|             for(j = i; j <4; j++) | ||||
|                 char_array_4[j] = 0; | ||||
|  | ||||
|             for(j = 0; j <4; j++) | ||||
|                 char_array_4[j] = base64_chars.find(char_array_4[j]); | ||||
|  | ||||
|             char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); | ||||
|             char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); | ||||
|             char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; | ||||
|  | ||||
|             for(j = 0; (j < i - 1); j++) ret += char_array_3[j]; | ||||
|         } | ||||
|  | ||||
|         return ret; | ||||
|     } | ||||
| } | ||||
| @@ -1,15 +0,0 @@ | ||||
| /* | ||||
|  *  base64.h | ||||
|  *  Author: Benjamin Sergeant | ||||
|  *  Copyright (c) 2018 Machine Zone. All rights reserved. | ||||
|  */ | ||||
|  | ||||
| #pragma once | ||||
|  | ||||
| #include <string> | ||||
|  | ||||
| namespace ix | ||||
| { | ||||
|     std::string base64_encode(const std::string& data, size_t len); | ||||
|     std::string base64_decode(const std::string& encoded_string); | ||||
| } | ||||
| @@ -1,27 +0,0 @@ | ||||
| /* | ||||
|  *  IXHMac.h | ||||
|  *  Author: Benjamin Sergeant | ||||
|  *  Copyright (c) 2018 Machine Zone. All rights reserved. | ||||
|  */ | ||||
| #include "IXHMac.h" | ||||
| #include "IXBase64.h" | ||||
|  | ||||
| #include <openssl/hmac.h> | ||||
|  | ||||
| namespace ix | ||||
| { | ||||
|     std::string hmac(const std::string& data, const std::string& key) | ||||
|     { | ||||
|         constexpr size_t hashSize = 16; | ||||
|         unsigned char hash[hashSize]; | ||||
|  | ||||
|         HMAC(EVP_md5(), | ||||
|              key.c_str(), (int) key.size(), | ||||
|              (unsigned char *) data.c_str(), (int) data.size(), | ||||
|              (unsigned char *) hash, nullptr); | ||||
|  | ||||
|         std::string hashString(reinterpret_cast<char*>(hash), hashSize); | ||||
|  | ||||
|         return base64_encode(hashString, (uint32_t) hashString.size()); | ||||
|     } | ||||
| } | ||||
| @@ -1,14 +0,0 @@ | ||||
| /* | ||||
|  *  IXHMac.h | ||||
|  *  Author: Benjamin Sergeant | ||||
|  *  Copyright (c) 2018 Machine Zone. All rights reserved. | ||||
|  */ | ||||
|  | ||||
| #pragma once | ||||
|  | ||||
| #include <string> | ||||
|  | ||||
| namespace ix | ||||
| { | ||||
|     std::string hmac(const std::string& data, const std::string& key); | ||||
| } | ||||
| @@ -1,22 +0,0 @@ | ||||
| /* | ||||
|  *  IXHash.h | ||||
|  *  Author: Benjamin Sergeant | ||||
|  *  Copyright (c) 2018 Machine Zone. All rights reserved. | ||||
|  */ | ||||
|  | ||||
| #include <string> | ||||
|  | ||||
| namespace ix | ||||
| { | ||||
|     uint64_t djb2Hash(const std::string& data) | ||||
|     { | ||||
|         uint64_t hashAddress = 5381; | ||||
|  | ||||
|         for (auto& c : data) | ||||
|         { | ||||
|             hashAddress = ((hashAddress << 5) + hashAddress) + c; | ||||
|         } | ||||
|  | ||||
|         return hashAddress; | ||||
|     } | ||||
| } | ||||
| @@ -1,15 +0,0 @@ | ||||
| /* | ||||
|  *  IXHash.h | ||||
|  *  Author: Benjamin Sergeant | ||||
|  *  Copyright (c) 2018 Machine Zone. All rights reserved. | ||||
|  */ | ||||
|  | ||||
| #pragma once | ||||
|  | ||||
| #include <string> | ||||
|  | ||||
| namespace ix | ||||
| { | ||||
|     uint64_t djb2Hash(const std::string& data); | ||||
| } | ||||
|  | ||||
| @@ -1,75 +0,0 @@ | ||||
| /* | ||||
|  *  IXUuid.cpp | ||||
|  *  Author: Benjamin Sergeant | ||||
|  *  Copyright (c) 2018 Machine Zone. All rights reserved. | ||||
|  */ | ||||
|  | ||||
| /** | ||||
|  * Generate a random uuid similar to the uuid python module | ||||
|  * | ||||
|  * >>> import uuid | ||||
|  * >>> uuid.uuid4().hex | ||||
|  * 'bec08155b37d4050a1f3c3fa0276bf12' | ||||
|  * | ||||
|  * Code adapted from https://github.com/r-lyeh-archived/sole | ||||
|  */ | ||||
|  | ||||
| #include "IXUuid.h" | ||||
|  | ||||
| #include <sstream> | ||||
| #include <string> | ||||
| #include <iomanip> | ||||
| #include <random> | ||||
|  | ||||
|  | ||||
| namespace ix | ||||
| { | ||||
|     class Uuid | ||||
|     { | ||||
|         public: | ||||
|             Uuid(); | ||||
|             std::string toString() const; | ||||
|  | ||||
|         private: | ||||
|             uint64_t _ab; | ||||
|             uint64_t _cd; | ||||
|     }; | ||||
|  | ||||
|     Uuid::Uuid() | ||||
|     { | ||||
|         static std::random_device rd; | ||||
|         static std::uniform_int_distribution<uint64_t> dist(0, (uint64_t)(~0)); | ||||
|  | ||||
|         _ab = dist(rd); | ||||
|         _cd = dist(rd); | ||||
|  | ||||
|         _ab = (_ab & 0xFFFFFFFFFFFF0FFFULL) | 0x0000000000004000ULL; | ||||
|         _cd = (_cd & 0x3FFFFFFFFFFFFFFFULL) | 0x8000000000000000ULL; | ||||
|     } | ||||
|  | ||||
|     std::string Uuid::toString() const | ||||
|     { | ||||
|         std::stringstream ss; | ||||
|         ss << std::hex << std::nouppercase << std::setfill('0'); | ||||
|  | ||||
|         uint32_t a = (_ab >> 32); | ||||
|         uint32_t b = (_ab & 0xFFFFFFFF); | ||||
|         uint32_t c = (_cd >> 32); | ||||
|         uint32_t d = (_cd & 0xFFFFFFFF); | ||||
|  | ||||
|         ss << std::setw(8) << (a); | ||||
|         ss << std::setw(4) << (b >> 16); | ||||
|         ss << std::setw(4) << (b & 0xFFFF); | ||||
|         ss << std::setw(4) << (c >> 16 ); | ||||
|         ss << std::setw(4) << (c & 0xFFFF); | ||||
|         ss << std::setw(8) << d; | ||||
|  | ||||
|         return ss.str(); | ||||
|     } | ||||
|  | ||||
|     std::string uuid4() | ||||
|     { | ||||
|         Uuid id; | ||||
|         return id.toString(); | ||||
|     } | ||||
| } | ||||
| @@ -1,17 +0,0 @@ | ||||
| /* | ||||
|  *  IXUuid.h | ||||
|  *  Author: Benjamin Sergeant | ||||
|  *  Copyright (c) 2017 Machine Zone. All rights reserved. | ||||
|  */ | ||||
| #pragma once | ||||
|  | ||||
| #include <string> | ||||
|  | ||||
| namespace ix | ||||
| { | ||||
|    /** | ||||
|     * Generate a random uuid | ||||
|     */ | ||||
|    std::string uuid4(); | ||||
|  | ||||
| } | ||||
							
								
								
									
										1
									
								
								examples/ws_receive/.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								examples/ws_receive/.gitignore
									
									
									
									
										vendored
									
									
								
							| @@ -1 +0,0 @@ | ||||
| build | ||||
| @@ -1,30 +0,0 @@ | ||||
| # | ||||
| # Author: Benjamin Sergeant | ||||
| # Copyright (c) 2019 Machine Zone, Inc. All rights reserved. | ||||
| # | ||||
|  | ||||
| cmake_minimum_required (VERSION 3.4.1) | ||||
| project (ws_receive) | ||||
|  | ||||
| # There's -Weverything too for clang | ||||
| set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic -Wshorten-64-to-32") | ||||
|  | ||||
| set (CMAKE_CXX_STANDARD 14) | ||||
|  | ||||
| option(USE_TLS "Add TLS support" ON) | ||||
|  | ||||
| add_subdirectory(${PROJECT_SOURCE_DIR}/../.. ixwebsocket) | ||||
|  | ||||
| include_directories(ws_receive .) | ||||
|  | ||||
| add_executable(ws_receive  | ||||
|   jsoncpp/jsoncpp.cpp | ||||
|   ixcrypto/IXBase64.cpp | ||||
|   ixcrypto/IXHash.cpp | ||||
|   ws_receive.cpp) | ||||
|  | ||||
| if (APPLE AND USE_TLS) | ||||
|     target_link_libraries(ws_receive "-framework foundation" "-framework security") | ||||
| endif() | ||||
|  | ||||
| target_link_libraries(ws_receive ixwebsocket) | ||||
| @@ -1 +0,0 @@ | ||||
| ws_receive is a simple server upload program. It needs to be used in conjonction with ws_send. | ||||
| @@ -1 +0,0 @@ | ||||
| ../cobra_publisher/ixcrypto | ||||
| @@ -1,333 +0,0 @@ | ||||
| /// Json-cpp amalgated forward header (http://jsoncpp.sourceforge.net/). | ||||
| /// It is intended to be used with #include "json/json-forwards.h" | ||||
| /// This header provides forward declaration for all JsonCpp types. | ||||
|  | ||||
| // ////////////////////////////////////////////////////////////////////// | ||||
| // Beginning of content of file: LICENSE | ||||
| // ////////////////////////////////////////////////////////////////////// | ||||
|  | ||||
| /* | ||||
| The JsonCpp library's source code, including accompanying documentation, | ||||
| tests and demonstration applications, are licensed under the following | ||||
| conditions... | ||||
|  | ||||
| Baptiste Lepilleur and The JsonCpp Authors explicitly disclaim copyright in all | ||||
| jurisdictions which recognize such a disclaimer. In such jurisdictions, | ||||
| this software is released into the Public Domain. | ||||
|  | ||||
| In jurisdictions which do not recognize Public Domain property (e.g. Germany as of | ||||
| 2010), this software is Copyright (c) 2007-2010 by Baptiste Lepilleur and | ||||
| The JsonCpp Authors, and is released under the terms of the MIT License (see below). | ||||
|  | ||||
| In jurisdictions which recognize Public Domain property, the user of this | ||||
| software may choose to accept it either as 1) Public Domain, 2) under the | ||||
| conditions of the MIT License (see below), or 3) under the terms of dual | ||||
| Public Domain/MIT License conditions described here, as they choose. | ||||
|  | ||||
| The MIT License is about as close to Public Domain as a license can get, and is | ||||
| described in clear, concise terms at: | ||||
|  | ||||
|    http://en.wikipedia.org/wiki/MIT_License | ||||
|  | ||||
| The full text of the MIT License follows: | ||||
|  | ||||
| ======================================================================== | ||||
| Copyright (c) 2007-2010 Baptiste Lepilleur and The JsonCpp Authors | ||||
|  | ||||
| Permission is hereby granted, free of charge, to any person | ||||
| obtaining a copy of this software and associated documentation | ||||
| files (the "Software"), to deal in the Software without | ||||
| restriction, including without limitation the rights to use, copy, | ||||
| modify, merge, publish, distribute, sublicense, and/or sell copies | ||||
| of the Software, and to permit persons to whom the Software is | ||||
| furnished to do so, subject to the following conditions: | ||||
|  | ||||
| The above copyright notice and this permission notice shall be | ||||
| included in all copies or substantial portions of the Software. | ||||
|  | ||||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||||
| EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||||
| MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||||
| NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | ||||
| BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | ||||
| ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||||
| CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||||
| SOFTWARE. | ||||
| ======================================================================== | ||||
| (END LICENSE TEXT) | ||||
|  | ||||
| The MIT license is compatible with both the GPL and commercial | ||||
| software, affording one all of the rights of Public Domain with the | ||||
| minor nuisance of being required to keep the above copyright notice | ||||
| and license text in the source code. Note also that by accepting the | ||||
| Public Domain "license" you can re-license your copy using whatever | ||||
| license you like. | ||||
|  | ||||
| */ | ||||
|  | ||||
| // ////////////////////////////////////////////////////////////////////// | ||||
| // End of content of file: LICENSE | ||||
| // ////////////////////////////////////////////////////////////////////// | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
| #ifndef JSON_FORWARD_AMALGATED_H_INCLUDED | ||||
| # define JSON_FORWARD_AMALGATED_H_INCLUDED | ||||
| /// If defined, indicates that the source file is amalgated | ||||
| /// to prevent private header inclusion. | ||||
| #define JSON_IS_AMALGAMATION | ||||
|  | ||||
| // ////////////////////////////////////////////////////////////////////// | ||||
| // Beginning of content of file: include/json/config.h | ||||
| // ////////////////////////////////////////////////////////////////////// | ||||
|  | ||||
| // Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors | ||||
| // Distributed under MIT license, or public domain if desired and | ||||
| // recognized in your jurisdiction. | ||||
| // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE | ||||
|  | ||||
| #ifndef JSON_CONFIG_H_INCLUDED | ||||
| #define JSON_CONFIG_H_INCLUDED | ||||
| #include <stddef.h> | ||||
| #include <string> //typedef String | ||||
| #include <stdint.h> //typedef int64_t, uint64_t | ||||
|  | ||||
| /// If defined, indicates that json library is embedded in CppTL library. | ||||
| //# define JSON_IN_CPPTL 1 | ||||
|  | ||||
| /// If defined, indicates that json may leverage CppTL library | ||||
| //#  define JSON_USE_CPPTL 1 | ||||
| /// If defined, indicates that cpptl vector based map should be used instead of | ||||
| /// std::map | ||||
| /// as Value container. | ||||
| //#  define JSON_USE_CPPTL_SMALLMAP 1 | ||||
|  | ||||
| // If non-zero, the library uses exceptions to report bad input instead of C | ||||
| // assertion macros. The default is to use exceptions. | ||||
| #ifndef JSON_USE_EXCEPTION | ||||
| #define JSON_USE_EXCEPTION 1 | ||||
| #endif | ||||
|  | ||||
| /// If defined, indicates that the source file is amalgated | ||||
| /// to prevent private header inclusion. | ||||
| /// Remarks: it is automatically defined in the generated amalgated header. | ||||
| // #define JSON_IS_AMALGAMATION | ||||
|  | ||||
| #ifdef JSON_IN_CPPTL | ||||
| #include <cpptl/config.h> | ||||
| #ifndef JSON_USE_CPPTL | ||||
| #define JSON_USE_CPPTL 1 | ||||
| #endif | ||||
| #endif | ||||
|  | ||||
| #ifdef JSON_IN_CPPTL | ||||
| #define JSON_API CPPTL_API | ||||
| #elif defined(JSON_DLL_BUILD) | ||||
| #if defined(_MSC_VER) || defined(__MINGW32__) | ||||
| #define JSON_API __declspec(dllexport) | ||||
| #define JSONCPP_DISABLE_DLL_INTERFACE_WARNING | ||||
| #endif // if defined(_MSC_VER) | ||||
| #elif defined(JSON_DLL) | ||||
| #if defined(_MSC_VER) || defined(__MINGW32__) | ||||
| #define JSON_API __declspec(dllimport) | ||||
| #define JSONCPP_DISABLE_DLL_INTERFACE_WARNING | ||||
| #endif // if defined(_MSC_VER) | ||||
| #endif // ifdef JSON_IN_CPPTL | ||||
| #if !defined(JSON_API) | ||||
| #define JSON_API | ||||
| #endif | ||||
|  | ||||
| // If JSON_NO_INT64 is defined, then Json only support C++ "int" type for | ||||
| // integer | ||||
| // Storages, and 64 bits integer support is disabled. | ||||
| // #define JSON_NO_INT64 1 | ||||
|  | ||||
| #if defined(_MSC_VER) // MSVC | ||||
| #  if _MSC_VER <= 1200 // MSVC 6 | ||||
|     // Microsoft Visual Studio 6 only support conversion from __int64 to double | ||||
|     // (no conversion from unsigned __int64). | ||||
| #    define JSON_USE_INT64_DOUBLE_CONVERSION 1 | ||||
|     // Disable warning 4786 for VS6 caused by STL (identifier was truncated to '255' | ||||
|     // characters in the debug information) | ||||
|     // All projects I've ever seen with VS6 were using this globally (not bothering | ||||
|     // with pragma push/pop). | ||||
| #    pragma warning(disable : 4786) | ||||
| #  endif // MSVC 6 | ||||
|  | ||||
| #  if _MSC_VER >= 1500 // MSVC 2008 | ||||
|     /// Indicates that the following function is deprecated. | ||||
| #    define JSONCPP_DEPRECATED(message) __declspec(deprecated(message)) | ||||
| #  endif | ||||
|  | ||||
| #endif // defined(_MSC_VER) | ||||
|  | ||||
| // In c++11 the override keyword allows you to explicity define that a function | ||||
| // is intended to override the base-class version.  This makes the code more | ||||
| // managable and fixes a set of common hard-to-find bugs. | ||||
| #if __cplusplus >= 201103L | ||||
| # define JSONCPP_OVERRIDE override | ||||
| # define JSONCPP_NOEXCEPT noexcept | ||||
| #elif defined(_MSC_VER) && _MSC_VER > 1600 && _MSC_VER < 1900 | ||||
| # define JSONCPP_OVERRIDE override | ||||
| # define JSONCPP_NOEXCEPT throw() | ||||
| #elif defined(_MSC_VER) && _MSC_VER >= 1900 | ||||
| # define JSONCPP_OVERRIDE override | ||||
| # define JSONCPP_NOEXCEPT noexcept | ||||
| #else | ||||
| # define JSONCPP_OVERRIDE | ||||
| # define JSONCPP_NOEXCEPT throw() | ||||
| #endif | ||||
|  | ||||
| #ifndef JSON_HAS_RVALUE_REFERENCES | ||||
|  | ||||
| #if defined(_MSC_VER) && _MSC_VER >= 1600 // MSVC >= 2010 | ||||
| #define JSON_HAS_RVALUE_REFERENCES 1 | ||||
| #endif // MSVC >= 2010 | ||||
|  | ||||
| #ifdef __clang__ | ||||
| #if __has_feature(cxx_rvalue_references) | ||||
| #define JSON_HAS_RVALUE_REFERENCES 1 | ||||
| #endif  // has_feature | ||||
|  | ||||
| #elif defined __GNUC__ // not clang (gcc comes later since clang emulates gcc) | ||||
| #if defined(__GXX_EXPERIMENTAL_CXX0X__) || (__cplusplus >= 201103L) | ||||
| #define JSON_HAS_RVALUE_REFERENCES 1 | ||||
| #endif  // GXX_EXPERIMENTAL | ||||
|  | ||||
| #endif // __clang__ || __GNUC__ | ||||
|  | ||||
| #endif // not defined JSON_HAS_RVALUE_REFERENCES | ||||
|  | ||||
| #ifndef JSON_HAS_RVALUE_REFERENCES | ||||
| #define JSON_HAS_RVALUE_REFERENCES 0 | ||||
| #endif | ||||
|  | ||||
| #ifdef __clang__ | ||||
| #  if __has_extension(attribute_deprecated_with_message) | ||||
| #    define JSONCPP_DEPRECATED(message)  __attribute__ ((deprecated(message))) | ||||
| #  endif | ||||
| #elif defined __GNUC__ // not clang (gcc comes later since clang emulates gcc) | ||||
| #  if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)) | ||||
| #    define JSONCPP_DEPRECATED(message)  __attribute__ ((deprecated(message))) | ||||
| #  elif (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)) | ||||
| #    define JSONCPP_DEPRECATED(message)  __attribute__((__deprecated__)) | ||||
| #  endif  // GNUC version | ||||
| #endif // __clang__ || __GNUC__ | ||||
|  | ||||
| #if !defined(JSONCPP_DEPRECATED) | ||||
| #define JSONCPP_DEPRECATED(message) | ||||
| #endif // if !defined(JSONCPP_DEPRECATED) | ||||
|  | ||||
| #if __GNUC__ >= 6 | ||||
| #  define JSON_USE_INT64_DOUBLE_CONVERSION 1 | ||||
| #endif | ||||
|  | ||||
| #if !defined(JSON_IS_AMALGAMATION) | ||||
|  | ||||
| # include "version.h" | ||||
|  | ||||
| # if JSONCPP_USING_SECURE_MEMORY | ||||
| #  include "allocator.h" //typedef Allocator | ||||
| # endif | ||||
|  | ||||
| #endif // if !defined(JSON_IS_AMALGAMATION) | ||||
|  | ||||
| namespace Json { | ||||
| typedef int Int; | ||||
| typedef unsigned int UInt; | ||||
| #if defined(JSON_NO_INT64) | ||||
| typedef int LargestInt; | ||||
| typedef unsigned int LargestUInt; | ||||
| #undef JSON_HAS_INT64 | ||||
| #else                 // if defined(JSON_NO_INT64) | ||||
| // For Microsoft Visual use specific types as long long is not supported | ||||
| #if defined(_MSC_VER) // Microsoft Visual Studio | ||||
| typedef __int64 Int64; | ||||
| typedef unsigned __int64 UInt64; | ||||
| #else                 // if defined(_MSC_VER) // Other platforms, use long long | ||||
| typedef int64_t Int64; | ||||
| typedef uint64_t UInt64; | ||||
| #endif // if defined(_MSC_VER) | ||||
| typedef Int64 LargestInt; | ||||
| typedef UInt64 LargestUInt; | ||||
| #define JSON_HAS_INT64 | ||||
| #endif // if defined(JSON_NO_INT64) | ||||
| #if JSONCPP_USING_SECURE_MEMORY | ||||
| #define JSONCPP_STRING        std::basic_string<char, std::char_traits<char>, Json::SecureAllocator<char> > | ||||
| #define JSONCPP_OSTRINGSTREAM std::basic_ostringstream<char, std::char_traits<char>, Json::SecureAllocator<char> > | ||||
| #define JSONCPP_OSTREAM       std::basic_ostream<char, std::char_traits<char>> | ||||
| #define JSONCPP_ISTRINGSTREAM std::basic_istringstream<char, std::char_traits<char>, Json::SecureAllocator<char> > | ||||
| #define JSONCPP_ISTREAM       std::istream | ||||
| #else | ||||
| #define JSONCPP_STRING        std::string | ||||
| #define JSONCPP_OSTRINGSTREAM std::ostringstream | ||||
| #define JSONCPP_OSTREAM       std::ostream | ||||
| #define JSONCPP_ISTRINGSTREAM std::istringstream | ||||
| #define JSONCPP_ISTREAM       std::istream | ||||
| #endif // if JSONCPP_USING_SECURE_MEMORY | ||||
| } // end namespace Json | ||||
|  | ||||
| #endif // JSON_CONFIG_H_INCLUDED | ||||
|  | ||||
| // ////////////////////////////////////////////////////////////////////// | ||||
| // End of content of file: include/json/config.h | ||||
| // ////////////////////////////////////////////////////////////////////// | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
| // ////////////////////////////////////////////////////////////////////// | ||||
| // Beginning of content of file: include/json/forwards.h | ||||
| // ////////////////////////////////////////////////////////////////////// | ||||
|  | ||||
| // Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors | ||||
| // Distributed under MIT license, or public domain if desired and | ||||
| // recognized in your jurisdiction. | ||||
| // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE | ||||
|  | ||||
| #ifndef JSON_FORWARDS_H_INCLUDED | ||||
| #define JSON_FORWARDS_H_INCLUDED | ||||
|  | ||||
| #if !defined(JSON_IS_AMALGAMATION) | ||||
| #include "config.h" | ||||
| #endif // if !defined(JSON_IS_AMALGAMATION) | ||||
|  | ||||
| namespace Json { | ||||
|  | ||||
| // writer.h | ||||
| class FastWriter; | ||||
| class StyledWriter; | ||||
|  | ||||
| // reader.h | ||||
| class Reader; | ||||
|  | ||||
| // features.h | ||||
| class Features; | ||||
|  | ||||
| // value.h | ||||
| typedef unsigned int ArrayIndex; | ||||
| class StaticString; | ||||
| class Path; | ||||
| class PathArgument; | ||||
| class Value; | ||||
| class ValueIteratorBase; | ||||
| class ValueIterator; | ||||
| class ValueConstIterator; | ||||
|  | ||||
| } // namespace Json | ||||
|  | ||||
| #endif // JSON_FORWARDS_H_INCLUDED | ||||
|  | ||||
| // ////////////////////////////////////////////////////////////////////// | ||||
| // End of content of file: include/json/forwards.h | ||||
| // ////////////////////////////////////////////////////////////////////// | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
| #endif //ifndef JSON_FORWARD_AMALGATED_H_INCLUDED | ||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										29
									
								
								examples/ws_receive/package-lock.json
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										29
									
								
								examples/ws_receive/package-lock.json
									
									
									
										generated
									
									
									
								
							| @@ -1,29 +0,0 @@ | ||||
| { | ||||
|   "requires": true, | ||||
|   "lockfileVersion": 1, | ||||
|   "dependencies": { | ||||
|     "async-limiter": { | ||||
|       "version": "1.0.0", | ||||
|       "resolved": "https://registry.npmjs.org/async-limiter/-/async-limiter-1.0.0.tgz", | ||||
|       "integrity": "sha512-jp/uFnooOiO+L211eZOoSyzpOITMXx1rBITauYykG3BRYPu8h0UcxsPNB04RR5vo4Tyz3+ay17tR6JVf9qzYWg==" | ||||
|     }, | ||||
|     "base-64": { | ||||
|       "version": "0.1.0", | ||||
|       "resolved": "https://registry.npmjs.org/base-64/-/base-64-0.1.0.tgz", | ||||
|       "integrity": "sha1-eAqZyE59YAJgNhURxId2E78k9rs=" | ||||
|     }, | ||||
|     "djb2": { | ||||
|       "version": "0.0.2", | ||||
|       "resolved": "https://registry.npmjs.org/djb2/-/djb2-0.0.2.tgz", | ||||
|       "integrity": "sha1-RAs4kao6uBQrVNRpsXe66p6W5O8=" | ||||
|     }, | ||||
|     "ws": { | ||||
|       "version": "6.1.4", | ||||
|       "resolved": "https://registry.npmjs.org/ws/-/ws-6.1.4.tgz", | ||||
|       "integrity": "sha512-eqZfL+NE/YQc1/ZynhojeV8q+H050oR8AZ2uIev7RU10svA9ZnJUddHcOUZTJLinZ9yEfdA2kSATS2qZK5fhJA==", | ||||
|       "requires": { | ||||
|         "async-limiter": "1.0.0" | ||||
|       } | ||||
|     } | ||||
|   } | ||||
| } | ||||
| @@ -1,153 +0,0 @@ | ||||
| /* | ||||
|  *  ws_receive.cpp | ||||
|  *  Author: Benjamin Sergeant | ||||
|  *  Copyright (c) 2018 Machine Zone, Inc. All rights reserved. | ||||
|  */ | ||||
|  | ||||
| #include <iostream> | ||||
| #include <sstream> | ||||
| #include <fstream> | ||||
| #include <ixwebsocket/IXWebSocketServer.h> | ||||
| #include <jsoncpp/json/json.h> | ||||
| #include <ixcrypto/IXBase64.h> | ||||
| #include <ixcrypto/IXHash.h> | ||||
|  | ||||
|  | ||||
| namespace | ||||
| { | ||||
|     // We should cleanup the file name and full path further to remove .. as well | ||||
|     std::string extractFilename(const std::string& path) | ||||
|     { | ||||
|         std::string filename("filename.conf"); | ||||
|         std::string::size_type idx; | ||||
|  | ||||
|         idx = path.rfind('/'); | ||||
|         if (idx != std::string::npos) | ||||
|         { | ||||
|             std::string filename = path.substr(idx+1); | ||||
|             return filename; | ||||
|         } | ||||
|         else | ||||
|         { | ||||
|             return std::string(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| namespace ix | ||||
| { | ||||
|     void errorHandler(const std::string& errMsg, | ||||
|                       const std::string& id, | ||||
|                       std::shared_ptr<ix::WebSocket> webSocket) | ||||
|     { | ||||
|         Json::Value pdu; | ||||
|         pdu["kind"] = "error"; | ||||
|         pdu["id"] = id; | ||||
|         pdu["message"] = errMsg; | ||||
|         webSocket->send(pdu.toStyledString()); | ||||
|     } | ||||
|  | ||||
|     void messageHandler(const std::string& str, | ||||
|                         std::shared_ptr<ix::WebSocket> webSocket) | ||||
|     { | ||||
|         std::cerr << "Received message: " << str.size() << std::endl; | ||||
|  | ||||
|         Json::Value data; | ||||
|         Json::Reader reader; | ||||
|         if (!reader.parse(str, data)) | ||||
|         { | ||||
|             errorHandler("Invalid JSON", std::string(), webSocket); | ||||
|             return; | ||||
|         } | ||||
|  | ||||
|         std::cout << "id: " << data["id"].asString() << std::endl; | ||||
|  | ||||
|         std::string content = ix::base64_decode(data["content"].asString()); | ||||
|         std::cout << "Content size: " << content.size() << std::endl; | ||||
|  | ||||
|         // Validate checksum | ||||
|         uint64_t cksum = ix::djb2Hash(data["content"].asString()); | ||||
|         uint64_t cksumRef = data["djb2_hash"].asUInt64(); | ||||
|  | ||||
|         std::cout << "Computed hash: " << cksum << std::endl; | ||||
|         std::cout << "Reference hash: " << cksumRef << std::endl; | ||||
|  | ||||
|         if (cksum != cksumRef) | ||||
|         { | ||||
|             errorHandler("Hash mismatch.", std::string(), webSocket); | ||||
|             return; | ||||
|         } | ||||
|  | ||||
|         std::string filename = data["filename"].asString(); | ||||
|         filename = extractFilename(filename); | ||||
|  | ||||
|         std::ofstream out(filename); | ||||
|         out << content; | ||||
|         out.close(); | ||||
|  | ||||
|         Json::Value pdu; | ||||
|         pdu["ack"] = true; | ||||
|         pdu["id"] = data["id"]; | ||||
|         pdu["filename"] = data["filename"]; | ||||
|         webSocket->send(pdu.toStyledString()); | ||||
|     } | ||||
| } | ||||
|  | ||||
| int main(int argc, char** argv) | ||||
| { | ||||
|     int port = 8080; | ||||
|     if (argc == 2) | ||||
|     { | ||||
|         std::stringstream ss; | ||||
|         ss << argv[1]; | ||||
|         ss >> port; | ||||
|     } | ||||
|  | ||||
|     ix::WebSocketServer server(port); | ||||
|  | ||||
|     server.setOnConnectionCallback( | ||||
|         [&server](std::shared_ptr<ix::WebSocket> webSocket) | ||||
|         { | ||||
|             webSocket->setOnMessageCallback( | ||||
|                 [webSocket, &server](ix::WebSocketMessageType messageType, | ||||
|                                      const std::string& str, | ||||
|                                      size_t wireSize, | ||||
|                                      const ix::WebSocketErrorInfo& error, | ||||
|                                      const ix::WebSocketOpenInfo& openInfo, | ||||
|                                      const ix::WebSocketCloseInfo& closeInfo) | ||||
|                 { | ||||
|                     if (messageType == ix::WebSocket_MessageType_Open) | ||||
|                     { | ||||
|                         std::cerr << "New connection" << std::endl; | ||||
|                         std::cerr << "Uri: " << openInfo.uri << std::endl; | ||||
|                         std::cerr << "Headers:" << std::endl; | ||||
|                         for (auto it : openInfo.headers) | ||||
|                         { | ||||
|                             std::cerr << it.first << ": " << it.second << std::endl; | ||||
|                         } | ||||
|                     } | ||||
|                     else if (messageType == ix::WebSocket_MessageType_Close) | ||||
|                     { | ||||
|                         std::cerr << "Closed connection" << std::endl; | ||||
|                     } | ||||
|                     else if (messageType == ix::WebSocket_MessageType_Message) | ||||
|                     { | ||||
|                         messageHandler(str, webSocket); | ||||
|                     } | ||||
|                 } | ||||
|             ); | ||||
|         } | ||||
|     ); | ||||
|  | ||||
|     auto res = server.listen(); | ||||
|     if (!res.first) | ||||
|     { | ||||
|         std::cerr << res.second << std::endl; | ||||
|         return 1; | ||||
|     } | ||||
|  | ||||
|     server.start(); | ||||
|     server.wait(); | ||||
|  | ||||
|     return 0; | ||||
| } | ||||
| @@ -1,43 +0,0 @@ | ||||
| /* | ||||
|  *  ws_receive.js | ||||
|  *  Author: Benjamin Sergeant | ||||
|  *  Copyright (c) 2019 Machine Zone, Inc. All rights reserved. | ||||
|  */ | ||||
| const WebSocket = require('ws') | ||||
| const djb2 = require('djb2') | ||||
| const fs = require('fs') | ||||
|  | ||||
| const wss = new WebSocket.Server({ port: 8080, | ||||
|                                    perMessageDeflate: false, | ||||
|                                    maxPayload: 1024 * 1024 * 1024 * 1024}); | ||||
|  | ||||
| wss.on('connection', function connection(ws) { | ||||
|   ws.on('message', function incoming(data) { | ||||
|     console.log('Received message') | ||||
|  | ||||
|     let str = data.toString() | ||||
|     let obj = JSON.parse(str) | ||||
|  | ||||
|     console.log(obj.id) | ||||
|     console.log(obj.djb2_hash) | ||||
|     console.log(djb2(obj.content)) | ||||
|  | ||||
|     var content = Buffer.from(obj.content, 'base64') | ||||
|     // let bytes = base64.decode(obj.content) | ||||
|  | ||||
|     let path = obj.filename | ||||
|     fs.writeFile(path, content, function(err) { | ||||
|       if (err) { | ||||
|         throw err | ||||
|       } else { | ||||
|         console.log('wrote data to disk') | ||||
|       } | ||||
|     }); | ||||
|  | ||||
|     let response = { | ||||
|       id: obj.id | ||||
|     } | ||||
|  | ||||
|     ws.send(JSON.stringify(response)) | ||||
|   }); | ||||
| }); | ||||
							
								
								
									
										1
									
								
								examples/ws_send/.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								examples/ws_send/.gitignore
									
									
									
									
										vendored
									
									
								
							| @@ -1 +0,0 @@ | ||||
| build | ||||
| @@ -1,31 +0,0 @@ | ||||
| # | ||||
| # Author: Benjamin Sergeant | ||||
| # Copyright (c) 2019 Machine Zone, Inc. All rights reserved. | ||||
| # | ||||
|  | ||||
| cmake_minimum_required (VERSION 3.4.1) | ||||
| project (ws_send) | ||||
|  | ||||
| # There's -Weverything too for clang | ||||
| set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic -Wshorten-64-to-32") | ||||
|  | ||||
| set (CMAKE_CXX_STANDARD 14) | ||||
|  | ||||
| option(USE_TLS "Add TLS support" ON) | ||||
|  | ||||
| add_subdirectory(${PROJECT_SOURCE_DIR}/../.. ixwebsocket) | ||||
|  | ||||
| include_directories(ws_send .) | ||||
|  | ||||
| add_executable(ws_send  | ||||
|   jsoncpp/jsoncpp.cpp | ||||
|   ixcrypto/IXBase64.cpp | ||||
|   ixcrypto/IXUuid.cpp | ||||
|   ixcrypto/IXHash.cpp | ||||
|   ws_send.cpp) | ||||
|  | ||||
| if (APPLE AND USE_TLS) | ||||
|     target_link_libraries(ws_send "-framework foundation" "-framework security") | ||||
| endif() | ||||
|  | ||||
| target_link_libraries(ws_send ixwebsocket) | ||||
| @@ -1 +0,0 @@ | ||||
| ws_send is a simple upload program. It needs to be used in conjonction with ws_receive. | ||||
| @@ -1 +0,0 @@ | ||||
| ../cobra_publisher/ixcrypto | ||||
| @@ -1 +0,0 @@ | ||||
| ../cobra_publisher/jsoncpp | ||||
| @@ -1,306 +0,0 @@ | ||||
| /* | ||||
|  *  ws_send.cpp | ||||
|  *  Author: Benjamin Sergeant | ||||
|  *  Copyright (c) 2017-2018 Machine Zone, Inc. All rights reserved. | ||||
|  */ | ||||
|  | ||||
| #include <iostream> | ||||
| #include <fstream> | ||||
| #include <sstream> | ||||
| #include <vector> | ||||
| #include <condition_variable> | ||||
| #include <mutex> | ||||
| #include <chrono> | ||||
| #include <ixwebsocket/IXWebSocket.h> | ||||
| #include <ixwebsocket/IXSocket.h> | ||||
| #include <ixcrypto/IXUuid.h> | ||||
| #include <ixcrypto/IXBase64.h> | ||||
| #include <ixcrypto/IXHash.h> | ||||
| #include <jsoncpp/json/json.h> | ||||
|  | ||||
| using namespace ix; | ||||
|  | ||||
| namespace | ||||
| { | ||||
|     void log(const std::string& msg) | ||||
|     { | ||||
|         std::cout << msg << std::endl; | ||||
|     } | ||||
|  | ||||
|     class WebSocketSender | ||||
|     { | ||||
|         public: | ||||
|             WebSocketSender(const std::string& _url, | ||||
|                             bool enablePerMessageDeflate); | ||||
|  | ||||
|             void subscribe(const std::string& channel); | ||||
|             void start(); | ||||
|             void stop(); | ||||
|  | ||||
|             void waitForConnection(); | ||||
|             void waitForAck(); | ||||
|  | ||||
|             void sendMessage(const std::string& filename, bool throttle); | ||||
|  | ||||
|         private: | ||||
|             std::string _url; | ||||
|             std::string _id; | ||||
|             ix::WebSocket _webSocket; | ||||
|             bool _enablePerMessageDeflate; | ||||
|  | ||||
|             std::mutex _conditionVariableMutex; | ||||
|             std::condition_variable _condition; | ||||
|     }; | ||||
|  | ||||
|     WebSocketSender::WebSocketSender(const std::string& url, | ||||
|                                      bool enablePerMessageDeflate) : | ||||
|         _url(url), | ||||
|         _enablePerMessageDeflate(enablePerMessageDeflate) | ||||
|     { | ||||
|         ; | ||||
|     } | ||||
|  | ||||
|     void WebSocketSender::stop() | ||||
|     { | ||||
|         _webSocket.stop(); | ||||
|     } | ||||
|  | ||||
|     void WebSocketSender::waitForConnection() | ||||
|     { | ||||
|         std::cout << "Connecting..." << std::endl; | ||||
|  | ||||
|         std::unique_lock<std::mutex> lock(_conditionVariableMutex); | ||||
|         _condition.wait(lock); | ||||
|     } | ||||
|  | ||||
|     void WebSocketSender::waitForAck() | ||||
|     { | ||||
|         std::cout << "Waiting for ack..." << std::endl; | ||||
|  | ||||
|         std::unique_lock<std::mutex> lock(_conditionVariableMutex); | ||||
|         _condition.wait(lock); | ||||
|     } | ||||
|  | ||||
|     std::string load(const std::string& path) | ||||
|     { | ||||
|         // std::vector<uint8_t> memblock; | ||||
|         std::string str; | ||||
|  | ||||
|         std::ifstream file(path); | ||||
|         if (!file.is_open()) return std::string(); | ||||
|  | ||||
|         file.seekg(0, file.end); | ||||
|         std::streamoff size = file.tellg(); | ||||
|         file.seekg(0, file.beg); | ||||
|  | ||||
|         str.resize(size); | ||||
|         file.read((char*)&str.front(), static_cast<std::streamsize>(size)); | ||||
|  | ||||
|         return str; | ||||
|     } | ||||
|  | ||||
|     void WebSocketSender::start() | ||||
|     { | ||||
|         _webSocket.setUrl(_url); | ||||
|  | ||||
|         ix::WebSocketPerMessageDeflateOptions webSocketPerMessageDeflateOptions( | ||||
|             _enablePerMessageDeflate, false, false, 15, 15); | ||||
|         _webSocket.setPerMessageDeflateOptions(webSocketPerMessageDeflateOptions); | ||||
|  | ||||
|         std::stringstream ss; | ||||
|         log(std::string("Connecting to url: ") + _url); | ||||
|  | ||||
|         _webSocket.setOnMessageCallback( | ||||
|             [this](ix::WebSocketMessageType messageType, | ||||
|                const std::string& str, | ||||
|                size_t wireSize, | ||||
|                const ix::WebSocketErrorInfo& error, | ||||
|                const ix::WebSocketOpenInfo& openInfo, | ||||
|                const ix::WebSocketCloseInfo& closeInfo) | ||||
|             { | ||||
|                 std::stringstream ss; | ||||
|                 if (messageType == ix::WebSocket_MessageType_Open) | ||||
|                 { | ||||
|                     _condition.notify_one(); | ||||
|  | ||||
|                     log("ws_send: connected"); | ||||
|                     std::cout << "Uri: " << openInfo.uri << std::endl; | ||||
|                     std::cout << "Handshake Headers:" << std::endl; | ||||
|                     for (auto it : openInfo.headers) | ||||
|                     { | ||||
|                         std::cout << it.first << ": " << it.second << std::endl; | ||||
|                     } | ||||
|                 } | ||||
|                 else if (messageType == ix::WebSocket_MessageType_Close) | ||||
|                 { | ||||
|                     ss << "ws_send: connection closed:"; | ||||
|                     ss << " code " << closeInfo.code; | ||||
|                     ss << " reason " << closeInfo.reason << std::endl; | ||||
|                     log(ss.str()); | ||||
|                 } | ||||
|                 else if (messageType == ix::WebSocket_MessageType_Message) | ||||
|                 { | ||||
|                     _condition.notify_one(); | ||||
|  | ||||
|                     ss << "ws_send: received message: " | ||||
|                        << str; | ||||
|                     log(ss.str()); | ||||
|  | ||||
|                     Json::Value data; | ||||
|                     Json::Reader reader; | ||||
|                     if (!reader.parse(str, data)) | ||||
|                     { | ||||
|                         std::cerr << "Invalid JSON response" << std::endl; | ||||
|                         return; | ||||
|                     } | ||||
|  | ||||
|                     std::string id = data["id"].asString(); | ||||
|                     if (_id != id) | ||||
|                     { | ||||
|                         std::cerr << "Invalid id" << std::endl; | ||||
|                     } | ||||
|                 } | ||||
|                 else if (messageType == ix::WebSocket_MessageType_Error) | ||||
|                 { | ||||
|                     ss << "Connection error: " << error.reason      << std::endl; | ||||
|                     ss << "#retries: "         << error.retries     << std::endl; | ||||
|                     ss << "Wait time(ms): "    << error.wait_time   << std::endl; | ||||
|                     ss << "HTTP Status: "      << error.http_status << std::endl; | ||||
|                     log(ss.str()); | ||||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     ss << "Invalid ix::WebSocketMessageType"; | ||||
|                     log(ss.str()); | ||||
|                 } | ||||
|             }); | ||||
|  | ||||
|         _webSocket.start(); | ||||
|     } | ||||
|  | ||||
|     class Bench | ||||
|     { | ||||
|         public: | ||||
|             Bench(const std::string& description) : | ||||
|                 _description(description), | ||||
|                 _start(std::chrono::system_clock::now()), | ||||
|                 _reported(false) | ||||
|             { | ||||
|                 ; | ||||
|             } | ||||
|  | ||||
|             ~Bench() | ||||
|             { | ||||
|                 if (!_reported) | ||||
|                 { | ||||
|                     report(); | ||||
|                 } | ||||
|             } | ||||
|  | ||||
|             void report() | ||||
|             { | ||||
|                 auto now = std::chrono::system_clock::now(); | ||||
|                 auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(now - _start); | ||||
|  | ||||
|                 _ms = milliseconds.count(); | ||||
|                 std::cout << _description << " completed in " | ||||
|                           << _ms << "ms" << std::endl; | ||||
|  | ||||
|                 _reported = true; | ||||
|             } | ||||
|  | ||||
|             uint64_t getDuration() const | ||||
|             { | ||||
|                 return _ms; | ||||
|             } | ||||
|  | ||||
|         private: | ||||
|             std::string _description; | ||||
|             std::chrono::time_point<std::chrono::system_clock> _start; | ||||
|             uint64_t _ms; | ||||
|             bool _reported; | ||||
|     }; | ||||
|  | ||||
|     void WebSocketSender::sendMessage(const std::string& filename, | ||||
|                                       bool throttle) | ||||
|     { | ||||
|         std::string content; | ||||
|         { | ||||
|             Bench bench("load file from disk"); | ||||
|             content = load(filename); | ||||
|         } | ||||
|  | ||||
|         _id = uuid4(); | ||||
|  | ||||
|         std::string b64Content; | ||||
|         { | ||||
|             Bench bench("base 64 encode file"); | ||||
|             b64Content = base64_encode(content, content.size()); | ||||
|         } | ||||
|  | ||||
|         Json::Value pdu; | ||||
|         pdu["kind"] = "send"; | ||||
|         pdu["id"] = _id; | ||||
|         pdu["content"] = b64Content; | ||||
|         pdu["djb2_hash"] = djb2Hash(b64Content); | ||||
|         pdu["filename"] = filename; | ||||
|  | ||||
|         Bench bench("Sending file through websocket"); | ||||
|         _webSocket.send(pdu.toStyledString(), | ||||
|                         [throttle](int current, int total) -> bool | ||||
|         { | ||||
|             std::cout << "Step " << current << " out of " << total << std::endl; | ||||
|  | ||||
|             if (throttle) | ||||
|             { | ||||
|                 std::chrono::duration<double, std::milli> duration(10); | ||||
|                 std::this_thread::sleep_for(duration); | ||||
|             } | ||||
|  | ||||
|             return true; | ||||
|         }); | ||||
|  | ||||
|         bench.report(); | ||||
|         auto duration = bench.getDuration(); | ||||
|         auto transferRate = 1000 * b64Content.size() / duration; | ||||
|         transferRate /= (1024 * 1024); | ||||
|         std::cout << "Send transfer rate: " << transferRate << "MB/s" << std::endl; | ||||
|     } | ||||
|  | ||||
|     void wsSend(const std::string& url, | ||||
|                 const std::string& path, | ||||
|                 bool enablePerMessageDeflate, | ||||
|                 bool throttle) | ||||
|     { | ||||
|         WebSocketSender webSocketSender(url, enablePerMessageDeflate); | ||||
|         webSocketSender.start(); | ||||
|  | ||||
|         webSocketSender.waitForConnection(); | ||||
|  | ||||
|         std::cout << "Sending..." << std::endl; | ||||
|         webSocketSender.sendMessage(path, throttle); | ||||
|  | ||||
|         webSocketSender.waitForAck(); | ||||
|  | ||||
|         std::cout << "Done !" << std::endl; | ||||
|         webSocketSender.stop(); | ||||
|     } | ||||
| } | ||||
|  | ||||
| int main(int argc, char** argv) | ||||
| { | ||||
|     if (argc != 3) | ||||
|     { | ||||
|         std::cerr << "Usage: ws_send <url> <path>" << std::endl; | ||||
|         return 1; | ||||
|     } | ||||
|     std::string url = argv[1]; | ||||
|     std::string path = argv[2]; | ||||
|  | ||||
|     bool throttle = false; | ||||
|     bool enablePerMessageDeflate = false; | ||||
|  | ||||
|     Socket::init(); | ||||
|     wsSend(url, path, enablePerMessageDeflate, throttle); | ||||
|     return 0; | ||||
| } | ||||
		Reference in New Issue
	
	Block a user