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
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
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
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
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.
|
|
|
|
*/
|
|
|
|
|
2018-10-20 01:30:13 +08:00
|
|
|
#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) {
|
2021-03-11 02:06:03 +08:00
|
|
|
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) {
|
2021-03-11 02:06:03 +08:00
|
|
|
while (true) {
|
2019-07-02 00:57:50 +08:00
|
|
|
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;
|
2021-03-11 02:06:03 +08:00
|
|
|
if (!bytes)
|
|
|
|
break;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
data = (uint8_t*)data + b;
|
2020-06-26 11:44:43 +08:00
|
|
|
nextBuffer(bytes);
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-02 09:14:32 +08:00
|
|
|
void PacketWriter::nextBuffer(size_t size) {
|
2019-07-02 00:57:50 +08:00
|
|
|
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) {
|
2019-07-02 00:57:50 +08:00
|
|
|
reliable->end = last_buffer_bytes_written;
|
2017-05-26 04:48:44 +08:00
|
|
|
reliable->cont = new ReliablePacket;
|
|
|
|
reliable = reliable->cont;
|
2021-03-11 02:06:03 +08:00
|
|
|
reliable->buffer = buffer;
|
|
|
|
buffer->addref();
|
2017-05-26 04:48:44 +08:00
|
|
|
reliable->begin = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-26 11:44:43 +08:00
|
|
|
// 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
|
2021-03-11 02:06:03 +08:00
|
|
|
void PacketWriter::writeAhead(int bytes, struct SplitBuffer* buf) {
|
2019-07-02 00:57:50 +08:00
|
|
|
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 {
|
2019-07-02 00:57:50 +08:00
|
|
|
buf->begin = buffer->data() + buffer->bytes_written;
|
|
|
|
buf->first_length = buffer->bytes_unwritten();
|
|
|
|
buffer->bytes_written = buffer->size();
|
2020-06-26 11:44:43 +08:00
|
|
|
size_t remaining = bytes - buf->first_length;
|
|
|
|
nextBuffer(remaining);
|
2019-07-02 00:57:50 +08:00
|
|
|
buf->next = buffer->data();
|
2020-06-26 11:44:43 +08:00
|
|
|
buffer->bytes_written = remaining;
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
void SplitBuffer::write(const void* data, int len) {
|
2017-05-26 04:48:44 +08:00
|
|
|
write(data, len, 0);
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
void SplitBuffer::write(const void* data, int len, int offset) {
|
2017-05-26 04:48:44 +08:00
|
|
|
if (len + offset <= first_length)
|
2021-03-11 02:06:03 +08:00
|
|
|
memcpy(begin + offset, data, len);
|
2017-05-26 04:48:44 +08:00
|
|
|
else {
|
|
|
|
if (offset >= first_length) {
|
2021-03-11 02:06:03 +08:00
|
|
|
memcpy(next + offset - first_length, data, len);
|
2017-05-26 04:48:44 +08:00
|
|
|
} else {
|
2021-03-11 02:06:03 +08:00
|
|
|
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) {
|
2021-03-11 02:06:03 +08:00
|
|
|
memcpy(begin, data, len);
|
2017-05-26 04:48:44 +08:00
|
|
|
begin += len;
|
|
|
|
first_length -= len;
|
2021-03-11 02:06:03 +08:00
|
|
|
} else {
|
|
|
|
memcpy(begin, data, first_length);
|
2017-05-26 04:48:44 +08:00
|
|
|
int s = len - first_length;
|
2021-03-11 02:06:03 +08:00
|
|
|
memcpy(next, (uint8_t*)data + first_length, s);
|
2017-05-26 04:48:44 +08:00
|
|
|
next += s;
|
|
|
|
first_length = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
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;
|
2021-03-11 02:06:03 +08:00
|
|
|
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) {
|
2021-03-11 02:06:03 +08:00
|
|
|
ASSERT(unsent_first);
|
2017-05-26 04:48:44 +08:00
|
|
|
PacketBuffer* b = unsent_first;
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
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;
|
2019-07-02 00:57:50 +08:00
|
|
|
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;
|
2019-07-02 00:57:50 +08:00
|
|
|
ASSERT(b->bytes_written <= b->size());
|
2020-11-07 11:13:45 +08:00
|
|
|
double queue_time = now() - b->enqueue_time;
|
|
|
|
sendQueueLatencyHistogram->sampleSeconds(queue_time);
|
2017-05-26 04:48:44 +08:00
|
|
|
unsent_first = b->nextPacketBuffer();
|
2021-03-11 02:06:03 +08:00
|
|
|
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) {
|
2021-03-11 02:06:03 +08:00
|
|
|
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;
|
2019-07-02 00:57:50 +08:00
|
|
|
if (into->bytes_written == into->size()) {
|
2020-06-26 11:44:43 +08:00
|
|
|
into->next = PacketBuffer::create(into->size());
|
2017-05-26 04:48:44 +08:00
|
|
|
into = into->nextPacketBuffer();
|
|
|
|
}
|
|
|
|
|
2019-07-02 00:57:50 +08:00
|
|
|
uint8_t* data = &c->buffer->data()[c->begin];
|
2021-03-11 02:06:03 +08:00
|
|
|
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;
|
2021-03-11 02:06:03 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-07-02 00:57:50 +08:00
|
|
|
memcpy(into->data() + into->bytes_written, data, len);
|
2021-03-11 02:06:03 +08:00
|
|
|
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();
|
|
|
|
}
|