TunDevice: separate poll and read
This commit is contained in:
parent
8ec3f20513
commit
a27e894fc1
2 changed files with 13 additions and 6 deletions
|
@ -53,7 +53,7 @@ TunDevice::~TunDevice() {
|
|||
close(_fd);
|
||||
}
|
||||
|
||||
size_t TunDevice::read_packet(char* read_buffer, size_t buf_size, int timeout) {
|
||||
size_t TunDevice::poll_packet(char* read_buffer, size_t buf_size, int timeout) {
|
||||
int poll_rc = poll(&_poll_fd, 1, timeout);
|
||||
if(poll_rc < 0) {
|
||||
if(errno == EINTR) // Interrupt.
|
||||
|
@ -66,7 +66,11 @@ size_t TunDevice::read_packet(char* read_buffer, size_t buf_size, int timeout) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
int nread = read(_fd, read_buffer, buf_size);
|
||||
return this->read(read_buffer, buf_size);
|
||||
}
|
||||
|
||||
size_t TunDevice::read(char* read_buffer, size_t buf_size) {
|
||||
int nread = ::read(_fd, read_buffer, buf_size);
|
||||
if(nread < 0) {
|
||||
throw TunDevice::NetError(
|
||||
"Error reading from interface", errno, true);
|
||||
|
@ -75,8 +79,8 @@ size_t TunDevice::read_packet(char* read_buffer, size_t buf_size, int timeout) {
|
|||
return _last_read_size;
|
||||
}
|
||||
|
||||
size_t TunDevice::write_packet(const char* data, size_t len) {
|
||||
int nwritten = write(_fd, data, len);
|
||||
size_t TunDevice::write(const char* data, size_t len) {
|
||||
int nwritten = ::write(_fd, data, len);
|
||||
if(nwritten < 0) {
|
||||
throw TunDevice::NetError(
|
||||
"Error writing to interface: ", errno, true);
|
||||
|
|
|
@ -33,9 +33,12 @@ class TunDevice {
|
|||
* Timeouts after `timeout` ms, or never if `timeout < 0`.
|
||||
* Upon timeout, returns 0.
|
||||
*/
|
||||
size_t read_packet(char* read_buffer, size_t buf_size, int timeout=-1);
|
||||
size_t poll_packet(char* read_buffer, size_t buf_size, int timeout=-1);
|
||||
|
||||
size_t write_packet(const char* data, size_t len);
|
||||
/* Reads a packet, blocking if none is available. */
|
||||
size_t read(char* read_buffer, size_t buf_size);
|
||||
|
||||
size_t write(const char* data, size_t len);
|
||||
|
||||
private:
|
||||
int _fd;
|
||||
|
|
Loading…
Reference in a new issue