Create Project for repo.or.cz
[vp.git] / src / ludp / LudpReceiver.h
blob97ed146781b725f6ac6aeb531e6bdc089ccc6135
1 /*******************************************************************************
2 * File : LudpReceiver.h
3 *
4 * Author : Henry He
5 * Created : 2009-10-13 9:02:32
6 * Description :
7 ******************************************************************************/
9 #ifndef __LUDPRECEIVER_INC_20091013_090232_HENRY__
10 #define __LUDPRECEIVER_INC_20091013_090232_HENRY__
13 /*******************************************************************************
14 * Desc : Includes Files
15 ******************************************************************************/
16 #include "utils/common.h"
17 #include "ludp/common.h"
21 /*******************************************************************************
22 * Desc : Macro Definations
23 ******************************************************************************/
26 ////////////////////////////////////////////////////////////////////////////////
27 // Error Number
29 #define LUDP_RECEIVER_ERR_FAILED (-1)
30 #define LUDP_RECEIVER_ERR_NOT_CREATED (-2)
31 #define LUDP_RECEIVER_ERR_ALREADY_CREATED (-3)
32 #define LUDP_RECEIVER_ERR_CANT_CREATE (-4)
34 ////////////////////////////////////////////////////////////////////////////////
35 // Max Values
37 #define LUDP_RECEIVER_MAX_RESEND_SLICE_SN_PER_SLICE 200
39 /*******************************************************************************
40 * Desc : Type Definations
41 ******************************************************************************/
43 ////////////////////////////////////////////////////////////////////////////////
44 // Struct of Receiver Package
46 typedef struct tagReceiverPackage
48 // Whether this package finished
49 bool bFinished;
51 // Timestamp of this package
52 UINT32 unTimestamp;
54 // Slice pointer number
55 UINT16 usSliceNum;
57 // Number of slices not received
58 UINT16 usSliceLeft;
60 // Array of slice pointers
61 Slice **ppzsSlices;
63 // If Timeout reached (unTimeout == 0), unRetry--,
64 // don't retry to ask sender to resend if unRetry == 0;
65 UINT32 unRetry;
66 // If unTimeout == 0 and unRetry != 0,
67 // ask sender to resend part of this package
68 UINT32 unTimeout;
70 } ReceiverPackage;
73 ////////////////////////////////////////////////////////////////////////////////
74 // Callback function
75 typedef void (*OnLudpRecvFunc) (void *pvParam, ReceiverPackage &zrpPkg);
78 /*******************************************************************************
79 * Desc : Global Variables
80 ******************************************************************************/
86 /*******************************************************************************
87 * Desc : Classes
88 ******************************************************************************/
94 /******************************************************************************
95 * Desc : Ludp Receiver
96 ******************************************************************************/
97 class LudpReceiver
100 public:
101 /******************************************************************************
102 * Desc : Constructor / Deconstructor
103 ******************************************************************************/
104 LudpReceiver ();
105 virtual ~LudpReceiver ();
108 public:
109 /******************************************************************************
110 * Desc : Operations
111 ******************************************************************************/
112 int Create (
113 HLH_UDPSock &zhuSock,
114 UINT8 ucBaseType,
115 OnLudpRecvFunc OnLudpRecv,
116 void *pvOnLudpRecvParam,
117 UINT32 unPkgNum,
118 UINT32 unTimeTickUs,
119 UINT32 unTimeout,
120 UINT32 unMaxRetry );
123 // Destroy this LudpReceiver
124 void Destroy ();
126 // Whether this instance created
127 bool IsCreated ();
130 private:
131 /******************************************************************************
132 * Desc : Operations
133 ******************************************************************************/
135 ////////////////////////////////////////////////////////////////////////////////
136 // Whether zhrTimestamp in current receive range
137 bool IsInRecvRange (HLH_RoundU32 zhrTimestamp);
140 // Update timestamp range and package list if need according to zhrTimestamp
141 bool UpdateRecvRange (HLH_RoundU32 zhrTimestamp);
143 // Clear package list in range (unPosLow ~ unPosHigh)
144 void ClearPkgListRange (UINT32 unPosLow, UINT32 unPosHigh);
146 ////////////////////////////////////////////////////////////////////////////////
147 // Process Slice received
148 int ProcessSlice (Slice * pzsSlice);
150 ////////////////////////////////////////////////////////////////////////////////
151 // Receive data from socket
152 static void OnReceived (
153 void *pvThis,
154 void *pvBuf, UINT32 unLen,
155 HLH_SockAddr &zhsPeerAddr);
158 ////////////////////////////////////////////////////////////////////////////////
159 // Send Request to sender to resend some slice of package (unTimestamp)
160 int SendRequest (UINT32 unTimestamp);
162 // Check for timeout and Send request to sender to resend some slice
163 void CheckTimeout (HLH_Thread &zhtTimeoutThread);
165 ////////////////////////////////////////////////////////////////////////////////
166 // TimeoutThreadFunc:
167 // Check for timeout and Send request to sender to resend some slice
168 static void * TimeoutThreadFunc (HLH_Thread &zhtTimeoutThread, void *pvThis);
172 private:
173 /******************************************************************************
174 * Desc : Private Data
175 ******************************************************************************/
176 ////////////////////////////////////////////////////////////////////////////////
177 // Mutex of this instance
178 HLH_Mutex m_zhmMutex;
180 // Whether this instance created
181 bool m_bCreated;
183 // Whether first package received
184 bool m_bReceived;
186 // Socket of ludp (shared with others)
187 HLH_UDPSock * m_pzhuSock;
189 // Base type of slices to process by this receiver
190 UINT8 m_ucBaseType;
193 ////////////////////////////////////////////////////////////////////////////////
194 // Timestamp Range, initialed by the first slice received
195 HLH_RoundU32 m_zhrTimestampHigh;
197 // Time tick value
198 HLH_Time m_zhtTimeTick;
200 // Default timeout value
201 UINT32 m_unMaxTimeout;
203 // Max retry times
204 UINT32 m_unMaxRetry;
207 ////////////////////////////////////////////////////////////////////////////////
208 // Ring Buffer of Package List Pointer
209 ReceiverPackage * m_pzrPkgList;
211 // Number of Ring Buffer Pointer
212 UINT32 m_unPkgNum;
214 // Current Positions of Ring Buffer
215 UINT32 m_unHighPos;
218 ////////////////////////////////////////////////////////////////////////////////
219 // Timeout Thread
220 HLH_Thread m_zhtTimeoutThread;
222 // Callback function called when finish receviing a package
223 OnLudpRecvFunc m_zolOnLudpRecv;
225 // Parameter to m_zolOnLudpRecv ()
226 void *m_pvOnLudpRecvParam;
232 #endif /* __LUDPRECEIVER_INC_20091013_090232_HENRY__ */