foundationdb/flow/Net2Packet.cpp

220 lines
5.8 KiB
C++
Raw Normal View History

2017-05-26 04:48:44 +08:00
/*
* Net2Packet.cpp
*
* This source file is part of the FoundationDB open source project
*
* Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
*
2017-05-26 04:48:44 +08:00
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
2017-05-26 04:48:44 +08:00
* http://www.apache.org/licenses/LICENSE-2.0
*
2017-05-26 04:48:44 +08:00
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "flow/Net2Packet.h"
2017-05-26 04:48:44 +08:00
void PacketWriter::init(PacketBuffer* buf, ReliablePacket* reliable) {
this->buffer = buf;
this->reliable = reliable;
this->length = 0;
length -= buffer->bytes_written;
if (reliable) {
reliable->buffer = buffer;
buffer->addref();
2017-05-26 04:48:44 +08:00
reliable->begin = buffer->bytes_written;
}
}
PacketBuffer* PacketWriter::finish() {
length += buffer->bytes_written;
if (reliable) {
2020-08-28 06:31:24 +08:00
reliable->cont = nullptr;
2017-05-26 04:48:44 +08:00
reliable->end = buffer->bytes_written;
}
return buffer;
}
void PacketWriter::serializeBytesAcrossBoundary(const void* data, int bytes) {
while (true) {
int b = std::min(bytes, buffer->bytes_unwritten());
memcpy(buffer->data() + buffer->bytes_written, data, b);
2017-05-26 04:48:44 +08:00
buffer->bytes_written += b;
bytes -= b;
if (!bytes)
break;
2017-05-26 04:48:44 +08:00
data = (uint8_t*)data + b;
nextBuffer(bytes);
2017-05-26 04:48:44 +08:00
}
}
2019-07-02 09:14:32 +08:00
void PacketWriter::nextBuffer(size_t size) {
auto last_buffer_bytes_written = buffer->bytes_written;
length += last_buffer_bytes_written;
2019-07-02 09:14:32 +08:00
buffer->next = PacketBuffer::create(size);
2017-05-26 04:48:44 +08:00
buffer = buffer->nextPacketBuffer();
if (reliable) {
reliable->end = last_buffer_bytes_written;
2017-05-26 04:48:44 +08:00
reliable->cont = new ReliablePacket;
reliable = reliable->cont;
reliable->buffer = buffer;
buffer->addref();
2017-05-26 04:48:44 +08:00
reliable->begin = 0;
}
}
// Adds exactly bytes of unwritten length to the buffer, possibly across packet buffer boundaries,
// and initializes buf to point to the packet buffer(s) that contain the unwritten space
void PacketWriter::writeAhead(int bytes, struct SplitBuffer* buf) {
if (bytes <= buffer->bytes_unwritten()) {
buf->begin = buffer->data() + buffer->bytes_written;
2017-05-26 04:48:44 +08:00
buf->first_length = bytes;
buffer->bytes_written += bytes;
buf->next = 0;
} else {
buf->begin = buffer->data() + buffer->bytes_written;
buf->first_length = buffer->bytes_unwritten();
buffer->bytes_written = buffer->size();
size_t remaining = bytes - buf->first_length;
nextBuffer(remaining);
buf->next = buffer->data();
buffer->bytes_written = remaining;
2017-05-26 04:48:44 +08:00
}
}
void SplitBuffer::write(const void* data, int len) {
2017-05-26 04:48:44 +08:00
write(data, len, 0);
}
void SplitBuffer::write(const void* data, int len, int offset) {
2017-05-26 04:48:44 +08:00
if (len + offset <= first_length)
memcpy(begin + offset, data, len);
2017-05-26 04:48:44 +08:00
else {
if (offset >= first_length) {
memcpy(next + offset - first_length, data, len);
2017-05-26 04:48:44 +08:00
} else {
memcpy(begin + offset, data, first_length - offset);
memcpy(next, (uint8_t*)data + first_length - offset, len - first_length + offset);
2017-05-26 04:48:44 +08:00
}
}
}
void SplitBuffer::writeAndShrink(const void* data, int len) {
if (len <= first_length) {
memcpy(begin, data, len);
2017-05-26 04:48:44 +08:00
begin += len;
first_length -= len;
} else {
memcpy(begin, data, first_length);
2017-05-26 04:48:44 +08:00
int s = len - first_length;
memcpy(next, (uint8_t*)data + first_length, s);
2017-05-26 04:48:44 +08:00
next += s;
first_length = 0;
}
}
void ReliablePacket::insertBefore(ReliablePacket* p) {
2017-05-26 04:48:44 +08:00
next = p;
prev = p->prev;
prev->next = next->prev = this;
}
void ReliablePacket::remove() {
next->prev = prev;
prev->next = next;
for (ReliablePacket* c = this; c;) {
2017-05-26 04:48:44 +08:00
ReliablePacket* n = c->cont;
c->buffer->delref();
delete c;
c = n;
}
}
void UnsentPacketQueue::sent(int bytes) {
while (bytes) {
ASSERT(unsent_first);
2017-05-26 04:48:44 +08:00
PacketBuffer* b = unsent_first;
if (b->bytes_sent + bytes <= b->bytes_written &&
(b->bytes_sent + bytes != b->bytes_written || (!b->next && b->bytes_unwritten()))) {
2017-05-26 04:48:44 +08:00
b->bytes_sent += bytes;
ASSERT(b->bytes_sent <= b->size());
2017-05-26 04:48:44 +08:00
break;
}
// We've sent an entire buffer
bytes -= b->bytes_written - b->bytes_sent;
b->bytes_sent = b->bytes_written;
ASSERT(b->bytes_written <= b->size());
double queue_time = now() - b->enqueue_time;
sendQueueLatencyHistogram->sampleSeconds(queue_time);
2017-05-26 04:48:44 +08:00
unsent_first = b->nextPacketBuffer();
if (!unsent_first)
unsent_last = nullptr;
2017-05-26 04:48:44 +08:00
b->delref();
}
}
void UnsentPacketQueue::discardAll() {
while (unsent_first) {
auto n = unsent_first->nextPacketBuffer();
unsent_first->delref();
unsent_first = n;
}
unsent_last = 0;
}
PacketBuffer* ReliablePacketList::compact(PacketBuffer* into, PacketBuffer* end) {
ReliablePacket* r = reliable.next;
while (r != &reliable) {
for (ReliablePacket* c = r; c; c = c->cont) {
if (c->buffer == end /*&& c->begin>=c->buffer->bytes_written*/) // quit when we hit the unsent range
2017-05-26 04:48:44 +08:00
return into;
if (into->bytes_written == into->size()) {
into->next = PacketBuffer::create(into->size());
2017-05-26 04:48:44 +08:00
into = into->nextPacketBuffer();
}
uint8_t* data = &c->buffer->data()[c->begin];
int len = c->end - c->begin;
2017-05-26 04:48:44 +08:00
if (len > into->bytes_unwritten()) {
// We have to split this ReliablePacket
len = into->bytes_unwritten();
ReliablePacket* e = new ReliablePacket;
e->cont = c->cont;
e->buffer = c->buffer;
e->buffer->addref();
2017-05-26 04:48:44 +08:00
e->begin = c->begin + len;
e->end = c->end;
c->cont = e;
}
memcpy(into->data() + into->bytes_written, data, len);
c->buffer->delref();
c->buffer = into;
c->buffer->addref();
2017-05-26 04:48:44 +08:00
c->begin = into->bytes_written;
into->bytes_written += len;
c->end = into->bytes_written;
}
r = r->next;
}
return into;
}
void ReliablePacketList::discardAll() {
while (reliable.next != &reliable)
reliable.next->remove();
}