From 3bf686858814e44762855751ecf26d7cee797424 Mon Sep 17 00:00:00 2001 From: John Pye Date: Wed, 24 Nov 2021 23:20:55 +1100 Subject: [PATCH 1/4] fix for gcc10 with MSYS2 attempting to copy apparently private changes at https://github.com/graphstream/gs-netstream/pull/9 changed Makefile to detect Windows using $(OS) instead of uname. --- c++/.gitignore | 6 ++++ c++/Makefile | 3 +- c++/src/netstream-sender.cpp | 6 ++-- c++/src/netstream-socket.cpp | 12 ++------ c++/src/netstream-socket.h | 26 +++++++++--------- c++/src/netstream-storage.cpp | 50 ++++++++++++++++----------------- c++/src/netstream-storage.h | 52 +++++++++++++++++------------------ 7 files changed, 77 insertions(+), 78 deletions(-) create mode 100644 c++/.gitignore diff --git a/c++/.gitignore b/c++/.gitignore new file mode 100644 index 0000000..291ff7f --- /dev/null +++ b/c++/.gitignore @@ -0,0 +1,6 @@ +netstream-main +*.exe +*.dll +*.so +*.o +*.a diff --git a/c++/Makefile b/c++/Makefile index 9df09d7..973442b 100644 --- a/c++/Makefile +++ b/c++/Makefile @@ -18,7 +18,8 @@ CDBG = -g $(CWARN) -fno-inline CFLAGS = -I$(INCDIR) -O3 DFLAGS = -I$(INCDIR) -g $(CWARN) -fno-inline -DDEBUG=1 -ifeq ($(shell uname),MINGW32_NT-6.1) +# https://stackoverflow.com/questions/714100/os-detecting-makefile +ifeq ($(OS),Windows_NT) SOCKET_LIB = -lwsock32 else SOCKET_LIB = diff --git a/c++/src/netstream-sender.cpp b/c++/src/netstream-sender.cpp index bd77a04..cd8cc61 100644 --- a/c++/src/netstream-sender.cpp +++ b/c++/src/netstream-sender.cpp @@ -55,9 +55,9 @@ void NetStreamSender::init() std::cout<<"."<= 0 ) return; @@ -282,7 +281,6 @@ namespace netstream void NetStreamSocket:: set_blocking(bool blocking) - throw(NetStreamSocketException ) { blocking_ = blocking; @@ -310,7 +308,6 @@ namespace netstream void NetStreamSocket:: connect() - throw( NetStreamSocketException ) { in_addr addr; if( !atoaddr( host_.c_str(), addr) ) @@ -359,7 +356,6 @@ namespace netstream void NetStreamSocket:: send( std::vector b) - throw( NetStreamSocketException ) { if( socket_ < 0 ) return; @@ -411,7 +407,6 @@ namespace netstream void NetStreamSocket:: sendExact( const NetStreamStorage &b) - throw( NetStreamSocketException ) { int length = static_cast(b.size()); NetStreamStorage length_storage; @@ -428,7 +423,6 @@ namespace netstream vector NetStreamSocket:: receive(int bufSize) - throw( NetStreamSocketException ) { vector b; @@ -474,7 +468,6 @@ namespace netstream bool NetStreamSocket:: receiveExact( NetStreamStorage &msg ) - throw( NetStreamSocketException ) { /* receive length of vector */ unsigned char * const bufLength = new unsigned char[4]; @@ -552,7 +545,6 @@ namespace netstream bool NetStreamSocket:: is_blocking() - throw() { return blocking_; } diff --git a/c++/src/netstream-socket.h b/c++/src/netstream-socket.h index 71c6394..f2993c7 100644 --- a/c++/src/netstream-socket.h +++ b/c++/src/netstream-socket.h @@ -48,18 +48,18 @@ namespace netstream private: std::string what_; public: - NetStreamSocketException( std::string what ) throw() + NetStreamSocketException( std::string what ) { what_ = what; //std::cerr << "netstream::NetStreamSocketException: " << what << std::endl << std::flush; } - virtual const char* what() const throw() + virtual const char* what() const noexcept { return what_.c_str(); } - ~NetStreamSocketException() throw() {} + ~NetStreamSocketException() {} }; class NetStreamSocket @@ -76,18 +76,18 @@ namespace netstream ~NetStreamSocket(); /// Connects to host_:port_ - void connect() throw( NetStreamSocketException ); + void connect(); /// Wait for a incoming connection to port_ - void accept() throw( NetStreamSocketException ); - void send( const std::vector ) throw( NetStreamSocketException ); - void sendExact( const NetStreamStorage & ) throw( NetStreamSocketException ); - std::vector receive( int bufSize = 2048 ) throw( NetStreamSocketException ); - bool receiveExact( NetStreamStorage &) throw( NetStreamSocketException ); + void accept(); + void send( const std::vector ) ; + void sendExact( const NetStreamStorage & ) ; + std::vector receive( int bufSize = 2048 ) ; + bool receiveExact( NetStreamStorage &) ; void close(); int port(); - void set_blocking(bool) throw( NetStreamSocketException ); - bool is_blocking() throw(); + void set_blocking(bool) ; + bool is_blocking() ; bool has_client_connection() const; // If verbose, each send and received data is written to stderr @@ -96,12 +96,12 @@ namespace netstream private: void init(); - void BailOnNetStreamSocketError( std::string ) const throw( NetStreamSocketException ); + void BailOnNetStreamSocketError( std::string ) const ; #ifdef WIN32 std::string GetWinsockErrorString(int err) const; #endif bool atoaddr(std::string, struct in_addr& addr); - bool datawaiting(int sock) const throw(); + bool datawaiting(int sock) const ; std::string host_; int port_; diff --git a/c++/src/netstream-storage.cpp b/c++/src/netstream-storage.cpp index 6b36e74..46934d9 100644 --- a/c++/src/netstream-storage.cpp +++ b/c++/src/netstream-storage.cpp @@ -90,7 +90,7 @@ namespace netstream * Reads a char form the array * @return The read char (between 0 and 255) */ - unsigned char NetStreamStorage::readChar() throw(std::invalid_argument) + unsigned char NetStreamStorage::readChar() { if ( !valid_pos() ) { @@ -104,7 +104,7 @@ namespace netstream /** * */ - void NetStreamStorage::writeChar(unsigned char value) throw() + void NetStreamStorage::writeChar(unsigned char value) { store.push_back(value); iter_ = store.begin(); @@ -138,7 +138,7 @@ namespace netstream * Reads a varint form the array * @return The read varint */ - int_fast64_t NetStreamStorage::readVarint() throw(std::invalid_argument) + int_fast64_t NetStreamStorage::readVarint() { uint_fast64_t number = readUnsignedVarint(); return (int_fast64_t)((number & 1) == 0) ? number >> 1 : -(number >> 1); @@ -147,7 +147,7 @@ namespace netstream /** * */ - void NetStreamStorage::writeVarint(int_fast64_t value) throw(std::invalid_argument) + void NetStreamStorage::writeVarint(int_fast64_t value) { writeUnsignedVarint((value << 1) ^ (value >> 63)); } @@ -156,7 +156,7 @@ namespace netstream * Reads a unsigned varint form the array * @return The read u_varint */ - uint_fast64_t NetStreamStorage::readUnsignedVarint() throw(std::invalid_argument) + uint_fast64_t NetStreamStorage::readUnsignedVarint() { // TODO return 0; @@ -165,7 +165,7 @@ namespace netstream /** * */ - void NetStreamStorage::writeUnsignedVarint(uint_fast64_t value) throw(std::invalid_argument) + void NetStreamStorage::writeUnsignedVarint(uint_fast64_t value) { size_t size = varintSize(value); @@ -185,7 +185,7 @@ namespace netstream * Reads a byte form the array * @return The read byte (between -128 and 127) */ - int NetStreamStorage::readByte() throw(std::invalid_argument) + int NetStreamStorage::readByte() { int i = static_cast(readChar()); if (i < 128) return i; @@ -197,7 +197,7 @@ namespace netstream /** * */ - void NetStreamStorage::writeByte(int value) throw(std::invalid_argument) + void NetStreamStorage::writeByte(int value) { if (value < -128 || value > 127) { @@ -212,7 +212,7 @@ namespace netstream * Reads an unsigned byte form the array * @return The read byte (between 0 and 255) */ - int NetStreamStorage::readUnsignedByte() throw(std::invalid_argument) + int NetStreamStorage::readUnsignedByte() { return static_cast(readChar()); } @@ -222,7 +222,7 @@ namespace netstream /** * */ - void NetStreamStorage::writeUnsignedByte(int value) throw(std::invalid_argument) + void NetStreamStorage::writeUnsignedByte(int value) { if (value < 0 || value > 255) { @@ -237,7 +237,7 @@ namespace netstream * Reads a string form the array * @return The read string */ - std::string NetStreamStorage::readString() throw(std::invalid_argument) + std::string NetStreamStorage::readString() { int len = readInt(); checkReadSafe(len); @@ -254,7 +254,7 @@ namespace netstream * Writes a string into the array; * @param s The string to be written */ - void NetStreamStorage::writeString(const std::string &s) throw() + void NetStreamStorage::writeString(const std::string &s) { writeUnsignedVarint(static_cast(s.length())); store.insert(store.end(), s.begin(), s.end()); @@ -267,7 +267,7 @@ namespace netstream * Reads a string list form the array * @return The read string */ - std::vector NetStreamStorage::readStringList() throw(std::invalid_argument) + std::vector NetStreamStorage::readStringList() { std::vector tmp; const int len = readInt(); @@ -285,7 +285,7 @@ namespace netstream * Writes a string into the array; * @param s The string to be written */ - void NetStreamStorage::writeStringList(const std::vector &s) throw() + void NetStreamStorage::writeStringList(const std::vector &s) { writeUnsignedVarint(s.size()); for (std::vector::const_iterator it = s.begin(); it!=s.end() ; it++) @@ -303,7 +303,7 @@ namespace netstream * * @return the unspoiled integer value (between -32768 and 32767) */ - int NetStreamStorage::readShort() throw(std::invalid_argument) + int NetStreamStorage::readShort() { short value = 0; unsigned char *p_value = reinterpret_cast(&value); @@ -313,7 +313,7 @@ namespace netstream // ---------------------------------------------------------------------- - void NetStreamStorage::writeShort( int value ) throw(std::invalid_argument) + void NetStreamStorage::writeShort( int value ) { if (value < -32768 || value > 32767) { @@ -335,7 +335,7 @@ namespace netstream * * @return the unspoiled integer value (between -2.147.483.648 and 2.147.483.647) */ - int NetStreamStorage::readInt() throw(std::invalid_argument) + int NetStreamStorage::readInt() { int value = 0; unsigned char *p_value = reinterpret_cast(&value); @@ -345,7 +345,7 @@ namespace netstream // ---------------------------------------------------------------------- - void NetStreamStorage::writeInt( int value ) throw() + void NetStreamStorage::writeInt( int value ) { unsigned char *p_value = reinterpret_cast(&value); writeByEndianess(p_value, 4); @@ -359,7 +359,7 @@ namespace netstream * * @return the unspoiled integer value (between -??? and ???) */ - long NetStreamStorage::readLong() throw(std::invalid_argument) + long NetStreamStorage::readLong() { long value = 0L; unsigned char *p_value = reinterpret_cast(&value); @@ -369,7 +369,7 @@ namespace netstream // ---------------------------------------------------------------------- - void NetStreamStorage::writeLong( long value ) throw() + void NetStreamStorage::writeLong( long value ) { unsigned char *p_value = reinterpret_cast(&value); writeByEndianess(p_value, 8); @@ -384,7 +384,7 @@ namespace netstream * * @return the unspoiled float value */ - float NetStreamStorage::readFloat() throw(std::invalid_argument) + float NetStreamStorage::readFloat() { float value = 0; unsigned char *p_value = reinterpret_cast(&value); @@ -394,7 +394,7 @@ namespace netstream // ---------------------------------------------------------------------- - void NetStreamStorage::writeFloat( float value ) throw() + void NetStreamStorage::writeFloat( float value ) { unsigned char *p_value = reinterpret_cast(&value); writeByEndianess(p_value, 4); @@ -402,7 +402,7 @@ namespace netstream // ---------------------------------------------------------------------- - void NetStreamStorage::writeDouble( double value ) throw () + void NetStreamStorage::writeDouble( double value ) { unsigned char *p_value = reinterpret_cast(&value); writeByEndianess(p_value, 8); @@ -410,7 +410,7 @@ namespace netstream // ---------------------------------------------------------------------- - double NetStreamStorage::readDouble( ) throw (std::invalid_argument) + double NetStreamStorage::readDouble( ) { double value = 0; unsigned char *p_value = reinterpret_cast(&value); @@ -437,7 +437,7 @@ namespace netstream // ---------------------------------------------------------------------- - void NetStreamStorage::checkReadSafe(unsigned int num) const throw(std::invalid_argument) + void NetStreamStorage::checkReadSafe(unsigned int num) const { if (std::distance(iter_, store.end()) < static_cast(num)) { diff --git a/c++/src/netstream-storage.h b/c++/src/netstream-storage.h index c6d90a7..c20fcba 100644 --- a/c++/src/netstream-storage.h +++ b/c++/src/netstream-storage.h @@ -41,7 +41,7 @@ class NetStreamStorage void init(); /// Check if the next \p num bytes can be read safely - void checkReadSafe(unsigned int num) const throw(std::invalid_argument); + void checkReadSafe(unsigned int num) const; /// Read a byte \em without validity check unsigned char readCharUnsafe(); /// Write \p size elements of array \p begin according to endianess @@ -68,42 +68,42 @@ class NetStreamStorage void reset(); virtual size_t varintSize(uint_fast64_t); - virtual uint_fast64_t readUnsignedVarint() throw(std::invalid_argument); - virtual void writeUnsignedVarint(uint_fast64_t) throw(std::invalid_argument); + virtual uint_fast64_t readUnsignedVarint() ; + virtual void writeUnsignedVarint(uint_fast64_t) ; - virtual int_fast64_t readVarint() throw(std::invalid_argument); - virtual void writeVarint(int_fast64_t) throw(std::invalid_argument); + virtual int_fast64_t readVarint() ; + virtual void writeVarint(int_fast64_t) ; - virtual unsigned char readChar() throw(std::invalid_argument); - virtual void writeChar(unsigned char) throw(); + virtual unsigned char readChar() ; + virtual void writeChar(unsigned char) ; - virtual int readByte() throw(std::invalid_argument); - virtual void writeByte(int) throw(std::invalid_argument); -// virtual void writeByte(unsigned char) throw(); + virtual int readByte() ; + virtual void writeByte(int) ; +// virtual void writeByte(unsigned char) ; - virtual int readUnsignedByte() throw(std::invalid_argument); - virtual void writeUnsignedByte(int) throw(std::invalid_argument); + virtual int readUnsignedByte() ; + virtual void writeUnsignedByte(int) ; - virtual std::string readString() throw(std::invalid_argument); - virtual void writeString(const std::string& s) throw(); + virtual std::string readString() ; + virtual void writeString(const std::string& s) ; - virtual std::vector readStringList() throw(std::invalid_argument); - virtual void writeStringList(const std::vector &s) throw(); + virtual std::vector readStringList() ; + virtual void writeStringList(const std::vector &s) ; - virtual int readShort() throw(std::invalid_argument); - virtual void writeShort(int) throw(std::invalid_argument); + virtual int readShort() ; + virtual void writeShort(int) ; - virtual int readInt() throw(std::invalid_argument); - virtual void writeInt(int) throw(); + virtual int readInt() ; + virtual void writeInt(int) ; - virtual long readLong() throw(std::invalid_argument); - virtual void writeLong(long) throw(); + virtual long readLong() ; + virtual void writeLong(long) ; - virtual float readFloat() throw(std::invalid_argument); - virtual void writeFloat( float ) throw(); + virtual float readFloat() ; + virtual void writeFloat( float ) ; - virtual double readDouble() throw(std::invalid_argument); - virtual void writeDouble( double ) throw(); + virtual double readDouble() ; + virtual void writeDouble( double ) ; virtual void writePacket(unsigned char* packet, int length); From 9a6db429f14b5790dca1530f3202f831d8602936 Mon Sep 17 00:00:00 2001 From: John Pye Date: Wed, 24 Nov 2021 23:30:30 +1100 Subject: [PATCH 2/4] Update README.rst --- README.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.rst b/README.rst index 55182e5..58329af 100644 --- a/README.rst +++ b/README.rst @@ -22,4 +22,9 @@ check the `manual`_ for a full description of the protocol .. _manual: https://github.com/graphstream/gs-netstream/wiki/NetStream-Manual +Building for C++ +---------------- +For C++, you can build gs-netstream using the code in the `c++` directory. Just run 'make'. + +This has been tested to build with gcc 11.2.0 on MSYS2 (as of Nov 2021) and with gcc 9.3 on Ubuntu 20.04. Only building was tested. From e67b6d28f00074776a728a176fddcb0582d15d07 Mon Sep 17 00:00:00 2001 From: John Pye Date: Sat, 15 Jan 2022 19:41:23 +1100 Subject: [PATCH 3/4] detect windows with _WIN32 --- c++/src/netstream-socket.cpp | 75 +++++++++++++++--------------------- c++/src/netstream-socket.h | 17 +++----- 2 files changed, 36 insertions(+), 56 deletions(-) diff --git a/c++/src/netstream-socket.cpp b/c++/src/netstream-socket.cpp index f5fa6d9..45bd8d5 100644 --- a/c++/src/netstream-socket.cpp +++ b/c++/src/netstream-socket.cpp @@ -13,29 +13,24 @@ #include "netstream-socket.h" #endif - - -#ifndef WIN32 - #include - #include - #include - #include - #include - #include - #include - #include - #include +#ifndef _WIN32 +# include +# include +# include +# include +# include +# include +# include +# include +# include #else - #ifdef ERROR - #undef ERROR - #endif - - #include - - #ifndef vsnprintf - #define vsnprintf _vsnprintf - #endif - +# ifdef ERROR +# undef ERROR +# endif +# include +# ifndef vsnprintf +# define vsnprintf _vsnprintf +# endif #endif #include @@ -45,7 +40,6 @@ #include #include #include -#include using namespace std; @@ -59,7 +53,7 @@ using namespace std; namespace netstream { -#ifdef WIN32 +#ifdef _WIN32 bool NetStreamSocket::init_windows_sockets_ = true; bool NetStreamSocket::windows_sockets_initialized_ = false; int NetStreamSocket::instance_count_ = 0; @@ -96,7 +90,7 @@ namespace netstream NetStreamSocket:: init() { -#ifdef WIN32 +#ifdef _WIN32 instance_count_++; if( init_windows_sockets_ && !windows_sockets_initialized_ ) @@ -115,14 +109,14 @@ namespace netstream { // Close first an existing client connection ... close(); -#ifdef WIN32 +#ifdef _WIN32 instance_count_--; #endif // ... then the server socket if( server_socket_ >= 0 ) { -#ifdef WIN32 +#ifdef _WIN32 ::closesocket( server_socket_ ); #else ::close( server_socket_ ); @@ -130,7 +124,7 @@ namespace netstream server_socket_ = -1; } -#ifdef WIN32 +#ifdef _WIN32 if( server_socket_ == -1 && socket_ == -1 && init_windows_sockets_ && instance_count_ == 0 ) WSACleanup(); @@ -144,7 +138,7 @@ namespace netstream BailOnNetStreamSocketError( std::string context) const { -#ifdef WIN32 +#ifdef _WIN32 int e = WSAGetLastError(); std::string msg = GetWinsockErrorString( e ); #else @@ -222,7 +216,7 @@ namespace netstream return; struct sockaddr_in client_addr; -#ifdef WIN32 +#ifdef _WIN32 int addrlen = sizeof(client_addr); #else socklen_t addrlen = sizeof(client_addr); @@ -241,7 +235,7 @@ namespace netstream { int reuseaddr = 1; - #ifdef WIN32 + #ifdef _WIN32 //setsockopt(server_socket_, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuseaddr, sizeof(reuseaddr)); // No address reuse in Windows!!! #else @@ -286,7 +280,7 @@ namespace netstream if( server_socket_ > 0 ) { -#ifdef WIN32 +#ifdef _WIN32 ULONG NonBlock = blocking_ ? 0 : 1; if (ioctlsocket(server_socket_, FIONBIO, &NonBlock) == SOCKET_ERROR) BailOnNetStreamSocketError("netstream::NetStreamSocket::set_blocking() Unable to initialize non blocking I/O"); @@ -342,7 +336,7 @@ namespace netstream // Close client-connection if( socket_ >= 0 ) { -#ifdef WIN32 +#ifdef _WIN32 ::closesocket( socket_ ); #else ::close( socket_ ); @@ -381,7 +375,7 @@ namespace netstream unsigned char const *buf_ptr = buf; while( numbytes > 0 ) { -#ifdef WIN32 +#ifdef _WIN32 int n = ::send( socket_, (const char*)buf_ptr, static_cast(numbytes), 0 ); #else int n = ::send( socket_, buf_ptr, numbytes, 0 ); @@ -550,7 +544,7 @@ namespace netstream } -#ifdef WIN32 +#ifdef _WIN32 // ---------------------------------------------------------------------- std::string NetStreamSocket:: @@ -615,15 +609,8 @@ namespace netstream return "unknown"; } -#endif // WIN32 +#endif // _WIN32 } // namespace tcpip - -/*----------------------------------------------------------------------- -* Source $Source: $ -* Version $Revision: 385 $ -* Date $Date: 2010-01-13 16:10:20 +0100 (Wed, 13 Jan 2010) $ -*----------------------------------------------------------------------- -* $Log: $ -*-----------------------------------------------------------------------*/ +// vim: ts=4:sw=4:noet diff --git a/c++/src/netstream-socket.h b/c++/src/netstream-socket.h index f2993c7..ea78ead 100644 --- a/c++/src/netstream-socket.h +++ b/c++/src/netstream-socket.h @@ -26,8 +26,8 @@ #endif // Disable exception handling warnings -#ifdef WIN32 - #pragma warning( disable : 4290 ) +#ifdef _WIN32 +# pragma warning( disable : 4290 ) #endif #include @@ -97,7 +97,7 @@ namespace netstream private: void init(); void BailOnNetStreamSocketError( std::string ) const ; -#ifdef WIN32 +#ifdef _WIN32 std::string GetWinsockErrorString(int err) const; #endif bool atoaddr(std::string, struct in_addr& addr); @@ -110,7 +110,7 @@ namespace netstream bool blocking_; bool verbose_; -#ifdef WIN32 +#ifdef _WIN32 static bool init_windows_sockets_; static bool windows_sockets_initialized_; static int instance_count_; @@ -121,11 +121,4 @@ namespace netstream #endif // BUILD_TCPIP - -/*----------------------------------------------------------------------- -* Source $Source: $ -* Version $Revision: 197 $ -* Date $Date: 2008-04-29 17:40:51 +0200 (Tue, 29 Apr 2008) $ -*----------------------------------------------------------------------- -* $Log:$ -*-----------------------------------------------------------------------*/ +// vim: ts=4:sw=4:noet From 68b8971cf7e5327b5ba679713c541ee1b2774fc6 Mon Sep 17 00:00:00 2001 From: John Pye Date: Sun, 16 Jan 2022 13:38:04 +1100 Subject: [PATCH 4/4] attempting to fix WIN32 handling of long and long long --- c++/src/netstream-constants.h | 20 ++++++++ c++/src/netstream-sender.cpp | 89 +++++++++++++++++++++++++++++------ c++/src/netstream-sender.h | 38 ++++++++++++--- c++/src/netstream-storage.cpp | 37 +++++++++++---- c++/src/netstream-storage.h | 3 ++ 5 files changed, 156 insertions(+), 31 deletions(-) diff --git a/c++/src/netstream-constants.h b/c++/src/netstream-constants.h index 620d6bf..5faa18d 100644 --- a/c++/src/netstream-constants.h +++ b/c++/src/netstream-constants.h @@ -154,16 +154,19 @@ namespace netstream{ * An array of bytes. Followed by first, a 16-bits integer for the number * of integers and then, a list of signed bytes. */ + const unsigned char TYPE_BYTE_ARRAY=0x53; /** * Followed by an 16-bit signed integer (a short) */ const unsigned char TYPE_SHORT=0x54; + /** * An array of shorts. Followed by first, a 16-bits integer for the number * of integers and then, a list of 16-bit signed shorts */ const unsigned char TYPE_SHORT_ARRAY=0x55; + /** * Followed by an 32-bit signed integer */ @@ -173,6 +176,7 @@ namespace netstream{ * of integers and then, a list of 32-bit signed integers */ const unsigned char TYPE_INT_ARRAY=0x57; + /** * Followed by an 64-bit signed integer */ @@ -182,6 +186,9 @@ namespace netstream{ * longs and then, a list of 62-bit signed integers */ const unsigned char TYPE_LONG_ARRAY=0x59; + +/** NOTE: see also LONGLONG below, out of order */ + /** * Followed by a single precision 32-bits floating point number */ @@ -219,6 +226,19 @@ namespace netstream{ */ const unsigned char TYPE_ARRAY=0x60; +/** + * Followed by an 64-bit signed integer + */ + const unsigned char TYPE_LONGLONG=0x61; +/** + * An array of longs. Followed by first, a 16-bits integer for the number of + * longs and then, a list of 62-bit signed integers + */ + const unsigned char TYPE_LONGLONG_ARRAY=0x62; + + + + }// end netstream namespace #endif diff --git a/c++/src/netstream-sender.cpp b/c++/src/netstream-sender.cpp index cd8cc61..2cba00c 100644 --- a/c++/src/netstream-sender.cpp +++ b/c++/src/netstream-sender.cpp @@ -74,21 +74,40 @@ int NetStreamSender::_getType(char object){ } int NetStreamSender::_getType(bool object){ + if(debug){ + cerr<<" NetStreamSender: _getType : bool"< & object){ + if(debug){ + cerr<<" NetStreamSender: _getType : short*"< & object){ -if(debug){ - cerr<<" NetStreamSender: _getType : int*"< & object){ -if(debug){ - cerr<<" NetStreamSender: _getType : long*"< & object){ + if(debug){ + cerr<<" NetStreamSender: _getType : long long*" < & object){ if(debug){ cerr<<" NetStreamSender: _getType : float*"< & val event.writeByte((*i)); } } +void NetStreamSender::_encode(NetStreamStorage & event, const vector & value){ + event.writeUnsignedVarint(value.size()); + for(vector::const_iterator i = value.begin(); i != value.end(); i++){ + event.writeVarint((*i)); + } +} void NetStreamSender::_encode(NetStreamStorage & event, const vector & value){ event.writeUnsignedVarint(value.size()); for(vector::const_iterator i = value.begin(); i != value.end(); i++){ event.writeVarint((*i)); - } + } } void NetStreamSender::_encode(NetStreamStorage & event, const vector & value){ event.writeUnsignedVarint(value.size()); @@ -185,6 +237,13 @@ void NetStreamSender::_encode(NetStreamStorage & event, const vector & val event.writeVarint((*i)); } } +void NetStreamSender::_encode(NetStreamStorage & event, const vector & value){ + event.writeUnsignedVarint(value.size()); + for(vector::const_iterator i = value.begin(); i != value.end(); i++){ + event.writeVarint((*i)); + } +} + void NetStreamSender::_encode(NetStreamStorage & event, const vector & value){ event.writeUnsignedVarint(value.size()); for(vector::const_iterator i = value.begin(); i != value.end(); i++){ diff --git a/c++/src/netstream-sender.h b/c++/src/netstream-sender.h index aabf366..5dfae8d 100644 --- a/c++/src/netstream-sender.h +++ b/c++/src/netstream-sender.h @@ -14,17 +14,35 @@ #define NETSTREAM_SENDER_H - - #ifndef WIN32 #include #else #include #endif +#include +#define STATIC_ASSERT(condition) typedef char __static_assert##__LINE__[(condition)?1:-1] #include +#ifdef _WIN32 +STATIC_ASSERT(sizeof(short)==sizeof(int16_t)); +STATIC_ASSERT(sizeof(int)==sizeof(int32_t)); +STATIC_ASSERT(sizeof(long)==sizeof(int32_t)); +STATIC_ASSERT(sizeof(long long)==sizeof(int64_t)); +STATIC_ASSERT(sizeof(void*)==sizeof(int64_t)); +#define GS_LONG long long +#else +STATIC_ASSERT(sizeof(short)==sizeof(int16_t)); +STATIC_ASSERT(sizeof(int)==sizeof(int32_t)); +STATIC_ASSERT(sizeof(long)==sizeof(int64_t)); +STATIC_ASSERT(sizeof(long long)==sizeof(int64_t)); +STATIC_ASSERT(sizeof(void*)==sizeof(int64_t)); +#define GS_LONG long +#endif + +#define GS_STRINGIFY(VAR) #VAR + #include "netstream-storage.h" #include "netstream-socket.h" #include "netstream-constants.h" @@ -59,15 +77,19 @@ class NetStreamSender{ int _getType(char object); int _getType(bool object); + int _getType(short object); int _getType(int object); int _getType(long object); + int _getType(long long object); int _getType(float object); int _getType(double object); int _getType(const string & object); int _getType( const vector & object); int _getType( const vector & object); + int _getType( const vector & object); int _getType( const vector & object); int _getType( const vector & object); + int _getType( const vector & object); int _getType( const vector & object); int _getType( const vector & object); @@ -81,18 +103,22 @@ class NetStreamSender{ } void _encode(NetStreamStorage & event, char value); void _encode(NetStreamStorage & event, bool value); + void _encode(NetStreamStorage & event, short value); void _encode(NetStreamStorage & event, int value); void _encode(NetStreamStorage & event, long value); + void _encode(NetStreamStorage & event, long long value); void _encode(NetStreamStorage & event, float value); void _encode(NetStreamStorage & event, double value); void _encode(NetStreamStorage & event, const string & value); void _encode(NetStreamStorage & event, const vector & value); void _encode(NetStreamStorage & event, const vector & value); + void _encode(NetStreamStorage & event, const vector & value); void _encode(NetStreamStorage & event, const vector & value); - void _encode(NetStreamStorage & event, const vector & value); - void _encode(NetStreamStorage & event, const vector & value); - void _encode(NetStreamStorage & event, const vector & value); + void _encode(NetStreamStorage & event, const vector & value); + void _encode(NetStreamStorage & event, const vector & value); + void _encode(NetStreamStorage & event, const vector & value); + void _encode(NetStreamStorage & event, const vector & value); void _sendEvent(NetStreamStorage &); @@ -211,4 +237,4 @@ class NetStreamSender{ }; } -#endif \ No newline at end of file +#endif diff --git a/c++/src/netstream-storage.cpp b/c++/src/netstream-storage.cpp index 46934d9..5cac5c7 100644 --- a/c++/src/netstream-storage.cpp +++ b/c++/src/netstream-storage.cpp @@ -114,21 +114,21 @@ namespace netstream size_t NetStreamStorage::varintSize(uint_fast64_t data){ // 7 bits -> 127 - if(data < (1L << 7)){return 1;} + if(data < (1LL << 7)){return 1;} // 14 bits -> 16383 - if(data < (1L << 14)){return 2;} + if(data < (1LL << 14)){return 2;} // 21 bits -> 2097151 - if(data < (1L << 21)){return 3;} + if(data < (1LL << 21)){return 3;} // 28 bits -> 268435455 - if(data < (1L << 28)){return 4;} + if(data < (1LL << 28)){return 4;} // 35 bits -> 34359738367 - if(data < (1L << 35)){return 5;} + if(data < (1LL << 35)){return 5;} // 42 bits -> 4398046511103 - if(data < (1L << 42)){return 6;} + if(data < (1LL << 42)){return 6;} // 49 bits -> 562949953421311 - if(data < (1L << 49)){return 7;} + if(data < (1LL << 49)){return 7;} // 56 bits -> 72057594037927935 - if(data < (1L << 56)){return 8;} + if(data < (1LL << 56)){return 8;} return 9; } @@ -363,7 +363,7 @@ namespace netstream { long value = 0L; unsigned char *p_value = reinterpret_cast(&value); - readByEndianess(p_value, 8); + readByEndianess(p_value, sizeof(long)); return value; } @@ -372,8 +372,25 @@ namespace netstream void NetStreamStorage::writeLong( long value ) { unsigned char *p_value = reinterpret_cast(&value); - writeByEndianess(p_value, 8); + writeByEndianess(p_value, sizeof(long)); + } + + + + long long NetStreamStorage::readLongLong() + { + long long value = 0L; + unsigned char *p_value = reinterpret_cast(&value); + readByEndianess(p_value, sizeof(long long)); + return value; } + + void NetStreamStorage::writeLongLong( long long value ) + { + unsigned char *p_value = reinterpret_cast(&value); + writeByEndianess(p_value, sizeof(long long)); + } + // ---------------------------------------------------------------------- diff --git a/c++/src/netstream-storage.h b/c++/src/netstream-storage.h index c20fcba..4a49d80 100644 --- a/c++/src/netstream-storage.h +++ b/c++/src/netstream-storage.h @@ -99,6 +99,9 @@ class NetStreamStorage virtual long readLong() ; virtual void writeLong(long) ; + virtual long long readLongLong() ; + virtual void writeLongLong(long long) ; + virtual float readFloat() ; virtual void writeFloat( float ) ;