00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
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;
00062
00063 uint64_t m_TimeStamp;
00064
00065 int m_iIPversion;
00066 sockaddr* m_pSelfAddr;
00067 sockaddr* m_pPeerAddr;
00068
00069 UDTSOCKET m_SocketID;
00070 UDTSOCKET m_ListenSocket;
00071
00072 UDTSOCKET m_PeerID;
00073 int32_t m_iISN;
00074
00075 CUDT* m_pUDT;
00076
00077 std::set<UDTSOCKET>* m_pQueuedSockets;
00078 std::set<UDTSOCKET>* m_pAcceptSockets;
00079
00080 pthread_cond_t m_AcceptCond;
00081 pthread_mutex_t m_AcceptLock;
00082
00083 unsigned int m_uiBackLog;
00084
00085 int m_iMuxID;
00086
00087 pthread_mutex_t m_ControlLock;
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
00108
00109
00110
00111
00112
00113
00114 int startup();
00115
00116
00117
00118
00119
00120
00121
00122
00123 int cleanup();
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133 UDTSOCKET newSocket(int af, int type);
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144 int newConnection(const UDTSOCKET listen, const sockaddr* peer, CHandShake* hs);
00145
00146
00147
00148
00149
00150
00151
00152
00153 CUDT* lookup(const UDTSOCKET u);
00154
00155
00156
00157
00158
00159
00160
00161
00162 UDTSTATUS getStatus(const UDTSOCKET u);
00163
00164
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
00185 int epoll_update_usock(const int eid, const UDTSOCKET u, const int* events = NULL);
00186
00187 int epoll_verify_usock(const int eid, const UDTSOCKET u, int* events);
00188
00189
00190
00191
00192
00193
00194
00195
00196 void setError(CUDTException* e);
00197
00198
00199
00200
00201
00202
00203
00204
00205 CUDTException* getError();
00206
00207 private:
00208
00209
00210 private:
00211 std::map<UDTSOCKET, CUDTSocket*> m_Sockets;
00212
00213 pthread_mutex_t m_ControlLock;
00214
00215 pthread_mutex_t m_IDLock;
00216 UDTSOCKET m_SocketID;
00217
00218 std::map<int64_t, std::set<UDTSOCKET> > m_PeerRec;
00219
00220 private:
00221 pthread_key_t m_TLSError;
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;
00239 pthread_mutex_t m_MultiplexerLock;
00240
00241 private:
00242 CCache<CInfoBlock>* m_pCache;
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;
00251 bool m_bGCStatus;
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;
00261
00262 void checkBrokenSockets();
00263 void removeSocket(const UDTSOCKET u);
00264
00265 private:
00266 CEPoll m_EPoll;
00267
00268 private:
00269 CUDTUnited(const CUDTUnited&);
00270 CUDTUnited& operator=(const CUDTUnited&);
00271 };
00272
00273 #endif