api.h

00001 /*****************************************************************************
00002 Copyright (c) 2001 - 2010, 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 09/28/2010
00039 *****************************************************************************/
00040 
00041 #ifndef __UDT_API_H__
00042 #define __UDT_API_H__
00043 
00044 
00045 #include <map>
00046 #include <vector>
00047 #include "udt.h"
00048 #include "packet.h"
00049 #include "queue.h"
00050 #include "cache.h"
00051 #include "epoll.h"
00052 
00053 class CUDT;
00054 
00055 class CUDTSocket
00056 {
00057 public:
00058    CUDTSocket();
00059    ~CUDTSocket();
00060 
00061    UDTSTATUS m_Status;                       // current socket state
00062 
00063    uint64_t m_TimeStamp;                     // time when the socket is closed
00064 
00065    int m_iIPversion;                         // IP version
00066    sockaddr* m_pSelfAddr;                    // pointer to the local address of the socket
00067    sockaddr* m_pPeerAddr;                    // pointer to the peer address of the socket
00068 
00069    UDTSOCKET m_SocketID;                     // socket ID
00070    UDTSOCKET m_ListenSocket;                 // ID of the listener socket; 0 means this is an independent socket
00071 
00072    UDTSOCKET m_PeerID;                       // peer socket ID
00073    int32_t m_iISN;                           // initial sequence number, used to tell different connection from same IP:port
00074 
00075    CUDT* m_pUDT;                             // pointer to the UDT entity
00076 
00077    std::set<UDTSOCKET>* m_pQueuedSockets;    // set of connections waiting for accept()
00078    std::set<UDTSOCKET>* m_pAcceptSockets;    // set of accept()ed connections
00079 
00080    pthread_cond_t m_AcceptCond;              // used to block "accept" call
00081    pthread_mutex_t m_AcceptLock;             // mutex associated to m_AcceptCond
00082 
00083    unsigned int m_uiBackLog;                 // maximum number of connections in queue
00084 
00085    int m_iMuxID;                             // multiplexer ID
00086 
00087    pthread_mutex_t m_ControlLock;            // lock this socket exclusively for control APIs: bind/listen/connect
00088 
00089 private:
00090    CUDTSocket(const CUDTSocket&);
00091    CUDTSocket& operator=(const CUDTSocket&);
00092 };
00093 
00095 
00096 class CUDTUnited
00097 {
00098 friend class CUDT;
00099 friend class CRendezvousQueue;
00100 
00101 public:
00102    CUDTUnited();
00103    ~CUDTUnited();
00104 
00105 public:
00106 
00107       // Functionality:
00108       //    initialize the UDT library.
00109       // Parameters:
00110       //    None.
00111       // Returned value:
00112       //    0 if success, otherwise -1 is returned.
00113 
00114    int startup();
00115 
00116       // Functionality:
00117       //    release the UDT library.
00118       // Parameters:
00119       //    None.
00120       // Returned value:
00121       //    0 if success, otherwise -1 is returned.
00122 
00123    int cleanup();
00124 
00125       // Functionality:
00126       //    Create a new UDT socket.
00127       // Parameters:
00128       //    0) [in] af: IP version, IPv4 (AF_INET) or IPv6 (AF_INET6).
00129       //    1) [in] type: socket type, SOCK_STREAM or SOCK_DGRAM
00130       // Returned value:
00131       //    The new UDT socket ID, or INVALID_SOCK.
00132 
00133    UDTSOCKET newSocket(int af, int type);
00134 
00135       // Functionality:
00136       //    Create a new UDT connection.
00137       // Parameters:
00138       //    0) [in] listen: the listening UDT socket;
00139       //    1) [in] peer: peer address.
00140       //    2) [in/out] hs: handshake information from peer side (in), negotiated value (out);
00141       // Returned value:
00142       //    If the new connection is successfully created: 1 success, 0 already exist, -1 error.
00143 
00144    int newConnection(const UDTSOCKET listen, const sockaddr* peer, CHandShake* hs);
00145 
00146       // Functionality:
00147       //    look up the UDT entity according to its ID.
00148       // Parameters:
00149       //    0) [in] u: the UDT socket ID.
00150       // Returned value:
00151       //    Pointer to the UDT entity.
00152 
00153    CUDT* lookup(const UDTSOCKET u);
00154 
00155       // Functionality:
00156       //    Check the status of the UDT socket.
00157       // Parameters:
00158       //    0) [in] u: the UDT socket ID.
00159       // Returned value:
00160       //    UDT socket status, or NONEXIST if not found.
00161 
00162    UDTSTATUS getStatus(const UDTSOCKET u);
00163 
00164       // socket APIs
00165 
00166    int bind(const UDTSOCKET u, const sockaddr* name, int namelen);
00167    int bind(const UDTSOCKET u, UDPSOCKET udpsock);
00168    int listen(const UDTSOCKET u, int backlog);
00169    UDTSOCKET accept(const UDTSOCKET listen, sockaddr* addr, int* addrlen);
00170    int connect(const UDTSOCKET u, const sockaddr* name, int namelen);
00171    int close(const UDTSOCKET u);
00172    int getpeername(const UDTSOCKET u, sockaddr* name, int* namelen);
00173    int getsockname(const UDTSOCKET u, sockaddr* name, int* namelen);
00174    int select(ud_set* readfds, ud_set* writefds, ud_set* exceptfds, const timeval* timeout);
00175    int selectEx(const std::vector<UDTSOCKET>& fds, std::vector<UDTSOCKET>* readfds, std::vector<UDTSOCKET>* writefds, std::vector<UDTSOCKET>* exceptfds, int64_t msTimeOut);
00176    int epoll_create();
00177    int epoll_add_usock(const int eid, const UDTSOCKET u, const int* events = NULL);
00178    int epoll_add_ssock(const int eid, const SYSSOCKET s, const int* events = NULL);
00179    int epoll_remove_usock(const int eid, const UDTSOCKET u);
00180    int epoll_remove_ssock(const int eid, const SYSSOCKET s);
00181    int epoll_wait(const int eid, std::set<UDTSOCKET>* readfds, std::set<UDTSOCKET>* writefds, int64_t msTimeOut, std::set<SYSSOCKET>* lrfds = NULL, std::set<SYSSOCKET>* lwfds = NULL);
00182    int epoll_release(const int eid);
00183 
00184    // BARCHART
00185    int epoll_update_usock(const int eid, const UDTSOCKET u, const int* events = NULL);
00186    // BARCHART
00187    int epoll_verify_usock(const int eid, const UDTSOCKET u, int* events);
00188 
00189       // Functionality:
00190       //    record the UDT exception.
00191       // Parameters:
00192       //    0) [in] e: pointer to a UDT exception instance.
00193       // Returned value:
00194       //    None.
00195 
00196    void setError(CUDTException* e);
00197 
00198       // Functionality:
00199       //    look up the most recent UDT exception.
00200       // Parameters:
00201       //    None.
00202       // Returned value:
00203       //    pointer to a UDT exception instance.
00204 
00205    CUDTException* getError();
00206 
00207 private:
00208 //   void init();
00209 
00210 private:
00211    std::map<UDTSOCKET, CUDTSocket*> m_Sockets;       // stores all the socket structures
00212 
00213    pthread_mutex_t m_ControlLock;                    // used to synchronize UDT API
00214 
00215    pthread_mutex_t m_IDLock;                         // used to synchronize ID generation
00216    UDTSOCKET m_SocketID;                             // seed to generate a new unique socket ID
00217 
00218    std::map<int64_t, std::set<UDTSOCKET> > m_PeerRec;// record sockets from peers to avoid repeated connection request, int64_t = (socker_id << 30) + isn
00219 
00220 private:
00221    pthread_key_t m_TLSError;                         // thread local error record (last error)
00222    #ifndef WIN32
00223       static void TLSDestroy(void* e) {if (NULL != e) delete (CUDTException*)e;}
00224    #else
00225       std::map<DWORD, CUDTException*> m_mTLSRecord;
00226       void checkTLSValue();
00227       pthread_mutex_t m_TLSLock;
00228    #endif
00229 
00230 private:
00231    void connect_complete(const UDTSOCKET u);
00232    CUDTSocket* locate(const UDTSOCKET u);
00233    CUDTSocket* locate(const sockaddr* peer, const UDTSOCKET id, int32_t isn);
00234    void updateMux(CUDTSocket* s, const sockaddr* addr = NULL, const UDPSOCKET* = NULL);
00235    void updateMux(CUDTSocket* s, const CUDTSocket* ls);
00236 
00237 private:
00238    std::map<int, CMultiplexer> m_mMultiplexer;          // UDP multiplexer
00239    pthread_mutex_t m_MultiplexerLock;
00240 
00241 private:
00242    CCache<CInfoBlock>* m_pCache;                        // UDT network information cache
00243 
00244 private:
00245    volatile bool m_bClosing;
00246    pthread_mutex_t m_GCStopLock;
00247    pthread_cond_t m_GCStopCond;
00248 
00249    pthread_mutex_t m_InitLock;
00250    int m_iInstanceCount;                                // number of startup() called by application
00251    bool m_bGCStatus;                                    // if the GC thread is working (true)
00252 
00253    pthread_t m_GCThread;
00254    #ifndef WIN32
00255       static void* garbageCollect(void*);
00256    #else
00257       static DWORD WINAPI garbageCollect(LPVOID);
00258    #endif
00259 
00260    std::map<UDTSOCKET, CUDTSocket*> m_ClosedSockets;   // temporarily store closed sockets
00261 
00262    void checkBrokenSockets();
00263    void removeSocket(const UDTSOCKET u);
00264 
00265 private:
00266    CEPoll m_EPoll;                                     // handling epoll data structures and events
00267 
00268 private:
00269    CUDTUnited(const CUDTUnited&);
00270    CUDTUnited& operator=(const CUDTUnited&);
00271 };
00272 
00273 #endif

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