Commit 5fa36a7a authored by Orvid King's avatar Orvid King Committed by Facebook Github Bot

Port AsyncSocketTest's helpers to use NetworkSocket

Summary: More porting.

Reviewed By: yfeldblum

Differential Revision: D12892420

fbshipit-source-id: 6f92eb7922a9cfdee3bb412aa171fea8d4627617
parent 7657c306
...@@ -17,6 +17,8 @@ ...@@ -17,6 +17,8 @@
#include <folly/io/async/AsyncSocket.h> #include <folly/io/async/AsyncSocket.h>
#include <folly/io/async/test/BlockingSocket.h> #include <folly/io/async/test/BlockingSocket.h>
#include <folly/net/NetOps.h>
#include <folly/net/NetworkSocket.h>
#include <folly/portability/Sockets.h> #include <folly/portability/Sockets.h>
#include <memory> #include <memory>
...@@ -268,16 +270,16 @@ class TestServer { ...@@ -268,16 +270,16 @@ class TestServer {
public: public:
// Create a TestServer. // Create a TestServer.
// This immediately starts listening on an ephemeral port. // This immediately starts listening on an ephemeral port.
explicit TestServer(bool enableTFO = false, int bufSize = -1) : fd_(-1) { explicit TestServer(bool enableTFO = false, int bufSize = -1) : fd_() {
namespace fsp = folly::portability::sockets; namespace fsp = folly::portability::sockets;
fd_ = fsp::socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); fd_ = folly::netops::socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (fd_ < 0) { if (fd_ == folly::NetworkSocket()) {
throw folly::AsyncSocketException( throw folly::AsyncSocketException(
folly::AsyncSocketException::INTERNAL_ERROR, folly::AsyncSocketException::INTERNAL_ERROR,
"failed to create test server socket", "failed to create test server socket",
errno); errno);
} }
if (fcntl(fd_, F_SETFL, O_NONBLOCK) != 0) { if (folly::netops::set_socket_non_blocking(fd_) != 0) {
throw folly::AsyncSocketException( throw folly::AsyncSocketException(
folly::AsyncSocketException::INTERNAL_ERROR, folly::AsyncSocketException::INTERNAL_ERROR,
"failed to put test server socket in " "failed to put test server socket in "
...@@ -309,18 +311,20 @@ class TestServer { ...@@ -309,18 +311,20 @@ class TestServer {
}; };
if (bufSize > 0) { if (bufSize > 0) {
setsockopt(fd_, SOL_SOCKET, SO_SNDBUF, &bufSize, sizeof(bufSize)); folly::netops::setsockopt(
setsockopt(fd_, SOL_SOCKET, SO_RCVBUF, &bufSize, sizeof(bufSize)); fd_, SOL_SOCKET, SO_SNDBUF, &bufSize, sizeof(bufSize));
folly::netops::setsockopt(
fd_, SOL_SOCKET, SO_RCVBUF, &bufSize, sizeof(bufSize));
} }
if (bind(fd_, res->ai_addr, res->ai_addrlen)) { if (folly::netops::bind(fd_, res->ai_addr, res->ai_addrlen)) {
throw folly::AsyncSocketException( throw folly::AsyncSocketException(
folly::AsyncSocketException::INTERNAL_ERROR, folly::AsyncSocketException::INTERNAL_ERROR,
"failed to bind to async server socket for port 10", "failed to bind to async server socket for port 10",
errno); errno);
} }
if (listen(fd_, 10) != 0) { if (folly::netops::listen(fd_, 10) != 0) {
throw folly::AsyncSocketException( throw folly::AsyncSocketException(
folly::AsyncSocketException::INTERNAL_ERROR, folly::AsyncSocketException::INTERNAL_ERROR,
"failed to listen on test server socket", "failed to listen on test server socket",
...@@ -334,8 +338,8 @@ class TestServer { ...@@ -334,8 +338,8 @@ class TestServer {
} }
~TestServer() { ~TestServer() {
if (fd_ != -1) { if (fd_ != folly::NetworkSocket()) {
close(fd_); folly::netops::close(fd_);
} }
} }
...@@ -344,12 +348,11 @@ class TestServer { ...@@ -344,12 +348,11 @@ class TestServer {
return address_; return address_;
} }
int acceptFD(int timeout = 50) { folly::NetworkSocket acceptFD(int timeout = 50) {
namespace fsp = folly::portability::sockets; folly::netops::PollDescriptor pfd;
struct pollfd pfd;
pfd.fd = fd_; pfd.fd = fd_;
pfd.events = POLLIN; pfd.events = POLLIN;
int ret = poll(&pfd, 1, timeout); int ret = folly::netops::poll(&pfd, 1, timeout);
if (ret == 0) { if (ret == 0) {
throw folly::AsyncSocketException( throw folly::AsyncSocketException(
folly::AsyncSocketException::INTERNAL_ERROR, folly::AsyncSocketException::INTERNAL_ERROR,
...@@ -361,8 +364,8 @@ class TestServer { ...@@ -361,8 +364,8 @@ class TestServer {
errno); errno);
} }
int acceptedFd = fsp::accept(fd_, nullptr, nullptr); auto acceptedFd = folly::netops::accept(fd_, nullptr, nullptr);
if (acceptedFd < 0) { if (acceptedFd == folly::NetworkSocket()) {
throw folly::AsyncSocketException( throw folly::AsyncSocketException(
folly::AsyncSocketException::INTERNAL_ERROR, folly::AsyncSocketException::INTERNAL_ERROR,
"test server accept() failed", "test server accept() failed",
...@@ -373,14 +376,14 @@ class TestServer { ...@@ -373,14 +376,14 @@ class TestServer {
} }
std::shared_ptr<BlockingSocket> accept(int timeout = 50) { std::shared_ptr<BlockingSocket> accept(int timeout = 50) {
int fd = acceptFD(timeout); auto fd = acceptFD(timeout);
return std::make_shared<BlockingSocket>(fd); return std::make_shared<BlockingSocket>(fd);
} }
std::shared_ptr<folly::AsyncSocket> acceptAsync( std::shared_ptr<folly::AsyncSocket> acceptAsync(
folly::EventBase* evb, folly::EventBase* evb,
int timeout = 50) { int timeout = 50) {
int fd = acceptFD(timeout); auto fd = acceptFD(timeout);
return folly::AsyncSocket::newSocket(evb, fd); return folly::AsyncSocket::newSocket(evb, fd);
} }
...@@ -401,6 +404,6 @@ class TestServer { ...@@ -401,6 +404,6 @@ class TestServer {
} }
private: private:
int fd_; folly::NetworkSocket fd_;
folly::SocketAddress address_; folly::SocketAddress address_;
}; };
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment