tcp_socket::connect

This commit is contained in:
Daniel Larimer 2012-10-16 21:48:09 -04:00
parent c174a93ffb
commit 1de7c609fa
4 changed files with 29 additions and 13 deletions

View file

@ -1,5 +1,4 @@
#ifndef _FC_IP_HPP_
#define _FC_IP_HPP_
#pragma once
#include <fc/string.hpp>
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_

View file

@ -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 );

View file

@ -5,6 +5,7 @@
#include <fstream>
#include <sstream>
#include <fc/fwd_impl.hpp>
#include <fc/log.hpp>
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;
}

View file

@ -3,6 +3,7 @@
#include <fc/fwd_impl.hpp>
#include <fc/log.hpp>
#include <fc/asio.hpp>
#include <fc/ip.hpp>
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<boost::system::error_code>::ptr p( new promise<boost::system::error_code>("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;