|
1 // reader.cpp |
|
2 // |
|
3 // Copyright (c) 2008 - 2010 Accenture. All rights reserved. |
|
4 // This component and the accompanying materials are made available |
|
5 // under the terms of the "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 // Accenture - Initial contribution |
|
11 // |
|
12 |
|
13 #include "reader.h" |
|
14 #include "panic.h" |
|
15 |
|
16 |
|
17 CReader* CReader::NewL(RSocket& aSocket, MReaderObserver& aObserver) |
|
18 { |
|
19 return new(ELeave) CReader(aSocket, aObserver); |
|
20 } |
|
21 |
|
22 CReader::~CReader() |
|
23 { |
|
24 Cancel(); |
|
25 iBuffer.Close(); |
|
26 } |
|
27 |
|
28 void CReader::StartRead(TInt aLength, TReadMode aMode) |
|
29 { |
|
30 __ASSERT_DEBUG(!IsActive() && (iPendingLength == 0), Panic(EPanicReadAlreadyPending)); |
|
31 |
|
32 if (iBuffer.MaxLength() < aLength) |
|
33 { |
|
34 TInt err = iBuffer.ReAlloc(aLength); |
|
35 if (err) |
|
36 { |
|
37 iObserver.ReadError(err); |
|
38 return; |
|
39 } |
|
40 } |
|
41 |
|
42 if (Connected()) |
|
43 { |
|
44 iBuffer.Zero(); |
|
45 |
|
46 switch (aMode) |
|
47 { |
|
48 case EFull: |
|
49 { |
|
50 iSocket.Recv(iBuffer, 0, iStatus); |
|
51 break; |
|
52 } |
|
53 case EOneOrMore: |
|
54 { |
|
55 iSocket.RecvOneOrMore(iBuffer, 0, iStatus, iXfrLength); |
|
56 break; |
|
57 } |
|
58 default: |
|
59 { |
|
60 __ASSERT_DEBUG(EFalse, Panic(EPanicInvalidReadMode)); |
|
61 } |
|
62 }; |
|
63 |
|
64 SetActive(); |
|
65 } |
|
66 else |
|
67 { |
|
68 iPendingLength = aLength; |
|
69 iPendingMode = aMode; |
|
70 } |
|
71 } |
|
72 |
|
73 void CReader::GetBufferLength(TInt& aLength) |
|
74 { |
|
75 aLength = iBuffer.Length(); |
|
76 } |
|
77 |
|
78 void CReader::Abort() |
|
79 { |
|
80 iPendingLength = 0; |
|
81 Cancel(); |
|
82 } |
|
83 |
|
84 void CReader::HandleSocketConnected() |
|
85 { |
|
86 if (iPendingLength > 0) |
|
87 { |
|
88 TInt pendingLength = iPendingLength; |
|
89 iPendingLength = 0; |
|
90 StartRead(pendingLength, iPendingMode); |
|
91 } |
|
92 } |
|
93 |
|
94 CReader::CReader(RSocket& aSocket, MReaderObserver& aObserver) |
|
95 : CSocketUser(aSocket), iObserver(aObserver) |
|
96 { |
|
97 CActiveScheduler::Add(this); |
|
98 } |
|
99 |
|
100 void CReader::RunL() |
|
101 { |
|
102 if (iStatus.Int()) |
|
103 { |
|
104 iObserver.ReadError(iStatus.Int()); |
|
105 } |
|
106 else |
|
107 { |
|
108 iObserver.ReadComplete(iBuffer); |
|
109 } |
|
110 } |
|
111 |
|
112 void CReader::DoCancel() |
|
113 { |
|
114 iSocket.CancelRecv(); |
|
115 } |
|
116 |