Commit 95ceac65 authored by Orvid King's avatar Orvid King Committed by Facebook Github Bot

NetworkSocket support in SocketPair

Summary: More!

Reviewed By: yfeldblum

Differential Revision: D12818656

fbshipit-source-id: acb269258524ae22bf8761407713b89cf3d57a3a
parent 1f1d9662
......@@ -17,6 +17,7 @@
#include <folly/io/async/test/SocketPair.h>
#include <folly/Conv.h>
#include <folly/net/NetOps.h>
#include <folly/portability/Fcntl.h>
#include <folly/portability/Sockets.h>
#include <folly/portability/Unistd.h>
......@@ -27,19 +28,19 @@
namespace folly {
SocketPair::SocketPair(Mode mode) {
if (socketpair(PF_UNIX, SOCK_STREAM, 0, fds_) != 0) {
if (netops::socketpair(PF_UNIX, SOCK_STREAM, 0, fds_) != 0) {
throw std::runtime_error(folly::to<std::string>(
"test::SocketPair: failed create socket pair", errno));
}
if (mode == NONBLOCKING) {
if (fcntl(fds_[0], F_SETFL, O_NONBLOCK) != 0) {
if (netops::set_socket_non_blocking(fds_[0]) != 0) {
throw std::runtime_error(folly::to<std::string>(
"test::SocketPair: failed to set non-blocking "
"read mode",
errno));
}
if (fcntl(fds_[1], F_SETFL, O_NONBLOCK) != 0) {
if (netops::set_socket_non_blocking(fds_[1]) != 0) {
throw std::runtime_error(folly::to<std::string>(
"test::SocketPair: failed to set non-blocking "
"write mode",
......@@ -54,16 +55,16 @@ SocketPair::~SocketPair() {
}
void SocketPair::closeFD0() {
if (fds_[0] >= 0) {
close(fds_[0]);
fds_[0] = -1;
if (fds_[0] != NetworkSocket()) {
netops::close(fds_[0]);
fds_[0] = NetworkSocket();
}
}
void SocketPair::closeFD1() {
if (fds_[1] >= 0) {
close(fds_[1]);
fds_[1] = -1;
if (fds_[1] != NetworkSocket()) {
netops::close(fds_[1]);
fds_[1] = NetworkSocket();
}
}
......
......@@ -16,6 +16,8 @@
#pragma once
#include <folly/net/NetworkSocket.h>
namespace folly {
class SocketPair {
......@@ -26,7 +28,7 @@ class SocketPair {
~SocketPair();
int operator[](int index) const {
return fds_[index];
return fds_[index].toFd();
}
void closeFD0();
......@@ -39,13 +41,13 @@ class SocketPair {
return extractFD(1);
}
int extractFD(int index) {
int fd = fds_[index];
fds_[index] = -1;
return fd;
auto fd = fds_[index];
fds_[index] = NetworkSocket();
return fd.toFd();
}
private:
int fds_[2];
NetworkSocket fds_[2];
};
} // namespace folly
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment