1 /* 2 * Net2Packet.h 3 * 4 * This source file is part of the FoundationDB open source project 5 * 6 * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors 7 * 8 * Licensed under the Apache License, Version 2.0 (the "License"); 9 * you may not use this file except in compliance with the License. 10 * You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20 21 #ifndef FLOW_NET2PACKET_H 22 #define FLOW_NET2PACKET_H 23 #pragma once 24 25 #include "flow/flow.h" 26 27 // PacketWriter and PacketBuffer are in serialize.h because they are needed by the SerializeSource<> template 28 29 struct ReliablePacket : FastAllocated<ReliablePacket> { 30 PacketBuffer* buffer; 31 ReliablePacket *cont; // More bytes in the same packet 32 ReliablePacket *prev, *next; // Linked list of reliable packets on the same connection (only for the first packet in the cont chain) 33 int begin, end; 34 ReliablePacketReliablePacket35 ReliablePacket() {} 36 37 void insertBefore(ReliablePacket* p); 38 void remove(); // Deletes this and cont chain, unlinks prev and next 39 }; 40 41 class UnsentPacketQueue : NonCopyable { 42 public: UnsentPacketQueue()43 UnsentPacketQueue() : unsent_first(0), unsent_last(0) {} ~UnsentPacketQueue()44 ~UnsentPacketQueue() { discardAll(); } 45 46 // Get a PacketBuffer to write new packets into getWriteBuffer()47 PacketBuffer* getWriteBuffer() { if (!unsent_last) { ASSERT( !unsent_first ); unsent_first = unsent_last = new PacketBuffer; }; return unsent_last; } 48 // Call after potentially adding to the chain returned by getWriteBuffer() setWriteBuffer(PacketBuffer * pb)49 void setWriteBuffer(PacketBuffer* pb) { unsent_last = pb; } 50 51 // Prepend the given range of packetBuffers to the beginning of the unsent queue prependWriteBuffer(PacketBuffer * first,PacketBuffer * last)52 void prependWriteBuffer( PacketBuffer* first, PacketBuffer* last ) { last->next = unsent_first; unsent_first = first; if (!unsent_last) unsent_last = last; } 53 54 // false if there is anything unsent empty()55 bool empty() const { return !unsent_first || unsent_first->bytes_sent == unsent_first->bytes_written; } 56 57 // Get the next PacketBuffer to send data from getUnsent()58 PacketBuffer *getUnsent() const { return unsent_first; } 59 // Call after sending bytes from getUnsent() 60 void sent(int bytes); 61 62 // Discard all unsent buffers 63 void discardAll(); 64 65 private: 66 PacketBuffer *unsent_first, *unsent_last; // Both NULL, or inclusive range of PacketBuffers that haven't been sent. The last one may have space for more packets to be written. 67 }; 68 69 class ReliablePacketList : NonCopyable { 70 public: ReliablePacketList()71 ReliablePacketList() { 72 reliable.buffer = 0; 73 reliable.prev = reliable.next = &reliable; 74 } empty()75 bool empty() const { return reliable.next == &reliable; } insert(ReliablePacket * rp)76 void insert( ReliablePacket* rp ) { rp->insertBefore(&reliable); } 77 78 // Concatenate those reliable packets which have already been sent (are not in the unsent range) 79 // into the given chain of packet buffers, and return the tail of that chain 80 PacketBuffer* compact(PacketBuffer* into, PacketBuffer* stopAt); 81 82 void discardAll(); // just for testing 83 private: 84 ReliablePacket reliable; // Head/tail of a circularly linked list of reliable packets to be resent after a close 85 }; 86 87 #endif 88