#include <boost/scoped_array.hpp>
#include <poll.h>
-// This is a test-only header
-/* using override */
-using namespace folly;
-
enum StateEnum {
STATE_WAITING,
STATE_SUCCEEDED,
typedef std::function<void()> VoidCallback;
-class ConnCallback : public AsyncSocket::ConnectCallback {
+class ConnCallback : public folly::AsyncSocket::ConnectCallback {
public:
ConnCallback()
- : state(STATE_WAITING)
- , exception(AsyncSocketException::UNKNOWN, "none") {}
+ : state(STATE_WAITING),
+ exception(folly::AsyncSocketException::UNKNOWN, "none") {}
void connectSuccess() noexcept override {
state = STATE_SUCCEEDED;
}
}
- void connectErr(const AsyncSocketException& ex) noexcept override {
+ void connectErr(const folly::AsyncSocketException& ex) noexcept override {
state = STATE_FAILED;
exception = ex;
if (errorCallback) {
}
StateEnum state;
- AsyncSocketException exception;
+ folly::AsyncSocketException exception;
VoidCallback successCallback;
VoidCallback errorCallback;
};
-class WriteCallback : public AsyncTransportWrapper::WriteCallback {
+class WriteCallback : public folly::AsyncTransportWrapper::WriteCallback {
public:
WriteCallback()
- : state(STATE_WAITING)
- , bytesWritten(0)
- , exception(AsyncSocketException::UNKNOWN, "none") {}
+ : state(STATE_WAITING),
+ bytesWritten(0),
+ exception(folly::AsyncSocketException::UNKNOWN, "none") {}
void writeSuccess() noexcept override {
state = STATE_SUCCEEDED;
}
void writeErr(size_t bytesWritten,
- const AsyncSocketException& ex) noexcept override {
+ const folly::AsyncSocketException& ex) noexcept override {
state = STATE_FAILED;
this->bytesWritten = bytesWritten;
exception = ex;
StateEnum state;
size_t bytesWritten;
- AsyncSocketException exception;
+ folly::AsyncSocketException exception;
VoidCallback successCallback;
VoidCallback errorCallback;
};
-class ReadCallback : public AsyncTransportWrapper::ReadCallback {
+class ReadCallback : public folly::AsyncTransportWrapper::ReadCallback {
public:
explicit ReadCallback(size_t _maxBufferSz = 4096)
- : state(STATE_WAITING)
- , exception(AsyncSocketException::UNKNOWN, "none")
- , buffers()
- , maxBufferSz(_maxBufferSz) {}
+ : state(STATE_WAITING),
+ exception(folly::AsyncSocketException::UNKNOWN, "none"),
+ buffers(),
+ maxBufferSz(_maxBufferSz) {}
~ReadCallback() {
for (std::vector<Buffer>::iterator it = buffers.begin();
state = STATE_SUCCEEDED;
}
- void readErr(const AsyncSocketException& ex) noexcept override {
+ void readErr(const folly::AsyncSocketException& ex) noexcept override {
state = STATE_FAILED;
exception = ex;
}
};
StateEnum state;
- AsyncSocketException exception;
+ folly::AsyncSocketException exception;
std::vector<Buffer> buffers;
Buffer currentBuffer;
VoidCallback dataAvailableCallback;
const size_t maxBufferSz;
};
-class BufferCallback : public AsyncTransport::BufferCallback {
+class BufferCallback : public folly::AsyncTransport::BufferCallback {
public:
BufferCallback() : buffered_(false), bufferCleared_(false) {}
: fd_(-1) {
fd_ = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (fd_ < 0) {
- throw AsyncSocketException(AsyncSocketException::INTERNAL_ERROR,
- "failed to create test server socket", errno);
+ throw folly::AsyncSocketException(
+ folly::AsyncSocketException::INTERNAL_ERROR,
+ "failed to create test server socket",
+ errno);
}
if (fcntl(fd_, F_SETFL, O_NONBLOCK) != 0) {
- throw AsyncSocketException(AsyncSocketException::INTERNAL_ERROR,
- "failed to put test server socket in "
- "non-blocking mode", errno);
+ throw folly::AsyncSocketException(
+ folly::AsyncSocketException::INTERNAL_ERROR,
+ "failed to put test server socket in "
+ "non-blocking mode",
+ errno);
}
if (listen(fd_, 10) != 0) {
- throw AsyncSocketException(AsyncSocketException::INTERNAL_ERROR,
- "failed to listen on test server socket",
- errno);
+ throw folly::AsyncSocketException(
+ folly::AsyncSocketException::INTERNAL_ERROR,
+ "failed to listen on test server socket",
+ errno);
}
address_.setFromLocalAddress(fd_);
pfd.events = POLLIN;
int ret = poll(&pfd, 1, timeout);
if (ret == 0) {
- throw AsyncSocketException(AsyncSocketException::INTERNAL_ERROR,
- "test server accept() timed out");
+ throw folly::AsyncSocketException(
+ folly::AsyncSocketException::INTERNAL_ERROR,
+ "test server accept() timed out");
} else if (ret < 0) {
- throw AsyncSocketException(AsyncSocketException::INTERNAL_ERROR,
- "test server accept() poll failed", errno);
+ throw folly::AsyncSocketException(
+ folly::AsyncSocketException::INTERNAL_ERROR,
+ "test server accept() poll failed",
+ errno);
}
int acceptedFd = ::accept(fd_, nullptr, nullptr);
if (acceptedFd < 0) {
- throw AsyncSocketException(AsyncSocketException::INTERNAL_ERROR,
- "test server accept() failed", errno);
+ throw folly::AsyncSocketException(
+ folly::AsyncSocketException::INTERNAL_ERROR,
+ "test server accept() failed",
+ errno);
}
return acceptedFd;
return std::shared_ptr<BlockingSocket>(new BlockingSocket(fd));
}
- std::shared_ptr<AsyncSocket> acceptAsync(EventBase* evb, int timeout=50) {
+ std::shared_ptr<folly::AsyncSocket> acceptAsync(folly::EventBase* evb,
+ int timeout = 50) {
int fd = acceptFD(timeout);
- return AsyncSocket::newSocket(evb, fd);
+ return folly::AsyncSocket::newSocket(evb, fd);
}
/**