2 * Copyright 2004-present Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <folly/io/async/test/SocketPair.h>
19 #include <folly/Conv.h>
20 #include <folly/portability/Fcntl.h>
21 #include <folly/portability/Sockets.h>
22 #include <folly/portability/Unistd.h>
29 SocketPair::SocketPair(Mode mode) {
30 if (socketpair(PF_UNIX, SOCK_STREAM, 0, fds_) != 0) {
31 throw std::runtime_error(
32 folly::to<std::string>("test::SocketPair: failed create socket pair",
36 if (mode == NONBLOCKING) {
37 if (fcntl(fds_[0], F_SETFL, O_NONBLOCK) != 0) {
38 throw std::runtime_error(
39 folly::to<std::string>("test::SocketPair: failed to set non-blocking "
42 if (fcntl(fds_[1], F_SETFL, O_NONBLOCK) != 0) {
43 throw std::runtime_error(
44 folly::to<std::string>("test::SocketPair: failed to set non-blocking "
45 "write mode", errno));
50 SocketPair::~SocketPair() {
55 void SocketPair::closeFD0() {
62 void SocketPair::closeFD1() {