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 @@ ...@@ -17,6 +17,7 @@
#include <folly/io/async/test/SocketPair.h> #include <folly/io/async/test/SocketPair.h>
#include <folly/Conv.h> #include <folly/Conv.h>
#include <folly/net/NetOps.h>
#include <folly/portability/Fcntl.h> #include <folly/portability/Fcntl.h>
#include <folly/portability/Sockets.h> #include <folly/portability/Sockets.h>
#include <folly/portability/Unistd.h> #include <folly/portability/Unistd.h>
...@@ -27,19 +28,19 @@ ...@@ -27,19 +28,19 @@
namespace folly { namespace folly {
SocketPair::SocketPair(Mode mode) { 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>( throw std::runtime_error(folly::to<std::string>(
"test::SocketPair: failed create socket pair", errno)); "test::SocketPair: failed create socket pair", errno));
} }
if (mode == NONBLOCKING) { 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>( throw std::runtime_error(folly::to<std::string>(
"test::SocketPair: failed to set non-blocking " "test::SocketPair: failed to set non-blocking "
"read mode", "read mode",
errno)); 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>( throw std::runtime_error(folly::to<std::string>(
"test::SocketPair: failed to set non-blocking " "test::SocketPair: failed to set non-blocking "
"write mode", "write mode",
...@@ -54,16 +55,16 @@ SocketPair::~SocketPair() { ...@@ -54,16 +55,16 @@ SocketPair::~SocketPair() {
} }
void SocketPair::closeFD0() { void SocketPair::closeFD0() {
if (fds_[0] >= 0) { if (fds_[0] != NetworkSocket()) {
close(fds_[0]); netops::close(fds_[0]);
fds_[0] = -1; fds_[0] = NetworkSocket();
} }
} }
void SocketPair::closeFD1() { void SocketPair::closeFD1() {
if (fds_[1] >= 0) { if (fds_[1] != NetworkSocket()) {
close(fds_[1]); netops::close(fds_[1]);
fds_[1] = -1; fds_[1] = NetworkSocket();
} }
} }
......
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
#pragma once #pragma once
#include <folly/net/NetworkSocket.h>
namespace folly { namespace folly {
class SocketPair { class SocketPair {
...@@ -26,7 +28,7 @@ class SocketPair { ...@@ -26,7 +28,7 @@ class SocketPair {
~SocketPair(); ~SocketPair();
int operator[](int index) const { int operator[](int index) const {
return fds_[index]; return fds_[index].toFd();
} }
void closeFD0(); void closeFD0();
...@@ -39,13 +41,13 @@ class SocketPair { ...@@ -39,13 +41,13 @@ class SocketPair {
return extractFD(1); return extractFD(1);
} }
int extractFD(int index) { int extractFD(int index) {
int fd = fds_[index]; auto fd = fds_[index];
fds_[index] = -1; fds_[index] = NetworkSocket();
return fd; return fd.toFd();
} }
private: private:
int fds_[2]; NetworkSocket fds_[2];
}; };
} // namespace folly } // 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