|
1 /* |
|
2 * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Handles socket connecting |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 #include "natfwconnectivityapidefs.h" |
|
22 |
|
23 #include "natfwsockethandler.h" |
|
24 #include "natfwsocketsender.h" |
|
25 #include "natfwsocketmediaconnwrapperlogs.h" |
|
26 |
|
27 |
|
28 // ======== MEMBER FUNCTIONS ======== |
|
29 |
|
30 |
|
31 // --------------------------------------------------------------------------- |
|
32 // CNATFWSocketHandler::CNATFWSocketHandler |
|
33 // --------------------------------------------------------------------------- |
|
34 // |
|
35 CNATFWSocketHandler::CNATFWSocketHandler( TUint aPort ) : |
|
36 CActive(EPriorityStandard), iPort( aPort ), iSendState ( EUnitialized ), |
|
37 iReceiveState( ENotReceiving ) |
|
38 { |
|
39 CActiveScheduler::Add(this); |
|
40 } |
|
41 |
|
42 |
|
43 // --------------------------------------------------------------------------- |
|
44 // CNATFWSocketHandler::ConstructL |
|
45 // --------------------------------------------------------------------------- |
|
46 // |
|
47 void CNATFWSocketHandler::ConstructL( |
|
48 MNATFWSocketReceiverObserver& aRecObs, |
|
49 MNATFWSocketSenderObserver& aSendObs, TUint aProtocol ) |
|
50 { |
|
51 __SOCKETMEDIACONNWRAPPER( "CNATFWSocketHandler::ConstructL" ) |
|
52 |
|
53 __ASSERT_ALWAYS( KProtocolInetTcp == aProtocol || |
|
54 KProtocolInetUdp == aProtocol, User::Leave( KErrArgument ) ); |
|
55 |
|
56 iProtocol = aProtocol; |
|
57 iSender = CNATFWSocketSender::NewL( aSendObs, iProtocol ); |
|
58 iReceiver = CNATFWSocketReceiver::NewL( aRecObs, iProtocol ); |
|
59 } |
|
60 |
|
61 |
|
62 // --------------------------------------------------------------------------- |
|
63 // CNATFWSocketHandler* CNATFWSocketHandler::NewL |
|
64 // --------------------------------------------------------------------------- |
|
65 // |
|
66 CNATFWSocketHandler* CNATFWSocketHandler::NewL( |
|
67 MNATFWSocketReceiverObserver& aRecObs, |
|
68 MNATFWSocketSenderObserver& aSendObs, TUint aProtocol, TUint aPort ) |
|
69 { |
|
70 CNATFWSocketHandler* self = CNATFWSocketHandler::NewLC( aRecObs, aSendObs, |
|
71 aProtocol, aPort ); |
|
72 CleanupStack::Pop( self ); |
|
73 return self; |
|
74 } |
|
75 |
|
76 |
|
77 // --------------------------------------------------------------------------- |
|
78 // CNATFWSocketHandler* CNATFWSocketHandler::NewLC |
|
79 // --------------------------------------------------------------------------- |
|
80 // |
|
81 CNATFWSocketHandler* CNATFWSocketHandler::NewLC( |
|
82 MNATFWSocketReceiverObserver& aRecObs, |
|
83 MNATFWSocketSenderObserver& aSendObs, TUint aProtocol, TUint aPort ) |
|
84 { |
|
85 CNATFWSocketHandler* self |
|
86 = new( ELeave ) CNATFWSocketHandler( aPort ); |
|
87 CleanupStack::PushL( self ); |
|
88 self->ConstructL( aRecObs, aSendObs, aProtocol ); |
|
89 return self; |
|
90 } |
|
91 |
|
92 |
|
93 // --------------------------------------------------------------------------- |
|
94 // CNATFWSocketHandler::~CNATFWSocketHandler() |
|
95 // --------------------------------------------------------------------------- |
|
96 // |
|
97 CNATFWSocketHandler::~CNATFWSocketHandler() |
|
98 { |
|
99 __SOCKETMEDIACONNWRAPPER( "CNATFWSocketHandler::~CNATFWSocketHandler" ) |
|
100 |
|
101 iMediaObserver = NULL; |
|
102 iTcpConnectionObserver = NULL; |
|
103 Cancel(); |
|
104 delete iSender; |
|
105 delete iReceiver; |
|
106 iSocket.Close(); |
|
107 iListeningSocket.Close(); |
|
108 } |
|
109 |
|
110 |
|
111 // --------------------------------------------------------------------------- |
|
112 // CNATFWSocketHandler::RunL( ) |
|
113 // --------------------------------------------------------------------------- |
|
114 // |
|
115 void CNATFWSocketHandler::RunL( ) |
|
116 { |
|
117 __SOCKETMEDIACONNWRAPPER_INT1( |
|
118 "CNATFWSocketHandler::RunL iStatus:", iStatus.Int() ) |
|
119 |
|
120 if ( KErrNone == iStatus.Int() ) |
|
121 { |
|
122 iSendState = EConnected; |
|
123 } |
|
124 else |
|
125 { |
|
126 iSendState = EUnitialized; |
|
127 } |
|
128 iTcpConnectionObserver->ConnectingCompleted( iStatus.Int() ); |
|
129 } |
|
130 |
|
131 |
|
132 // --------------------------------------------------------------------------- |
|
133 // CNATFWSocketHandler::DoCancel( ) |
|
134 // --------------------------------------------------------------------------- |
|
135 // |
|
136 void CNATFWSocketHandler::DoCancel( ) |
|
137 { |
|
138 __SOCKETMEDIACONNWRAPPER( "CNATFWSocketHandler::DoCancel" ) |
|
139 |
|
140 iSocket.CancelAll(); |
|
141 if ( ETcpSetupPassive == iTcpConnectionType ) |
|
142 { |
|
143 iListeningSocket.CancelAll(); |
|
144 } |
|
145 } |
|
146 |
|
147 |
|
148 // --------------------------------------------------------------------------- |
|
149 // CNATFWSocketHandler::LocalAddress( ) |
|
150 // --------------------------------------------------------------------------- |
|
151 // |
|
152 void CNATFWSocketHandler::LocalAddress( TSockAddr& aSocketToSendForPeer ) |
|
153 { |
|
154 __SOCKETMEDIACONNWRAPPER_ADDRLOG( |
|
155 "CNATFWSocketHandler::LocalAddress - Local IP Address: ", iLocalAddress ) |
|
156 |
|
157 aSocketToSendForPeer = iLocalAddress; |
|
158 } |
|
159 |
|
160 // --------------------------------------------------------------------------- |
|
161 // CNATFWSocketHandler::GetRemoteAddress( ) |
|
162 // --------------------------------------------------------------------------- |
|
163 // |
|
164 void CNATFWSocketHandler::GetRemoteAddress( TSockAddr& aRemoteAddress ) |
|
165 { |
|
166 __SOCKETMEDIACONNWRAPPER_ADDRLOG( |
|
167 "CNATFWSocketHandler::GetRemoteAddress - Remote IP Address: ", iRemoteAddress ) |
|
168 |
|
169 aRemoteAddress = iRemoteAddress; |
|
170 } |
|
171 |
|
172 // --------------------------------------------------------------------------- |
|
173 // CNATFWSocketHandler::SetAddrL( ) |
|
174 // --------------------------------------------------------------------------- |
|
175 // |
|
176 void CNATFWSocketHandler::SetAddrL( const TSockAddr& aSocketToSendForPeer ) |
|
177 { |
|
178 __SOCKETMEDIACONNWRAPPER( "CNATFWSocketHandler::SetAddrL" ) |
|
179 |
|
180 __ASSERT_ALWAYS( EUnitialized == iSendState, User::Leave( KErrArgument ) ); |
|
181 |
|
182 if ( KAfInet6 == aSocketToSendForPeer.Family() ) |
|
183 { |
|
184 iRemoteAddress.SetAddress( KInet6AddrLoop ); |
|
185 } |
|
186 else |
|
187 { |
|
188 iRemoteAddress.SetAddress( INET_ADDR( 127, 0, 0, 1 ) ); |
|
189 } |
|
190 |
|
191 iRemoteAddress.SetPort( aSocketToSendForPeer.Port() ); |
|
192 |
|
193 TBool requiredPortOpen( EFalse ); |
|
194 TInt error( KErrNone ); |
|
195 |
|
196 if ( KProtocolInetTcp == iProtocol ) |
|
197 { |
|
198 User::LeaveIfError( iSocket.Open( iSocketServer, |
|
199 KAfInet, KSockStream, KProtocolInetTcp )); |
|
200 } |
|
201 else if ( KProtocolInetUdp == iProtocol ) |
|
202 { |
|
203 User::LeaveIfError( iSocket.Open( iSocketServer, |
|
204 KAfInet, KSockDatagram, KProtocolInetUdp )); |
|
205 } |
|
206 else |
|
207 { |
|
208 // Nothing to do here |
|
209 } |
|
210 |
|
211 while ( !requiredPortOpen ) |
|
212 { |
|
213 if ( KProtocolInetUdp == iProtocol ) |
|
214 { |
|
215 error = iSocket.SetLocalPort( iLocalAddress.Port() ); |
|
216 } |
|
217 if ( KProtocolInetTcp == iProtocol ) |
|
218 { |
|
219 error = iSocket.Bind( iLocalAddress ); |
|
220 } |
|
221 |
|
222 if ( KErrNone != error ) |
|
223 { |
|
224 TUint port( 0 ); |
|
225 iMediaObserver->GetNewFreePort( port ); |
|
226 iLocalAddress.SetPort( port ); |
|
227 } |
|
228 else |
|
229 { |
|
230 requiredPortOpen = ETrue; |
|
231 } |
|
232 } |
|
233 |
|
234 if ( KProtocolInetUdp == iProtocol ) |
|
235 { |
|
236 iSendState = EConnected; |
|
237 } |
|
238 __SOCKETMEDIACONNWRAPPER_ADDRLOG( "Remote IP Address: ", iRemoteAddress ) |
|
239 __SOCKETMEDIACONNWRAPPER_ADDRLOG( "Local IP Address: ", iLocalAddress ) |
|
240 } |
|
241 |
|
242 |
|
243 // --------------------------------------------------------------------------- |
|
244 // CNATFWSocketHandler::SetReceivingStateL( ) |
|
245 // --------------------------------------------------------------------------- |
|
246 // |
|
247 void CNATFWSocketHandler::SetReceivingStateL( ) |
|
248 { |
|
249 __SOCKETMEDIACONNWRAPPER( "CNATFWSocketHandler::SetReceivingStateL" ) |
|
250 |
|
251 __ASSERT_ALWAYS( EConnected == iSendState || ESending == iSendState, |
|
252 User::Leave( KErrArgument ) ); |
|
253 __ASSERT_ALWAYS( iReceiveState == ENotReceiving, |
|
254 User::Leave( KErrInUse ) ); |
|
255 |
|
256 if ( ETcpSetupPassive == iTcpConnectionType ) |
|
257 { |
|
258 iReceiver->StartListening( iListeningSocket ); |
|
259 } |
|
260 else |
|
261 { |
|
262 iReceiver->StartListening( iSocket ); |
|
263 } |
|
264 |
|
265 iReceiveState = EReceiving; |
|
266 } |
|
267 |
|
268 |
|
269 // --------------------------------------------------------------------------- |
|
270 // CNATFWSocketHandler::SetSendingStateL( ) |
|
271 // --------------------------------------------------------------------------- |
|
272 // |
|
273 void CNATFWSocketHandler::SetSendingStateL( ) |
|
274 { |
|
275 __SOCKETMEDIACONNWRAPPER( "CNATFWSocketHandler::SetSendingStateL" ) |
|
276 |
|
277 __ASSERT_ALWAYS( EConnected == iSendState, User::Leave( KErrNotReady ) ); |
|
278 |
|
279 if ( ETcpSetupPassive == iTcpConnectionType ) |
|
280 { |
|
281 iSender->SetRemoteAddress( iRemoteAddress, iListeningSocket ); |
|
282 } |
|
283 else |
|
284 { |
|
285 iSender->SetRemoteAddress( iRemoteAddress, iSocket ); |
|
286 } |
|
287 |
|
288 iSendState = ESending; |
|
289 } |
|
290 |
|
291 |
|
292 // --------------------------------------------------------------------------- |
|
293 // CNATFWSocketHandler::SetReceivingStateForMuxWrapper( ) |
|
294 // --------------------------------------------------------------------------- |
|
295 // |
|
296 void CNATFWSocketHandler::SetReceivingStateForMuxWrapper( |
|
297 TNATFWStreamingState aState ) |
|
298 { |
|
299 __SOCKETMEDIACONNWRAPPER( |
|
300 "CNATFWSocketHandler::SetReceivingStateForMuxWrapper" ) |
|
301 |
|
302 iReceiver->SetReceivingStateForMuxWrapper( aState ); |
|
303 } |
|
304 |
|
305 |
|
306 // --------------------------------------------------------------------------- |
|
307 // CNATFWSocketHandler::SendL( ) |
|
308 // --------------------------------------------------------------------------- |
|
309 // |
|
310 void CNATFWSocketHandler::SendL( const TDesC8& aStreamPortion ) |
|
311 { |
|
312 __SOCKETMEDIACONNWRAPPER( "CNATFWSocketHandler::SendL" ) |
|
313 |
|
314 __ASSERT_ALWAYS( ESending == iSendState, User::Leave( KErrNotReady ) ); |
|
315 |
|
316 iSender->SendL( aStreamPortion ); |
|
317 } |
|
318 |
|
319 |
|
320 // --------------------------------------------------------------------------- |
|
321 // CNATFWSocketHandler::SetLocalAddress( ) |
|
322 // --------------------------------------------------------------------------- |
|
323 // |
|
324 void CNATFWSocketHandler::SetLocalAddress( |
|
325 const RSocketServ& aSocketServer, const TSockAddr& /*aLocalAddress*/ ) |
|
326 { |
|
327 iSocketServer = aSocketServer; |
|
328 |
|
329 iLocalAddress.SetAddress( INET_ADDR( 127, 0, 0, 1 ) ); |
|
330 iLocalAddress.SetPort( iPort ); |
|
331 } |
|
332 |
|
333 // --------------------------------------------------------------------------- |
|
334 // CNATFWSocketHandler::SetMediaObserverL( ) |
|
335 // --------------------------------------------------------------------------- |
|
336 // |
|
337 void CNATFWSocketHandler::SetMediaObserverL( |
|
338 MNATFWMediaWrapperObserver* aMediaObserver ) |
|
339 { |
|
340 __ASSERT_ALWAYS( aMediaObserver, User::Leave( KErrNotReady ) ); |
|
341 |
|
342 iMediaObserver = aMediaObserver; |
|
343 } |
|
344 |
|
345 // --------------------------------------------------------------------------- |
|
346 // CNATFWSocketHandler::DeactivateSending( ) |
|
347 // --------------------------------------------------------------------------- |
|
348 // |
|
349 void CNATFWSocketHandler::DeactivateSending( ) |
|
350 { |
|
351 __SOCKETMEDIACONNWRAPPER( "CNATFWSocketHandler::DeactivateSending" ) |
|
352 |
|
353 iSender->Cancel(); |
|
354 iSendState = EConnected; |
|
355 } |
|
356 |
|
357 |
|
358 // --------------------------------------------------------------------------- |
|
359 // CNATFWSocketHandler::DeactivateReceiving( ) |
|
360 // --------------------------------------------------------------------------- |
|
361 // |
|
362 void CNATFWSocketHandler::DeactivateReceiving( ) |
|
363 { |
|
364 __SOCKETMEDIACONNWRAPPER( "CNATFWSocketHandler::DeactivateReceiving" ) |
|
365 |
|
366 iReceiver->Cancel(); |
|
367 iReceiveState = ENotReceiving; |
|
368 } |
|
369 |
|
370 |
|
371 // --------------------------------------------------------------------------- |
|
372 // CNATFWSocketHandler::OpenTcpConnectionL() |
|
373 // --------------------------------------------------------------------------- |
|
374 // |
|
375 void CNATFWSocketHandler::OpenTcpConnectionL( |
|
376 TNATFWTcpConnectionSetup aSetup, MNsmcwTcpConnectionObserver& aObserver ) |
|
377 { |
|
378 __SOCKETMEDIACONNWRAPPER( "CNATFWSocketHandler::OpenTcpConnectionL" ) |
|
379 |
|
380 __ASSERT_ALWAYS( EUnitialized == iSendState, User::Leave( KErrInUse ) ); |
|
381 |
|
382 iTcpConnectionType = aSetup; |
|
383 iTcpConnectionObserver = &aObserver; |
|
384 |
|
385 if ( ETcpSetupActive == iTcpConnectionType ) |
|
386 { |
|
387 iSocket.Connect( iRemoteAddress, iStatus ); |
|
388 } |
|
389 if ( ETcpSetupPassive == iTcpConnectionType ) |
|
390 { |
|
391 User::LeaveIfError( iSocket.Listen( 1 ) ); |
|
392 User::LeaveIfError( iListeningSocket.Open( iSocketServer ) ); |
|
393 iSocket.Accept( iListeningSocket, iStatus ); |
|
394 } |
|
395 iSendState = EGettingConnection; |
|
396 SetActive(); |
|
397 } |
|
398 |
|
399 |
|
400 // --------------------------------------------------------------------------- |
|
401 // CNATFWSocketHandler::CloseTcpConnection() |
|
402 // --------------------------------------------------------------------------- |
|
403 // |
|
404 void CNATFWSocketHandler::CloseTcpConnection() |
|
405 { |
|
406 __SOCKETMEDIACONNWRAPPER( |
|
407 "CNATFWSocketHandler::CloseTcpConnection" ) |
|
408 |
|
409 Cancel(); |
|
410 iSendState = EUnitialized; |
|
411 iTcpConnectionObserver->ConnectingCompleted( KErrCancel ); |
|
412 } |
|
413 |