Use dup() instead of move semantics for Network::Socket
This commit is contained in:
@@ -140,8 +140,7 @@ void Connection::prune_sockets( void )
|
|||||||
}
|
}
|
||||||
|
|
||||||
Connection::Socket::Socket()
|
Connection::Socket::Socket()
|
||||||
: _fd( socket( AF_INET, SOCK_DGRAM, 0 ) ),
|
: _fd( socket( AF_INET, SOCK_DGRAM, 0 ) )
|
||||||
_moved( false )
|
|
||||||
{
|
{
|
||||||
if ( _fd < 0 ) {
|
if ( _fd < 0 ) {
|
||||||
throw NetworkException( "socket", errno );
|
throw NetworkException( "socket", errno );
|
||||||
@@ -570,25 +569,26 @@ uint64_t Connection::timeout( void ) const
|
|||||||
|
|
||||||
Connection::Socket::~Socket()
|
Connection::Socket::~Socket()
|
||||||
{
|
{
|
||||||
if ( !_moved ) {
|
|
||||||
if ( close( _fd ) < 0 ) {
|
if ( close( _fd ) < 0 ) {
|
||||||
throw NetworkException( "close", errno );
|
throw NetworkException( "close", errno );
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Connection::Socket::Socket( const Socket & other )
|
Connection::Socket::Socket( const Socket & other )
|
||||||
: _fd( other._fd ),
|
: _fd( dup( other._fd ) )
|
||||||
_moved( false )
|
|
||||||
{
|
{
|
||||||
other.move();
|
if ( _fd < 0 ) {
|
||||||
|
throw NetworkException( "socket", errno );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const Connection::Socket & Connection::Socket::operator=( const Socket & other )
|
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;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -107,11 +107,9 @@ namespace Network {
|
|||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
int _fd;
|
int _fd;
|
||||||
mutable bool _moved;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
int fd( void ) const { assert( !_moved ); return _fd; }
|
int fd( void ) const { return _fd; }
|
||||||
void move( void ) const { assert( !_moved ); _moved = true; }
|
|
||||||
Socket();
|
Socket();
|
||||||
~Socket();
|
~Socket();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user