Skip to content

Commit

Permalink
Use dup() instead of move semantics for Network::Socket
Browse files Browse the repository at this point in the history
  • Loading branch information
keithw committed Nov 23, 2012
1 parent 05ec364 commit 6a16eec
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
22 changes: 11 additions & 11 deletions src/network/network.cc
Expand Up @@ -140,8 +140,7 @@ void Connection::prune_sockets( void )
}

Connection::Socket::Socket()
: _fd( socket( AF_INET, SOCK_DGRAM, 0 ) ),
_moved( false )
: _fd( socket( AF_INET, SOCK_DGRAM, 0 ) )
{
if ( _fd < 0 ) {
throw NetworkException( "socket", errno );
Expand Down Expand Up @@ -570,25 +569,26 @@ uint64_t Connection::timeout( void ) const

Connection::Socket::~Socket()
{
if ( !_moved ) {
if ( close( _fd ) < 0 ) {
throw NetworkException( "close", errno );
}
if ( close( _fd ) < 0 ) {
throw NetworkException( "close", errno );
}
}

Connection::Socket::Socket( const Socket & other )
: _fd( other._fd ),
_moved( false )
: _fd( dup( other._fd ) )
{
other.move();
if ( _fd < 0 ) {
throw NetworkException( "socket", errno );
}
}

const Connection::Socket & Connection::Socket::operator=( const Socket & other )
{
_fd = other._fd;
_fd = dup( other._fd );

other.move();
if ( _fd < 0 ) {
throw NetworkException( "socket", errno );
}

return *this;
}
Expand Down
4 changes: 1 addition & 3 deletions src/network/network.h
Expand Up @@ -107,11 +107,9 @@ namespace Network {
{
private:
int _fd;
mutable bool _moved;

public:
int fd( void ) const { assert( !_moved ); return _fd; }
void move( void ) const { assert( !_moved ); _moved = true; }
int fd( void ) const { return _fd; }
Socket();
~Socket();

Expand Down

0 comments on commit 6a16eec

Please sign in to comment.