Commit 39f73024 authored by Dan Melnic's avatar Dan Melnic Committed by Facebook GitHub Bot

Adding AsyncSSLSocket::performReadv support

Summary: Adding AsyncSSLSocket::performReadv

Reviewed By: afrind

Differential Revision: D33717297

fbshipit-source-id: 413f50fcaf1d59cec98765a095a5a5964f634e23
parent 62fb14dc
......@@ -1533,6 +1533,34 @@ AsyncSocket::ReadResult AsyncSSLSocket::performRead(
}
}
AsyncSocket::ReadResult AsyncSSLSocket::performReadv(
struct iovec* iovs, size_t num) {
ssize_t totalRead = 0;
ssize_t res = 1;
for (size_t i = 0; i < num && res > 0; i++) {
auto* buf = iovs[i].iov_base;
auto bufLen = iovs[i].iov_len;
while (bufLen > 0 && res > 0) {
// Should offset be into buf? It seems unused. Also buf and buflen
// are not really out params anymore.
size_t offset = 0;
auto readRes = performRead(&buf, &bufLen, &offset);
res = readRes.readReturn;
if (res > 0) {
CHECK_GE(bufLen, res);
buf = static_cast<uint8_t*>(buf) + res;
bufLen -= res;
totalRead += res;
} else if (ReadResultEnum(res) == READ_ERROR) {
return readRes;
} else if (ReadResultEnum(res) == READ_BLOCKING) {
return ReadResult(totalRead);
}
}
}
return ReadResult(totalRead);
}
void AsyncSSLSocket::handleWrite() noexcept {
VLOG(5) << "AsyncSSLSocket::handleWrite() this=" << this << ", fd=" << fd_
<< ", state=" << int(state_) << ", "
......
......@@ -906,6 +906,7 @@ class AsyncSSLSocket : public AsyncSocket {
WriteResult interpretSSLError(int rc, int error);
ReadResult performRead(void** buf, size_t* buflen, size_t* offset) override;
ReadResult performReadv(struct iovec* iovs, size_t num) override;
WriteResult performWrite(
const iovec* vec,
uint32_t count,
......
......@@ -163,11 +163,9 @@ std::string getFileAsBuf(const char* fileName) {
return buffer;
}
/**
* Test connecting to, writing to, reading from, and closing the
* connection to the SSL server.
*/
TEST(AsyncSSLSocketTest, ConnectWriteReadClose) {
namespace {
void connectWriteReadClose(
folly::AsyncReader::ReadCallback::ReadMode readMode) {
// Start listening on a local port
WriteCallbackBase writeCallback;
ReadCallback readCallback(&writeCallback);
......@@ -184,6 +182,7 @@ TEST(AsyncSSLSocketTest, ConnectWriteReadClose) {
// connect
auto socket =
std::make_shared<BlockingSocket>(server.getAddress(), sslContext);
socket->setReadMode(readMode);
socket->open(std::chrono::milliseconds(10000));
// write()
......@@ -203,6 +202,20 @@ TEST(AsyncSSLSocketTest, ConnectWriteReadClose) {
cerr << "ConnectWriteReadClose test completed" << endl;
EXPECT_EQ(socket->getSSLSocket()->getTotalConnectTimeout().count(), 10000);
}
} // namespace
/**
* Test connecting to, writing to, reading from, and closing the
* connection to the SSL server.
*/
TEST(AsyncSSLSocketTest, ConnectWriteReadClose) {
connectWriteReadClose(folly::AsyncReader::ReadCallback::ReadMode::ReadBuffer);
}
TEST(AsyncSSLSocketTest, ConnectWriteReadvClose) {
connectWriteReadClose(folly::AsyncReader::ReadCallback::ReadMode::ReadVec);
;
}
TEST(AsyncSSLSocketTest, ConnectWriteReadCloseReadable) {
// Same as above, but test AsyncSSLSocket::readable along the way
......
......@@ -125,10 +125,21 @@ class BlockingSocket : public folly::AsyncSocket::ConnectCallback,
void readDataAvailable(size_t len) noexcept override {
readBuf_ += len;
readLen_ -= len;
if (readLen_ == 0) {
sock_->setReadCB(nullptr);
}
}
void getReadBuffers(folly::IOBufIovecBuilder::IoVecVec& iovs) override {
// we reuse the same readBuf_
iovs.clear();
for (size_t i = 0; i < readLen_; i++) {
struct iovec iov;
iov.iov_base = &readBuf_[i];
iov.iov_len = 1;
iovs.push_back(iov);
}
}
void readEOF() noexcept override {}
void readErr(const folly::AsyncSocketException& ex) noexcept override {
err_ = ex;
......
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