forked from ravinet/mahimahi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_descriptor.hh
109 lines (84 loc) · 2.71 KB
/
file_descriptor.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* -*-mode:c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
#ifndef FILE_DESCRIPTOR_HH
#define FILE_DESCRIPTOR_HH
#include <unistd.h>
#include <fcntl.h>
#include "exception.hh"
#include "ezio.hh"
class FileDescriptor
{
private:
int fd_;
bool eof_;
unsigned int read_count_, write_count_;
protected:
void register_read( void ) { read_count_++; }
void register_write( void ) { write_count_++; }
public:
FileDescriptor( const int s_fd )
: fd_( s_fd ), eof_( false ),
read_count_(), write_count_()
{
if ( fd_ <= 2 ) { /* make sure not overwriting stdout/stderr */
throw Exception( "FileDescriptor", "fd <= 2" );
}
/* set close-on-exec flag so our file descriptors
aren't passed on to unrelated children (like a shell) */
SystemCall( "fcntl FD_CLOEXEC", fcntl( fd_, F_SETFD, FD_CLOEXEC ) );
}
virtual ~FileDescriptor()
{
if ( fd_ < 0 ) { /* has already been moved away */
return;
}
try {
SystemCall( "close", close( fd_ ) );
} catch ( const Exception & e ) { /* don't throw from destructor */
e.perror();
}
}
const int & num( void ) { return fd_; }
const bool & eof( void ) const { return eof_; }
/* forbid copying FileDescriptor objects or assigning them */
FileDescriptor( const FileDescriptor & other ) = delete;
const FileDescriptor & operator=( const FileDescriptor & other ) = delete;
/* allow moving FileDescriptor objects */
FileDescriptor( FileDescriptor && other )
: fd_( other.fd_ ), eof_( other.eof_ ),
read_count_( other.read_count_ ),
write_count_( other.write_count_)
{
other.fd_ = -1; /* disable the other FileDescriptor */
}
void write( const std::string & buffer )
{
writeall( num(), buffer );
register_write();
}
std::string::const_iterator write_some( const std::string::const_iterator & begin,
const std::string::const_iterator & end )
{
return ::write_some( num(), begin, end );
}
std::string read( void )
{
auto ret = readall( num() );
if ( ret.empty() ) { eof_ = true; }
register_read();
return ret;
}
std::string read( const size_t limit )
{
auto ret = readall( num(), limit );
if ( ret.empty() ) { eof_ = true; }
register_read();
return ret;
}
void set_eof( void )
{
eof_ = true;
}
unsigned int read_count( void ) const { return read_count_; }
unsigned int write_count( void ) const { return write_count_; }
};
#endif /* FILE_DESCRIPTOR_HH */