1 /* |
|
2 * Copyright (c) 2009 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: Writes to socket. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 // INCLUDE FILES |
|
20 #include "socketswriter.h" |
|
21 #include "timeouttimer.h" |
|
22 #include "BtSerialEngine.pan" |
|
23 #include "SocketObserver.h" |
|
24 #include "HtiBtEngineLogging.h" |
|
25 |
|
26 // STATIC MEMBER INITIALISATIONS |
|
27 const TInt KTimeOut = 10 * 1000 * 1000; // 10 seconds time-out |
|
28 |
|
29 // ========================= MEMBER FUNCTIONS ================================== |
|
30 |
|
31 // ----------------------------------------------------------------------------- |
|
32 CSocketsWriter* CSocketsWriter::NewL( MSocketObserver& aEngineNotifier, |
|
33 RSocket& aSocket ) |
|
34 { |
|
35 CSocketsWriter* self = new ( ELeave ) CSocketsWriter( aEngineNotifier, |
|
36 aSocket ); |
|
37 CleanupStack::PushL( self ); |
|
38 self->ConstructL(); |
|
39 CleanupStack::Pop( self ); |
|
40 return self; |
|
41 } |
|
42 |
|
43 // ----------------------------------------------------------------------------- |
|
44 CSocketsWriter::CSocketsWriter( MSocketObserver& aEngineNotifier, |
|
45 RSocket& aSocket ) |
|
46 : CActive( EPriorityStandard ), |
|
47 iSocket( aSocket ), |
|
48 iObserver( aEngineNotifier ) |
|
49 { |
|
50 // No implementation required |
|
51 } |
|
52 |
|
53 // ----------------------------------------------------------------------------- |
|
54 void CSocketsWriter::ConstructL() |
|
55 { |
|
56 CActiveScheduler::Add( this ); |
|
57 iTimeOut = KTimeOut; |
|
58 iTimer = CTimeOutTimer::NewL( CActive::EPriorityUserInput, *this ); |
|
59 iWriteStatus = EIdle; |
|
60 } |
|
61 |
|
62 // ----------------------------------------------------------------------------- |
|
63 CSocketsWriter::~CSocketsWriter() |
|
64 { |
|
65 CancelSending(); |
|
66 delete iTimer; |
|
67 } |
|
68 |
|
69 // ----------------------------------------------------------------------------- |
|
70 void CSocketsWriter::DoCancel() |
|
71 { |
|
72 // Cancel asychronous write request |
|
73 iSocket.CancelWrite(); |
|
74 iTimer->Cancel(); |
|
75 iWriteStatus = EIdle; |
|
76 } |
|
77 |
|
78 // ----------------------------------------------------------------------------- |
|
79 void CSocketsWriter::RunL() |
|
80 { |
|
81 iTimer->Cancel(); |
|
82 |
|
83 // Active object request complete handler |
|
84 if ( iStatus == KErrNone ) |
|
85 { |
|
86 switch ( iWriteStatus ) |
|
87 { |
|
88 // Character has been written to socket |
|
89 case ESending: |
|
90 SendNextPacket(); |
|
91 break; |
|
92 default: |
|
93 LOGFMT_E( "CSocketsWriter: Bad write status: %d", iWriteStatus ) |
|
94 Panic( EBTSerialEngineWriteSocketBadState ); |
|
95 break; |
|
96 }; |
|
97 } |
|
98 else |
|
99 { |
|
100 iWriteStatus = EIdle; |
|
101 iWriteBuffer.Zero(); |
|
102 iTransferBuffer.Zero(); |
|
103 iObserver.ReportError( MSocketObserver::EGeneralWriteError, |
|
104 iStatus.Int() ); |
|
105 } |
|
106 } |
|
107 |
|
108 // ----------------------------------------------------------------------------- |
|
109 void CSocketsWriter::TimerExpired() |
|
110 { |
|
111 Cancel(); |
|
112 iWriteStatus = EIdle; |
|
113 iObserver.ReportError( MSocketObserver::ETimeOutOnWrite, |
|
114 KErrTimedOut ); |
|
115 } |
|
116 |
|
117 // ----------------------------------------------------------------------------- |
|
118 void CSocketsWriter::SendL(const TDesC8& aData) |
|
119 { |
|
120 if ( aData.Length() > FreeSpaceInSendBuffer() ) |
|
121 User::Leave( KErrOverflow ); |
|
122 |
|
123 iTransferBuffer.Append( aData ); |
|
124 SendNextPacket(); |
|
125 } |
|
126 |
|
127 // ----------------------------------------------------------------------------- |
|
128 TInt CSocketsWriter::FreeSpaceInSendBuffer() |
|
129 { |
|
130 return iTransferBuffer.MaxSize() - iTransferBuffer.Size(); |
|
131 } |
|
132 |
|
133 // ----------------------------------------------------------------------------- |
|
134 TInt CSocketsWriter::SendBufferMaxSize() |
|
135 { |
|
136 return iTransferBuffer.MaxLength(); |
|
137 } |
|
138 |
|
139 // ----------------------------------------------------------------------------- |
|
140 void CSocketsWriter::SendNextPacket() |
|
141 { |
|
142 if ( IsActive() ) |
|
143 { |
|
144 return; // already sending |
|
145 } |
|
146 if ( iTransferBuffer.Length() > 0 ) |
|
147 { |
|
148 // Move data from transfer buffer to actual write buffer |
|
149 iWriteBuffer.Copy(iTransferBuffer.Left(iWriteBuffer.MaxLength())); |
|
150 iTransferBuffer.Delete(0, iWriteBuffer.MaxLength()); |
|
151 iSocket.Write( iWriteBuffer, iStatus ); // Initiate actual write |
|
152 |
|
153 iWriteStatus = ESending; |
|
154 iTimer->After( iTimeOut ); |
|
155 SetActive(); |
|
156 } |
|
157 else |
|
158 { |
|
159 iWriteStatus = EIdle; |
|
160 iObserver.AllBufferedDataSent(); |
|
161 } |
|
162 } |
|
163 |
|
164 // ----------------------------------------------------------------------------- |
|
165 void CSocketsWriter::CancelSending() |
|
166 { |
|
167 Cancel(); |
|
168 iWriteBuffer.Zero(); |
|
169 iTransferBuffer.Zero(); |
|
170 } |
|
171 // End of File |
|