|
1 /* |
|
2 * Copyright (c) 2007 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 the License "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 tcp/udp socket* |
|
15 */ |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 // INCLUDE FILES |
|
21 #include "CCRSock.h" |
|
22 #include "videoserviceutilsLogger.h" |
|
23 |
|
24 // CONSTANTS |
|
25 _LIT( KCRSockLocalhost, "127.0.0.1" ); |
|
26 |
|
27 // ============================ MEMBER FUNCTIONS =============================== |
|
28 |
|
29 // ----------------------------------------------------------------------------- |
|
30 // CCRSock::CCRSock |
|
31 // C++ default constructor can NOT contain any code, that might leave. |
|
32 // ----------------------------------------------------------------------------- |
|
33 // |
|
34 CCRSock::CCRSock( |
|
35 MCRSockObserver& aObserver, |
|
36 TInt aSockId, |
|
37 RConnection& aConnection, |
|
38 RSocketServ& aSockServer, |
|
39 TBool aProtoTCP, |
|
40 TBool aIssueRead ) |
|
41 : CActive( CActive::EPriorityStandard ), |
|
42 iSockServer( aSockServer ), |
|
43 iSockStatus( CCRSock::EInitNeeded ), |
|
44 iObserver( aObserver ), |
|
45 iSockId( aSockId ), |
|
46 iProtoTCP( aProtoTCP ), |
|
47 iIssueRead( aIssueRead ), |
|
48 iReceivedData( NULL, 0 ), |
|
49 iSentData( NULL, 0 ), |
|
50 iConnection( aConnection ) |
|
51 { |
|
52 // None |
|
53 } |
|
54 |
|
55 // ----------------------------------------------------------------------------- |
|
56 // CCRSock::NewL |
|
57 // Two-phased constructor. |
|
58 // ----------------------------------------------------------------------------- |
|
59 // |
|
60 CCRSock* CCRSock::NewL( |
|
61 MCRSockObserver& aObserver, |
|
62 TInt aSockId, |
|
63 RConnection& aConnection, |
|
64 RSocketServ& aSockServer, |
|
65 TBool aProtoTCP, |
|
66 TBool aIssueRead ) |
|
67 { |
|
68 CCRSock* self = new( ELeave ) CCRSock( aObserver, aSockId, aConnection, |
|
69 aSockServer, aProtoTCP, aIssueRead ); |
|
70 CleanupStack::PushL( self ); |
|
71 self->ConstructL(); |
|
72 CleanupStack::Pop( self ); |
|
73 return self; |
|
74 } |
|
75 |
|
76 // ----------------------------------------------------------------------------- |
|
77 // CCRSock::ConstructL |
|
78 // Symbian 2nd phase constructor can leave. |
|
79 // ----------------------------------------------------------------------------- |
|
80 // |
|
81 void CCRSock::ConstructL() |
|
82 { |
|
83 LOG( "CCRSock::ConstructL() in" ); |
|
84 |
|
85 iReceivedDataBuf = HBufC8::NewL( KMaxDataSize ); |
|
86 iReceivedData.Set( iReceivedDataBuf->Des() ); |
|
87 iSentDataBuf = HBufC8::NewL( KMaxDataSize ); |
|
88 iSentData.Set( iSentDataBuf->Des() ); |
|
89 |
|
90 // Add self to active scheduler |
|
91 CActiveScheduler::Add( this ); |
|
92 if ( iIssueRead ) |
|
93 { |
|
94 iReader = CCRSockReader::NewL( *this, iConnection, iSockServer ); |
|
95 } |
|
96 |
|
97 iToAddr.SetPort( 0 ); |
|
98 |
|
99 LOG( "CCRSock::ConstructL() out" ); |
|
100 } |
|
101 |
|
102 // ----------------------------------------------------------------------------- |
|
103 // CCRSock::~CCRSock |
|
104 // Destructor. |
|
105 // ----------------------------------------------------------------------------- |
|
106 // |
|
107 CCRSock::~CCRSock() |
|
108 { |
|
109 LOG( "CCRSock::~CCRSock()" ); |
|
110 |
|
111 CleanUp(); |
|
112 delete iReader; |
|
113 delete iSentDataBuf; |
|
114 delete iReceivedDataBuf; |
|
115 } |
|
116 |
|
117 |
|
118 // ----------------------------------------------------------------------------- |
|
119 // CCRSock::RunL |
|
120 // "Brain" |
|
121 // ----------------------------------------------------------------------------- |
|
122 // |
|
123 void CCRSock::RunL() |
|
124 { |
|
125 TInt err( KErrNone ); |
|
126 |
|
127 if ( iStatus == KErrEof && iWasListening ) |
|
128 { |
|
129 iSocket.Close(); |
|
130 err = iSocket.Open( iSockServer ); |
|
131 if ( err == KErrNone ) |
|
132 { |
|
133 LOG1( "CCRSock::RunL(), reopening sock: %d for listen", iSockId ); |
|
134 iIsiSocketOpen = ETrue; |
|
135 iListenSocket.Accept( iSocket, iStatus ); |
|
136 iSockStatus = CCRSock::EListening; |
|
137 SetActive(); |
|
138 } |
|
139 else |
|
140 { |
|
141 LOG2( "CCRSock::RunL(), iSocket.Open FAILED id: %d err: %d", |
|
142 iSockId, err ); |
|
143 } |
|
144 } |
|
145 else |
|
146 { |
|
147 switch ( iSockStatus ) |
|
148 { |
|
149 case EResolving: // in connection, this is usually 1st time to come to RunL |
|
150 if ( iStatus == KErrNone ) |
|
151 { // host name found |
|
152 iHostAddress().iAddr.SetPort( iPort ); |
|
153 iSocket.Close(); |
|
154 err = iSocket.Open( iSockServer, |
|
155 KAfInet, |
|
156 iProtoTCP ? KSockStream : KSockDatagram, |
|
157 iProtoTCP ? KProtocolInetTcp : KProtocolInetUdp, |
|
158 iConnection ) ; |
|
159 if ( err ) |
|
160 { |
|
161 iSockStatus = CCRSock::EFailed; |
|
162 iObserver.SockStatusChange( iSockId, iSockStatus, err ); |
|
163 iResolver.Close(); |
|
164 LOG2( "CCRSock::RunL(), iSockId: %d, err: %d", iSockId, err ); |
|
165 } |
|
166 else |
|
167 { |
|
168 iIsiSocketOpen = ETrue; |
|
169 if ( iLocalPort > 0 ) |
|
170 { |
|
171 TInetAddr bindAddr( KInetAddrAny, iLocalPort ); |
|
172 err = iSocket.Bind( bindAddr ); |
|
173 if ( err != KErrNone ) |
|
174 { |
|
175 LOG2( "CCRSock::ConnectSock(), Bind FAILED, Id: %d, err %d", iSockId, err ); |
|
176 } |
|
177 } |
|
178 |
|
179 LOG2( "CCRSock::RunL(), iSockId: %d, port: %d", |
|
180 iSockId, iHostAddress().iAddr.Port() ); |
|
181 iSocket.Connect( iHostAddress().iAddr, iStatus ); |
|
182 iToAddr = iHostAddress().iAddr; |
|
183 err = iSocket.SetOpt( KSOBlockingIO, KSOLSocket); |
|
184 if ( err != KErrNone ) |
|
185 { |
|
186 LOG1( "CCRSock::RunL(), iSocket.SetOpt FAILED: %d", err ); |
|
187 } |
|
188 iSockStatus = CCRSock::EConnecting; |
|
189 iObserver.SockStatusChange( iSockId, iSockStatus, err ); |
|
190 SetActive(); |
|
191 iResolver.Close(); |
|
192 } |
|
193 } |
|
194 else |
|
195 { // resolving not ok |
|
196 iSockStatus = CCRSock::EFailed; |
|
197 iObserver.SockStatusChange( iSockId, iSockStatus, iStatus.Int() ); |
|
198 iResolver.Close(); |
|
199 } |
|
200 break; |
|
201 |
|
202 case EConnecting: |
|
203 if ( iStatus == KErrNone ) // success |
|
204 { |
|
205 iSockStatus = CCRSock::EIdle; |
|
206 // next action is up to user, don't do SetActive here. |
|
207 LOG1( "CCRSock::RunL(), iSockId: %d", iSockId ); |
|
208 |
|
209 if ( iIssueRead && iReader && ( !iReader->IsActive() ) ) |
|
210 { |
|
211 iReader->IssueRead(); |
|
212 } |
|
213 } |
|
214 else |
|
215 { |
|
216 iSockStatus = CCRSock::EFailed; |
|
217 iObserver.SockStatusChange( iSockId, iSockStatus, iStatus.Int() ); |
|
218 CleanUp(); /* close everything */ |
|
219 } |
|
220 iObserver.SockStatusChange( iSockId, iSockStatus, iStatus.Int() ); |
|
221 break; |
|
222 |
|
223 case ESending: |
|
224 // send has been finished,somehow: |
|
225 if ( iStatus == KErrNone ) // success |
|
226 { |
|
227 #if defined ( LIVE_TV_FILE_TRACE ) || defined ( LIVE_TV_RDEBUG_TRACE ) |
|
228 sendBytes += iSentDataLen(); |
|
229 sendCount ++; |
|
230 if ( ( sendCount % 50 ) == 0 ) |
|
231 { |
|
232 LOG3( "CCRSock::RunL(), sendCount: %d, sendBytes: %d, iSockId: %d", |
|
233 sendCount, sendBytes, iSockId ); |
|
234 } |
|
235 #endif |
|
236 iSockStatus = CCRSock::EIdle; |
|
237 // next action is up to user, don't do SetActive here. |
|
238 } |
|
239 else |
|
240 { |
|
241 iSockStatus = CCRSock::EFailed; |
|
242 CleanUp(); /* close everything */ |
|
243 } |
|
244 iObserver.SockStatusChange( iSockId, iSockStatus, iStatus.Int() ); |
|
245 break; |
|
246 |
|
247 case EListening: |
|
248 if ( iStatus == KErrNone ) // success, da zocket is open |
|
249 { |
|
250 iSockStatus = CCRSock::EIdle; |
|
251 if ( iIssueRead && iReader && ( !iReader->IsActive() ) ) |
|
252 { |
|
253 iReader->IssueRead(); |
|
254 } |
|
255 } |
|
256 else |
|
257 { |
|
258 iSockStatus = CCRSock::EFailed; |
|
259 CleanUp(); /* close everything */ |
|
260 } |
|
261 iObserver.SockStatusChange( iSockId, iSockStatus, iStatus.Int() ); |
|
262 break; |
|
263 |
|
264 default: |
|
265 __ASSERT_DEBUG( 1==2, User::Panic( _L("CRRTP"), KErrArgument) ); |
|
266 break; /* this should not happend? */ |
|
267 } |
|
268 } |
|
269 } |
|
270 |
|
271 // ----------------------------------------------------------------------------- |
|
272 // CCRSock::DoCancel |
|
273 // Cancels pending actions |
|
274 // ----------------------------------------------------------------------------- |
|
275 // |
|
276 void CCRSock::DoCancel() |
|
277 { |
|
278 LOG( "CCRSock::DoCancel() in" ); |
|
279 if ( iIsiSocketOpen ) |
|
280 { |
|
281 iSocket.CancelAll(); |
|
282 } |
|
283 if ( iIsiListenSocketOpen ) |
|
284 { |
|
285 iListenSocket.CancelAll(); |
|
286 } |
|
287 |
|
288 LOG( "CCRSock::DoCancel() out" ); |
|
289 } |
|
290 |
|
291 // ----------------------------------------------------------------------------- |
|
292 // CCRSock::CopySendData |
|
293 // Handles send buffer size. |
|
294 // ----------------------------------------------------------------------------- |
|
295 // |
|
296 void CCRSock::CopySendData( const TDesC8& aData ) |
|
297 { |
|
298 if ( aData.Length() > iSentData.MaxLength() ) |
|
299 { |
|
300 // Alloc more than 8k |
|
301 delete iSentDataBuf; iSentDataBuf = NULL; |
|
302 iSentDataBuf = HBufC8::New( aData.Length() ); |
|
303 iSentData.Set( iSentDataBuf->Des() ); |
|
304 } |
|
305 else |
|
306 { |
|
307 if ( iSentData.MaxLength() > KMaxDataSize && |
|
308 aData.Length() <= KMaxDataSize ) |
|
309 { |
|
310 // Back to 8k if not more needed |
|
311 delete iSentDataBuf; iSentDataBuf = NULL; |
|
312 iSentDataBuf = HBufC8::New( KMaxDataSize ); |
|
313 iSentData.Set( iSentDataBuf->Des() ); |
|
314 } |
|
315 } |
|
316 |
|
317 iSentData.Copy( aData ); |
|
318 } |
|
319 |
|
320 // ----------------------------------------------------------------------------- |
|
321 // CCRSock::CleanUp |
|
322 // Performs cleanup |
|
323 // ----------------------------------------------------------------------------- |
|
324 // |
|
325 void CCRSock::CleanUp() |
|
326 { |
|
327 LOG( "CCRSock::CleanUp() in" ); |
|
328 Cancel(); |
|
329 |
|
330 iResolver.Close(); |
|
331 iSocket.Close(); |
|
332 iListenSocket.Close(); |
|
333 iSockStatus = CCRSock::EInitNeeded; |
|
334 |
|
335 iIsiSocketOpen = EFalse; |
|
336 iIsiListenSocketOpen = EFalse; |
|
337 LOG( "CCRSock::CleanUp() out" ); |
|
338 } |
|
339 |
|
340 // ----------------------------------------------------------------------------- |
|
341 // CCRSock::RunError |
|
342 // Q: Is anything wrong |
|
343 // A: Thanks for asking. About everything. |
|
344 // ----------------------------------------------------------------------------- |
|
345 // |
|
346 TInt CCRSock::RunError( TInt aError ) |
|
347 { |
|
348 LOG1( "CCRSock::RunError(), aError: %d", aError ); |
|
349 ( void )aError; // Prevent compiler warning |
|
350 |
|
351 return KErrNone; |
|
352 } |
|
353 |
|
354 // ----------------------------------------------------------------------------- |
|
355 // CCRSock::ConnectSock |
|
356 // Initiates connection to remote addr. |
|
357 // ----------------------------------------------------------------------------- |
|
358 // |
|
359 TInt CCRSock::ConnectSock( |
|
360 const TDesC& aAddr, |
|
361 TUint aPort, |
|
362 TInt aLocalPort ) |
|
363 { |
|
364 LOG( "CCRSock::ConnectSock()" ); |
|
365 |
|
366 TInt retval( KErrNone ); |
|
367 if ( IsActive() ) |
|
368 { |
|
369 retval = KErrInUse; |
|
370 } |
|
371 else |
|
372 { |
|
373 iWasListening = EFalse; |
|
374 iPort = aPort; |
|
375 iLocalPort = aLocalPort; |
|
376 if ( aAddr.Compare( KCRSockLocalhost() ) != 0 ) |
|
377 { |
|
378 iResolver.Close(); |
|
379 if ( (retval = iResolver.Open( iSockServer, KAfInet, |
|
380 KProtocolInetTcp, iConnection) ) == KErrNone ) |
|
381 { |
|
382 iResolver.GetByName( aAddr, iHostAddress, iStatus ); |
|
383 iSockStatus = CCRSock::EResolving; |
|
384 SetActive(); |
|
385 } |
|
386 else |
|
387 { |
|
388 LOG2( "CCRSock::ConnectSock(), Resolver.Open id: %d, err: %d", |
|
389 iSockId, retval ); |
|
390 iSockStatus = CCRSock::EFailed; |
|
391 } |
|
392 } |
|
393 else |
|
394 { // localhost, no need to resolve |
|
395 iHostAddress().iAddr.SetPort( iPort ); |
|
396 iSocket.Close(); |
|
397 retval = iSocket.Open( iSockServer, |
|
398 KAfInet, |
|
399 iProtoTCP ? KSockStream : KSockDatagram, |
|
400 iProtoTCP ? KProtocolInetTcp : KProtocolInetUdp, |
|
401 iConnection ) ; |
|
402 if ( retval ) |
|
403 { |
|
404 LOG2( "CCRSock::ConnectSock(), Socket.Open id: %d, err: %d", |
|
405 iSockId, retval ); |
|
406 iSockStatus = CCRSock::EFailed; |
|
407 } |
|
408 else |
|
409 { |
|
410 iIsiSocketOpen = ETrue; |
|
411 iSockStatus = CCRSock::EConnecting; |
|
412 if ( aLocalPort > 0 ) |
|
413 { |
|
414 TInetAddr bindAddr( KInetAddrAny, aLocalPort ); |
|
415 TInt err( iSocket.Bind( bindAddr ) ); |
|
416 if ( err != KErrNone ) |
|
417 { |
|
418 LOG2( "CCRSock::ConnectSock(), Bind FAILED iSockId: %d, err: %d", |
|
419 iSockId, err ); |
|
420 } |
|
421 } |
|
422 iToAddr = TInetAddr( KInetAddrLoop, aPort ); |
|
423 LOG2( "CCRSock::ConnectSock(), iSockId %d port %d", |
|
424 iSockId, aPort ); |
|
425 iSocket.Connect( iToAddr, iStatus ); |
|
426 SetActive(); |
|
427 if ( iProtoTCP ) |
|
428 { |
|
429 retval = iSocket.SetOpt( KSOBlockingIO, KSOLSocket ); |
|
430 } |
|
431 iObserver.SockStatusChange( iSockId, iSockStatus, retval ); |
|
432 } |
|
433 } |
|
434 } |
|
435 |
|
436 LOG1( "CCRSock::ConnectSock(), retVal: %d", retval ); |
|
437 return retval; |
|
438 } |
|
439 |
|
440 // ----------------------------------------------------------------------------- |
|
441 // CCRSock::ConnectSock |
|
442 // Initiates connection to remote addr without resolving. |
|
443 // ----------------------------------------------------------------------------- |
|
444 // |
|
445 TInt CCRSock::ConnectSock( |
|
446 const TSockAddr& aAddr, |
|
447 TInt aLocalPort ) |
|
448 { |
|
449 LOG( "CCRSock::ConnectSock(), no dns" ); |
|
450 |
|
451 TInt retval( KErrNone ); |
|
452 if ( IsActive() ) |
|
453 { |
|
454 retval = KErrInUse; |
|
455 } |
|
456 else |
|
457 { |
|
458 iWasListening = EFalse; |
|
459 iPort = aAddr.Port(); |
|
460 iLocalPort = aLocalPort; |
|
461 iHostAddress().iAddr = aAddr; |
|
462 iSocket.Close(); |
|
463 retval = iSocket.Open( iSockServer, |
|
464 KAfInet, |
|
465 iProtoTCP ? KSockStream : KSockDatagram, |
|
466 iProtoTCP ? KProtocolInetTcp : KProtocolInetUdp, |
|
467 iConnection ) ; |
|
468 if ( retval ) |
|
469 { |
|
470 LOG2( "CCRSock::ConnectSock(), Socket.Open id: %d, err: %d", |
|
471 iSockId, retval ); |
|
472 iSockStatus = CCRSock::EFailed; |
|
473 } |
|
474 else |
|
475 { |
|
476 iIsiSocketOpen = ETrue; |
|
477 iSockStatus = CCRSock::EConnecting; |
|
478 if ( aLocalPort > 0 ) |
|
479 { |
|
480 TInetAddr bindAddr( KInetAddrAny, aLocalPort ); |
|
481 TInt err( iSocket.Bind( bindAddr ) ); |
|
482 if ( err != KErrNone ) |
|
483 { |
|
484 LOG2( "CCRSock::ConnectSock(), Bind FAILED id: %d err: %d", |
|
485 iSockId, err ); |
|
486 } |
|
487 } |
|
488 iToAddr = aAddr; |
|
489 LOG2( "CCRSock::ConnectSock(), id: %d, port: %d", iSockId, iPort ); |
|
490 iSocket.Connect( iToAddr, iStatus ); |
|
491 SetActive(); |
|
492 if ( iProtoTCP ) |
|
493 { |
|
494 retval = iSocket.SetOpt( KSOBlockingIO, KSOLSocket ); |
|
495 } |
|
496 iObserver.SockStatusChange( iSockId, iSockStatus, retval ); |
|
497 } |
|
498 } |
|
499 |
|
500 LOG1( "CCRSock::ConnectSock(), retVal: %d", retval ); |
|
501 return retval; |
|
502 } |
|
503 |
|
504 // ----------------------------------------------------------------------------- |
|
505 // CCRSock::ListenPort |
|
506 // Starts listening to port. Synchronous. |
|
507 // ----------------------------------------------------------------------------- |
|
508 // |
|
509 TInt CCRSock::ListenPort( TUint aPort ) |
|
510 { |
|
511 LOG1( "CCRSock::ListenPort(), aPort: %d", aPort ); |
|
512 |
|
513 TInt retval( KErrNone ); |
|
514 if ( IsActive() ) |
|
515 { |
|
516 return KErrInUse; |
|
517 } |
|
518 if ( iSockStatus != CCRSock::EInitNeeded ) |
|
519 { |
|
520 return KErrNotReady; |
|
521 } |
|
522 |
|
523 iHostAddress().iAddr.SetPort( iPort ); |
|
524 iWasListening = ETrue; |
|
525 if ( iProtoTCP ) |
|
526 { |
|
527 iListenSocket.Close(); |
|
528 if ( ( retval = iListenSocket.Open( iSockServer, KAfInet, |
|
529 KSockStream, KProtocolInetTcp, iConnection ) ) == KErrNone ) |
|
530 { |
|
531 iIsiListenSocketOpen = ETrue; |
|
532 TInetAddr listenAddr( KInetAddrAny, aPort ); |
|
533 LOG2( "CCRSock::ListenPort(), id: %d, port: %d", iSockId,(TInt)aPort); |
|
534 retval = iListenSocket.Bind( listenAddr ); |
|
535 if ( retval == KErrNone ) |
|
536 { |
|
537 retval = iListenSocket.Listen( 5 ); |
|
538 if ( retval == KErrNone ) |
|
539 { |
|
540 iSocket.Close(); |
|
541 retval = iSocket.Open( iSockServer ); |
|
542 if ( retval == KErrNone ) |
|
543 { |
|
544 iIsiSocketOpen = ETrue; |
|
545 iListenSocket.Accept( iSocket, iStatus ); |
|
546 iSockStatus = CCRSock::EListening; |
|
547 SetActive(); |
|
548 } |
|
549 else |
|
550 { |
|
551 LOG1( "CCRSock::ListenPort(), iSocket.Open FAILED retval: %d", retval ); |
|
552 } |
|
553 } |
|
554 else |
|
555 { |
|
556 LOG1( "CCRSock::ListenPort(), iListenSock.Listen FAILED retval: %d", retval ); |
|
557 } |
|
558 } |
|
559 else |
|
560 { |
|
561 LOG2( "CCRSock::ListenPort() iListenSocket.Bind FAILED Id: %d, retval: %d", iSockId, retval); |
|
562 } |
|
563 } |
|
564 else |
|
565 { |
|
566 LOG2( "ListenSocket.Open id: %d, err: %d", iSockId, retval ); |
|
567 } |
|
568 } |
|
569 else |
|
570 { |
|
571 // for UDP things are different: just open, bind |
|
572 iSocket.Close(); |
|
573 if ( ( retval = iSocket.Open( iSockServer, |
|
574 KAfInet, |
|
575 KSockDatagram, |
|
576 KProtocolInetUdp, |
|
577 iConnection ) ) != KErrNone ) |
|
578 { |
|
579 iSockStatus = CCRSock::EFailed; |
|
580 LOG2( "CCRSock::ListenPort(), UDPSocket.Open id: %d, err: %d", iSockId, retval ); |
|
581 } |
|
582 else |
|
583 { |
|
584 TInetAddr listenAddr( KInetAddrAny, aPort ); |
|
585 retval = iSocket.Bind( listenAddr ); |
|
586 if ( retval == KErrNone ) |
|
587 { |
|
588 LOG2( "CCRSock::ListenPort(), udp: %d ok, id: %d", aPort,iSockId ); |
|
589 iSockStatus = CCRSock::EIdle; |
|
590 iIsiSocketOpen = ETrue; |
|
591 } |
|
592 else |
|
593 { |
|
594 LOG2( "CCRSock::ListenPort(), UDPSocket.Bind FAILED id: %d, retval: %d", iSockId, retval ); |
|
595 iSockStatus = CCRSock::EFailed; |
|
596 } |
|
597 if ( iIssueRead && iReader && ( !iReader->IsActive() ) ) |
|
598 { |
|
599 iReader->IssueRead(); |
|
600 } |
|
601 } |
|
602 } |
|
603 |
|
604 LOG1( "CCRSock::ListenPort(), retval: %d", retval ); |
|
605 return retval; |
|
606 } |
|
607 |
|
608 // ----------------------------------------------------------------------------- |
|
609 // CCRSock::JoinGroup |
|
610 // Joins a multicast group. Synchronous. |
|
611 // ----------------------------------------------------------------------------- |
|
612 // |
|
613 TInt CCRSock::JoinGroup( const TInetAddr& aGroupAddr ) |
|
614 { |
|
615 LOG( "CCRSock::JoinGroup()" ); |
|
616 |
|
617 TPckgBuf<TIp6Mreq> request; |
|
618 request().iAddr = aGroupAddr.Ip6Address(); |
|
619 request().iInterface = 0; |
|
620 return iSocket.SetOpt( KSoIp6JoinGroup, KSolInetIp, request ); |
|
621 } |
|
622 |
|
623 // ----------------------------------------------------------------------------- |
|
624 // CCRSock::SendData |
|
625 // Initiates async data sending |
|
626 // ----------------------------------------------------------------------------- |
|
627 // |
|
628 void CCRSock::SendData( const TDesC8& aDataThatIsSentOverSocket ) |
|
629 { |
|
630 #if defined ( LIVE_TV_FILE_TRACE ) || defined ( LIVE_TV_RDEBUG_TRACE ) |
|
631 if ( iProtoTCP && aDataThatIsSentOverSocket.Length() && |
|
632 aDataThatIsSentOverSocket[0] != ( TUint8 )( '$' ) ) |
|
633 { |
|
634 LOG2("CCRSock::SendData(), id: %d, len: %d", |
|
635 iSockId, aDataThatIsSentOverSocket.Length() ); |
|
636 TChar c; |
|
637 TName d; |
|
638 for ( TInt i( 0 ); i < aDataThatIsSentOverSocket.Length(); i++ ) |
|
639 { |
|
640 c = aDataThatIsSentOverSocket[i]; |
|
641 d.Append( c ); |
|
642 if ( ( i > 0 ) && ( i % 80 ) == 0 ) |
|
643 { |
|
644 LOG1( ">%S<", &d ); |
|
645 d.Zero(); |
|
646 } |
|
647 } |
|
648 |
|
649 LOG1( ">%S<", &d ); |
|
650 } |
|
651 #endif |
|
652 |
|
653 // Data to socket |
|
654 if ( !IsActive() ) |
|
655 { |
|
656 CopySendData( aDataThatIsSentOverSocket ); |
|
657 if ( iProtoTCP ) |
|
658 { |
|
659 iSocket.Write( iSentData, iStatus ); |
|
660 iSockStatus = CCRSock::ESending; |
|
661 SetActive(); |
|
662 } |
|
663 else |
|
664 { |
|
665 if ( iToAddr.Port() != 0 ) |
|
666 { |
|
667 iSocket.SendTo( iSentData, iToAddr, 0, iStatus, iSentDataLen ); |
|
668 iSockStatus = CCRSock::ESending; |
|
669 SetActive(); |
|
670 } |
|
671 else |
|
672 { |
|
673 LOG1( "CCRSock::SendData(), Discarding send, id: %d" ,iSockId ); |
|
674 } |
|
675 } |
|
676 } |
|
677 else |
|
678 { |
|
679 LOG2( "CCRSock::SendData(), id: %d, Already active, Dumped packet, len: %d" , |
|
680 iSockId, aDataThatIsSentOverSocket.Length() ); |
|
681 } |
|
682 } |
|
683 |
|
684 // ----------------------------------------------------------------------------- |
|
685 // CCRSock::SockStatus |
|
686 // returns status |
|
687 // ----------------------------------------------------------------------------- |
|
688 // |
|
689 CCRSock::TCRSockStatus CCRSock::SockStatus() const |
|
690 { |
|
691 return iSockStatus; |
|
692 } |
|
693 |
|
694 // ----------------------------------------------------------------------------- |
|
695 // CCRSock::ConnectedAddr |
|
696 // returns endpoint addr of this sock |
|
697 // ----------------------------------------------------------------------------- |
|
698 // |
|
699 TInetAddr CCRSock::ConnectedAddr( void ) |
|
700 { |
|
701 TInetAddr addr; |
|
702 iSocket.RemoteName( addr ); |
|
703 return addr; |
|
704 } |
|
705 |
|
706 // ----------------------------------------------------------------------------- |
|
707 // CCRSock::LocalAddr |
|
708 // returns local addr of this sock |
|
709 // ----------------------------------------------------------------------------- |
|
710 // |
|
711 TInetAddr CCRSock::LocalAddr( void ) |
|
712 { |
|
713 TInetAddr addr; |
|
714 iSocket.LocalName( addr ); |
|
715 return addr; |
|
716 } |
|
717 |
|
718 // ----------------------------------------------------------------------------- |
|
719 // CCRSock::SetToAddr |
|
720 // sets "to" addr of this sock |
|
721 // ----------------------------------------------------------------------------- |
|
722 // |
|
723 void CCRSock::SetToAddr( const TInetAddr &aAddr ) |
|
724 { |
|
725 LOG( "CCRSock::SetToAddr() in" ); |
|
726 iToAddr = aAddr; |
|
727 |
|
728 #if defined ( LIVE_TV_FILE_TRACE ) || defined ( LIVE_TV_RDEBUG_TRACE ) |
|
729 TName an_addr; |
|
730 iToAddr.Output( an_addr ); |
|
731 LOG3( "CCRSock::SetToAddr(), id: %d, addr: %S, port: %d", iSockId, &an_addr, aAddr.Port() ); |
|
732 #endif |
|
733 |
|
734 } |
|
735 |
|
736 // ----------------------------------------------------------------------------- |
|
737 // CCRSock::Socket |
|
738 // ----------------------------------------------------------------------------- |
|
739 RSocket& CCRSock::Socket() |
|
740 { |
|
741 return iSocket; |
|
742 } |
|
743 |
|
744 // ----------------------------------------------------------------------------- |
|
745 // ----------- here begins implementation of "SockReader" helper class---------- |
|
746 // ----------------------------------------------------------------------------- |
|
747 // ----------------------------------------------------------------------------- |
|
748 // |
|
749 |
|
750 // ----------------------------------------------------------------------------- |
|
751 // CCRSockReader::NewL |
|
752 // Construction startpoint |
|
753 // ----------------------------------------------------------------------------- |
|
754 // |
|
755 CCRSockReader* CCRSockReader::NewL( |
|
756 CCRSock& aSock, |
|
757 RConnection& aConnection, RSocketServ& aSockServer ) |
|
758 { |
|
759 CCRSockReader* self = new ( ELeave ) CCRSockReader( |
|
760 aSock, aConnection, aSockServer ); |
|
761 CleanupStack::PushL( self ); |
|
762 self->ConstructL(); |
|
763 CleanupStack::Pop( self ); |
|
764 return self; |
|
765 } |
|
766 // ----------------------------------------------------------------------------- |
|
767 // CCRSockReader::CCRSockReader |
|
768 // Default constructor |
|
769 // ----------------------------------------------------------------------------- |
|
770 // |
|
771 CCRSockReader::CCRSockReader( |
|
772 CCRSock& aSock, RConnection& aConnection, |
|
773 RSocketServ& aSockServer ) |
|
774 : CActive( EPriorityStandard ), |
|
775 iSock( aSock ), |
|
776 iConnection( aConnection ), |
|
777 iSockServer( aSockServer ) |
|
778 { |
|
779 } |
|
780 // ----------------------------------------------------------------------------- |
|
781 // CCRSockReader::ConstructL |
|
782 // Actual constructor |
|
783 // ----------------------------------------------------------------------------- |
|
784 // |
|
785 void CCRSockReader::ConstructL() |
|
786 { |
|
787 LOG( "CCRSockReader::ConstructL()" ); |
|
788 |
|
789 // Add self to active scheduler |
|
790 CActiveScheduler::Add( this ); |
|
791 } |
|
792 |
|
793 // ----------------------------------------------------------------------------- |
|
794 // CCRSock::~CCRSockReader |
|
795 // Destructor |
|
796 // ----------------------------------------------------------------------------- |
|
797 // |
|
798 CCRSockReader::~CCRSockReader() |
|
799 { |
|
800 LOG( "CCRSockReader::~CCRSockReader()" ); |
|
801 Cancel(); |
|
802 } |
|
803 |
|
804 // ----------------------------------------------------------------------------- |
|
805 // CCRSockReader::RunL |
|
806 // Work-horse |
|
807 // ----------------------------------------------------------------------------- |
|
808 // |
|
809 void CCRSockReader::RunL() |
|
810 { |
|
811 #if defined ( LIVE_TV_FILE_TRACE ) || defined ( LIVE_TV_RDEBUG_TRACE ) |
|
812 if ( iSock.iProtoTCP && iStatus.Int() != KErrNone ) |
|
813 { |
|
814 LOG2( "CCRSockReader::RunL(), id: %d, status: %d", iSock.iSockId, iStatus.Int() ); |
|
815 } |
|
816 #endif |
|
817 |
|
818 switch ( iStatus.Int() ) |
|
819 { |
|
820 case KErrNone: |
|
821 { |
|
822 #if defined( LIVE_TV_FILE_TRACE ) || defined( LIVE_TV_RDEBUG_TRACE ) |
|
823 if ( !iSock.iProtoTCP ) |
|
824 { |
|
825 recvBytes += iSock.iReceivedData.Length(); |
|
826 recvCount ++; |
|
827 if ( ( recvCount % 50 ) == 0 ) |
|
828 { |
|
829 LOG3( "CCRSockReader::RunL(), recvCount: %d, recvBytes: %d, id: %d", |
|
830 recvCount, recvBytes, iSock.iSockId ); |
|
831 TName an_addr; |
|
832 iSock.iFromAddr.Output( an_addr ); |
|
833 TInt a_byte2 = iSock.iReceivedData[2]; |
|
834 TInt a_byte3 = iSock.iReceivedData[3]; |
|
835 LOG3( "CCRSockReader::RunL(), Addr %S, port: %d, last seq: %d", |
|
836 &an_addr, iSock.iFromAddr.Port(), ( a_byte2 * 255 ) + a_byte3 ); |
|
837 } |
|
838 } |
|
839 #endif // LIVE_TV_FILE_TRACE || LIVE_TV_RDEBUG_TRACE |
|
840 |
|
841 iSock.iObserver.DataReceived( iSock.iSockId, iSock.iReceivedData ); |
|
842 IssueRead(); |
|
843 } |
|
844 break; |
|
845 |
|
846 default: // error cases |
|
847 { |
|
848 LOG2( "CCRSockReader::RunL(), id: %d, status: %d", iSock.iSockId, iStatus.Int() ); |
|
849 iSock.iSockStatus = CCRSock::EFailed; |
|
850 iSock.iObserver.SockStatusChange( |
|
851 iSock.iSockId, iSock.iSockStatus, iStatus.Int() ); |
|
852 } |
|
853 break; |
|
854 } |
|
855 } |
|
856 |
|
857 // ----------------------------------------------------------------------------- |
|
858 // CCRSockReader::IssueRead |
|
859 // Asks for more data |
|
860 // ----------------------------------------------------------------------------- |
|
861 // |
|
862 void CCRSockReader::IssueRead() |
|
863 { |
|
864 if ( IsActive() ) |
|
865 { |
|
866 LOG( "CCRSockReader::IssueRead(), IsActive! return" ); |
|
867 return; |
|
868 } |
|
869 |
|
870 iSock.iReceivedData.Zero(); |
|
871 if ( iSock.iProtoTCP ) |
|
872 { |
|
873 iSock.iSocket.RecvOneOrMore( iSock.iReceivedData, 0, iStatus, |
|
874 iSock.iReceivedDataLen ); |
|
875 } |
|
876 else |
|
877 { |
|
878 iSock.iSocket.RecvFrom( iSock.iReceivedData, iSock.iFromAddr, 0, iStatus ); |
|
879 } |
|
880 |
|
881 SetActive(); |
|
882 } |
|
883 |
|
884 // ----------------------------------------------------------------------------- |
|
885 // CCRSockReader::DoCancel |
|
886 // Cancels outstanding operations |
|
887 // ----------------------------------------------------------------------------- |
|
888 // |
|
889 void CCRSockReader::DoCancel() |
|
890 { |
|
891 LOG( "CCRSockReader::DoCancel()" ); |
|
892 // CCRSock::DoCancel() has already called CancelAll to socket so no need to do it here |
|
893 } |
|
894 // ----------------------------------------------------------------------------- |
|
895 // CCRSockReader::RunError |
|
896 // If anything goes wrong |
|
897 // ----------------------------------------------------------------------------- |
|
898 // |
|
899 TInt CCRSockReader::RunError( TInt aError ) |
|
900 { |
|
901 LOG1( "CCRSockReader::RunError(), aError: %d, return KErrNone", aError ); |
|
902 ( void )aError; // Prevent compiler warning |
|
903 |
|
904 return KErrNone; |
|
905 } |
|
906 |
|
907 // ----------------------------------------------------------------------------- |
|
908 // ----------- here ends implementation of "SockReader" helper class---------- |
|
909 // ----------------------------------------------------------------------------- |
|
910 // ----------------------------------------------------------------------------- |
|
911 // |
|
912 // End of File |
|
913 |