From cd34f696ce15c048962f4268cf904a32fd172229 Mon Sep 17 00:00:00 2001 From: Eric Frias Date: Mon, 21 Apr 2014 14:34:46 -0400 Subject: [PATCH 01/15] - Add four-argument version of json-rpc call function - fix bug in json parser that prevented correct parsing of a true/false/null at the end of input - prevent infinite recursion in a json helper function --- include/fc/io/json.hpp | 2 +- include/fc/rpc/json_connection.hpp | 17 +++++++ src/io/json.cpp | 79 +++++++++++++++++++----------- src/rpc/json_connection.cpp | 23 +++++++++ 4 files changed, 91 insertions(+), 30 deletions(-) diff --git a/include/fc/io/json.hpp b/include/fc/io/json.hpp index 2117fd0..16233e6 100644 --- a/include/fc/io/json.hpp +++ b/include/fc/io/json.hpp @@ -56,7 +56,7 @@ namespace fc template static void save_to_file( const T& v, const string& p, bool pretty = true ) { - save_to_file( variant(v), p, pretty ); + save_to_file( variant(v), fc::path(p), pretty ); } }; diff --git a/include/fc/rpc/json_connection.hpp b/include/fc/rpc/json_connection.hpp index 0a71fb7..c64e70c 100644 --- a/include/fc/rpc/json_connection.hpp +++ b/include/fc/rpc/json_connection.hpp @@ -76,6 +76,12 @@ namespace fc { namespace rpc { const variant& a2, const variant& a3 ); + future async_call( const fc::string& method, + const variant& a1, + const variant& a2, + const variant& a3, + const variant& a4 ); + template Result call( const fc::string& method, const variant& a1, @@ -86,6 +92,17 @@ namespace fc { namespace rpc { return async_call( method, a1, a2, a3 ).wait(timeout).as(); } + template + Result call( const fc::string& method, + const variant& a1, + const variant& a2, + const variant& a3, + const variant& a4, + microseconds timeout = microseconds::maximum()) + { + return async_call( method, a1, a2, a3, a4).wait(timeout).as(); + } + template Result call( const fc::string& method, const variant& a1, diff --git a/src/io/json.cpp b/src/io/json.cpp index 38c4fca..f28bc3e 100644 --- a/src/io/json.cpp +++ b/src/io/json.cpp @@ -264,37 +264,58 @@ namespace fc variant token_from_stream( T& in ) { fc::stringstream ss; - while( char c = in.peek() ) + bool parsed_unexpected_character = false; + bool received_eof = false; + try { - switch( c ) - { - case 'n': - case 'u': - case 'l': - case 't': - case 'r': - case 'e': - case 'f': - case 'a': - case 's': - ss.put( in.get() ); - break; - default: - { - fc::string str = ss.str(); - if( str == "null" ) return variant(); - if( str == "true" ) return true; - if( str == "false" ) return false; - else - { - return str; - // FC_THROW_EXCEPTION( parse_error_exception, "Invalid token '${token}'", - // ("token",str) ); - } - } - } + char c; + while( (c = in.peek()) && !parsed_unexpected_character) + { + switch( c ) + { + case 'n': + case 'u': + case 'l': + case 't': + case 'r': + case 'e': + case 'f': + case 'a': + case 's': + ss.put( in.get() ); + break; + default: + parsed_unexpected_character = true; + break; + } + } + } + catch (fc::eof_exception&) + { + received_eof = true; + } + + // we can get here either by processing a delimiter as in "null," + // an EOF like "null", or an invalid token like "nullZ" + fc::string str = ss.str(); + if( str == "null" ) return variant(); + if( str == "true" ) return true; + if( str == "false" ) return false; + else + { + if (received_eof) + FC_THROW_EXCEPTION( parse_error_exception, "Unexpected EOF" ); + else + { + // I'm not sure why we do this, a comment would be helpful. + // if we've reached this point, we've either seen a partial + // token ("tru") or something our simple parser couldn't + // make out ("falfe") + return str; + // FC_THROW_EXCEPTION( parse_error_exception, "Invalid token '${token}'", + // ("token",str) ); + } } - FC_THROW_EXCEPTION( parse_error_exception, "Unexpected EOF" ); } diff --git a/src/rpc/json_connection.cpp b/src/rpc/json_connection.cpp index 0915f20..81bbf42 100644 --- a/src/rpc/json_connection.cpp +++ b/src/rpc/json_connection.cpp @@ -406,7 +406,30 @@ namespace fc { namespace rpc { return my->_awaiting[id]; } + future json_connection::async_call( const fc::string& method, const variant& a1, const variant& a2, const variant& a3, const variant& a4 ) + { + auto id = my->_next_id++; + my->_awaiting[id] = fc::promise::ptr( new fc::promise() ); + { + fc::scoped_lock lock(my->_write_mutex); + *my->_out << "{\"id\":"; + *my->_out << id; + *my->_out << ",\"method\":"; + json::to_stream( *my->_out, method ); + *my->_out << ",\"params\":["; + fc::json::to_stream( *my->_out, a1 ); + *my->_out << ","; + fc::json::to_stream( *my->_out, a2 ); + *my->_out << ","; + fc::json::to_stream( *my->_out, a3 ); + *my->_out << ","; + fc::json::to_stream( *my->_out, a4 ); + *my->_out << "]}\n"; + } + my->_out->flush(); + return my->_awaiting[id]; + } future json_connection::async_call( const fc::string& method, mutable_variant_object named_args ) { From e36ccb3cfd2871fbe5467c42d7df5a272dd9ac06 Mon Sep 17 00:00:00 2001 From: Daniel Larimer Date: Tue, 22 Apr 2014 10:22:17 -0400 Subject: [PATCH 02/15] adding NotAuthorized HTTP response code --- include/fc/io/json.hpp | 4 ++-- include/fc/io/varint.hpp | 5 +++++ include/fc/network/http/connection.hpp | 1 + 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/include/fc/io/json.hpp b/include/fc/io/json.hpp index 16233e6..eada29a 100644 --- a/include/fc/io/json.hpp +++ b/include/fc/io/json.hpp @@ -1,9 +1,9 @@ #pragma once #include +#include namespace fc { - class path; class ostream; class buffered_istream; @@ -54,7 +54,7 @@ namespace fc } template - static void save_to_file( const T& v, const string& p, bool pretty = true ) + static void save_to_file( const T& v, const std::string& p, bool pretty = true ) { save_to_file( variant(v), fc::path(p), pretty ); } diff --git a/include/fc/io/varint.hpp b/include/fc/io/varint.hpp index ddc1c73..c292bcc 100644 --- a/include/fc/io/varint.hpp +++ b/include/fc/io/varint.hpp @@ -27,6 +27,11 @@ struct unsigned_int { friend bool operator>=( const unsigned_int& i, const unsigned_int& v ) { return v >= i.value; } }; +/** + * @brief serializes a 32 bit signed interger in as few bytes as possible + * + * Uses the google protobuf algorithm for seralizing signed numbers + */ struct signed_int { signed_int( int32_t v = 0 ):value(v){} operator int32_t()const { return value; } diff --git a/include/fc/network/http/connection.hpp b/include/fc/network/http/connection.hpp index 974deb6..723edbd 100644 --- a/include/fc/network/http/connection.hpp +++ b/include/fc/network/http/connection.hpp @@ -25,6 +25,7 @@ namespace fc { enum status_code { OK = 200, RecordCreated = 201, + NotAuthorized = 401, NotFound = 404, Found = 302, InternalServerError = 500 From 1bdc40368f92b6768b6408f1de30ff107477d078 Mon Sep 17 00:00:00 2001 From: Vikram Rajkumar Date: Tue, 22 Apr 2014 15:57:11 -0400 Subject: [PATCH 03/15] Make fc::reflector::to_string work with C++11 strongly typed enumerations --- include/fc/reflect/reflect.hpp | 40 ++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/include/fc/reflect/reflect.hpp b/include/fc/reflect/reflect.hpp index 837b431..da62732 100644 --- a/include/fc/reflect/reflect.hpp +++ b/include/fc/reflect/reflect.hpp @@ -17,7 +17,7 @@ #include -namespace fc { +namespace fc { /** * @brief defines visit functions for T @@ -32,13 +32,13 @@ template struct reflector{ typedef T type; typedef fc::false_type is_defined; - typedef fc::false_type is_enum; + typedef fc::false_type is_enum; /** * @tparam Visitor a function object of the form: - * + * * @code - * struct functor { + * struct functor { * template * void operator()( const char* name )const; * }; @@ -46,19 +46,19 @@ struct reflector{ * * If T is an enum then the functor has the following form: * @code - * struct functor { + * struct functor { * template * void operator()( const char* name )const; * }; * @endcode - * + * * @param v a functor that will be called for each member on T * * @note - this method is not defined for non-reflected types. */ #ifdef DOXYGEN template - static inline void visit( const Visitor& v ); + static inline void visit( const Visitor& v ); #endif // DOXYGEN }; @@ -77,7 +77,7 @@ void throw_bad_enum_cast( const char* k, const char* e ); #define TEMPLATE template #else // Disable warning C4482: nonstandard extention used: enum 'enum_type::enum_value' used in qualified name - #pragma warning( disable: 4482 ) + #pragma warning( disable: 4482 ) #define TEMPLATE #endif @@ -98,14 +98,14 @@ template\ static inline void visit( const Visitor& v ) { \ BOOST_PP_SEQ_FOR_EACH( FC_REFLECT_VISIT_BASE, v, INHERITS ) \ BOOST_PP_SEQ_FOR_EACH( FC_REFLECT_VISIT_MEMBER, v, MEMBERS ) \ -} +} #define FC_REFLECT_DERIVED_IMPL_EXT( TYPE, INHERITS, MEMBERS ) \ template\ void fc::reflector::visit( const Visitor& v ) { \ BOOST_PP_SEQ_FOR_EACH( FC_REFLECT_VISIT_BASE, v, INHERITS ) \ BOOST_PP_SEQ_FOR_EACH( FC_REFLECT_VISIT_MEMBER, v, MEMBERS ) \ -} +} #endif // DOXYGEN @@ -132,15 +132,23 @@ template<> struct reflector { \ }\ return nullptr; \ } \ + static const char* to_string(ENUM elem) { \ + switch( elem ) { \ + BOOST_PP_SEQ_FOR_EACH( FC_REFLECT_ENUM_TO_STRING, ENUM, FIELDS ) \ + default: \ + fc::throw_bad_enum_cast( BOOST_PP_STRINGIZE(elem), BOOST_PP_STRINGIZE(ENUM) ); \ + }\ + return nullptr; \ + } \ static ENUM from_string( const char* s ) { \ BOOST_PP_SEQ_FOR_EACH( FC_REFLECT_ENUM_FROM_STRING, ENUM, FIELDS ) \ fc::throw_bad_enum_cast( s, BOOST_PP_STRINGIZE(ENUM) ); \ return ENUM();\ } \ }; \ -} +} -/* Note: FC_REFLECT_ENUM previously defined this function, but I don't think it ever +/* Note: FC_REFLECT_ENUM previously defined this function, but I don't think it ever * did what we expected it to do. I've disabled it for now. * * template \ @@ -152,7 +160,7 @@ template<> struct reflector { \ /** * @def FC_REFLECT_DERIVED(TYPE,INHERITS,MEMBERS) * - * @brief Specializes fc::reflector for TYPE where + * @brief Specializes fc::reflector for TYPE where * type inherits other reflected classes * * @param INHERITS - a sequence of base class names (basea)(baseb)(basec) @@ -170,7 +178,7 @@ template<> struct reflector {\ total_member_count = local_member_count BOOST_PP_SEQ_FOR_EACH( FC_REFLECT_BASE_MEMBER_COUNT, +, INHERITS )\ }; \ FC_REFLECT_DERIVED_IMPL_INLINE( TYPE, INHERITS, MEMBERS ) \ -}; } +}; } #define FC_REFLECT_DERIVED_TEMPLATE( TEMPLATE_ARGS, TYPE, INHERITS, MEMBERS ) \ namespace fc { \ template struct get_typename { static const char* name() { return BOOST_PP_STRINGIZE(TYPE); } }; \ @@ -183,9 +191,9 @@ template struct reflector {\ total_member_count = local_member_count BOOST_PP_SEQ_FOR_EACH( FC_REFLECT_BASE_MEMBER_COUNT, +, INHERITS )\ }; \ FC_REFLECT_DERIVED_IMPL_INLINE( TYPE, INHERITS, MEMBERS ) \ -}; } +}; } -//BOOST_PP_SEQ_SIZE(MEMBERS), +//BOOST_PP_SEQ_SIZE(MEMBERS), /** * @def FC_REFLECT(TYPE,MEMBERS) From 61f2ac467939e8c1babaa3b24e24f9b1f39f4589 Mon Sep 17 00:00:00 2001 From: Daniel Larimer Date: Tue, 22 Apr 2014 17:25:07 -0400 Subject: [PATCH 04/15] adding HTTP response code --- include/fc/network/http/connection.hpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/include/fc/network/http/connection.hpp b/include/fc/network/http/connection.hpp index 723edbd..3c8de77 100644 --- a/include/fc/network/http/connection.hpp +++ b/include/fc/network/http/connection.hpp @@ -25,6 +25,7 @@ namespace fc { enum status_code { OK = 200, RecordCreated = 201, + BadRequest = 400, NotAuthorized = 401, NotFound = 404, Found = 302, @@ -75,3 +76,6 @@ namespace fc { } } // fc::http +#include +FC_REFLECT( fc::http::header, (key)(val) ) + From c3ea6cc62c531f3567ffff8ee3888f8e0365c246 Mon Sep 17 00:00:00 2001 From: Eric Frias Date: Tue, 22 Apr 2014 17:55:46 -0400 Subject: [PATCH 05/15] Assert to warn when calling unimplemented functions --- src/io/iostream.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/io/iostream.cpp b/src/io/iostream.cpp index 1f9b6a4..5a01cc7 100644 --- a/src/io/iostream.cpp +++ b/src/io/iostream.cpp @@ -264,12 +264,14 @@ namespace fc { istream& operator>>( istream& o, std::string& v ) { + assert(false && "not implemented"); return o; } #ifdef USE_FC_STRING istream& operator>>( istream& o, fc::string& v ) { + assert(false && "not implemented"); return o; } #endif @@ -282,51 +284,61 @@ namespace fc { istream& operator>>( istream& o, double& v ) { + assert(false && "not implemented"); return o; } istream& operator>>( istream& o, float& v ) { + assert(false && "not implemented"); return o; } istream& operator>>( istream& o, int64_t& v ) { + assert(false && "not implemented"); return o; } istream& operator>>( istream& o, uint64_t& v ) { + assert(false && "not implemented"); return o; } istream& operator>>( istream& o, int32_t& v ) { + assert(false && "not implemented"); return o; } istream& operator>>( istream& o, uint32_t& v ) { + assert(false && "not implemented"); return o; } istream& operator>>( istream& o, int16_t& v ) { + assert(false && "not implemented"); return o; } istream& operator>>( istream& o, uint16_t& v ) { + assert(false && "not implemented"); return o; } istream& operator>>( istream& o, int8_t& v ) { + assert(false && "not implemented"); return o; } istream& operator>>( istream& o, uint8_t& v ) { + assert(false && "not implemented"); return o; } From 9731fac9f374f4388713355ce49501cb33ef05cd Mon Sep 17 00:00:00 2001 From: Eric Frias Date: Tue, 22 Apr 2014 17:56:12 -0400 Subject: [PATCH 06/15] Fix error parsing a numeric constant at the end of file --- src/io/json.cpp | 80 ++++++++++++++++++++++++++----------------------- 1 file changed, 42 insertions(+), 38 deletions(-) diff --git a/src/io/json.cpp b/src/io/json.cpp index f28bc3e..a746ddf 100644 --- a/src/io/json.cpp +++ b/src/io/json.cpp @@ -211,54 +211,58 @@ namespace fc template variant number_from_stream( T& in ) { - char buf[30]; - memset( buf, 0, sizeof(buf) ); - char* pos = buf; + fc::stringstream ss; + bool dot = false; bool neg = false; if( in.peek() == '-') { neg = true; - *pos = in.get(); - ++pos; + ss.put( in.get() ); } bool done = false; - while( !done) + + try { - char c = in.peek(); - switch( c ) + char c; + while((c = in.peek()) && !done) { - case '.': - { - if( dot ) - { - done = true; - break; - } - dot = true; - } - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - *pos = c; - ++pos; - in.get(); - break; - default: - done = true; - break; + + switch( c ) + { + case '.': + if (dot) + FC_THROW_EXCEPTION(parse_error_exception, "Can't parse a number with two decimal places"); + dot = true; + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + ss.put( in.get() ); + break; + default: + done = true; + break; + } } } - if( dot ) return to_double(buf); - if( neg ) return to_int64(buf); - return to_uint64(buf); + catch (fc::eof_exception&) + { + } + fc::string str = ss.str(); + if (str == "-." || str == ".") // check the obviously wrong things we could have encountered + FC_THROW_EXCEPTION(parse_error_exception, "Can't parse token \"${token}\" as a JSON numeric constant", ("token", str)); + if( dot ) + return to_double(str); + if( neg ) + return to_int64(str); + return to_uint64(str); } template variant token_from_stream( T& in ) @@ -269,7 +273,7 @@ namespace fc try { char c; - while( (c = in.peek()) && !parsed_unexpected_character) + while((c = in.peek()) && !parsed_unexpected_character) { switch( c ) { From 9301771405375a7977d9f8d364e85d3721313459 Mon Sep 17 00:00:00 2001 From: Daniel Larimer Date: Fri, 25 Apr 2014 15:17:06 -0400 Subject: [PATCH 07/15] enhance error message in variant --- src/variant.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/variant.cpp b/src/variant.cpp index 1917d7d..c6077f1 100644 --- a/src/variant.cpp +++ b/src/variant.cpp @@ -486,7 +486,7 @@ const string& variant::get_string()const { if( get_type() == string_type ) return **reinterpret_cast(this); - FC_THROW_EXCEPTION( bad_cast_exception, "Invalid cast from ${type} to Object" ); + FC_THROW_EXCEPTION( bad_cast_exception, "Invalid cast from type '${type}' to Object", ("type",int(get_type())) ); } @@ -495,7 +495,7 @@ const variant_object& variant::get_object()const { if( get_type() == object_type ) return **reinterpret_cast(this); - FC_THROW_EXCEPTION( bad_cast_exception, "Invalid cast from ${type} to Object" ); + FC_THROW_EXCEPTION( bad_cast_exception, "Invalid cast from type '${type}' to Object", ("type",int(get_type())) ); } void to_variant( const std::string& s, variant& v ) From 7672754c5154ccd07fd1a0a40908deee586be8ea Mon Sep 17 00:00:00 2001 From: dnotestein Date: Sat, 26 Apr 2014 18:02:31 -0400 Subject: [PATCH 08/15] add some minor comments --- src/rpc/json_connection.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/rpc/json_connection.cpp b/src/rpc/json_connection.cpp index 81bbf42..ffd05f2 100644 --- a/src/rpc/json_connection.cpp +++ b/src/rpc/json_connection.cpp @@ -141,7 +141,7 @@ namespace fc { namespace rpc { } } } - else if( i != obj.end() ) + else if( i != obj.end() ) //handle any received JSON response { uint64_t id = i->value().as_int64(); auto await = _awaiting.find(id); @@ -149,11 +149,11 @@ namespace fc { namespace rpc { { auto r = obj.find("result"); auto e = obj.find("error"); - if( r != obj.end() ) + if( r != obj.end() ) //if regular result response { await->second->set_value( r->value() ); } - else if( e != obj.end() ) + else if( e != obj.end() ) //if error response { try { From 6f466979ccfe83e59275f82052c52b21a2e888f2 Mon Sep 17 00:00:00 2001 From: Daniel Larimer Date: Sun, 27 Apr 2014 21:20:40 -0400 Subject: [PATCH 09/15] adding helper methods --- include/fc/time.hpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/include/fc/time.hpp b/include/fc/time.hpp index 9c0eda2..6a6146d 100644 --- a/include/fc/time.hpp +++ b/include/fc/time.hpp @@ -45,13 +45,14 @@ namespace fc { static time_point from_iso_string( const fc::string& s ); const microseconds& time_since_epoch()const { return elapsed; } + uint32_t sec_since_epoch()const { return elapsed.count() / 1000000; } bool operator > ( const time_point& t )const { return elapsed._count > t.elapsed._count; } bool operator >=( const time_point& t )const { return elapsed._count >=t.elapsed._count; } bool operator < ( const time_point& t )const { return elapsed._count < t.elapsed._count; } bool operator <=( const time_point& t )const { return elapsed._count <=t.elapsed._count; } bool operator ==( const time_point& t )const { return elapsed._count ==t.elapsed._count; } bool operator !=( const time_point& t )const { return elapsed._count !=t.elapsed._count; } - time_point& operator += ( const microseconds& m) { elapsed+=m; return *this; } + time_point& operator += ( const microseconds& m) { elapsed+=m; return *this; } time_point operator + (const microseconds& m) const { return time_point(elapsed+m); } time_point operator - (const microseconds& m) const { return time_point(elapsed-m); } microseconds operator - (const time_point& m) const { return microseconds(elapsed.count() - m.elapsed.count()); } @@ -87,6 +88,7 @@ namespace fc { friend bool operator <= ( const time_point_sec& a, const time_point_sec& b ) { return a.utc_seconds <= b.utc_seconds; } friend bool operator >= ( const time_point_sec& a, const time_point_sec& b ) { return a.utc_seconds >= b.utc_seconds; } friend bool operator == ( const time_point_sec& a, const time_point_sec& b ) { return a.utc_seconds == b.utc_seconds; } + friend bool operator != ( const time_point_sec& a, const time_point_sec& b ) { return a.utc_seconds != b.utc_seconds; } time_point_sec& operator += ( uint32_t m ) { utc_seconds+=m; return *this; } friend time_point operator - ( const time_point_sec& t, const microseconds& m ) { return time_point(t) - m; } From 19f28694900ce98b9788e30e470b59dd1ceaf3d7 Mon Sep 17 00:00:00 2001 From: Eric Frias Date: Wed, 30 Apr 2014 10:52:16 -0400 Subject: [PATCH 10/15] Disable keepalives on old clang compiler used for nightly until we can upgrade it --- src/network/tcp_socket.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/network/tcp_socket.cpp b/src/network/tcp_socket.cpp index 382a938..a5c276a 100644 --- a/src/network/tcp_socket.cpp +++ b/src/network/tcp_socket.cpp @@ -85,19 +85,22 @@ namespace fc { NULL, 0, &dwBytesRet, NULL, NULL) == SOCKET_ERROR) wlog("Error setting TCP keepalive values"); #else +# if !defined(__clang__) || (__clang_major__ >= 6) + a b a c d asdflkjasdfjklasldjf lkjasdjfklja ksdfj k // This should work for modern Linuxes and for OSX >= Mountain Lion int timeout_sec = interval.count() / fc::seconds(1).count(); if (setsockopt(my->_sock.native(), IPPROTO_TCP, -# if defined( __APPLE__ ) +# if defined( __APPLE__ ) TCP_KEEPALIVE, -# else +# else TCP_KEEPIDLE, -# endif +# endif (char*)&timeout_sec, sizeof(timeout_sec)) < 0) wlog("Error setting TCP keepalive idle time"); if (setsockopt(my->_sock.native(), IPPROTO_TCP, TCP_KEEPINTVL, (char*)&timeout_sec, sizeof(timeout_sec)) < 0) wlog("Error setting TCP keepalive interval"); +# endif #endif } else From 00edd3958c5ca8165fd41c61080adeeced185247 Mon Sep 17 00:00:00 2001 From: Eric Frias Date: Wed, 30 Apr 2014 10:56:51 -0400 Subject: [PATCH 11/15] Remove accidentally-committed garbage --- src/network/tcp_socket.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/network/tcp_socket.cpp b/src/network/tcp_socket.cpp index a5c276a..11eddef 100644 --- a/src/network/tcp_socket.cpp +++ b/src/network/tcp_socket.cpp @@ -86,7 +86,6 @@ namespace fc { wlog("Error setting TCP keepalive values"); #else # if !defined(__clang__) || (__clang_major__ >= 6) - a b a c d asdflkjasdfjklasldjf lkjasdjfklja ksdfj k // This should work for modern Linuxes and for OSX >= Mountain Lion int timeout_sec = interval.count() / fc::seconds(1).count(); if (setsockopt(my->_sock.native(), IPPROTO_TCP, From aa111510f35947c0e05107e2457d0890d85d67a9 Mon Sep 17 00:00:00 2001 From: Eric Frias Date: Tue, 29 Apr 2014 19:54:43 -0400 Subject: [PATCH 12/15] Expose enough functions in tcp_socket and tcp_server to allow listening and originating connections on the same port. So far, this seems to work on win32, other platforms untested. Add a local_endpoint() function so we can find out which local interface a socket is bound to --- include/fc/network/tcp_socket.hpp | 7 ++- src/network/tcp_socket.cpp | 93 ++++++++++++++++++++++--------- 2 files changed, 74 insertions(+), 26 deletions(-) diff --git a/include/fc/network/tcp_socket.hpp b/include/fc/network/tcp_socket.hpp index 3229f87..44b384a 100644 --- a/include/fc/network/tcp_socket.hpp +++ b/include/fc/network/tcp_socket.hpp @@ -15,13 +15,16 @@ namespace fc { void connect_to( const fc::ip::endpoint& remote_endpoint ); void connect_to( const fc::ip::endpoint& remote_endpoint, const fc::ip::endpoint& local_endpoint ); void enable_keep_alives(const fc::microseconds& interval); - fc::ip::endpoint remote_endpoint()const; + void set_reuse_address(bool enable = true); // set SO_REUSEADDR + fc::ip::endpoint remote_endpoint() const; + fc::ip::endpoint local_endpoint() const; void get( char& c ) { read( &c, 1 ); } + /// istream interface /// @{ virtual size_t readsome( char* buffer, size_t max ); @@ -35,6 +38,7 @@ namespace fc { virtual void close(); /// @} + void open(); bool is_open()const; private: @@ -57,6 +61,7 @@ namespace fc { void close(); void accept( tcp_socket& s ); + void set_reuse_address(bool enable = true); // set SO_REUSEADDR, call before listen void listen( uint16_t port ); void listen( const fc::ip::endpoint& ep ); uint16_t get_port()const; diff --git a/src/network/tcp_socket.cpp b/src/network/tcp_socket.cpp index 11eddef..292121b 100644 --- a/src/network/tcp_socket.cpp +++ b/src/network/tcp_socket.cpp @@ -14,12 +14,21 @@ namespace fc { class tcp_socket::impl { public: - impl():_sock( fc::asio::default_io_service() ){} + impl() : + _sock( fc::asio::default_io_service() ) + {} ~impl(){ - if( _sock.is_open() ) _sock.close(); + if( _sock.is_open() ) + _sock.close(); } boost::asio::ip::tcp::socket _sock; }; + + void tcp_socket::open() + { + my->_sock.open(boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 0).protocol()); + } + bool tcp_socket::is_open()const { return my->_sock.is_open(); } @@ -46,11 +55,17 @@ namespace fc { return fc::asio::write_some( my->_sock, boost::asio::buffer( buf, len ) ); } - fc::ip::endpoint tcp_socket::remote_endpoint()const - { - auto rep = my->_sock.remote_endpoint(); - return fc::ip::endpoint(rep.address().to_v4().to_ulong(), rep.port() ); - } + fc::ip::endpoint tcp_socket::remote_endpoint()const + { + auto rep = my->_sock.remote_endpoint(); + return fc::ip::endpoint(rep.address().to_v4().to_ulong(), rep.port() ); + } + + fc::ip::endpoint tcp_socket::local_endpoint() const + { + auto boost_local_endpoint = my->_sock.local_endpoint(); + return fc::ip::endpoint(boost_local_endpoint.address().to_v4().to_ulong(), boost_local_endpoint.port() ); + } size_t tcp_socket::readsome( char* buf, size_t len ) { auto r = fc::asio::read_some( my->_sock, boost::asio::buffer( buf, len ) ); @@ -62,10 +77,17 @@ namespace fc { } void tcp_socket::connect_to( const fc::ip::endpoint& remote_endpoint, const fc::ip::endpoint& local_endpoint ) { - my->_sock = boost::asio::ip::tcp::socket(fc::asio::default_io_service(), - boost::asio::ip::tcp::endpoint(boost::asio::ip::address_v4(local_endpoint.get_address()), - local_endpoint.port())); - fc::asio::tcp::connect(my->_sock, fc::asio::tcp::endpoint( boost::asio::ip::address_v4(remote_endpoint.get_address()), remote_endpoint.port() ) ); + try + { + my->_sock.bind(boost::asio::ip::tcp::endpoint(boost::asio::ip::address_v4(local_endpoint.get_address()), + local_endpoint.port())); + } + catch (std::exception& except) + { + elog("Exception binding outgoing connection to desired local endpoint: ${what}", ("what", except.what())); + throw; + } + fc::asio::tcp::connect(my->_sock, fc::asio::tcp::endpoint(boost::asio::ip::address_v4(remote_endpoint.get_address()), remote_endpoint.port())); } void tcp_socket::enable_keep_alives(const fc::microseconds& interval) @@ -77,8 +99,8 @@ namespace fc { #if defined _WIN32 || defined WIN32 || defined OS_WIN64 || defined _WIN64 || defined WIN64 || defined WINNT struct tcp_keepalive keepalive_settings; keepalive_settings.onoff = 1; - keepalive_settings.keepalivetime = interval.count() / fc::milliseconds(1).count(); - keepalive_settings.keepaliveinterval = interval.count() / fc::milliseconds(1).count(); + keepalive_settings.keepalivetime = (ULONG)(interval.count() / fc::milliseconds(1).count()); + keepalive_settings.keepaliveinterval = (ULONG)(interval.count() / fc::milliseconds(1).count()); DWORD dwBytesRet = 0; if (WSAIoctl(my->_sock.native(), SIO_KEEPALIVE_VALS, &keepalive_settings, sizeof(keepalive_settings), @@ -109,13 +131,21 @@ namespace fc { } } + void tcp_socket::set_reuse_address(bool enable /* = true */) + { + FC_ASSERT(my->_sock.is_open()); + boost::asio::socket_base::reuse_address option(enable); + my->_sock.set_option(option); + } + + class tcp_server::impl { public: - impl(uint16_t port) - :_accept( fc::asio::default_io_service(), boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port) ){} - - impl(const fc::ip::endpoint& ep ) - :_accept( fc::asio::default_io_service(), boost::asio::ip::tcp::endpoint(boost::asio::ip::address_v4( ep.get_address()), ep.port()) ){} + impl() + :_accept( fc::asio::default_io_service() ) + { + _accept.open(boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 0).protocol()); + } ~impl(){ try { @@ -130,8 +160,10 @@ namespace fc { boost::asio::ip::tcp::acceptor _accept; }; void tcp_server::close() { - if( my && my->_accept.is_open() ) my->_accept.close(); - delete my; my = nullptr; + if( my && my->_accept.is_open() ) + my->_accept.close(); + delete my; + my = nullptr; } tcp_server::tcp_server() :my(nullptr) { @@ -149,20 +181,31 @@ namespace fc { fc::asio::tcp::accept( my->_accept, s.my->_sock ); } FC_RETHROW_EXCEPTIONS( warn, "Unable to accept connection on socket." ); } - + void tcp_server::set_reuse_address(bool enable /* = true */) + { + if( !my ) + my = new impl; + boost::asio::ip::tcp::acceptor::reuse_address option(enable); + my->_accept.set_option(option); + } void tcp_server::listen( uint16_t port ) { - if( my ) delete my; - my = new impl(port); + if( !my ) + my = new impl; + my->_accept.bind(boost::asio::ip::tcp::endpoint(boost::asio::ip::address_v4(), port)); + my->_accept.listen(); } void tcp_server::listen( const fc::ip::endpoint& ep ) { - if( my ) delete my; - my = new impl(ep); + if( !my ) + my = new impl; + my->_accept.bind(boost::asio::ip::tcp::endpoint(boost::asio::ip::address_v4::from_string((string)ep.get_address()), ep.port())); + my->_accept.listen(); } uint16_t tcp_server::get_port()const { + FC_ASSERT( my != nullptr ); return my->_accept.local_endpoint().port(); } From 9d3bddf09a6fb2a9248115a0cfc824b2db783baa Mon Sep 17 00:00:00 2001 From: Eric Frias Date: Thu, 1 May 2014 14:04:44 -0400 Subject: [PATCH 13/15] constification --- include/fc/variant_object.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/fc/variant_object.hpp b/include/fc/variant_object.hpp index 32d8e83..b2f2e4e 100644 --- a/include/fc/variant_object.hpp +++ b/include/fc/variant_object.hpp @@ -59,7 +59,7 @@ namespace fc const variant& operator[]( const string& key )const; const variant& operator[]( const char* key )const; size_t size()const; - bool contains( const char* key ) { return find(key) != end(); } + bool contains( const char* key ) const { return find(key) != end(); } ///@} variant_object(); From 65328399387cb6da4833e0057693174d0dad24d5 Mon Sep 17 00:00:00 2001 From: Daniel Larimer Date: Fri, 2 May 2014 14:09:29 -0400 Subject: [PATCH 14/15] fix apple build --- src/network/tcp_socket.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/network/tcp_socket.cpp b/src/network/tcp_socket.cpp index 382a938..bb4d15b 100644 --- a/src/network/tcp_socket.cpp +++ b/src/network/tcp_socket.cpp @@ -95,9 +95,12 @@ namespace fc { # endif (char*)&timeout_sec, sizeof(timeout_sec)) < 0) wlog("Error setting TCP keepalive idle time"); +#ifndef __APPLE__ // TCP_KEEPINTVL does not seem to work on 10.8.4 if (setsockopt(my->_sock.native(), IPPROTO_TCP, TCP_KEEPINTVL, (char*)&timeout_sec, sizeof(timeout_sec)) < 0) wlog("Error setting TCP keepalive interval"); +#endif // !__APPLE__ + #endif } else From b8a7531eab08af3756eb4670437484ae2e137d74 Mon Sep 17 00:00:00 2001 From: Eric Frias Date: Tue, 6 May 2014 17:20:04 -0400 Subject: [PATCH 15/15] Fix error message printed when unable to deserialize a json object, improve logging of return values and add logging of exceptional returns from json function calls. Continue my endless quest to break the mac build. --- include/fc/io/datastream.hpp | 2 +- src/io/datastream.cpp | 2 +- src/network/tcp_socket.cpp | 8 ++++---- src/rpc/json_connection.cpp | 4 +++- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/include/fc/io/datastream.hpp b/include/fc/io/datastream.hpp index 5e54998..75a4269 100644 --- a/include/fc/io/datastream.hpp +++ b/include/fc/io/datastream.hpp @@ -7,7 +7,7 @@ namespace fc { namespace detail { - NO_RETURN void throw_datastream_range_error( const char* file, size_t len, size_t over ); + NO_RETURN void throw_datastream_range_error( const char* file, size_t len, int64_t over ); } /** diff --git a/src/io/datastream.cpp b/src/io/datastream.cpp index da38e14..cfe1104 100644 --- a/src/io/datastream.cpp +++ b/src/io/datastream.cpp @@ -1,7 +1,7 @@ #include #include -NO_RETURN void fc::detail::throw_datastream_range_error(char const* method, size_t len, size_t over) +NO_RETURN void fc::detail::throw_datastream_range_error(char const* method, size_t len, int64_t over) { FC_THROW_EXCEPTION( out_of_range_exception, "${method} datastream of length ${len} over by ${over}", ("method",fc::string(method))("len",len)("over",over) ); } diff --git a/src/network/tcp_socket.cpp b/src/network/tcp_socket.cpp index b6e807a..b9fdab7 100644 --- a/src/network/tcp_socket.cpp +++ b/src/network/tcp_socket.cpp @@ -110,19 +110,19 @@ namespace fc { // This should work for modern Linuxes and for OSX >= Mountain Lion int timeout_sec = interval.count() / fc::seconds(1).count(); if (setsockopt(my->_sock.native(), IPPROTO_TCP, - #if defined( __APPLE__ ) + #if defined( __APPLE__ ) TCP_KEEPALIVE, #else TCP_KEEPIDLE, #endif (char*)&timeout_sec, sizeof(timeout_sec)) < 0) wlog("Error setting TCP keepalive idle time"); - #ifndef __APPLE__ // TCP_KEEPINTVL does not seem to work on 10.8.4 +# if !defined(__APPLE__) || defined(TCP_KEEPINTVL) // TCP_KEEPINTVL not defined before 10.9 if (setsockopt(my->_sock.native(), IPPROTO_TCP, TCP_KEEPINTVL, (char*)&timeout_sec, sizeof(timeout_sec)) < 0) wlog("Error setting TCP keepalive interval"); - #endif // !__APPLE__ -#endif +# endif // !__APPLE__ || TCP_KEEPINTVL +#endif // !WIN32 } else { diff --git a/src/rpc/json_connection.cpp b/src/rpc/json_connection.cpp index ffd05f2..4a112b4 100644 --- a/src/rpc/json_connection.cpp +++ b/src/rpc/json_connection.cpp @@ -35,7 +35,7 @@ namespace fc { namespace rpc { void send_result( variant id, variant result ) { - ilog( "send ${i} ${r}", ("i",id)("r",result) ); + ilog( "send: {\"id\": ${i}, \"result\": ${r}}", ("i",id)("r",result) ); { fc::scoped_lock lock(_write_mutex); *_out << "{\"id\":"; @@ -48,6 +48,8 @@ namespace fc { namespace rpc { } void send_error( variant id, fc::exception& e ) { + ilog( "send: {\"id\": ${i}, \"error\":{\"message\": ${what},\"code\":0,\"data\":${data}}}", + ("i",id)("what",e.what())("data", e) ); { fc::scoped_lock lock(_write_mutex); *_out << "{\"id\":";