Skip to content

Commit

Permalink
Bug 1336855 - Enqueue packets directly, r=franziskus
Browse files Browse the repository at this point in the history
--HG--
extra : amend_source : 755f136042f0de9f1922d7c726b45a6a21ac6702
extra : histedit_source : 3fc80d28ab90014621ec9e1fbc34d539f2b87de6
  • Loading branch information
martinthomson committed Feb 6, 2017
1 parent 874d863 commit 360eda5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 50 deletions.
58 changes: 11 additions & 47 deletions gtests/ssl_gtest/test_io.cc
Expand Up @@ -15,8 +15,6 @@
#include "prlog.h"
#include "prthread.h"

#include "databuffer.h"

extern bool g_ssl_gtest_verbose;

namespace nss_test {
Expand All @@ -34,22 +32,6 @@ static PRDescIdentity test_fd_identity = PR_INVALID_IO_LAYER;
if (g_ssl_gtest_verbose) LOG(a); \
} while (false)

class Packet : public DataBuffer {
public:
Packet(const DataBuffer &buf) : DataBuffer(buf), offset_(0) {}

void Advance(size_t delta) {
PR_ASSERT(offset_ + delta <= len());
offset_ = std::min(len(), offset_ + delta);
}

size_t offset() const { return offset_; }
size_t remaining() const { return len() - offset_; }

private:
size_t offset_;
};

// Implementation of NSPR methods
static PRStatus DummyClose(PRFileDesc *f) {
f->secret = nullptr;
Expand Down Expand Up @@ -130,11 +112,7 @@ static PRStatus DummyListen(PRFileDesc *f, int32_t depth) {
return PR_FAILURE;
}

static PRStatus DummyShutdown(PRFileDesc *f, int32_t how) {
DummyPrSocket *io = reinterpret_cast<DummyPrSocket *>(f->secret);
io->Reset();
return PR_SUCCESS;
}
static PRStatus DummyShutdown(PRFileDesc *f, int32_t how) { return PR_SUCCESS; }

// This function does not support peek.
static int32_t DummyRecv(PRFileDesc *f, void *buf, int32_t buflen,
Expand Down Expand Up @@ -256,21 +234,10 @@ static int32_t DummyReserved(PRFileDesc *f) {
return -1;
}

DummyPrSocket::~DummyPrSocket() { Reset(); }

void DummyPrSocket::SetPacketFilter(std::shared_ptr<PacketFilter> filter) {
filter_ = filter;
}

void DummyPrSocket::Reset() {
peer_.reset();
while (!input_.empty()) {
Packet *front = input_.front();
input_.pop();
delete front;
}
}

static const struct PRIOMethods DummyMethods = {
PR_DESC_LAYERED, DummyClose,
DummyRead, DummyWrite,
Expand Down Expand Up @@ -302,7 +269,7 @@ ScopedPRFileDesc DummyPrSocket::CreateFD() {
}

void DummyPrSocket::PacketReceived(const DataBuffer &packet) {
input_.push(new Packet(packet));
input_.push(Packet(packet));
}

int32_t DummyPrSocket::Read(void *data, int32_t len) {
Expand All @@ -319,16 +286,15 @@ int32_t DummyPrSocket::Read(void *data, int32_t len) {
return -1;
}

Packet *front = input_.front();
auto &front = input_.front();
size_t to_read =
std::min(static_cast<size_t>(len), front->len() - front->offset());
memcpy(data, static_cast<const void *>(front->data() + front->offset()),
std::min(static_cast<size_t>(len), front.len() - front.offset());
memcpy(data, static_cast<const void *>(front.data() + front.offset()),
to_read);
front->Advance(to_read);
front.Advance(to_read);

if (!front->remaining()) {
if (!front.remaining()) {
input_.pop();
delete front;
}

return static_cast<int32_t>(to_read);
Expand All @@ -340,19 +306,17 @@ int32_t DummyPrSocket::Recv(void *buf, int32_t buflen) {
return -1;
}

Packet *front = input_.front();
if (static_cast<size_t>(buflen) < front->len()) {
auto &front = input_.front();
if (static_cast<size_t>(buflen) < front.len()) {
PR_ASSERT(false);
PR_SetError(PR_BUFFER_OVERFLOW_ERROR, 0);
return -1;
}

size_t count = front->len();
memcpy(buf, front->data(), count);
size_t count = front.len();
memcpy(buf, front.data(), count);

input_.pop();
delete front;

return static_cast<int32_t>(count);
}

Expand Down
22 changes: 19 additions & 3 deletions gtests/ssl_gtest/test_io.h
Expand Up @@ -14,13 +14,13 @@
#include <queue>
#include <string>

#include "databuffer.h"
#include "prio.h"
#include "scoped_ptrs.h"

namespace nss_test {

class DataBuffer;
class Packet;
class DummyPrSocket; // Fwd decl.

// Allow us to inspect a packet before it is written.
Expand Down Expand Up @@ -58,7 +58,7 @@ class DummyPrSocket {
input_(),
filter_(nullptr),
writeable_(true) {}
~DummyPrSocket();
virtual ~DummyPrSocket() {}

// Create a file descriptor that will reference this object. The fd must not
// live longer than this adapter; call PR_Close() before.
Expand All @@ -80,10 +80,26 @@ class DummyPrSocket {
bool readable() const { return !input_.empty(); }

private:
class Packet : public DataBuffer {
public:
Packet(const DataBuffer& buf) : DataBuffer(buf), offset_(0) {}

void Advance(size_t delta) {
PR_ASSERT(offset_ + delta <= len());
offset_ = std::min(len(), offset_ + delta);
}

size_t offset() const { return offset_; }
size_t remaining() const { return len() - offset_; }

private:
size_t offset_;
};

const std::string name_;
Mode mode_;
std::weak_ptr<DummyPrSocket> peer_;
std::queue<Packet*> input_;
std::queue<Packet> input_;
std::shared_ptr<PacketFilter> filter_;
bool writeable_;
};
Expand Down

0 comments on commit 360eda5

Please sign in to comment.