1 | /* |
2 | * Copyright 2014-present Facebook, Inc. |
3 | * |
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 |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
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. |
15 | */ |
16 | |
17 | #pragma once |
18 | |
19 | #include <folly/net/NetworkSocket.h> |
20 | |
21 | namespace folly { |
22 | |
23 | class SocketPair { |
24 | public: |
25 | enum Mode { BLOCKING, NONBLOCKING }; |
26 | |
27 | explicit SocketPair(Mode mode = NONBLOCKING); |
28 | ~SocketPair(); |
29 | |
30 | int operator[](int index) const { |
31 | return fds_[index].toFd(); |
32 | } |
33 | |
34 | void closeFD0(); |
35 | void closeFD1(); |
36 | |
37 | int () { |
38 | return extractFD(0); |
39 | } |
40 | int () { |
41 | return extractFD(1); |
42 | } |
43 | int (int index) { |
44 | auto fd = fds_[index]; |
45 | fds_[index] = NetworkSocket(); |
46 | return fd.toFd(); |
47 | } |
48 | |
49 | private: |
50 | NetworkSocket fds_[2]; |
51 | }; |
52 | |
53 | } // namespace folly |
54 | |