OLD | NEW |
| (Empty) |
1 /***************************************************************************** | |
2 Copyright (c) 2001 - 2010, 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 09/28/2010 | |
39 *****************************************************************************/ | |
40 | |
41 #ifndef __UDT_API_H__ | |
42 #define __UDT_API_H__ | |
43 | |
44 | |
45 #include <map> | |
46 #include <vector> | |
47 #include "udt.h" | |
48 #include "packet.h" | |
49 #include "queue.h" | |
50 #include "cache.h" | |
51 #include "epoll.h" | |
52 | |
53 class CUDT; | |
54 | |
55 class CUDTSocket | |
56 { | |
57 public: | |
58 CUDTSocket(); | |
59 ~CUDTSocket(); | |
60 | |
61 UDTSTATUS m_Status; // current socket state | |
62 | |
63 uint64_t m_TimeStamp; // time when the socket is closed | |
64 | |
65 int m_iIPversion; // IP version | |
66 sockaddr* m_pSelfAddr; // pointer to the local address of
the socket | |
67 sockaddr* m_pPeerAddr; // pointer to the peer address of t
he socket | |
68 | |
69 UDTSOCKET m_SocketID; // socket ID | |
70 UDTSOCKET m_ListenSocket; // ID of the listener socket; 0 mea
ns this is an independent socket | |
71 | |
72 UDTSOCKET m_PeerID; // peer socket ID | |
73 int32_t m_iISN; // initial sequence number, used to
tell different connection from same IP:port | |
74 | |
75 CUDT* m_pUDT; // pointer to the UDT entity | |
76 | |
77 std::set<UDTSOCKET>* m_pQueuedSockets; // set of connections waiting for a
ccept() | |
78 std::set<UDTSOCKET>* m_pAcceptSockets; // set of accept()ed connections | |
79 | |
80 pthread_cond_t m_AcceptCond; // used to block "accept" call | |
81 pthread_mutex_t m_AcceptLock; // mutex associated to m_AcceptCond | |
82 | |
83 unsigned int m_uiBackLog; // maximum number of connections in
queue | |
84 | |
85 int m_iMuxID; // multiplexer ID | |
86 | |
87 private: | |
88 CUDTSocket(const CUDTSocket&); | |
89 CUDTSocket& operator=(const CUDTSocket&); | |
90 }; | |
91 | |
92 //////////////////////////////////////////////////////////////////////////////// | |
93 | |
94 class CUDTUnited | |
95 { | |
96 friend class CUDT; | |
97 | |
98 public: | |
99 CUDTUnited(); | |
100 ~CUDTUnited(); | |
101 | |
102 public: | |
103 | |
104 // Functionality: | |
105 // initialize the UDT library. | |
106 // Parameters: | |
107 // None. | |
108 // Returned value: | |
109 // 0 if success, otherwise -1 is returned. | |
110 | |
111 int startup(); | |
112 | |
113 // Functionality: | |
114 // release the UDT library. | |
115 // Parameters: | |
116 // None. | |
117 // Returned value: | |
118 // 0 if success, otherwise -1 is returned. | |
119 | |
120 int cleanup(); | |
121 | |
122 // Functionality: | |
123 // Create a new UDT socket. | |
124 // Parameters: | |
125 // 0) [in] af: IP version, IPv4 (AF_INET) or IPv6 (AF_INET6). | |
126 // 1) [in] type: socket type, SOCK_STREAM or SOCK_DGRAM | |
127 // Returned value: | |
128 // The new UDT socket ID, or INVALID_SOCK. | |
129 | |
130 UDTSOCKET newSocket(const int& af, const int& type); | |
131 | |
132 // Functionality: | |
133 // Create a new UDT connection. | |
134 // Parameters: | |
135 // 0) [in] listen: the listening UDT socket; | |
136 // 1) [in] peer: peer address. | |
137 // 2) [in/out] hs: handshake information from peer side (in), negotiate
d value (out); | |
138 // Returned value: | |
139 // If the new connection is successfully created: 1 success, 0 already
exist, -1 error. | |
140 | |
141 int newConnection(const UDTSOCKET listen, const sockaddr* peer, CHandShake* h
s); | |
142 | |
143 // Functionality: | |
144 // look up the UDT entity according to its ID. | |
145 // Parameters: | |
146 // 0) [in] u: the UDT socket ID. | |
147 // Returned value: | |
148 // Pointer to the UDT entity. | |
149 | |
150 CUDT* lookup(const UDTSOCKET u); | |
151 | |
152 // Functionality: | |
153 // Check the status of the UDT socket. | |
154 // Parameters: | |
155 // 0) [in] u: the UDT socket ID. | |
156 // Returned value: | |
157 // UDT socket status, or NONEXIST if not found. | |
158 | |
159 UDTSTATUS getStatus(const UDTSOCKET u); | |
160 | |
161 // socket APIs | |
162 | |
163 int bind(const UDTSOCKET u, const sockaddr* name, const int& namelen); | |
164 int bind(const UDTSOCKET u, UDPSOCKET udpsock); | |
165 int listen(const UDTSOCKET u, const int& backlog); | |
166 UDTSOCKET accept(const UDTSOCKET listen, sockaddr* addr, int* addrlen); | |
167 int connect(const UDTSOCKET u, const sockaddr* name, const int& namelen); | |
168 int close(const UDTSOCKET u); | |
169 int getpeername(const UDTSOCKET u, sockaddr* name, int* namelen); | |
170 int getsockname(const UDTSOCKET u, sockaddr* name, int* namelen); | |
171 int select(ud_set* readfds, ud_set* writefds, ud_set* exceptfds, const timeva
l* timeout); | |
172 int selectEx(const std::vector<UDTSOCKET>& fds, std::vector<UDTSOCKET>* readf
ds, std::vector<UDTSOCKET>* writefds, std::vector<UDTSOCKET>* exceptfds, int64_t
msTimeOut); | |
173 int epoll_create(); | |
174 int epoll_add_usock(const int eid, const UDTSOCKET u, const int* events = NUL
L); | |
175 int epoll_add_ssock(const int eid, const SYSSOCKET s, const int* events = NUL
L); | |
176 int epoll_remove_usock(const int eid, const UDTSOCKET u, const int* events =
NULL); | |
177 int epoll_remove_ssock(const int eid, const SYSSOCKET s, const int* events =
NULL); | |
178 int epoll_wait(const int eid, std::set<UDTSOCKET>* readfds, std::set<UDTSOCKE
T>* writefds, int64_t msTimeOut, std::set<SYSSOCKET>* lrfds = NULL, std::set<SYS
SOCKET>* lwfds = NULL); | |
179 int epoll_release(const int eid); | |
180 | |
181 // Functionality: | |
182 // record the UDT exception. | |
183 // Parameters: | |
184 // 0) [in] e: pointer to a UDT exception instance. | |
185 // Returned value: | |
186 // None. | |
187 | |
188 void setError(CUDTException* e); | |
189 | |
190 // Functionality: | |
191 // look up the most recent UDT exception. | |
192 // Parameters: | |
193 // None. | |
194 // Returned value: | |
195 // pointer to a UDT exception instance. | |
196 | |
197 CUDTException* getError(); | |
198 | |
199 private: | |
200 std::map<UDTSOCKET, CUDTSocket*> m_Sockets; // stores all the socket st
ructures | |
201 | |
202 pthread_mutex_t m_ControlLock; // used to synchronize UDT
API | |
203 | |
204 pthread_mutex_t m_IDLock; // used to synchronize ID g
eneration | |
205 UDTSOCKET m_SocketID; // seed to generate a new u
nique socket ID | |
206 | |
207 std::map<int64_t, std::set<UDTSOCKET> > m_PeerRec;// record sockets from peer
s to avoid repeated connection request, int64_t = (socker_id << 30) + isn | |
208 | |
209 private: | |
210 pthread_key_t m_TLSError; // thread local error recor
d (last error) | |
211 #ifndef WIN32 | |
212 static void TLSDestroy(void* e) {if (NULL != e) delete (CUDTException*)e;} | |
213 #else | |
214 std::map<DWORD, CUDTException*> m_mTLSRecord; | |
215 void checkTLSValue(); | |
216 pthread_mutex_t m_TLSLock; | |
217 #endif | |
218 | |
219 private: | |
220 CUDTSocket* locate(const UDTSOCKET u); | |
221 CUDTSocket* locate(const sockaddr* peer, const UDTSOCKET& id, const int32_t&
isn); | |
222 void updateMux(CUDTSocket* s, const sockaddr* addr = NULL, const UDPSOCKET* =
NULL); | |
223 void updateMux(CUDTSocket* s, const CUDTSocket* ls); | |
224 | |
225 private: | |
226 std::map<int, CMultiplexer> m_mMultiplexer; // UDP multiplexer | |
227 pthread_mutex_t m_MultiplexerLock; | |
228 | |
229 private: | |
230 CCache* m_pCache; // UDT network informati
on cache | |
231 | |
232 private: | |
233 volatile bool m_bClosing; | |
234 pthread_mutex_t m_GCStopLock; | |
235 pthread_cond_t m_GCStopCond; | |
236 | |
237 pthread_mutex_t m_InitLock; | |
238 int m_iInstanceCount; // number of startup() c
alled by application | |
239 bool m_bGCStatus; // if the GC thread is w
orking (true) | |
240 | |
241 pthread_t m_GCThread; | |
242 #ifndef WIN32 | |
243 static void* garbageCollect(void*); | |
244 #else | |
245 static DWORD WINAPI garbageCollect(LPVOID); | |
246 #endif | |
247 | |
248 std::map<UDTSOCKET, CUDTSocket*> m_ClosedSockets; // temporarily store clos
ed sockets | |
249 | |
250 void checkBrokenSockets(); | |
251 void removeSocket(const UDTSOCKET u); | |
252 | |
253 private: | |
254 CEPoll m_EPoll; // handling epoll data st
ructures and events | |
255 | |
256 private: | |
257 CUDTUnited(const CUDTUnited&); | |
258 CUDTUnited& operator=(const CUDTUnited&); | |
259 }; | |
260 | |
261 #endif | |
OLD | NEW |