MCPcopy Create free account
hub / github.com/3rdparty/libprocess / accept

Method accept

src/posix/poll_socket.cpp:53–113  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

51
52
53Future<std::shared_ptr<SocketImpl>> PollSocketImpl::accept()
54{
55 // Need to hold a copy of `this` so that the underlying socket
56 // doesn't end up getting reused before we return from the call to
57 // `io::poll` and end up accepting a socket incorrectly.
58 auto self = shared(this);
59
60 return io::poll(get(), io::READ)
61 .then([self]() -> Future<std::shared_ptr<SocketImpl>> {
62 Try<int_fd> accepted = network::accept(self->get());
63 if (accepted.isError()) {
64 return Failure(accepted.error());
65 }
66
67 int_fd s = accepted.get();
68 Try<Nothing> nonblock = os::nonblock(s);
69 if (nonblock.isError()) {
70 os::close(s);
71 return Failure("Failed to accept, nonblock: " + nonblock.error());
72 }
73
74 Try<Nothing> cloexec = os::cloexec(s);
75 if (cloexec.isError()) {
76 os::close(s);
77 return Failure("Failed to accept, cloexec: " + cloexec.error());
78 }
79
80 Try<Address> address = network::address(s);
81 if (address.isError()) {
82 os::close(s);
83 return Failure("Failed to get address: " + address.error());
84 }
85
86 // Turn off Nagle (TCP_NODELAY) so pipelined requests don't wait.
87 // NOTE: We cast to `char*` here because the function prototypes
88 // on Windows use `char*` instead of `void*`.
89 if (address->family() == Address::Family::INET4 ||
90 address->family() == Address::Family::INET6) {
91 int on = 1;
92 if (::setsockopt(
93 s,
94 SOL_TCP,
95 TCP_NODELAY,
96 reinterpret_cast<const char*>(&on),
97 sizeof(on)) < 0) {
98 const string error = os::strerror(errno);
99 os::close(s);
100 return Failure(
101 "Failed to turn off the Nagle algorithm: " + stringify(error));
102 }
103 }
104
105 Try<std::shared_ptr<SocketImpl>> impl = create(s);
106 if (impl.isError()) {
107 os::close(s);
108 return Failure("Failed to create socket: " + impl.error());
109 }
110

Callers 12

runMethod · 0.45
synchronizedFunction · 0.45
initializeFunction · 0.45
TEST_FFunction · 0.45
TEST_FFunction · 0.45
TEST_PFunction · 0.45
TEST_FFunction · 0.45
on_acceptFunction · 0.45
mainFunction · 0.45
TEST_FFunction · 0.45
TEST_PFunction · 0.45
foreachFunction · 0.45

Calls 10

getFunction · 0.85
FailureClass · 0.85
closeFunction · 0.85
cloexecFunction · 0.85
familyMethod · 0.80
pollFunction · 0.50
acceptFunction · 0.50
addressFunction · 0.50
thenMethod · 0.45
getMethod · 0.45

Tested by 9

TEST_FFunction · 0.36
TEST_FFunction · 0.36
TEST_PFunction · 0.36
TEST_FFunction · 0.36
on_acceptFunction · 0.36
mainFunction · 0.36
TEST_FFunction · 0.36
TEST_PFunction · 0.36
foreachFunction · 0.36