diff --git a/include/fc/endpoint.hpp b/include/fc/endpoint.hpp index 939ed07..286592e 100644 --- a/include/fc/endpoint.hpp +++ b/include/fc/endpoint.hpp @@ -1,5 +1,4 @@ -#ifndef _FC_IP_HPP_ -#define _FC_IP_HPP_ +#pragma once #include namespace fc { @@ -10,9 +9,13 @@ namespace fc { address( uint32_t _ip = 0 ); address( const fc::string& s ); + address& operator=( const fc::string& s ); operator fc::string()const; + uint32_t ip()const { return _ip; } + +d private: uint32_t _ip; }; @@ -23,8 +26,8 @@ namespace fc { endpoint( const fc::string& i, uint16_t p ); endpoint( const address& i, uint16_t p ); - uint16_t port() { return _port; } - ip::address get_address() { return _ip; } + uint16_t port()const { return _port; } + fc::ip::address get_address()const { return _ip; } private: uint16_t _port; @@ -32,4 +35,3 @@ namespace fc { }; } } -#endif // _FC_ENDPOINT_HPP_ diff --git a/include/fc/tcp_socket.hpp b/include/fc/tcp_socket.hpp index e90d555..bb5fddb 100644 --- a/include/fc/tcp_socket.hpp +++ b/include/fc/tcp_socket.hpp @@ -29,6 +29,7 @@ namespace fc { tcp_server(uint16_t port=0); ~tcp_server(); + void close(); bool accept( tcp_socket& s ); // void listen( uint16_t port ); diff --git a/src/stream.cpp b/src/stream.cpp index bba12f1..5e4be9d 100644 --- a/src/stream.cpp +++ b/src/stream.cpp @@ -5,6 +5,7 @@ #include #include #include +#include namespace fc { namespace detail { @@ -21,6 +22,7 @@ namespace fc { std::streamsize read( char* s, std::streamsize n ) { int r = std::cin.readsome(s,n); + slog( "%d", r ); if( std::cin && r <= 0 ) { std::cin.read( s, 1 ); if( std::cin.eof() ) return -1; @@ -362,6 +364,16 @@ namespace fc { } bool getline( istream& in, fc::string& f ) { + std::stringstream ss; + char c; + while( in.readsome( &c, sizeof(c) ) > 0 ) { + slog( "%c", c ); + if( c != '\n' ) ss << c; + else { + f = ss.str(); + return true; + } + } return false; } diff --git a/src/tcp_socket.cpp b/src/tcp_socket.cpp index 07abe9a..5f645f5 100644 --- a/src/tcp_socket.cpp +++ b/src/tcp_socket.cpp @@ -3,6 +3,7 @@ #include #include #include +#include namespace fc { @@ -10,7 +11,7 @@ namespace fc { public: impl():_sock( fc::asio::default_io_service() ){} ~impl(){ - _sock.close(); + if( _sock.is_open() ) _sock.close(); } boost::asio::ip::tcp::socket _sock; @@ -68,17 +69,22 @@ namespace fc { } return r; } + void tcp_socket::connect_to( const fc::ip::endpoint& e ) { + fc::asio::tcp::connect(my->_sock, fc::asio::tcp::endpoint( boost::asio::ip::address_v4(e.get_address()), e.port() ) ); + } 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(){ - _accept.cancel(); + _accept.close(); } boost::asio::ip::tcp::acceptor _accept; }; - + void tcp_server::close() { + if( my->_accept.is_open() ) my->_accept.close(); + } tcp_server::tcp_server(uint16_t port) :my(port) { } @@ -87,16 +93,11 @@ namespace fc { bool tcp_server::accept( tcp_socket& s ) { - slog( "accept!" ); fc::promise::ptr p( new promise("mace::cmt::asio::tcp::accept") ); - slog( "." ); my->_accept.async_accept( s.my->_sock, [=]( const boost::system::error_code& e ) { - slog( "\aaccept!" ); p->set_value(e); } ); - slog( "." ); auto ec = p->wait(); - slog( "." ); if( !ec ) s.my->_sock.non_blocking(true); if( ec ) BOOST_THROW_EXCEPTION( boost::system::system_error(ec) ); return true;