Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(787)

Unified Diff: native_client_sdk/src/libraries/nacl_io/fifo_packet.cc

Issue 23498015: [NaCl SDK] Support non blocking TCP/UDP (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added Fifo Tests Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: native_client_sdk/src/libraries/nacl_io/fifo_packet.cc
diff --git a/native_client_sdk/src/libraries/nacl_io/fifo_packet.cc b/native_client_sdk/src/libraries/nacl_io/fifo_packet.cc
new file mode 100644
index 0000000000000000000000000000000000000000..7ff5d515c35e0d3f0c10476f85495f127cb41209
--- /dev/null
+++ b/native_client_sdk/src/libraries/nacl_io/fifo_packet.cc
@@ -0,0 +1,70 @@
+// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "nacl_io/fifo_packet.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+#include <algorithm>
+
+namespace nacl_io {
+
+FIFOPacket::FIFOPacket(size_t size)
+ : max_bytes_(size),
+ cur_bytes_(0) {}
+
+FIFOPacket::~FIFOPacket() {
+ while (!IsEmpty())
+ delete ReadPacket();
+}
+
+bool FIFOPacket::IsEmpty() {
+ return packets_.empty();
+}
+
+bool FIFOPacket::Resize(size_t len) {
+ max_bytes_ = len;
+ return true;
+}
+
+size_t FIFOPacket::ReadAvailable() {
+ return cur_bytes_;
+}
+
+size_t FIFOPacket::WriteAvailable() {
+ if (cur_bytes_ > max_bytes_)
+ return 0;
+
+ return max_bytes_ - cur_bytes_;
+}
+
+bool FIFOPacket::IsFull() {
+ return cur_bytes_ >= max_bytes_;
+}
+
+Packet* FIFOPacket::PeekPacket() {
+ if (packets_.empty())
+ return NULL;
+
+ return packets_.back();
+}
+
+Packet* FIFOPacket::ReadPacket() {
+ if (packets_.empty())
+ return NULL;
+
+ Packet* out = packets_.back();
+ packets_.pop_back();
+
+ cur_bytes_ -= out->buffer_.size();
+ return out;
+}
+
+void FIFOPacket::WritePacket(Packet* packet) {
+ cur_bytes_ += packet->buffer_.size();
+ packets_.push_front(packet);
+}
+
+} // namespace nacl_io

Powered by Google App Engine
This is Rietveld 408576698