1 // Copyright 2019 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 "platform/impl/tls_write_buffer.h"
6
7 #include <algorithm>
8 #include <cstring>
9
10 #include "platform/api/tls_connection.h"
11 #include "util/osp_logging.h"
12
13 namespace openscreen {
14
15 TlsWriteBuffer::TlsWriteBuffer() = default;
16 TlsWriteBuffer::~TlsWriteBuffer() = default;
17
Push(const void * data,size_t len)18 bool TlsWriteBuffer::Push(const void* data, size_t len) {
19 const size_t currently_written_bytes =
20 bytes_written_so_far_.load(std::memory_order_relaxed);
21 const size_t current_read_bytes =
22 bytes_read_so_far_.load(std::memory_order_acquire);
23
24 // Calculates the current size of the buffer.
25 const size_t bytes_currently_used =
26 currently_written_bytes - current_read_bytes;
27 OSP_DCHECK_LE(bytes_currently_used, kBufferSizeBytes);
28 if ((kBufferSizeBytes - bytes_currently_used) < len) {
29 return false;
30 }
31
32 // Calculates the number of bytes out of |len| to write in the first memcpy
33 // operation, which is either all of |len| or the number that can be written
34 // before wrapping around to the beginning of the underlying array.
35 const size_t current_write_index = currently_written_bytes % kBufferSizeBytes;
36 const size_t first_write_len =
37 std::min(len, kBufferSizeBytes - current_write_index);
38 memcpy(&buffer_[current_write_index], data, first_write_len);
39
40 // If fewer than |len| bytes were transferred in the previous memcpy, copy any
41 // remaining bytes to the array, starting at 0 (since the last write must have
42 // finished at the end of the array).
43 if (first_write_len != len) {
44 const uint8_t* new_start =
45 static_cast<const uint8_t*>(data) + first_write_len;
46 memcpy(buffer_, new_start, len - first_write_len);
47 }
48
49 // Store and return updated values.
50 const size_t new_write_size = currently_written_bytes + len;
51 bytes_written_so_far_.store(new_write_size, std::memory_order_release);
52 return true;
53 }
54
GetReadableRegion()55 absl::Span<const uint8_t> TlsWriteBuffer::GetReadableRegion() {
56 const size_t current_read_bytes =
57 bytes_read_so_far_.load(std::memory_order_relaxed);
58 const size_t currently_written_bytes =
59 bytes_written_so_far_.load(std::memory_order_acquire);
60
61 // Stop reading at either the end of the array or the current write index,
62 // whichever is sooner. While there may be more data wrapped around after the
63 // end of the array, the API for GetReadableRegion() only guarantees to return
64 // a subset of all available read data, so there is no reason to introduce
65 // this additional level of complexity.
66 const size_t avail = currently_written_bytes - current_read_bytes;
67 const size_t begin = current_read_bytes % kBufferSizeBytes;
68 const size_t end = std::min(begin + avail, kBufferSizeBytes);
69 return absl::Span<const uint8_t>(&buffer_[begin], end - begin);
70 }
71
Consume(size_t byte_count)72 void TlsWriteBuffer::Consume(size_t byte_count) {
73 const size_t current_read_bytes =
74 bytes_read_so_far_.load(std::memory_order_relaxed);
75 const size_t currently_written_bytes =
76 bytes_written_so_far_.load(std::memory_order_acquire);
77
78 OSP_DCHECK_GE(currently_written_bytes - current_read_bytes, byte_count);
79 const size_t new_read_index = current_read_bytes + byte_count;
80 bytes_read_so_far_.store(new_read_index, std::memory_order_release);
81 }
82
83 // static
84 constexpr size_t TlsWriteBuffer::kBufferSizeBytes;
85
86 } // namespace openscreen
87