|
1 /* |
|
2 * Copyright (c) 2005-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: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 /******************************************************************************** |
|
21 * |
|
22 * System Includes |
|
23 * |
|
24 *******************************************************************************/ |
|
25 #include <es_sock.h> |
|
26 #include <in_sock.h> |
|
27 |
|
28 /******************************************************************************** |
|
29 * |
|
30 * Local Includes |
|
31 * |
|
32 *******************************************************************************/ |
|
33 #include "assert.h" |
|
34 #include "ntoh.h" |
|
35 #include "stat_tcpip.h" |
|
36 #include "../../../../common/inc/serialpacketsize.h" |
|
37 |
|
38 /******************************************************************************** |
|
39 * |
|
40 * Macro functions |
|
41 * |
|
42 ********************************************************************************/ |
|
43 |
|
44 /******************************************************************************** |
|
45 * |
|
46 * CStatTransportTCPIP -- Constructor |
|
47 * |
|
48 *******************************************************************************/ |
|
49 CStatTransportTCPIP *CStatTransportTCPIP::NewL( void ) |
|
50 { |
|
51 CStatTransportTCPIP *self = new (ELeave) CStatTransportTCPIP(); |
|
52 CleanupStack::PushL(self); |
|
53 self->ConstructL( ); |
|
54 CleanupStack::Pop(); |
|
55 return self; |
|
56 } |
|
57 |
|
58 CStatTransportTCPIP::CStatTransportTCPIP() : CActive(EPriorityStandard) |
|
59 { |
|
60 } |
|
61 |
|
62 void CStatTransportTCPIP::ConstructL( void ) |
|
63 { |
|
64 // add this to active scheduler |
|
65 CActiveScheduler::Add(this); |
|
66 |
|
67 // initialise all params |
|
68 |
|
69 iBufferPtr = NULL; |
|
70 iTransport = NULL; |
|
71 iTransportStatus = EIdle; |
|
72 iRWStatus = ENoRW; |
|
73 |
|
74 iMaxPacketSize = KMaxTCPIPPacketSize; |
|
75 |
|
76 iBuffer = HBufC8::New( iMaxPacketSize ); |
|
77 |
|
78 } |
|
79 |
|
80 CStatTransportTCPIP::~CStatTransportTCPIP() |
|
81 { |
|
82 // this will call cancel and remove the active object -- this will call cancel |
|
83 Deque(); |
|
84 |
|
85 // cleanup the sockets |
|
86 switch( iTransportStatus ) { |
|
87 |
|
88 case EIdle: |
|
89 case EInitialised: |
|
90 case EDisconnected: |
|
91 case EError: |
|
92 break; |
|
93 |
|
94 case EConnected: |
|
95 iDataSocket.Shutdown( RSocket::EImmediate, iStatus ); |
|
96 /* fall through */ |
|
97 |
|
98 case EConnecting: |
|
99 iDataSocket.Close(); |
|
100 iListenSocket.Shutdown( RSocket::EImmediate, iStatus ); |
|
101 iListenSocket.Close(); |
|
102 iSocketServ.Close(); |
|
103 break; |
|
104 |
|
105 case EDisconnectingData: |
|
106 case EDisconnectingListen: |
|
107 ; |
|
108 break; |
|
109 } |
|
110 |
|
111 if( iBuffer ) |
|
112 { |
|
113 delete iBuffer; |
|
114 iBuffer = NULL; |
|
115 } |
|
116 |
|
117 if( iBufferPtr ) |
|
118 { |
|
119 delete iBufferPtr; |
|
120 iBufferPtr = NULL; |
|
121 } |
|
122 } |
|
123 |
|
124 /******************************************************************************** |
|
125 * |
|
126 * CStatTransportTCPIP -- MStatApiTransport |
|
127 * |
|
128 *******************************************************************************/ |
|
129 TInt CStatTransportTCPIP::InitialiseL( MNotifyStatTransport *aTransport ) |
|
130 { |
|
131 // save the transport interface |
|
132 iTransport = aTransport; |
|
133 |
|
134 // everything here is done in connect |
|
135 iTransportStatus = EInitialised; |
|
136 return KSTErrSuccess; |
|
137 } |
|
138 |
|
139 TInt CStatTransportTCPIP::Release( void ) |
|
140 { |
|
141 // release has nothing to do |
|
142 asserte( (iTransportStatus == EDisconnected) || (iTransportStatus == EInitialised) ); |
|
143 iTransportStatus = EIdle; |
|
144 return KSTErrSuccess; |
|
145 } |
|
146 |
|
147 TInt CStatTransportTCPIP::ConnectL( TDesC* /*aRemoteHost*/ ) |
|
148 { |
|
149 // make sure we are in the correct state |
|
150 asserte( iTransportStatus == EInitialised ); |
|
151 |
|
152 // connect to the socket server, create a socket, bind, listen, accept |
|
153 User::LeaveIfError( iSocketServ.Connect() ); |
|
154 User::LeaveIfError( iListenSocket.Open(iSocketServ, KAfInet, KSockStream, KProtocolInetTcp) ); |
|
155 User::LeaveIfError( iListenSocket.SetLocalPort( KLittleStatPort) ); |
|
156 User::LeaveIfError( iListenSocket.Listen(KLittleStatListenQueue) ); |
|
157 |
|
158 // create a blank socket which is used as the data socket |
|
159 User::LeaveIfError( iDataSocket.Open(iSocketServ) ); |
|
160 |
|
161 // everything should now be set up, we just wait for a stat connection |
|
162 asserte( !IsActive() ); |
|
163 iListenSocket.Accept( iDataSocket, iStatus ); |
|
164 SetActive(); |
|
165 iTransportStatus = EConnecting; |
|
166 |
|
167 // tell the client to wait for an asynchronous response |
|
168 return KSTErrAsynchronous; |
|
169 } |
|
170 |
|
171 TInt CStatTransportTCPIP::Disconnect( void ) |
|
172 { |
|
173 // must be connected |
|
174 asserte( (iTransportStatus == EInitialised) || |
|
175 (iTransportStatus == EConnected) || |
|
176 (iTransportStatus == EConnecting) || |
|
177 (iTransportStatus == EDisconnectingData) || |
|
178 (iTransportStatus == EDisconnectingListen) ); |
|
179 |
|
180 // cancel any pending ops |
|
181 Cancel(); |
|
182 |
|
183 // clean up the sockets depending on the state |
|
184 switch( iTransportStatus ) { |
|
185 |
|
186 case EConnected: |
|
187 iDataSocket.Shutdown( RSocket::ENormal, iStatus ); |
|
188 SetActive(); |
|
189 iTransportStatus = EDisconnectingData; |
|
190 return KSTErrAsynchronous; |
|
191 |
|
192 case EConnecting: |
|
193 case EDisconnectingData: |
|
194 iDataSocket.Close(); |
|
195 iListenSocket.Shutdown( RSocket::ENormal, iStatus ); |
|
196 SetActive(); |
|
197 iTransportStatus = EDisconnectingListen; |
|
198 return KSTErrAsynchronous; |
|
199 |
|
200 case EInitialised: |
|
201 case EDisconnectingListen: |
|
202 // initialised may mean that ConnectL threw an error -- so close the resources |
|
203 iDataSocket.Close(); |
|
204 iListenSocket.Close(); |
|
205 iSocketServ.Close(); |
|
206 return KSTErrSuccess; |
|
207 |
|
208 default: |
|
209 ; |
|
210 } |
|
211 return KSTErrSuccess; |
|
212 } |
|
213 |
|
214 TInt CStatTransportTCPIP::RequestSend( TDesC8 *aData, const TUint /*aDataLength*/ ) |
|
215 { |
|
216 |
|
217 // make sure the state is correct |
|
218 asserte( iTransportStatus == EConnected ); |
|
219 asserte( iRWStatus == ENoRW ); |
|
220 iRWStatus = EWritePending; |
|
221 |
|
222 iDataSocket.Write( *aData, iStatus ); |
|
223 SetActive(); |
|
224 |
|
225 // tell the caller to wait for an asynchronous response |
|
226 return KSTErrAsynchronous; |
|
227 |
|
228 |
|
229 } |
|
230 |
|
231 TInt CStatTransportTCPIP::RequestReceive( TUint aByteCount ) |
|
232 { |
|
233 |
|
234 // ensure that there are no reads in progress |
|
235 asserte( iTransportStatus == EConnected ); |
|
236 asserte( !IsActive() ); |
|
237 asserte( iRWStatus == ENoRW ); |
|
238 iRWStatus = EReadPending; |
|
239 |
|
240 asserte( aByteCount <= static_cast<TUint>(iMaxPacketSize) ); |
|
241 asserte( !IsActive() ); |
|
242 |
|
243 if(!iBufferPtr) |
|
244 { |
|
245 iBufferPtr = new TPtr8(const_cast<unsigned char*>(iBuffer->Ptr( )),aByteCount); |
|
246 } |
|
247 else |
|
248 if(iBufferPtr->MaxLength()!=aByteCount) |
|
249 { |
|
250 delete iBufferPtr; |
|
251 iBufferPtr = new TPtr8(const_cast<unsigned char*>(iBuffer->Ptr( )),aByteCount); |
|
252 } |
|
253 |
|
254 iDataSocket.Read( *iBufferPtr, iStatus ); |
|
255 SetActive(); |
|
256 |
|
257 // return to the caller |
|
258 return KSTErrAsynchronous; |
|
259 |
|
260 } |
|
261 |
|
262 TText8 *CStatTransportTCPIP::Error( void ) |
|
263 { |
|
264 return NULL; |
|
265 } |
|
266 |
|
267 TInt CStatTransportTCPIP::GetPacketSize() |
|
268 { |
|
269 // The packet size is configured when we initialise the port. |
|
270 return iMaxPacketSize; |
|
271 } |
|
272 |
|
273 /******************************************************************************** |
|
274 * |
|
275 * CStatTransportTCPIP -- Active Object |
|
276 * |
|
277 *******************************************************************************/ |
|
278 void CStatTransportTCPIP::RunL( void ) |
|
279 { |
|
280 // if there was an error during connectiong then tell the engine this |
|
281 if( (iTransportStatus == EConnecting) && (iStatus != KErrNone) ) { |
|
282 iTransport->HandleError( KSTErrConnectFailure, (void*)iStatus.Int() ); |
|
283 return; |
|
284 } |
|
285 |
|
286 //reconnect without restarting STAT |
|
287 if(iStatus != KErrNone) |
|
288 { |
|
289 //if no error due to end of socket connection |
|
290 if(iStatus != KErrDisconnected && iStatus != KErrEof) |
|
291 { |
|
292 _LIT(KFormat,"Error during TCPIP: %d\n"); |
|
293 TBuf<50> lBuf; |
|
294 lBuf.Format(KFormat,iStatus.Int()); |
|
295 iTransport->HandleInfo(&lBuf); |
|
296 } |
|
297 |
|
298 // close and reopen the socket |
|
299 iDataSocket.Close(); |
|
300 User::LeaveIfError(iDataSocket.Open(iSocketServ)); |
|
301 |
|
302 // wait for a new connection |
|
303 iTransportStatus = EConnecting; |
|
304 iRWStatus = ENoRW; |
|
305 iListenSocket.Accept( iDataSocket, iStatus ); |
|
306 SetActive(); |
|
307 return; |
|
308 } |
|
309 |
|
310 // handle connection response |
|
311 if( iTransportStatus == EConnecting ) { |
|
312 asserte( iStatus == KErrNone ); |
|
313 iTransportStatus = EConnected; |
|
314 iTransport->HandleConnect( KErrNone ); |
|
315 return; |
|
316 } |
|
317 |
|
318 // handle shutdown data socket |
|
319 if( iTransportStatus == EDisconnectingData ) { |
|
320 iDataSocket.Close(); |
|
321 iListenSocket.Shutdown( RSocket::ENormal, iStatus ); |
|
322 SetActive(); |
|
323 iTransportStatus = EDisconnectingListen; |
|
324 return; |
|
325 } |
|
326 |
|
327 // handle shutdown listen |
|
328 if( iTransportStatus == EDisconnectingListen ) { |
|
329 iListenSocket.Close(); |
|
330 iTransportStatus = EDisconnected; |
|
331 iSocketServ.Close(); |
|
332 iTransport->HandleDisconnect( KErrNone ); |
|
333 return; |
|
334 } |
|
335 |
|
336 // if we are writing then notify of the write |
|
337 if( iRWStatus == EWritePending ) { |
|
338 iRWStatus = ENoRW; |
|
339 iTransport->HandleSend( KErrNone ); |
|
340 return; |
|
341 } |
|
342 |
|
343 // if we are reading then notify of the read |
|
344 if( iRWStatus == EReadPending ) { |
|
345 iRWStatus = ENoRW; |
|
346 iTransport->HandleReceive( KErrNone, iBufferPtr, iBufferPtr->Length( ) ); |
|
347 return; |
|
348 } |
|
349 } |
|
350 |
|
351 void CStatTransportTCPIP::DoCancel( void ) |
|
352 { |
|
353 if( iTransportStatus == EConnecting ) |
|
354 { |
|
355 iListenSocket.CancelAccept(); |
|
356 } |
|
357 |
|
358 if( iRWStatus == EReadPending ) |
|
359 { |
|
360 iDataSocket.CancelRead(); |
|
361 } |
|
362 |
|
363 if( iRWStatus == EWritePending ) |
|
364 { |
|
365 iDataSocket.CancelWrite(); |
|
366 } |
|
367 |
|
368 iRWStatus = ENoRW; |
|
369 } |
|
370 |