|
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 "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 #include "testserver.h" |
|
22 |
|
23 #include <in_sock.h> |
|
24 #include <e32debug.h> |
|
25 |
|
26 |
|
27 CTestServer* CTestServer::NewL( MTestServerObserver* aObserver ) |
|
28 { |
|
29 CTestServer* self = CTestServer::NewLC( aObserver ); |
|
30 CleanupStack::Pop( self ); |
|
31 return self; |
|
32 } |
|
33 |
|
34 CTestServer* CTestServer::NewLC( MTestServerObserver* aObserver ) |
|
35 { |
|
36 CTestServer* self = new( ELeave ) CTestServer( aObserver ); |
|
37 CleanupStack::PushL( self ); |
|
38 self->ConstructL( ); |
|
39 return self; |
|
40 } |
|
41 |
|
42 CTestServer::CTestServer( MTestServerObserver* aObserver ) : |
|
43 CActive( EPriorityStandard ), iObserver( aObserver ) |
|
44 { |
|
45 CActiveScheduler::Add( this ); |
|
46 } |
|
47 |
|
48 void CTestServer::ConstructL() |
|
49 { |
|
50 RDebug::Print( _L( "CTestServer::ConstructL" ) ); |
|
51 User::LeaveIfError( iServer.Connect() ); |
|
52 iState = EServerDisconnected; |
|
53 |
|
54 iTimer = CTimeOutTimer::NewL( EPriorityStandard, *this ); |
|
55 } |
|
56 |
|
57 CTestServer::~CTestServer() |
|
58 { |
|
59 RDebug::Print( _L( "CTestServer::~CTestServer" ) ); |
|
60 Cancel(); |
|
61 iSocket.Close(); |
|
62 iListenSocket.Close(); |
|
63 iServer.Close(); |
|
64 iTimer->Cancel(); |
|
65 delete iTimer; |
|
66 RDebug::Print( _L( "CTestServer::~CTestServer - done" ) ); |
|
67 } |
|
68 |
|
69 // --------------------------------------------------------------------------- |
|
70 // CTestServer::OpenL |
|
71 // --------------------------------------------------------------------------- |
|
72 // |
|
73 void CTestServer::OpenL( TInetAddr& aAddr, TUint aTimeoutTime ) |
|
74 { |
|
75 RDebug::Print( _L( "CTestServer::OpenL" ) ); |
|
76 __ASSERT_ALWAYS( EServerDisconnected == iState, User::Leave( KErrAlreadyExists ) ); |
|
77 |
|
78 User::LeaveIfError( iListenSocket.Open( iServer, KAfInet, |
|
79 KSockStream, KProtocolInetTcp ) ); |
|
80 |
|
81 User::LeaveIfError( iSocket.Open( iServer ) ); |
|
82 |
|
83 User::LeaveIfError( iListenSocket.SetOpt( KSoReuseAddr, |
|
84 KProtocolInetIp, 1 ) ); |
|
85 |
|
86 User::LeaveIfError( iListenSocket.Bind( aAddr ) ); |
|
87 |
|
88 User::LeaveIfError( iListenSocket.Listen( 1 ) ); |
|
89 |
|
90 iListenSocket.Accept( iSocket, iStatus ); |
|
91 |
|
92 iState = EServerWaitingConnection; |
|
93 |
|
94 if ( aTimeoutTime ) |
|
95 { |
|
96 iTimer->After( aTimeoutTime ); |
|
97 } |
|
98 |
|
99 SetActive(); |
|
100 } |
|
101 |
|
102 // --------------------------------------------------------------------------- |
|
103 // CTestServer::RunL |
|
104 // from CActive |
|
105 // --------------------------------------------------------------------------- |
|
106 // |
|
107 void CTestServer::RunL() |
|
108 { |
|
109 RDebug::Print( _L( "CTestServer::RunL - iStatus: %d" ), iStatus.Int() ); |
|
110 |
|
111 switch( iState ) |
|
112 { |
|
113 case EServerWaitingConnection: |
|
114 if( iStatus.Int() == KErrNone ) |
|
115 { |
|
116 iState = EServerConnected; |
|
117 iObserver->Notify( MTestServerObserver::EConnected, KErrNone ); |
|
118 } |
|
119 else |
|
120 { |
|
121 iState = EServerDisconnected; |
|
122 iObserver->Notify( MTestServerObserver::EConnecting, iStatus.Int() ); |
|
123 } |
|
124 break; |
|
125 case EServerConnected: |
|
126 iObserver->Notify( MTestServerObserver::EConnected, iStatus.Int() ); |
|
127 break; |
|
128 case EServerDisconnected: |
|
129 iObserver->Notify( MTestServerObserver::EDisconnected, iStatus.Int() ); |
|
130 break; |
|
131 } |
|
132 } |
|
133 |
|
134 // --------------------------------------------------------------------------- |
|
135 // CTestServer::DoCancel |
|
136 // from CActive |
|
137 // --------------------------------------------------------------------------- |
|
138 // |
|
139 void CTestServer::DoCancel() |
|
140 { |
|
141 iTimer->Cancel(); |
|
142 } |
|
143 |
|
144 // --------------------------------------------------------------------------- |
|
145 // CTestServer::TimerExpired |
|
146 // from MTimeoutNotifier |
|
147 // --------------------------------------------------------------------------- |
|
148 // |
|
149 void CTestServer::TimerExpired() |
|
150 { |
|
151 iObserver->Notify( MTestServerObserver::EShutdownTimer, KErrNone ); |
|
152 } |