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 08/01/2009 | |
39 *****************************************************************************/ | |
40 | |
41 #ifndef __UDT_COMMON_H__ | |
42 #define __UDT_COMMON_H__ | |
43 | |
44 | |
45 #ifndef WIN32 | |
46 #include <sys/time.h> | |
47 #include <sys/uio.h> | |
48 #include <pthread.h> | |
49 #else | |
50 #include <windows.h> | |
51 #endif | |
52 #include <cstdlib> | |
53 #include "udt.h" | |
54 | |
55 | |
56 #ifdef WIN32 | |
57 // Windows compability | |
58 typedef HANDLE pthread_t; | |
59 typedef HANDLE pthread_mutex_t; | |
60 typedef HANDLE pthread_cond_t; | |
61 typedef DWORD pthread_key_t; | |
62 #endif | |
63 | |
64 | |
65 //////////////////////////////////////////////////////////////////////////////// | |
66 | |
67 class CTimer | |
68 { | |
69 public: | |
70 CTimer(); | |
71 ~CTimer(); | |
72 | |
73 public: | |
74 | |
75 // Functionality: | |
76 // Sleep for "interval" CCs. | |
77 // Parameters: | |
78 // 0) [in] interval: CCs to sleep. | |
79 // Returned value: | |
80 // None. | |
81 | |
82 void sleep(const uint64_t& interval); | |
83 | |
84 // Functionality: | |
85 // Seelp until CC "nexttime". | |
86 // Parameters: | |
87 // 0) [in] nexttime: next time the caller is waken up. | |
88 // Returned value: | |
89 // None. | |
90 | |
91 void sleepto(const uint64_t& nexttime); | |
92 | |
93 // Functionality: | |
94 // Stop the sleep() or sleepto() methods. | |
95 // Parameters: | |
96 // None. | |
97 // Returned value: | |
98 // None. | |
99 | |
100 void interrupt(); | |
101 | |
102 // Functionality: | |
103 // trigger the clock for a tick, for better granuality in no_busy_waiti
ng timer. | |
104 // Parameters: | |
105 // None. | |
106 // Returned value: | |
107 // None. | |
108 | |
109 void tick(); | |
110 | |
111 public: | |
112 | |
113 // Functionality: | |
114 // Read the CPU clock cycle into x. | |
115 // Parameters: | |
116 // 0) [out] x: to record cpu clock cycles. | |
117 // Returned value: | |
118 // None. | |
119 | |
120 static void rdtsc(uint64_t &x); | |
121 | |
122 // Functionality: | |
123 // return the CPU frequency. | |
124 // Parameters: | |
125 // None. | |
126 // Returned value: | |
127 // CPU frequency. | |
128 | |
129 static uint64_t getCPUFrequency(); | |
130 | |
131 // Functionality: | |
132 // check the current time, 64bit, in microseconds. | |
133 // Parameters: | |
134 // None. | |
135 // Returned value: | |
136 // current time in microseconds. | |
137 | |
138 static uint64_t getTime(); | |
139 | |
140 // Functionality: | |
141 // trigger an event such as new connection, close, new data, etc. for "
select" call. | |
142 // Parameters: | |
143 // None. | |
144 // Returned value: | |
145 // None. | |
146 | |
147 static void triggerEvent(); | |
148 | |
149 // Functionality: | |
150 // wait for an event to br triggered by "triggerEvent". | |
151 // Parameters: | |
152 // None. | |
153 // Returned value: | |
154 // None. | |
155 | |
156 static void waitForEvent(); | |
157 | |
158 // Functionality: | |
159 // sleep for a short interval. exact sleep time does not matter | |
160 // Parameters: | |
161 // None. | |
162 // Returned value: | |
163 // None. | |
164 | |
165 static void sleep(); | |
166 | |
167 private: | |
168 uint64_t m_ullSchedTime; // next schedulled time | |
169 | |
170 pthread_cond_t m_TickCond; | |
171 pthread_mutex_t m_TickLock; | |
172 | |
173 static pthread_cond_t m_EventCond; | |
174 static pthread_mutex_t m_EventLock; | |
175 | |
176 private: | |
177 static uint64_t s_ullCPUFrequency; // CPU frequency : clock cycles per micr
osecond | |
178 static uint64_t readCPUFrequency(); | |
179 }; | |
180 | |
181 //////////////////////////////////////////////////////////////////////////////// | |
182 | |
183 class CGuard | |
184 { | |
185 public: | |
186 CGuard(pthread_mutex_t& lock); | |
187 ~CGuard(); | |
188 | |
189 public: | |
190 static void enterCS(pthread_mutex_t& lock); | |
191 static void leaveCS(pthread_mutex_t& lock); | |
192 | |
193 static void createMutex(pthread_mutex_t& lock); | |
194 static void releaseMutex(pthread_mutex_t& lock); | |
195 | |
196 static void createCond(pthread_cond_t& cond); | |
197 static void releaseCond(pthread_cond_t& cond); | |
198 | |
199 private: | |
200 pthread_mutex_t& m_Mutex; // Alias name of the mutex to be protect
ed | |
201 int m_iLocked; // Locking status | |
202 | |
203 CGuard& operator=(const CGuard&); | |
204 }; | |
205 | |
206 | |
207 | |
208 //////////////////////////////////////////////////////////////////////////////// | |
209 | |
210 // UDT Sequence Number 0 - (2^31 - 1) | |
211 | |
212 // seqcmp: compare two seq#, considering the wraping | |
213 // seqlen: length from the 1st to the 2nd seq#, including both | |
214 // seqoff: offset from the 2nd to the 1st seq# | |
215 // incseq: increase the seq# by 1 | |
216 // decseq: decrease the seq# by 1 | |
217 // incseq: increase the seq# by a given offset | |
218 | |
219 class CSeqNo | |
220 { | |
221 public: | |
222 inline static int seqcmp(const int32_t& seq1, const int32_t& seq2) | |
223 {return (abs(seq1 - seq2) < m_iSeqNoTH) ? (seq1 - seq2) : (seq2 - seq1);} | |
224 | |
225 inline static int seqlen(const int32_t& seq1, const int32_t& seq2) | |
226 {return (seq1 <= seq2) ? (seq2 - seq1 + 1) : (seq2 - seq1 + m_iMaxSeqNo + 2);
} | |
227 | |
228 inline static int seqoff(const int32_t& seq1, const int32_t& seq2) | |
229 { | |
230 if (abs(seq1 - seq2) < m_iSeqNoTH) | |
231 return seq2 - seq1; | |
232 | |
233 if (seq1 < seq2) | |
234 return seq2 - seq1 - m_iMaxSeqNo - 1; | |
235 | |
236 return seq2 - seq1 + m_iMaxSeqNo + 1; | |
237 } | |
238 | |
239 inline static int32_t incseq(const int32_t seq) | |
240 {return (seq == m_iMaxSeqNo) ? 0 : seq + 1;} | |
241 | |
242 inline static int32_t decseq(const int32_t& seq) | |
243 {return (seq == 0) ? m_iMaxSeqNo : seq - 1;} | |
244 | |
245 inline static int32_t incseq(const int32_t& seq, const int32_t& inc) | |
246 {return (m_iMaxSeqNo - seq >= inc) ? seq + inc : seq - m_iMaxSeqNo + inc - 1;
} | |
247 | |
248 public: | |
249 static const int32_t m_iSeqNoTH; // threshold for comparing seq.
no. | |
250 static const int32_t m_iMaxSeqNo; // maximum sequence number used
in UDT | |
251 }; | |
252 | |
253 //////////////////////////////////////////////////////////////////////////////// | |
254 | |
255 // UDT ACK Sub-sequence Number: 0 - (2^31 - 1) | |
256 | |
257 class CAckNo | |
258 { | |
259 public: | |
260 inline static int32_t incack(const int32_t& ackno) | |
261 {return (ackno == m_iMaxAckSeqNo) ? 0 : ackno + 1;} | |
262 | |
263 public: | |
264 static const int32_t m_iMaxAckSeqNo; // maximum ACK sub-sequence numb
er used in UDT | |
265 }; | |
266 | |
267 //////////////////////////////////////////////////////////////////////////////// | |
268 | |
269 // UDT Message Number: 0 - (2^29 - 1) | |
270 | |
271 class CMsgNo | |
272 { | |
273 public: | |
274 inline static int msgcmp(const int32_t& msgno1, const int32_t& msgno2) | |
275 {return (abs(msgno1 - msgno2) < m_iMsgNoTH) ? (msgno1 - msgno2) : (msgno2 - m
sgno1);} | |
276 | |
277 inline static int msglen(const int32_t& msgno1, const int32_t& msgno2) | |
278 {return (msgno1 <= msgno2) ? (msgno2 - msgno1 + 1) : (msgno2 - msgno1 + m_iMa
xMsgNo + 2);} | |
279 | |
280 inline static int msgoff(const int32_t& msgno1, const int32_t& msgno2) | |
281 { | |
282 if (abs(msgno1 - msgno2) < m_iMsgNoTH) | |
283 return msgno2 - msgno1; | |
284 | |
285 if (msgno1 < msgno2) | |
286 return msgno2 - msgno1 - m_iMaxMsgNo - 1; | |
287 | |
288 return msgno2 - msgno1 + m_iMaxMsgNo + 1; | |
289 } | |
290 | |
291 inline static int32_t incmsg(const int32_t& msgno) | |
292 {return (msgno == m_iMaxMsgNo) ? 0 : msgno + 1;} | |
293 | |
294 public: | |
295 static const int32_t m_iMsgNoTH; // threshold for comparing msg.
no. | |
296 static const int32_t m_iMaxMsgNo; // maximum message number used i
n UDT | |
297 }; | |
298 | |
299 //////////////////////////////////////////////////////////////////////////////// | |
300 | |
301 struct CIPAddress | |
302 { | |
303 static bool ipcmp(const sockaddr* addr1, const sockaddr* addr2, const int& ve
r = AF_INET); | |
304 static void ntop(const sockaddr* addr, uint32_t ip[4], const int& ver = AF_IN
ET); | |
305 static void pton(sockaddr* addr, const uint32_t ip[4], const int& ver = AF_IN
ET); | |
306 }; | |
307 | |
308 //////////////////////////////////////////////////////////////////////////////// | |
309 | |
310 struct CMD5 | |
311 { | |
312 static void compute(const char* input, unsigned char result[16]); | |
313 }; | |
314 | |
315 | |
316 #endif | |
OLD | NEW |