| OLD | NEW |
| (Empty) |
| 1 /***************************************************************************** | |
| 2 Copyright (c) 2001 - 2009, The Board of Trustees of the University of Illinois. | |
| 3 All rights reserved. | |
| 4 | |
| 5 Redistribution and use in source and binary forms, with or without | |
| 6 modification, are permitted provided that the following conditions are | |
| 7 met: | |
| 8 | |
| 9 * Redistributions of source code must retain the above | |
| 10 copyright notice, this list of conditions and the | |
| 11 following disclaimer. | |
| 12 | |
| 13 * Redistributions in binary form must reproduce the | |
| 14 above copyright notice, this list of conditions | |
| 15 and the following disclaimer in the documentation | |
| 16 and/or other materials provided with the distribution. | |
| 17 | |
| 18 * Neither the name of the University of Illinois | |
| 19 nor the names of its contributors may be used to | |
| 20 endorse or promote products derived from this | |
| 21 software without specific prior written permission. | |
| 22 | |
| 23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS | |
| 24 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | |
| 25 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
| 26 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
| 27 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 28 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 29 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 30 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
| 31 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
| 32 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
| 33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 34 *****************************************************************************/ | |
| 35 | |
| 36 /***************************************************************************** | |
| 37 written by | |
| 38 Yunhong Gu, last updated 05/05/2009 | |
| 39 *****************************************************************************/ | |
| 40 | |
| 41 #ifndef __UDT_BUFFER_H__ | |
| 42 #define __UDT_BUFFER_H__ | |
| 43 | |
| 44 | |
| 45 #include "udt.h" | |
| 46 #include "list.h" | |
| 47 #include "queue.h" | |
| 48 #include <fstream> | |
| 49 | |
| 50 class CSndBuffer | |
| 51 { | |
| 52 public: | |
| 53 CSndBuffer(const int& size = 32, const int& mss = 1500); | |
| 54 ~CSndBuffer(); | |
| 55 | |
| 56 // Functionality: | |
| 57 // Insert a user buffer into the sending list. | |
| 58 // Parameters: | |
| 59 // 0) [in] data: pointer to the user data block. | |
| 60 // 1) [in] len: size of the block. | |
| 61 // 2) [in] ttl: time to live in milliseconds | |
| 62 // 3) [in] order: if the block should be delivered in order, for DGRAM
only | |
| 63 // Returned value: | |
| 64 // None. | |
| 65 | |
| 66 void addBuffer(const char* data, const int& len, const int& ttl = -1, const b
ool& order = false); | |
| 67 | |
| 68 // Functionality: | |
| 69 // Read a block of data from file and insert it into the sending list. | |
| 70 // Parameters: | |
| 71 // 0) [in] ifs: input file stream. | |
| 72 // 1) [in] len: size of the block. | |
| 73 // Returned value: | |
| 74 // actual size of data added from the file. | |
| 75 | |
| 76 int addBufferFromFile(std::fstream& ifs, const int& len); | |
| 77 | |
| 78 // Functionality: | |
| 79 // Find data position to pack a DATA packet from the furthest reading p
oint. | |
| 80 // Parameters: | |
| 81 // 0) [out] data: the pointer to the data position. | |
| 82 // 1) [out] msgno: message number of the packet. | |
| 83 // Returned value: | |
| 84 // Actual length of data read. | |
| 85 | |
| 86 int readData(char** data, int32_t& msgno); | |
| 87 | |
| 88 // Functionality: | |
| 89 // Find data position to pack a DATA packet for a retransmission. | |
| 90 // Parameters: | |
| 91 // 0) [out] data: the pointer to the data position. | |
| 92 // 1) [in] offset: offset from the last ACK point. | |
| 93 // 2) [out] msgno: message number of the packet. | |
| 94 // 3) [out] msglen: length of the message | |
| 95 // Returned value: | |
| 96 // Actual length of data read. | |
| 97 | |
| 98 int readData(char** data, const int offset, int32_t& msgno, int& msglen); | |
| 99 | |
| 100 // Functionality: | |
| 101 // Update the ACK point and may release/unmap/return the user data acco
rding to the flag. | |
| 102 // Parameters: | |
| 103 // 0) [in] offset: number of packets acknowledged. | |
| 104 // Returned value: | |
| 105 // None. | |
| 106 | |
| 107 void ackData(const int& offset); | |
| 108 | |
| 109 // Functionality: | |
| 110 // Read size of data still in the sending list. | |
| 111 // Parameters: | |
| 112 // None. | |
| 113 // Returned value: | |
| 114 // Current size of the data in the sending list. | |
| 115 | |
| 116 int getCurrBufSize() const; | |
| 117 | |
| 118 private: | |
| 119 void increase(); | |
| 120 | |
| 121 private: | |
| 122 pthread_mutex_t m_BufLock; // used to synchronize buffer operation | |
| 123 | |
| 124 struct Block | |
| 125 { | |
| 126 char* m_pcData; // pointer to the data block | |
| 127 int m_iLength; // length of the block | |
| 128 | |
| 129 int32_t m_iMsgNo; // message number | |
| 130 uint64_t m_OriginTime; // original request time | |
| 131 int m_iTTL; // time to live (milliseconds) | |
| 132 | |
| 133 Block* m_pNext; // next block | |
| 134 } *m_pBlock, *m_pFirstBlock, *m_pCurrBlock, *m_pLastBlock; | |
| 135 | |
| 136 // m_pBlock: The head pointer | |
| 137 // m_pFirstBlock: The first block | |
| 138 // m_pCurrBlock: The current block | |
| 139 // m_pLastBlock: The last block (if first == last, buffer is empty) | |
| 140 | |
| 141 struct Buffer | |
| 142 { | |
| 143 char* m_pcData; // buffer | |
| 144 int m_iSize; // size | |
| 145 Buffer* m_pNext; // next buffer | |
| 146 } *m_pBuffer; // physical buffer | |
| 147 | |
| 148 int32_t m_iNextMsgNo; // next message number | |
| 149 | |
| 150 int m_iSize; // buffer size (number of packets) | |
| 151 int m_iMSS; // maximum seqment/packet size | |
| 152 | |
| 153 int m_iCount; // number of used blocks | |
| 154 | |
| 155 private: | |
| 156 CSndBuffer(const CSndBuffer&); | |
| 157 CSndBuffer& operator=(const CSndBuffer&); | |
| 158 }; | |
| 159 | |
| 160 //////////////////////////////////////////////////////////////////////////////// | |
| 161 | |
| 162 class CRcvBuffer | |
| 163 { | |
| 164 public: | |
| 165 CRcvBuffer(CUnitQueue* queue, const int& bufsize = 65536); | |
| 166 ~CRcvBuffer(); | |
| 167 | |
| 168 // Functionality: | |
| 169 // Write data into the buffer. | |
| 170 // Parameters: | |
| 171 // 0) [in] unit: pointer to a data unit containing new packet | |
| 172 // 1) [in] offset: offset from last ACK point. | |
| 173 // Returned value: | |
| 174 // 0 is success, -1 if data is repeated. | |
| 175 | |
| 176 int addData(CUnit* unit, int offset); | |
| 177 | |
| 178 // Functionality: | |
| 179 // Read data into a user buffer. | |
| 180 // Parameters: | |
| 181 // 0) [in] data: pointer to user buffer. | |
| 182 // 1) [in] len: length of user buffer. | |
| 183 // Returned value: | |
| 184 // size of data read. | |
| 185 | |
| 186 int readBuffer(char* data, const int& len); | |
| 187 | |
| 188 // Functionality: | |
| 189 // Read data directly into file. | |
| 190 // Parameters: | |
| 191 // 0) [in] file: C++ file stream. | |
| 192 // 1) [in] len: expected length of data to write into the file. | |
| 193 // Returned value: | |
| 194 // size of data read. | |
| 195 | |
| 196 int readBufferToFile(std::fstream& ofs, const int& len); | |
| 197 | |
| 198 // Functionality: | |
| 199 // Update the ACK point of the buffer. | |
| 200 // Parameters: | |
| 201 // 0) [in] len: size of data to be acknowledged. | |
| 202 // Returned value: | |
| 203 // 1 if a user buffer is fulfilled, otherwise 0. | |
| 204 | |
| 205 void ackData(const int& len); | |
| 206 | |
| 207 // Functionality: | |
| 208 // Query how many buffer space left for data receiving. | |
| 209 // Parameters: | |
| 210 // None. | |
| 211 // Returned value: | |
| 212 // size of available buffer space (including user buffer) for data rece
iving. | |
| 213 | |
| 214 int getAvailBufSize() const; | |
| 215 | |
| 216 // Functionality: | |
| 217 // Query how many data has been continuously received (for reading). | |
| 218 // Parameters: | |
| 219 // None. | |
| 220 // Returned value: | |
| 221 // size of valid (continous) data for reading. | |
| 222 | |
| 223 int getRcvDataSize() const; | |
| 224 | |
| 225 // Functionality: | |
| 226 // mark the message to be dropped from the message list. | |
| 227 // Parameters: | |
| 228 // 0) [in] msgno: message nuumer. | |
| 229 // Returned value: | |
| 230 // None. | |
| 231 | |
| 232 void dropMsg(const int32_t& msgno); | |
| 233 | |
| 234 // Functionality: | |
| 235 // read a message. | |
| 236 // Parameters: | |
| 237 // 0) [out] data: buffer to write the message into. | |
| 238 // 1) [in] len: size of the buffer. | |
| 239 // Returned value: | |
| 240 // actuall size of data read. | |
| 241 | |
| 242 int readMsg(char* data, const int& len); | |
| 243 | |
| 244 // Functionality: | |
| 245 // Query how many messages are available now. | |
| 246 // Parameters: | |
| 247 // None. | |
| 248 // Returned value: | |
| 249 // number of messages available for recvmsg. | |
| 250 | |
| 251 int getRcvMsgNum(); | |
| 252 | |
| 253 private: | |
| 254 bool scanMsg(int& start, int& end, bool& passack); | |
| 255 | |
| 256 private: | |
| 257 CUnit** m_pUnit; // pointer to the protocol buffer | |
| 258 int m_iSize; // size of the protocol buffer | |
| 259 CUnitQueue* m_pUnitQueue; // the shared unit queue | |
| 260 | |
| 261 int m_iStartPos; // the head position for I/O (inclusive) | |
| 262 int m_iLastAckPos; // the last ACKed position (exclusive) | |
| 263 // EMPTY: m_iStartPos = m_iLastAckPos
FULL: m_iStartPos = m_iLastAckPos + 1 | |
| 264 int m_iMaxPos; // the furthest data position | |
| 265 | |
| 266 int m_iNotch; // the starting read point of the first
unit | |
| 267 | |
| 268 private: | |
| 269 CRcvBuffer(); | |
| 270 CRcvBuffer(const CRcvBuffer&); | |
| 271 CRcvBuffer& operator=(const CRcvBuffer&); | |
| 272 }; | |
| 273 | |
| 274 | |
| 275 #endif | |
| OLD | NEW |