1 /* |
|
2 * Copyright (c) 2002-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 /** @file |
|
19 @internalComponent |
|
20 @test |
|
21 */ |
|
22 |
|
23 #include "tcptest.h" |
|
24 #include <in_sock.h> |
|
25 #include <commdbconnpref.h> |
|
26 #include <es_enum.h> |
|
27 #include "tcpcommand.h" |
|
28 #include "ncmtestconsole.h" |
|
29 |
|
30 _LIT8(KSendData, "TCP-Packet::HelloWorld\n"); |
|
31 |
|
32 static const TInt KMaxNumOfChars = 255; |
|
33 |
|
34 //The title of TCP test console |
|
35 _LIT(KTcpServerMode, "Tcp Server"); |
|
36 _LIT(KTcpClientMode, "Tcp Client"); |
|
37 |
|
38 //The default value of data size |
|
39 static const TInt KDefaultDataSize = 30; |
|
40 |
|
41 CTcpProcess* CTcpProcess::NewL(CConsoleBase& aConsole, TInetAddr& aAddr, TInt aPort, TInt aSize, TBool aMode) |
|
42 { |
|
43 CTcpProcess* self = new(ELeave) CTcpProcess(aConsole, aAddr, aPort, aSize, aMode); |
|
44 CleanupStack::PushL(self); |
|
45 self->ConstructL(); |
|
46 CleanupStack::Pop(self); |
|
47 return self; |
|
48 } |
|
49 |
|
50 CTcpProcess::~CTcpProcess() |
|
51 { |
|
52 Cancel(); |
|
53 iRecvBuf.Close(); |
|
54 iSendBuf.Close(); |
|
55 iSocket.Close(); |
|
56 iListenSocket.Close(); |
|
57 iSockServ.Close(); |
|
58 } |
|
59 |
|
60 CTcpProcess::CTcpProcess(CConsoleBase& aConsole, TInetAddr& aAddr, TInt aPort, TInt aSize, TBool aMode) |
|
61 : CActive(EPriorityStandard), iConsole(aConsole), iAddr(aAddr), iPort(aPort), iSize(aSize), iMode(aMode) |
|
62 { |
|
63 CActiveScheduler::Add(this); |
|
64 } |
|
65 |
|
66 void CTcpProcess::ConstructL() |
|
67 { |
|
68 //Create the data buffer |
|
69 User::LeaveIfError(iSendBuf.Create(iSize)); |
|
70 User::LeaveIfError(iRecvBuf.Create(iSize)); |
|
71 iRecvSize = iSize; |
|
72 |
|
73 iProcessState = ECreateConnection; |
|
74 |
|
75 User::LeaveIfError(iSockServ.Connect()); |
|
76 if(iMode) |
|
77 { |
|
78 //Listen at specified port |
|
79 TBuf<5> protocol = _L("tcp"); |
|
80 User::LeaveIfError(iListenSocket.Open(iSockServ, protocol)); |
|
81 User::LeaveIfError(iListenSocket.SetOpt(KSoReuseAddr, KProtocolInetIp, 1)); |
|
82 User::LeaveIfError(iListenSocket.SetLocalPort(iPort)); |
|
83 User::LeaveIfError(iListenSocket.Listen(5)); |
|
84 User::LeaveIfError(iSocket.Open(iSockServ)); |
|
85 iListenSocket.Accept(iSocket, iStatus); |
|
86 iConsole.Printf(_L("\nWait for a connection at port[%d].\n"), iPort); |
|
87 } |
|
88 else |
|
89 { |
|
90 RConnection conn; |
|
91 User::LeaveIfError(conn.Open(iSockServ)); |
|
92 TCommDbConnPref pref; |
|
93 pref.SetIapId(13); |
|
94 pref.SetDialogPreference(ECommDbDialogPrefDoNotPrompt); |
|
95 User::LeaveIfError(conn.Start(pref)); |
|
96 |
|
97 //Connect to specified server |
|
98 User::LeaveIfError(iSocket.Open(iSockServ, KAfInet, KSockStream, KProtocolInetTcp, conn)); |
|
99 iSocket.Connect(iAddr, iStatus); |
|
100 |
|
101 iConsole.Printf(_L("\nConnecting....\n")); |
|
102 } |
|
103 SetActive(); |
|
104 } |
|
105 |
|
106 |
|
107 void CTcpProcess::RunL() |
|
108 { |
|
109 switch(iProcessState) |
|
110 { |
|
111 case ECreateConnection: |
|
112 //Get result of connect |
|
113 if(iStatus.Int() != KErrNone) |
|
114 { |
|
115 iConsole.Printf(_L("Connect err[%d].\nPress any key to quit."), iStatus.Int()); |
|
116 return; |
|
117 } |
|
118 else |
|
119 { |
|
120 //A TCP connection is created. |
|
121 if(iMode) |
|
122 { |
|
123 iConsole.Printf(_L("Get a connection.\n")); |
|
124 } |
|
125 else |
|
126 { |
|
127 iConsole.Printf(_L("\nConnected.\n")); |
|
128 } |
|
129 iProcessState = EDataTransfer; |
|
130 |
|
131 //Ready for read data |
|
132 iSocket.RecvOneOrMore(iRecvBuf, 0, iStatus, iRecvSize); |
|
133 __FLOG_STATIC1(KSubSys, KLogComponent , _L8("Ready for read data"), iRecvSize); |
|
134 SetActive(); |
|
135 if(!iMode) |
|
136 { |
|
137 //As a client, send data first. |
|
138 iSendBuf.SetLength(iSize); |
|
139 iSendBuf.Repeat(KSendData()); |
|
140 |
|
141 iConsole.Printf(_L("Send data.")); |
|
142 PrintData(iSendBuf); |
|
143 TInt sendLen = SendDataL(iSendBuf, iSize); |
|
144 if(sendLen != iSize) |
|
145 { |
|
146 iConsole.Printf(_L("The length of data sent is not equal to requested! requested[%d], sent[%d]"), |
|
147 iSize, sendLen); |
|
148 } |
|
149 } |
|
150 } |
|
151 break; |
|
152 |
|
153 case EDataTransfer: |
|
154 //In data transfer, some data is received |
|
155 iConsole.Printf(_L("recv Package, size[%d], status[%d]\n"), iRecvSize(), iStatus.Int()); |
|
156 if((KErrEof == iStatus.Int()) || (KErrDisconnected == iStatus.Int())) |
|
157 { |
|
158 iConsole.Printf(_L("Connection closed!")); |
|
159 return; |
|
160 } |
|
161 else if(KErrNone == iStatus.Int()) |
|
162 { |
|
163 iConsole.Printf(_L("Receive data.")); |
|
164 PrintData(iRecvBuf); |
|
165 |
|
166 if(iMode) |
|
167 { |
|
168 //As a server, send back the data received |
|
169 TInt len = SendDataL(iRecvBuf, iRecvSize()); |
|
170 iConsole.Printf(_L("Send back the data. len[%d]"), len); |
|
171 } |
|
172 } |
|
173 iRecvBuf.SetLength(0); |
|
174 iRecvSize = iSize; |
|
175 iSocket.RecvOneOrMore(iRecvBuf, 0, iStatus, iRecvSize); |
|
176 SetActive(); |
|
177 |
|
178 break; |
|
179 } |
|
180 } |
|
181 |
|
182 void CTcpProcess::DoCancel() |
|
183 { |
|
184 switch(iProcessState) |
|
185 { |
|
186 case ECreateConnection: |
|
187 if(iMode) |
|
188 { |
|
189 iListenSocket.CancelAccept(); |
|
190 } |
|
191 else |
|
192 { |
|
193 iSocket.CancelConnect(); |
|
194 } |
|
195 break; |
|
196 case EDataTransfer: |
|
197 iSocket.CancelRecv(); |
|
198 break; |
|
199 } |
|
200 } |
|
201 |
|
202 TInt CTcpProcess::RunError(TInt aError) |
|
203 { |
|
204 User::Panic(_L("CTcpProcess"), aError); |
|
205 return aError; |
|
206 } |
|
207 |
|
208 TInt CTcpProcess::SendDataL(TDes8& aData, TInt aSize) |
|
209 /** |
|
210 Send data. |
|
211 @return the size of data sent |
|
212 */ |
|
213 { |
|
214 TRequestStatus status; |
|
215 TSockXfrLength sendSize = aSize; |
|
216 iSocket.Send(aData, 0, status, sendSize); |
|
217 User::WaitForRequest(status); |
|
218 |
|
219 switch(status.Int()) |
|
220 { |
|
221 case KErrEof: |
|
222 iConsole.Printf(_L("Connection closed!")); |
|
223 return 0; |
|
224 case KErrNone: |
|
225 iConsole.Printf(_L("Send successfully.")); |
|
226 break; |
|
227 default: |
|
228 User::LeaveIfError(status.Int()); |
|
229 break; |
|
230 } |
|
231 return sendSize(); |
|
232 } |
|
233 |
|
234 void CTcpProcess::PrintData(TDes8& aData) |
|
235 { |
|
236 iConsole.Printf(_L("The data is: \n")); |
|
237 for(TInt i=0; i< aData.Length();i++) |
|
238 { |
|
239 iConsole.Printf(_L("%c"), aData[i]); |
|
240 } |
|
241 iConsole.Printf(_L("\n")); |
|
242 } |
|
243 |
|
244 CUdpProcess* CUdpProcess::NewL(CConsoleBase& aConsole, TInetAddr& aAddr, |
|
245 TInt aPort, TInt aSize, TBool aMode) |
|
246 { |
|
247 CUdpProcess* self = new (ELeave) CUdpProcess(aConsole, aAddr, aPort, |
|
248 aSize, aMode); |
|
249 CleanupStack::PushL(self); |
|
250 self->ConstructL(); |
|
251 CleanupStack::Pop(self); |
|
252 return self; |
|
253 } |
|
254 |
|
255 CUdpProcess::~CUdpProcess() |
|
256 { |
|
257 Cancel(); |
|
258 iRecvBuf.Close(); |
|
259 iSendBuf.Close(); |
|
260 iSocket.Close(); |
|
261 iListenSocket.Close(); |
|
262 iSockServ.Close(); |
|
263 } |
|
264 |
|
265 CUdpProcess::CUdpProcess(CConsoleBase& aConsole, TInetAddr& aAddr, |
|
266 TInt aPort, TInt aSize, TBool aMode) : |
|
267 CActive(EPriorityStandard), iConsole(aConsole), iAddr(aAddr), |
|
268 iPort(aPort), iSize(aSize), iMode(aMode) |
|
269 { |
|
270 CActiveScheduler::Add(this); |
|
271 } |
|
272 |
|
273 void CUdpProcess::ConstructL() |
|
274 { |
|
275 //Create the data buffer |
|
276 User::LeaveIfError(iSendBuf.Create(iSize)); |
|
277 User::LeaveIfError(iRecvBuf.Create(iSize)); |
|
278 iRecvSize = iSize; |
|
279 |
|
280 User::LeaveIfError(iSockServ.Connect()); |
|
281 |
|
282 // Start NCM IAP |
|
283 RConnection conn; |
|
284 User::LeaveIfError(conn.Open(iSockServ)); |
|
285 TCommDbConnPref pref; |
|
286 pref.SetIapId(13); |
|
287 pref.SetDialogPreference(ECommDbDialogPrefDoNotPrompt); |
|
288 User::LeaveIfError(conn.Start(pref)); |
|
289 |
|
290 User::LeaveIfError(iSocket.Open(iSockServ, KAfInet, KSockDatagram, |
|
291 KProtocolInetUdp, conn)); |
|
292 |
|
293 iConsole.Printf(_L("In constructL, port=%d"), iAddr.Port()); |
|
294 |
|
295 if (iMode) |
|
296 { |
|
297 iProcessState = EDataTransfer; |
|
298 |
|
299 User::LeaveIfError(iSocket.SetLocalPort(iPort)); |
|
300 iSocket.RecvFrom(iRecvBuf, iPeerAddr, 0, iStatus); |
|
301 iConsole.Printf(_L("\nWait for UDP incoming data at port[%d]...\n"),iPort); |
|
302 SetActive(); |
|
303 } |
|
304 else |
|
305 { |
|
306 iProcessState = EDataSending; |
|
307 SetActive(); |
|
308 TRequestStatus* status = &iStatus; |
|
309 User::RequestComplete(status, KErrNone); |
|
310 } |
|
311 } |
|
312 |
|
313 |
|
314 void CUdpProcess::RunL() |
|
315 { |
|
316 __FLOG_STATIC0(KSubSys, KLogComponent , _L8("CUdpProcess::RunL")); |
|
317 switch (iProcessState) |
|
318 { |
|
319 case EDataSending: |
|
320 //As a client, send data first. |
|
321 iSendBuf.SetLength(iSize); |
|
322 iSendBuf.Repeat(KSendData()); |
|
323 iConsole.Printf(_L("Send data..")); |
|
324 PrintData(iSendBuf); |
|
325 iConsole.Printf(_L("In RunL, port=%d"), iAddr.Port()); |
|
326 TInt sendLen = SendDataL(iSendBuf, iAddr, iSize); |
|
327 |
|
328 if (sendLen != iSize) |
|
329 { |
|
330 iConsole.Printf( |
|
331 _L("The length of data sent is not equal to requested! requested[%d], sent[%d]"), |
|
332 iSize, sendLen); |
|
333 } |
|
334 break; |
|
335 |
|
336 case EDataTransfer: |
|
337 //In data transfer, some data is received |
|
338 iConsole.Printf(_L("recv Package, size[%d], status[%d]\n"), |
|
339 iRecvSize(), iStatus.Int()); |
|
340 if (KErrNone == iStatus.Int()) |
|
341 { |
|
342 iConsole.Printf(_L("Receive data.")); |
|
343 PrintData(iRecvBuf); |
|
344 |
|
345 if (iMode) |
|
346 { |
|
347 //As a server, send back the data received |
|
348 TInt len = SendDataL(iRecvBuf, iPeerAddr, iRecvBuf.Length()); |
|
349 iConsole.Printf(_L("Send back the data. len[%d]"), len); |
|
350 } |
|
351 } |
|
352 else |
|
353 { |
|
354 iConsole.Printf(_L("Something is wrong...")); |
|
355 return; |
|
356 } |
|
357 |
|
358 iRecvBuf.SetLength(0); |
|
359 iRecvSize = iSize; |
|
360 //iListenSocket.RecvFrom(iRecvBuf, iPeerAddr, 0, iStatus); |
|
361 iSocket.RecvFrom(iRecvBuf, iPeerAddr, 0, iStatus); |
|
362 SetActive(); |
|
363 iConsole.Printf(_L("\nWait for UDP incoming data at port[%d]...\n"),iPort); |
|
364 break; |
|
365 } |
|
366 } |
|
367 |
|
368 void CUdpProcess::DoCancel() |
|
369 { |
|
370 switch(iProcessState) |
|
371 { |
|
372 case EDataTransfer: |
|
373 //iListenSocket.CancelRecv(); |
|
374 iSocket.CancelRecv(); |
|
375 break; |
|
376 } |
|
377 } |
|
378 |
|
379 TInt CUdpProcess::RunError(TInt aError) |
|
380 { |
|
381 User::Panic(_L("CUdpProcess"), aError); |
|
382 return aError; |
|
383 } |
|
384 |
|
385 TInt CUdpProcess::SendDataL(TDes8& aData, TInetAddr& aAddr, TInt aSize) |
|
386 /** |
|
387 Send data. |
|
388 @return the size of data sent |
|
389 */ |
|
390 { |
|
391 TRequestStatus status; |
|
392 |
|
393 TInt port = aAddr.Port(); |
|
394 iConsole.Printf(_L("Before sending, size = %d, port=%d\n"), aSize, port); |
|
395 TSockXfrLength sendSize = 0; //aSize; |
|
396 iSocket.SendTo(aData, aAddr, 0, status, sendSize); |
|
397 User::WaitForRequest(status); |
|
398 iConsole.Printf(_L("Sending result = %d, and sent=%d\n"), status.Int(), sendSize()); |
|
399 |
|
400 switch(status.Int()) |
|
401 { |
|
402 case KErrEof: |
|
403 iConsole.Printf(_L("Connection closed!")); |
|
404 return 0; |
|
405 case KErrNone: |
|
406 iConsole.Printf(_L("Send successfully.\n")); |
|
407 break; |
|
408 default: |
|
409 User::LeaveIfError(status.Int()); |
|
410 break; |
|
411 } |
|
412 return sendSize(); |
|
413 } |
|
414 |
|
415 void CUdpProcess::PrintData(TDes8& aData) |
|
416 { |
|
417 iConsole.Printf(_L("The data is: \n")); |
|
418 for(TInt i=0; i< aData.Length();i++) |
|
419 { |
|
420 iConsole.Printf(_L("%c"), aData[i]); |
|
421 } |
|
422 iConsole.Printf(_L("\n")); |
|
423 } |
|
424 |
|
425 |
|
426 |
|
427 CTcpTestConsole* CTcpTestConsole::NewL(TBool aIsTcp, TBool aMode, TDesC& aDefautAddr, TInt aIndex, CTcpCommand& aOwner) |
|
428 { |
|
429 CTcpTestConsole* self = new(ELeave) CTcpTestConsole(aIsTcp, aMode, aDefautAddr, aIndex, aOwner); |
|
430 CleanupStack::PushL(self); |
|
431 self->ConstructL(); |
|
432 CleanupStack::Pop(self); |
|
433 return self; |
|
434 |
|
435 } |
|
436 |
|
437 CTcpTestConsole::CTcpTestConsole(TBool aIsTcp, TBool aMode, TDesC& aDefautAddr, TInt aIndex, CTcpCommand& aOwner) |
|
438 : CActive(EPriorityStandard),iCommandMode(ECommandInit), iIsTcp(aIsTcp), iMode(aMode), |
|
439 iDefaultAddr(aDefautAddr), iIndex(aIndex), iOwner(aOwner) |
|
440 { |
|
441 CActiveScheduler::Add(this); |
|
442 } |
|
443 |
|
444 CTcpTestConsole::~CTcpTestConsole() |
|
445 { |
|
446 Cancel(); |
|
447 delete iTcp; |
|
448 delete iUdp; |
|
449 delete iConsole; |
|
450 iChars.Close(); |
|
451 } |
|
452 |
|
453 |
|
454 void CTcpTestConsole::ConstructL() |
|
455 { |
|
456 if(iMode) |
|
457 { |
|
458 iConsole = Console::NewL(KTcpServerMode(),TSize(KConsFullScreen,KConsFullScreen)); |
|
459 } |
|
460 else |
|
461 { |
|
462 iConsole = Console::NewL(KTcpClientMode(),TSize(KConsFullScreen,KConsFullScreen)); |
|
463 } |
|
464 User::LeaveIfError(iChars.Create(KMaxNumOfChars)); |
|
465 //Generate the default value and display on screen, user can modify it if they want to use other value. |
|
466 iChars.AppendFormat(_L("%S %d"), &iDefaultAddr, KDefaultDataSize); |
|
467 Help(); |
|
468 //wait user input |
|
469 iConsole->Read(iStatus); |
|
470 SetActive(); |
|
471 } |
|
472 |
|
473 void CTcpTestConsole::Help() |
|
474 { |
|
475 iConsole->Printf(_L("Please change the parameters, then press enter\n")); |
|
476 if(iMode) |
|
477 { |
|
478 iConsole->Printf(_L(" The parameters are port size \n")); |
|
479 iConsole->Printf(_L(" port - The port of the Tcp/udp Server listen to \n")); |
|
480 iConsole->Printf(_L(" size - the max package size \n")); |
|
481 } |
|
482 else |
|
483 { |
|
484 iConsole->Printf(_L(" The parameters are destAddr port size \n")); |
|
485 iConsole->Printf(_L(" destAddr - The ip address of Tcp/udp Client connect to.\n")); |
|
486 iConsole->Printf(_L(" port - The port of Tcp Client connect to \n")); |
|
487 iConsole->Printf(_L(" size - the package size \n")); |
|
488 } |
|
489 //Display the default value |
|
490 iConsole->Printf(_L("%S"), &iChars); |
|
491 |
|
492 } |
|
493 TBool CTcpTestConsole::StartL() |
|
494 { |
|
495 TLex args(iChars); |
|
496 // args are separated by spaces |
|
497 args.SkipSpace(); |
|
498 |
|
499 TInetAddr addr; |
|
500 TInt size; |
|
501 |
|
502 if(!iMode) |
|
503 { |
|
504 //Get ip addr |
|
505 TPtrC cmdAddr = args.NextToken(); |
|
506 if(!args.Eos()) |
|
507 { |
|
508 if(KErrNone == addr.Input(cmdAddr)) |
|
509 { |
|
510 args.Inc(); |
|
511 } |
|
512 else |
|
513 { |
|
514 return EFalse; |
|
515 } |
|
516 } |
|
517 else |
|
518 { |
|
519 return EFalse; |
|
520 } |
|
521 } |
|
522 |
|
523 //Get port |
|
524 TInt port; |
|
525 if(KErrNone != args.Val(port)) |
|
526 { |
|
527 return EFalse; |
|
528 } |
|
529 addr.SetPort(port); |
|
530 |
|
531 //Get pkg size |
|
532 args.Inc(); |
|
533 if(KErrNone != args.Val(size)) |
|
534 { |
|
535 return EFalse; |
|
536 } |
|
537 |
|
538 iCommandMode = ECommandRunning; |
|
539 if (iIsTcp) |
|
540 { |
|
541 iConsole->Printf(_L("Test for TCP...\n")); |
|
542 iTcp = CTcpProcess::NewL(*iConsole, addr, port, size, iMode); |
|
543 } |
|
544 else |
|
545 { |
|
546 iConsole->Printf(_L("Test for UDP...\n")); |
|
547 iUdp = CUdpProcess::NewL(*iConsole, addr, port, size, iMode); |
|
548 } |
|
549 |
|
550 return ETrue; |
|
551 |
|
552 } |
|
553 |
|
554 void CTcpTestConsole::DoCancel() |
|
555 { |
|
556 iConsole->ReadCancel(); |
|
557 } |
|
558 |
|
559 void CTcpTestConsole::RunL() |
|
560 { |
|
561 User::LeaveIfError(iStatus.Int()); |
|
562 switch(iCommandMode) |
|
563 { |
|
564 case ECommandInit: |
|
565 { |
|
566 TKeyCode code = iConsole->KeyCode(); |
|
567 switch(code) |
|
568 { |
|
569 case EKeyEnter: |
|
570 { |
|
571 //User input ok |
|
572 if(!StartL()) |
|
573 { |
|
574 Help(); |
|
575 } |
|
576 } |
|
577 break; |
|
578 case EKeyEscape: |
|
579 //connection has not been created. User want to cancel and quit |
|
580 iOwner.CloseTcpTest(iIndex); |
|
581 return; |
|
582 case EKeyBackspace: |
|
583 if(iChars.Length() > 0) |
|
584 { |
|
585 iConsole->SetCursorPosRel(TPoint(-1, 0)); |
|
586 iConsole->ClearToEndOfLine(); |
|
587 iChars.SetLength(iChars.Length()-1); |
|
588 } |
|
589 break; |
|
590 default: |
|
591 iChars.Append(code); |
|
592 iConsole->Printf(_L("%c"), code); |
|
593 break; |
|
594 } |
|
595 iConsole->Read(iStatus); |
|
596 SetActive(); |
|
597 } |
|
598 break; |
|
599 case ECommandRunning: |
|
600 //Connection has been created. User quit by press any key. |
|
601 iOwner.CloseTcpTest(iIndex); |
|
602 break; |
|
603 } |
|
604 } |
|
605 |
|
606 TInt CTcpTestConsole::RunError(TInt aError) |
|
607 { |
|
608 User::Panic(_L("CTcpTestConsole"), aError); |
|
609 return aError; |
|
610 } |
|
611 |
|
612 |
|