buffer.h

00001 /*****************************************************************************
00002 Copyright (c) 2001 - 2009, The Board of Trustees of the University of Illinois.
00003 All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without
00006 modification, are permitted provided that the following conditions are
00007 met:
00008 
00009 * Redistributions of source code must retain the above
00010   copyright notice, this list of conditions and the
00011   following disclaimer.
00012 
00013 * Redistributions in binary form must reproduce the
00014   above copyright notice, this list of conditions
00015   and the following disclaimer in the documentation
00016   and/or other materials provided with the distribution.
00017 
00018 * Neither the name of the University of Illinois
00019   nor the names of its contributors may be used to
00020   endorse or promote products derived from this
00021   software without specific prior written permission.
00022 
00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00024 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
00025 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00026 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
00027 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00028 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00029 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00030 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00031 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00032 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00033 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034 *****************************************************************************/
00035 
00036 /*****************************************************************************
00037 written by
00038    Yunhong Gu, last updated 05/05/2009
00039 *****************************************************************************/
00040 
00041 #ifndef __UDT_BUFFER_H__
00042 #define __UDT_BUFFER_H__
00043 
00044 
00045 #include "udt.h"
00046 #include "list.h"
00047 #include "queue.h"
00048 #include <fstream>
00049 
00050 class CSndBuffer
00051 {
00052 public:
00053    CSndBuffer(int size = 32, int mss = 1500);
00054    ~CSndBuffer();
00055 
00056       // Functionality:
00057       //    Insert a user buffer into the sending list.
00058       // Parameters:
00059       //    0) [in] data: pointer to the user data block.
00060       //    1) [in] len: size of the block.
00061       //    2) [in] ttl: time to live in milliseconds
00062       //    3) [in] order: if the block should be delivered in order, for DGRAM only
00063       // Returned value:
00064       //    None.
00065 
00066    void addBuffer(const char* data, int len, int ttl = -1, bool order = false);
00067 
00068       // Functionality:
00069       //    Read a block of data from file and insert it into the sending list.
00070       // Parameters:
00071       //    0) [in] ifs: input file stream.
00072       //    1) [in] len: size of the block.
00073       // Returned value:
00074       //    actual size of data added from the file.
00075 
00076    int addBufferFromFile(std::fstream& ifs, int len);
00077 
00078       // Functionality:
00079       //    Find data position to pack a DATA packet from the furthest reading point.
00080       // Parameters:
00081       //    0) [out] data: the pointer to the data position.
00082       //    1) [out] msgno: message number of the packet.
00083       // Returned value:
00084       //    Actual length of data read.
00085 
00086    int readData(char** data, int32_t& msgno);
00087 
00088       // Functionality:
00089       //    Find data position to pack a DATA packet for a retransmission.
00090       // Parameters:
00091       //    0) [out] data: the pointer to the data position.
00092       //    1) [in] offset: offset from the last ACK point.
00093       //    2) [out] msgno: message number of the packet.
00094       //    3) [out] msglen: length of the message
00095       // Returned value:
00096       //    Actual length of data read.
00097 
00098    int readData(char** data, const int offset, int32_t& msgno, int& msglen);
00099 
00100       // Functionality:
00101       //    Update the ACK point and may release/unmap/return the user data according to the flag.
00102       // Parameters:
00103       //    0) [in] offset: number of packets acknowledged.
00104       // Returned value:
00105       //    None.
00106 
00107    void ackData(int offset);
00108 
00109       // Functionality:
00110       //    Read size of data still in the sending list.
00111       // Parameters:
00112       //    None.
00113       // Returned value:
00114       //    Current size of the data in the sending list.
00115 
00116    int getCurrBufSize() const;
00117 
00118 private:
00119    void increase();
00120 
00121 private:
00122    pthread_mutex_t m_BufLock;           // used to synchronize buffer operation
00123 
00124    struct Block
00125    {
00126       char* m_pcData;                   // pointer to the data block
00127       int m_iLength;                    // length of the block
00128 
00129       int32_t m_iMsgNo;                 // message number
00130       uint64_t m_OriginTime;            // original request time
00131       int m_iTTL;                       // time to live (milliseconds)
00132 
00133       Block* m_pNext;                   // next block
00134    } *m_pBlock, *m_pFirstBlock, *m_pCurrBlock, *m_pLastBlock;
00135 
00136    // m_pBlock:         The head pointer
00137    // m_pFirstBlock:    The first block
00138    // m_pCurrBlock:     The current block
00139    // m_pLastBlock:     The last block (if first == last, buffer is empty)
00140 
00141    struct Buffer
00142    {
00143       char* m_pcData;                   // buffer
00144       int m_iSize;                      // size
00145       Buffer* m_pNext;                  // next buffer
00146    } *m_pBuffer;                        // physical buffer
00147 
00148    int32_t m_iNextMsgNo;                // next message number
00149 
00150    int m_iSize;                         // buffer size (number of packets)
00151    int m_iMSS;                          // maximum seqment/packet size
00152 
00153    int m_iCount;                        // number of used blocks
00154 
00155 private:
00156    CSndBuffer(const CSndBuffer&);
00157    CSndBuffer& operator=(const CSndBuffer&);
00158 };
00159 
00161 
00162 class CRcvBuffer
00163 {
00164 public:
00165    CRcvBuffer(CUnitQueue* queue, int bufsize = 65536);
00166    ~CRcvBuffer();
00167 
00168       // Functionality:
00169       //    Write data into the buffer.
00170       // Parameters:
00171       //    0) [in] unit: pointer to a data unit containing new packet
00172       //    1) [in] offset: offset from last ACK point.
00173       // Returned value:
00174       //    0 is success, -1 if data is repeated.
00175 
00176    int addData(CUnit* unit, int offset);
00177 
00178       // Functionality:
00179       //    Read data into a user buffer.
00180       // Parameters:
00181       //    0) [in] data: pointer to user buffer.
00182       //    1) [in] len: length of user buffer.
00183       // Returned value:
00184       //    size of data read.
00185 
00186    int readBuffer(char* data, int len);
00187 
00188       // Functionality:
00189       //    Read data directly into file.
00190       // Parameters:
00191       //    0) [in] file: C++ file stream.
00192       //    1) [in] len: expected length of data to write into the file.
00193       // Returned value:
00194       //    size of data read.
00195 
00196    int readBufferToFile(std::fstream& ofs, int len);
00197 
00198       // Functionality:
00199       //    Update the ACK point of the buffer.
00200       // Parameters:
00201       //    0) [in] len: size of data to be acknowledged.
00202       // Returned value:
00203       //    1 if a user buffer is fulfilled, otherwise 0.
00204 
00205    void ackData(int len);
00206 
00207       // Functionality:
00208       //    Query how many buffer space left for data receiving.
00209       // Parameters:
00210       //    None.
00211       // Returned value:
00212       //    size of available buffer space (including user buffer) for data receiving.
00213 
00214    int getAvailBufSize() const;
00215 
00216       // Functionality:
00217       //    Query how many data has been continuously received (for reading).
00218       // Parameters:
00219       //    None.
00220       // Returned value:
00221       //    size of valid (continous) data for reading.
00222 
00223    int getRcvDataSize() const;
00224 
00225       // Functionality:
00226       //    mark the message to be dropped from the message list.
00227       // Parameters:
00228       //    0) [in] msgno: message nuumer.
00229       // Returned value:
00230       //    None.
00231 
00232    void dropMsg(int32_t msgno);
00233 
00234       // Functionality:
00235       //    read a message.
00236       // Parameters:
00237       //    0) [out] data: buffer to write the message into.
00238       //    1) [in] len: size of the buffer.
00239       // Returned value:
00240       //    actuall size of data read.
00241 
00242    int readMsg(char* data, int len);
00243 
00244       // Functionality:
00245       //    Query how many messages are available now.
00246       // Parameters:
00247       //    None.
00248       // Returned value:
00249       //    number of messages available for recvmsg.
00250 
00251    int getRcvMsgNum();
00252 
00253 private:
00254    bool scanMsg(int& start, int& end, bool& passack);
00255 
00256 private:
00257    CUnit** m_pUnit;                     // pointer to the protocol buffer
00258    int m_iSize;                         // size of the protocol buffer
00259    CUnitQueue* m_pUnitQueue;            // the shared unit queue
00260 
00261    int m_iStartPos;                     // the head position for I/O (inclusive)
00262    int m_iLastAckPos;                   // the last ACKed position (exclusive)
00263                                         // EMPTY: m_iStartPos = m_iLastAckPos   FULL: m_iStartPos = m_iLastAckPos + 1
00264    int m_iMaxPos;                       // the furthest data position
00265 
00266    int m_iNotch;                        // the starting read point of the first unit
00267 
00268 private:
00269    CRcvBuffer();
00270    CRcvBuffer(const CRcvBuffer&);
00271    CRcvBuffer& operator=(const CRcvBuffer&);
00272 };
00273 
00274 
00275 #endif

Generated on 9 Feb 2013 for barchart-udt-core-2.2.2 by  doxygen 1.6.1