various updates

This commit is contained in:
Daniel Larimer 2012-10-09 21:40:29 -04:00
parent c99759a489
commit 3f73d25e44
12 changed files with 145 additions and 17 deletions

View file

@ -88,6 +88,7 @@ namespace fc {
throw_exception_( func, file, line, msg, to_string(fc::forward<T1>(a1) ), to_string( fc::forward<T2>(a2) ), to_string( fc::forward<T3>(a3) ) ); throw_exception_( func, file, line, msg, to_string(fc::forward<T1>(a1) ), to_string( fc::forward<T2>(a2) ), to_string( fc::forward<T3>(a3) ) );
} }
fc::string except_str();
} // namespace fc } // namespace fc
#define FC_THROW(X) throw X #define FC_THROW(X) throw X

View file

@ -6,6 +6,7 @@
namespace boost { namespace boost {
namespace filesystem { namespace filesystem {
class path; class path;
class directory_iterator;
} }
} }
@ -31,15 +32,34 @@ namespace fc {
operator boost::filesystem::path& (); operator boost::filesystem::path& ();
operator const boost::filesystem::path& ()const; operator const boost::filesystem::path& ()const;
fc::path stem()const;
fc::path extension()const;
fc::path filename()const; fc::path filename()const;
fc::path parent_path()const;
fc::string string()const; fc::string string()const;
private: private:
fwd<boost::filesystem::path,8> _p; fwd<boost::filesystem::path,8> _p;
}; };
class directory_iterator {
public:
directory_iterator( const fc::path& p );
directory_iterator();
~directory_iterator();
fc::path operator*()const;
directory_iterator& operator++(int);
directory_iterator& operator++();
friend bool operator==( const directory_iterator&, const directory_iterator& );
friend bool operator!=( const directory_iterator&, const directory_iterator& );
private:
fwd<boost::filesystem::directory_iterator,8> _p;
};
bool exists( const path& p ); bool exists( const path& p );
bool is_directory( const path& p ); bool is_directory( const path& p );
bool is_regular( const path& p ); bool is_regular_file( const path& p );
void create_directories( const path& p ); void create_directories( const path& p );
uint64_t file_size( const path& p ); uint64_t file_size( const path& p );
} }

View file

@ -196,6 +196,7 @@ namespace fc {
friend stringstream& operator<<( stringstream&, const bool& ); friend stringstream& operator<<( stringstream&, const bool& );
friend stringstream& operator<<( stringstream&, const char& ); friend stringstream& operator<<( stringstream&, const char& );
friend stringstream& operator<<( stringstream&, const fc::string& ); friend stringstream& operator<<( stringstream&, const fc::string& );
friend stringstream& operator<<( stringstream&, const char* );
private: private:
class impl; class impl;
fwd<impl,368> my; fwd<impl,368> my;

View file

@ -56,6 +56,8 @@ namespace fc {
friend string operator + ( const string&, const string& ); friend string operator + ( const string&, const string& );
friend string operator + ( const string&, char c ); friend string operator + ( const string&, char c );
fc::string substr( int32_t start, int32_t len = 0x7fffffff );
private: private:
void* my; void* my;

View file

@ -16,6 +16,8 @@ namespace fc {
size_t readsome( char* buffer, size_t max ); size_t readsome( char* buffer, size_t max );
size_t read( char* buffer, size_t s ); size_t read( char* buffer, size_t s );
void flush();
private: private:
friend class tcp_server; friend class tcp_server;
class impl; class impl;
@ -24,11 +26,11 @@ namespace fc {
class tcp_server { class tcp_server {
public: public:
tcp_server(); tcp_server(uint16_t port=0);
~tcp_server(); ~tcp_server();
bool accept( tcp_socket& s ); bool accept( tcp_socket& s );
void listen( uint16_t port ); // void listen( uint16_t port );
private: private:
class impl; class impl;

View file

@ -1,5 +1,6 @@
#include <fc/asio.hpp> #include <fc/asio.hpp>
#include <fc/thread.hpp> #include <fc/thread.hpp>
#include <fc/log.hpp>
#include <boost/thread.hpp> #include <boost/thread.hpp>
namespace fc { namespace fc {

View file

@ -21,6 +21,7 @@
#include <algorithm> #include <algorithm>
#include <string.h> #include <string.h>
#include <fc/log.hpp>
#include <fc/string.hpp> #include <fc/string.hpp>
#include <fc/exception.hpp> #include <fc/exception.hpp>
@ -534,6 +535,7 @@ inline std::string EncodeBase58(const unsigned char* pbegin, const unsigned char
// Convert little endian std::string to big endian // Convert little endian std::string to big endian
reverse(str.begin(), str.end()); reverse(str.begin(), str.end());
// slog( "Encode '%s'", str.c_str() );
return str; return str;
} }
@ -563,8 +565,10 @@ inline bool DecodeBase58(const char* psz, std::vector<unsigned char>& vchRet)
{ {
while (isspace(*p)) while (isspace(*p))
p++; p++;
if (*p != '\0') if (*p != '\0') {
slog( "%s '%c'", pszBase58,*p );
return false; return false;
}
break; break;
} }
bnChar.setulong(p1 - pszBase58); bnChar.setulong(p1 - pszBase58);
@ -609,6 +613,7 @@ fc::string to_base58( const char* d, uint32_t s ) {
* @return the number of bytes decoded * @return the number of bytes decoded
*/ */
size_t from_base58( const fc::string& base58_str, char* out_data, size_t out_data_len ) { size_t from_base58( const fc::string& base58_str, char* out_data, size_t out_data_len ) {
//slog( "%s", base58_str.c_str() );
std::vector<unsigned char> out; std::vector<unsigned char> out;
if( !DecodeBase58( base58_str.c_str(), out ) ) { if( !DecodeBase58( base58_str.c_str(), out ) ) {
FC_THROW_MSG( "Unable to decode base58 string '%s'", base58_str ); FC_THROW_MSG( "Unable to decode base58 string '%s'", base58_str );

View file

@ -102,5 +102,7 @@ namespace fc {
const fc::string& a1, const fc::string& a2, const fc::string& a3, const fc::string& a4 ) { const fc::string& a1, const fc::string& a2, const fc::string& a3, const fc::string& a4 ) {
::boost::exception_detail::throw_exception_(fc::generic_exception(msg),func, file, line ); ::boost::exception_detail::throw_exception_(fc::generic_exception(msg),func, file, line );
} }
fc::string except_str() { return fc::current_exception().diagnostic_information(); }
} }

View file

@ -56,11 +56,20 @@ namespace fc {
fc::path path::filename()const { fc::path path::filename()const {
return _p->filename(); return _p->filename();
} }
fc::path path::extension()const {
return _p->extension();
}
fc::path path::stem()const {
return _p->stem();
}
fc::path path::parent_path()const {
return _p->parent_path();
}
bool exists( const path& p ) { return boost::filesystem::exists(p); } bool exists( const path& p ) { return boost::filesystem::exists(p); }
void create_directories( const path& p ) { boost::filesystem::create_directories(p); } void create_directories( const path& p ) { boost::filesystem::create_directories(p); }
bool is_directory( const path& p ) { return boost::filesystem::is_directory(p); } bool is_directory( const path& p ) { return boost::filesystem::is_directory(p); }
bool is_regular( const path& p ) { return boost::filesystem::is_regular(p); } bool is_regular_file( const path& p ) { return boost::filesystem::is_regular_file(p); }
uint64_t file_size( const path& p ) { return boost::filesystem::file_size(p); } uint64_t file_size( const path& p ) { return boost::filesystem::file_size(p); }
} }

View file

@ -233,16 +233,81 @@ namespace fc {
public: public:
impl( fc::string&s ) impl( fc::string&s )
:ss( reinterpret_cast<std::string&>(s) ) :ss( reinterpret_cast<std::string&>(s) )
{ { }
} impl(){}
std::stringstream ss; std::stringstream ss;
}; };
stringstream::stringstream( fc::string& s ) stringstream::stringstream( fc::string& s )
:my(s) { :my(s) {
} }
stringstream::stringstream(){}
stringstream::~stringstream(){} stringstream::~stringstream(){}
stringstream& operator<<( stringstream& s, const int64_t& v ){
s.my->ss << v;
return s;
}
stringstream& operator<<( stringstream& s, const uint64_t& v ){
s.my->ss << v;
return s;
}
stringstream& operator<<( stringstream& s, const int32_t& v ){
s.my->ss << v;
return s;
}
stringstream& operator<<( stringstream& s, const uint32_t& v ){
s.my->ss << v;
return s;
}
stringstream& operator<<( stringstream& s, const int16_t& v ){
s.my->ss << v;
return s;
}
stringstream& operator<<( stringstream& s, const uint16_t& v ){
s.my->ss << v;
return s;
}
stringstream& operator<<( stringstream& s, const int8_t& v ){
s.my->ss << v;
return s;
}
stringstream& operator<<( stringstream& s, const uint8_t& v ){
s.my->ss << v;
return s;
}
stringstream& operator<<( stringstream& s, const float& v ){
s.my->ss << v;
return s;
}
stringstream& operator<<( stringstream& s, const double& v ){
s.my->ss << v;
return s;
}
stringstream& operator<<( stringstream& s, const bool& v ){
s.my->ss << v;
return s;
}
stringstream& operator<<( stringstream& s, const char& v ){
s.my->ss << v;
return s;
}
stringstream& operator<<( stringstream& s, const char* cs ) {
s.my->ss << cs;
return s;
}
stringstream& operator<<( stringstream& s, const fc::string& v ){
s.my->ss <<reinterpret_cast<const std::string&>(v);
return s;
}
fc::string stringstream::str(){
//std::string st = my->ss.str();
return my->ss.str().c_str();//*reinterpret_cast<fc::string*>(&st);
}
stringstream& operator>>( stringstream& s, int64_t& v ){ stringstream& operator>>( stringstream& s, int64_t& v ){
s.my->ss >> v; s.my->ss >> v;
return s; return s;

View file

@ -64,6 +64,10 @@ namespace fc {
void string::clear() { return reinterpret_cast<std::string*>(this)->clear(); } void string::clear() { return reinterpret_cast<std::string*>(this)->clear(); }
void string::resize( uint64_t s ) { reinterpret_cast<std::string*>(this)->resize(s); } void string::resize( uint64_t s ) { reinterpret_cast<std::string*>(this)->resize(s); }
fc::string string::substr( int32_t start, int32_t len ) {
return reinterpret_cast<const std::string&>(*this).substr(start,len).c_str();
}
const char* string::c_str()const { return reinterpret_cast<const std::string*>(this)->c_str(); } const char* string::c_str()const { return reinterpret_cast<const std::string*>(this)->c_str(); }
bool string::operator == ( const char* s )const { bool string::operator == ( const char* s )const {

View file

@ -1,6 +1,7 @@
#include <fc/tcp_socket.hpp> #include <fc/tcp_socket.hpp>
#include <fc/ip.hpp> #include <fc/ip.hpp>
#include <fc/fwd_impl.hpp> #include <fc/fwd_impl.hpp>
#include <fc/log.hpp>
#include <fc/asio.hpp> #include <fc/asio.hpp>
namespace fc { namespace fc {
@ -9,7 +10,7 @@ namespace fc {
public: public:
impl():_sock( fc::asio::default_io_service() ){} impl():_sock( fc::asio::default_io_service() ){}
~impl(){ ~impl(){
_sock.cancel(); _sock.close();
} }
boost::asio::ip::tcp::socket _sock; boost::asio::ip::tcp::socket _sock;
@ -24,12 +25,14 @@ namespace fc {
boost::system::error_code ec; boost::system::error_code ec;
size_t w = my->_sock.write_some( boost::asio::buffer( buf, len ), ec ); size_t w = my->_sock.write_some( boost::asio::buffer( buf, len ), ec );
slog( "wrote %d ", w );
if( w < len ) { if( w < len ) {
buf += w; buf += w;
len -= w; len -= w;
} }
if( ec == boost::asio::error::would_block ) { if( ec == boost::asio::error::would_block ) {
wlog( "world block" );
promise<size_t>::ptr p(new promise<size_t>("tcp_socket::write")); promise<size_t>::ptr p(new promise<size_t>("tcp_socket::write"));
boost::asio::async_write( my->_sock, boost::asio::buffer(buf, len), boost::asio::async_write( my->_sock, boost::asio::buffer(buf, len),
[=]( const boost::system::error_code& ec, size_t bt ) { [=]( const boost::system::error_code& ec, size_t bt ) {
@ -38,6 +41,7 @@ namespace fc {
}); });
p->wait(); p->wait();
} else if( ec ) { } else if( ec ) {
wlog( "throw" );
throw boost::system::system_error(ec); throw boost::system::system_error(ec);
} }
} }
@ -67,7 +71,7 @@ namespace fc {
class tcp_server::impl { class tcp_server::impl {
public: public:
impl():_accept( fc::asio::default_io_service() ){} impl(uint16_t port):_accept( fc::asio::default_io_service(), boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port) ){}
~impl(){ ~impl(){
_accept.cancel(); _accept.cancel();
} }
@ -75,26 +79,38 @@ namespace fc {
boost::asio::ip::tcp::acceptor _accept; boost::asio::ip::tcp::acceptor _accept;
}; };
tcp_server::tcp_server() { tcp_server::tcp_server(uint16_t port)
:my(port) {
} }
tcp_server::~tcp_server() { tcp_server::~tcp_server() {
} }
bool tcp_server::accept( tcp_socket& s ) { bool tcp_server::accept( tcp_socket& s ) {
fc::promise<boost::system::error_code>::ptr p( slog( "accept!" );
new promise<boost::system::error_code>("mace::cmt::asio::tcp::accept") ); fc::promise<boost::system::error_code>::ptr p( new promise<boost::system::error_code>("mace::cmt::asio::tcp::accept") );
my->_accept.async_accept( s.my->_sock, slog( "." );
[=]( const boost::system::error_code& e ) { my->_accept.async_accept( s.my->_sock, [=]( const boost::system::error_code& e ) {
p->set_value(e); slog( "\aaccept!" );
} ); p->set_value(e);
} );
slog( "." );
auto ec = p->wait(); auto ec = p->wait();
slog( "." );
if( !ec ) s.my->_sock.non_blocking(true); if( !ec ) s.my->_sock.non_blocking(true);
if( ec ) BOOST_THROW_EXCEPTION( boost::system::system_error(ec) ); if( ec ) BOOST_THROW_EXCEPTION( boost::system::system_error(ec) );
return true; return true;
} }
#if 0
void tcp_server::listen( uint16_t port ) { void tcp_server::listen( uint16_t port ) {
/*
slog( "listen %d!", port );
my->_accept.bind(
slog( "listen %d!", port );
my->_accept.listen(port); my->_accept.listen(port);
slog( "listen %d!", port );
*/
} }
#endif
} // namespace fc } // namespace fc