From 9a8767a645199de470620e6f2de26d55c2b810ee Mon Sep 17 00:00:00 2001 From: Daniel Larimer Date: Sat, 15 Dec 2012 10:26:03 -0500 Subject: [PATCH] Fixed error_report to_string Updated fc::datastream and base64 to use error_report Added url to cmake lists various other bug fixes --- CMakeLists.txt | 1 + include/fc/base58.hpp | 4 ++-- include/fc/datastream.hpp | 15 +++++++++------ include/fc/task.hpp | 14 ++++++++------ include/fc/thread.hpp | 5 +++-- src/base58.cpp | 3 ++- src/error_report.cpp | 4 +++- src/http_server.cpp | 19 ++++++++++++++++--- 8 files changed, 44 insertions(+), 21 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7da9be8..12106a0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,6 +48,7 @@ include_directories( include ) set( sources src/ssh.cpp + src/url.cpp src/process.cpp src/http_connection.cpp src/http_server.cpp diff --git a/include/fc/base58.hpp b/include/fc/base58.hpp index 4198bfb..3c5a714 100644 --- a/include/fc/base58.hpp +++ b/include/fc/base58.hpp @@ -3,7 +3,7 @@ #include namespace fc { -fc::string to_base58( const char* d, uint32_t s ); -size_t from_base58( const fc::string& base58_str, char* out_data, size_t out_data_len ); + fc::string to_base58( const char* d, uint32_t s ); + size_t from_base58( const fc::string& base58_str, char* out_data, size_t out_data_len ); } #endif // _FC_BASE58_HPP_ diff --git a/include/fc/datastream.hpp b/include/fc/datastream.hpp index c333935..f56945b 100644 --- a/include/fc/datastream.hpp +++ b/include/fc/datastream.hpp @@ -1,7 +1,7 @@ #ifndef _FC_DATASTREAM_HPP_ #define _FC_DATASTREAM_HPP_ #include -#include +#include #include #include @@ -40,8 +40,8 @@ struct datastream { m_pos += s; return true; } - FC_THROW_MSG( "Attempt to read %s bytes beyond end of buffer of size %s", - int64_t(-((m_end-m_pos) - s)), int64_t(m_end-m_start) ); + FC_THROW_REPORT( "Attempt to read ${bytes_past} bytes beyond end of buffer with size ${buffer_size} ", + fc::value().set("bytes_past",int64_t(-((m_end-m_pos) - s))).set("buffer_size", int64_t(m_end-m_start)) ); return false; } @@ -51,7 +51,8 @@ struct datastream { m_pos += s; return true; } - FC_THROW_MSG( "Attempt to write %s bytes beyond end of buffer of size %s", int64_t(-((m_end-m_pos) - s)),int64_t(m_end-m_start) ); + FC_THROW_REPORT( "Attempt to write ${bytes_past} bytes beyond end of buffer with size ${buffer_size} ", + fc::value().set("bytes_past",int64_t(-((m_end-m_pos) - s))).set("buffer_size", int64_t(m_end-m_start)) ); return false; } @@ -61,7 +62,8 @@ struct datastream { ++m_pos; return true; } - FC_THROW_MSG( "Attempt to write %s byte beyond end of buffer of size %s", int64_t(-((m_end-m_pos) - 1)), int64_t(m_end-m_start) ); + FC_THROW_REPORT( "Attempt to write ${bytes_past} bytes beyond end of buffer with size ${buffer_size} ", + fc::value().set("bytes_past",int64_t(-((m_end-m_pos) - 1))).set("buffer_size", int64_t(m_end-m_start)) ); return false; } @@ -72,7 +74,8 @@ struct datastream { ++m_pos; return true; } - FC_THROW_MSG( "Attempt to read %s byte beyond end of buffer of size %s", int64_t(-((m_end-m_pos) - 1)), int64_t(m_end-m_start) ); + FC_THROW_REPORT( "Attempt to read ${bytes_past} bytes beyond end of buffer of size ${buffer_size} ", + fc::value().set("bytes_past",int64_t(-((m_end-m_pos) - 1))).set("buffer_size", int64_t(m_end-m_start)) ); ++m_pos; return false; } diff --git a/include/fc/task.hpp b/include/fc/task.hpp index 6c3cdcb..9c36e6d 100644 --- a/include/fc/task.hpp +++ b/include/fc/task.hpp @@ -61,12 +61,13 @@ namespace fc { public: template task( Functor&& f ):task_base(&_functor) { + typedef typename fc::deduce::type FunctorType; static_assert( sizeof(f) <= sizeof(_functor), "sizeof(Functor) is larger than FunctorSize" ); - new ((char*)&_functor) Functor( fc::forward(f) ); - _destroy_functor = &detail::functor_destructor::destroy; + new ((char*)&_functor) FunctorType( fc::forward(f) ); + _destroy_functor = &detail::functor_destructor::destroy; _promise_impl = static_cast*>(this); - _run_functor = &detail::functor_run::run; + _run_functor = &detail::functor_run::run; } aligned _functor; private: @@ -77,12 +78,13 @@ namespace fc { public: template task( Functor&& f ):task_base(&_functor) { + typedef typename fc::deduce::type FunctorType; static_assert( sizeof(f) <= sizeof(_functor), "sizeof(Functor) is larger than FunctorSize" ); - new ((char*)&_functor) Functor( fc::forward(f) ); - _destroy_functor = &detail::functor_destructor::destroy; + new ((char*)&_functor) FunctorType( fc::forward(f) ); + _destroy_functor = &detail::functor_destructor::destroy; _promise_impl = static_cast*>(this); - _run_functor = &detail::void_functor_run::run; + _run_functor = &detail::void_functor_run::run; } aligned _functor; private: diff --git a/include/fc/thread.hpp b/include/fc/thread.hpp index 334b56f..3d8c960 100644 --- a/include/fc/thread.hpp +++ b/include/fc/thread.hpp @@ -52,8 +52,9 @@ namespace fc { template auto async( Functor&& f, const char* desc ="", priority prio = priority()) -> fc::future { typedef decltype(f()) Result; - fc::task* tsk = - new fc::task( fc::forward(f) ); + typedef typename fc::deduce::type FunctorType; + fc::task* tsk = + new fc::task( fc::forward(f) ); fc::future r(fc::shared_ptr< fc::promise >(tsk,true) ); async_task(tsk,prio,desc); return r; diff --git a/src/base58.cpp b/src/base58.cpp index 1fc5dc9..9145db6 100644 --- a/src/base58.cpp +++ b/src/base58.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -616,7 +617,7 @@ size_t from_base58( const fc::string& base58_str, char* out_data, size_t out_dat //slog( "%s", base58_str.c_str() ); std::vector out; if( !DecodeBase58( base58_str.c_str(), out ) ) { - FC_THROW_MSG( "Unable to decode base58 string '%s'", base58_str ); + FC_THROW_REPORT( "Unable to decode base58 string ${base58_str}", fc::value().set("base58_str",base58_str) ); } memcpy( out_data, out.data(), out.size() ); diff --git a/src/error_report.cpp b/src/error_report.cpp index 4a817e9..c14e393 100644 --- a/src/error_report.cpp +++ b/src/error_report.cpp @@ -86,8 +86,9 @@ fc::string error_frame::to_string()const { int64_t prev = 0; auto next = desc.find( '$' ); while( prev != int64_t(fc::string::npos) && prev < int64_t(desc.size()) ) { + // slog( "prev: %d next %d %s", prev, next, desc.substr(prev,next).c_str() ); // print everything from the last pos until the first '$' - ss << desc.substr( prev, next ); + ss << desc.substr( prev, next-prev ); // if we got to the end, return it. if( next == string::npos ) { return ss.str(); } @@ -102,6 +103,7 @@ fc::string error_frame::to_string()const { if( next != fc::string::npos ) { // the key is between prev and next fc::string key = desc.substr( prev+1, (next-prev-1) ); + //slog( "key '%s'", key.c_str() ); if( meta ) { auto itr = meta->find( key.c_str() ); if( itr != meta->end() ) { diff --git a/src/http_server.cpp b/src/http_server.cpp index 09db676..6349f06 100644 --- a/src/http_server.cpp +++ b/src/http_server.cpp @@ -56,6 +56,7 @@ namespace fc { namespace http { try { http::connection con; while( tcp_serv.accept( con.get_socket() ) ) { + slog( "Accept Connection" ); fc::async( [=](){ handle_connection( con, on_req ); } ); con = http::connection(); } @@ -66,9 +67,14 @@ namespace fc { namespace http { void handle_connection( const http::connection& c, std::function do_on_req ) { - http::server::response rep( fc::shared_ptr( new response::impl(c, [=](){ this->handle_connection(c,do_on_req); } ) ) ); - auto req = c.read_request(); - if( do_on_req ) do_on_req( req, rep ); + wlog( "reading request.." ); + try { + http::server::response rep( fc::shared_ptr( new response::impl(c, [=](){ this->handle_connection(c,do_on_req); } ) ) ); + auto req = c.read_request(); + if( do_on_req ) do_on_req( req, rep ); + } catch ( ... ) { + wlog( "unable to read request %s", fc::except_str().c_str()); + } } std::function on_req; fc::tcp_server tcp_serv; @@ -126,6 +132,13 @@ namespace fc { namespace http { } my->body_bytes_sent += len; my->con.get_socket().write( data, len ); + if( my->body_bytes_sent == int64_t(my->body_length) ) { + if( my->handle_next_req ) { + slog( "handle next request..." ); + //fc::async( std::function(my->handle_next_req) ); + fc::async( my->handle_next_req ); + } + } } server::response::~response(){}