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