1 // Copyright 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "nacl_io/socket/fifo_packet.h" 6 7 #include <stdlib.h> 8 #include <string.h> 9 10 #include <algorithm> 11 12 #include "nacl_io/socket/packet.h" 13 14 namespace nacl_io { 15 16 FIFOPacket::FIFOPacket(size_t size) : max_bytes_(size), cur_bytes_(0) { 17 } 18 19 FIFOPacket::~FIFOPacket() { 20 while (!IsEmpty()) 21 delete ReadPacket(); 22 } 23 24 bool FIFOPacket::IsEmpty() { 25 return packets_.empty(); 26 } 27 28 bool FIFOPacket::Resize(size_t len) { 29 max_bytes_ = len; 30 return true; 31 } 32 33 size_t FIFOPacket::ReadAvailable() { 34 return cur_bytes_; 35 } 36 37 size_t FIFOPacket::WriteAvailable() { 38 if (cur_bytes_ > max_bytes_) 39 return 0; 40 41 return max_bytes_ - cur_bytes_; 42 } 43 44 bool FIFOPacket::IsFull() { 45 return cur_bytes_ >= max_bytes_; 46 } 47 48 Packet* FIFOPacket::PeekPacket() { 49 if (packets_.empty()) 50 return NULL; 51 52 return packets_.back(); 53 } 54 55 Packet* FIFOPacket::ReadPacket() { 56 if (packets_.empty()) 57 return NULL; 58 59 Packet* out = packets_.back(); 60 packets_.pop_back(); 61 62 cur_bytes_ -= out->len(); 63 return out; 64 } 65 66 void FIFOPacket::WritePacket(Packet* packet) { 67 cur_bytes_ += packet->len(); 68 packets_.push_front(packet); 69 } 70 71 } // namespace nacl_io 72