Handler for the server socket. NOTE: This keeps all incoming sockets open forever because the link tests need "half-open" connections for some tests. A connection becomes "half-open" when either the read or write direction of the socket is closed via a `::shutdown()`. Normally, the peer will discover a "half-open" connection if a read/write returns data of length zero. The test-linkee does not a
| 52 | // The test-linkee does not act on this information in order to give |
| 53 | // the link tests full control over the lifetime of sockets. |
| 54 | void on_accept(const Future<Socket>& incoming) |
| 55 | { |
| 56 | if (incoming.isReady()) { |
| 57 | // NOTE: We copy and explicitly leak the socket here. |
| 58 | // `Socket` is a shared pointer that closes itself after the |
| 59 | // reference count goes to zero. By leaking the socket here, we |
| 60 | // ensure that each incoming socket is never closed by the linkee. |
| 61 | new Socket(incoming.get()); |
| 62 | |
| 63 | const size_t size = 1024; |
| 64 | char* data = new char[size]; |
| 65 | |
| 66 | incoming->recv(data, size) |
| 67 | .then([data](const size_t size) -> Future<Nothing> { |
| 68 | delete[] data; |
| 69 | |
| 70 | // If there was any content at all, assume this is a message |
| 71 | // telling the linkee to terminate. |
| 72 | if (size > 0) { |
| 73 | EXIT(EXIT_SUCCESS); |
| 74 | } |
| 75 | |
| 76 | return Nothing(); |
| 77 | }); |
| 78 | } |
| 79 | |
| 80 | __s__->accept() |
| 81 | .onAny(lambda::bind(&on_accept, lambda::_1)); |
| 82 | } |
| 83 | |
| 84 | |
| 85 | /** |