proxygen
folly::SocketPair Class Reference

#include <SocketPair.h>

Public Types

enum  Mode { BLOCKING, NONBLOCKING }
 

Public Member Functions

 SocketPair (Mode mode=NONBLOCKING)
 
 ~SocketPair ()
 
int operator[] (int index) const
 
void closeFD0 ()
 
void closeFD1 ()
 
int extractFD0 ()
 
int extractFD1 ()
 
int extractFD (int index)
 

Private Attributes

int fds_ [2]
 

Detailed Description

Definition at line 21 of file SocketPair.h.

Member Enumeration Documentation

Enumerator
BLOCKING 
NONBLOCKING 

Definition at line 23 of file SocketPair.h.

Constructor & Destructor Documentation

folly::SocketPair::SocketPair ( Mode  mode = NONBLOCKING)
explicit

Definition at line 29 of file SocketPair.cpp.

References fds_, NONBLOCKING, and folly::netops::socketpair().

29  {
30  if (socketpair(PF_UNIX, SOCK_STREAM, 0, fds_) != 0) {
31  throw std::runtime_error(folly::to<std::string>(
32  "test::SocketPair: failed create socket pair", errno));
33  }
34 
35  if (mode == NONBLOCKING) {
36  if (fcntl(fds_[0], F_SETFL, O_NONBLOCK) != 0) {
37  throw std::runtime_error(folly::to<std::string>(
38  "test::SocketPair: failed to set non-blocking "
39  "read mode",
40  errno));
41  }
42  if (fcntl(fds_[1], F_SETFL, O_NONBLOCK) != 0) {
43  throw std::runtime_error(folly::to<std::string>(
44  "test::SocketPair: failed to set non-blocking "
45  "write mode",
46  errno));
47  }
48  }
49 }
folly::Optional< PskKeyExchangeMode > mode
int socketpair(int domain, int type, int protocol, NetworkSocket sv[2])
Definition: NetOps.cpp:416
folly::SocketPair::~SocketPair ( )

Definition at line 51 of file SocketPair.cpp.

References closeFD0(), and closeFD1().

51  {
52  closeFD0();
53  closeFD1();
54 }

Member Function Documentation

void folly::SocketPair::closeFD0 ( )

Definition at line 56 of file SocketPair.cpp.

References folly::netops::close(), and fds_.

Referenced by operator[](), and ~SocketPair().

56  {
57  if (fds_[0] >= 0) {
58  close(fds_[0]);
59  fds_[0] = -1;
60  }
61 }
int close(NetworkSocket s)
Definition: NetOps.cpp:90
void folly::SocketPair::closeFD1 ( )

Definition at line 63 of file SocketPair.cpp.

References folly::netops::close(), and fds_.

Referenced by operator[](), and ~SocketPair().

63  {
64  if (fds_[1] >= 0) {
65  close(fds_[1]);
66  fds_[1] = -1;
67  }
68 }
int close(NetworkSocket s)
Definition: NetOps.cpp:90
int folly::SocketPair::extractFD ( int  index)
inline

Definition at line 41 of file SocketPair.h.

References fds_.

Referenced by extractFD0(), and extractFD1().

41  {
42  int fd = fds_[index];
43  fds_[index] = -1;
44  return fd;
45  }
int folly::SocketPair::extractFD0 ( )
inline

Definition at line 35 of file SocketPair.h.

References extractFD().

35  {
36  return extractFD(0);
37  }
int extractFD(int index)
Definition: SocketPair.h:41
int folly::SocketPair::extractFD1 ( )
inline

Definition at line 38 of file SocketPair.h.

References extractFD().

38  {
39  return extractFD(1);
40  }
int extractFD(int index)
Definition: SocketPair.h:41
int folly::SocketPair::operator[] ( int  index) const
inline

Definition at line 28 of file SocketPair.h.

References closeFD0(), closeFD1(), and fds_.

28  {
29  return fds_[index];
30  }

Member Data Documentation

int folly::SocketPair::fds_[2]
private

Definition at line 48 of file SocketPair.h.

Referenced by closeFD0(), closeFD1(), extractFD(), operator[](), and SocketPair().


The documentation for this class was generated from the following files: