|
1 /* |
|
2 * Copyright (c) 2006 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 #include <in_sock.h> |
|
20 #include <ip4_hdr.h> |
|
21 #include "JavaDebugAgentKeepAlive.h" |
|
22 |
|
23 // CJavaDebugAgentKeepAlive::CReceiver |
|
24 class CJavaDebugAgentKeepAlive::CReceiver : public CActive |
|
25 { |
|
26 private: |
|
27 RSocket* iSocket; |
|
28 TSockXfrLength iLength; |
|
29 TBuf8<1> iBuf; |
|
30 public: |
|
31 CReceiver(RSocket* aSocket); |
|
32 void Recv(); |
|
33 virtual void RunL(); |
|
34 virtual void DoCancel(); |
|
35 }; |
|
36 |
|
37 CJavaDebugAgentKeepAlive::CReceiver::CReceiver(RSocket* aSocket) : |
|
38 CActive(EPriorityStandard), |
|
39 iSocket(aSocket) |
|
40 { |
|
41 CActiveScheduler::Add(this); |
|
42 } |
|
43 |
|
44 void CJavaDebugAgentKeepAlive::CReceiver::RunL() |
|
45 { |
|
46 if (iStatus.Int() == KErrNone) Recv(); |
|
47 } |
|
48 |
|
49 void CJavaDebugAgentKeepAlive::CReceiver::DoCancel() |
|
50 { |
|
51 iSocket->CancelRead(); |
|
52 } |
|
53 |
|
54 void CJavaDebugAgentKeepAlive::CReceiver::Recv() |
|
55 { |
|
56 SetActive(); |
|
57 iStatus = KRequestPending; |
|
58 iSocket->RecvOneOrMore(iBuf, 0, iStatus, iLength); |
|
59 } |
|
60 |
|
61 // CJavaDebugAgentKeepAlive |
|
62 CJavaDebugAgentKeepAlive* |
|
63 CJavaDebugAgentKeepAlive::NewL(RSocketServ* aSocketServ, |
|
64 const TSockAddr* aAddr, |
|
65 MJavaDebugAgentLog* aLog, |
|
66 TInt aPeriod) |
|
67 { |
|
68 CJavaDebugAgentKeepAlive* self = NewLC(aSocketServ, aAddr, aLog, aPeriod); |
|
69 CleanupStack::Pop(self); |
|
70 return self; |
|
71 } |
|
72 |
|
73 CJavaDebugAgentKeepAlive* |
|
74 CJavaDebugAgentKeepAlive::NewLC(RSocketServ* aSocketServ, |
|
75 const TSockAddr* aAddr, |
|
76 MJavaDebugAgentLog* aLog, |
|
77 TInt aPeriod) |
|
78 { |
|
79 CJavaDebugAgentKeepAlive* self = new(ELeave) |
|
80 CJavaDebugAgentKeepAlive(aSocketServ, aAddr, aLog, aPeriod); |
|
81 CleanupStack::PushL(self); |
|
82 self->ConstructL(); |
|
83 return self; |
|
84 } |
|
85 |
|
86 CJavaDebugAgentKeepAlive::~CJavaDebugAgentKeepAlive() |
|
87 { |
|
88 Cancel(); |
|
89 delete iReceiver; |
|
90 iSocket.Close(); |
|
91 iTimer.Close(); |
|
92 } |
|
93 |
|
94 CJavaDebugAgentKeepAlive:: |
|
95 CJavaDebugAgentKeepAlive(RSocketServ* aSocketServ, |
|
96 const TSockAddr* aAddr, |
|
97 MJavaDebugAgentLog* aLog, |
|
98 TInt aPeriodInMilliseconds) : |
|
99 CActive(EPriorityStandard), |
|
100 iPeriod(aPeriodInMilliseconds*1000), |
|
101 iSocketServ(aSocketServ), |
|
102 iAddress(*aAddr), |
|
103 iLog(aLog) |
|
104 { |
|
105 CActiveScheduler::Add(this); |
|
106 iMsg.FillZ(iMsg.MaxLength()); |
|
107 iMsg[0] = KInet4ICMP_Echo; |
|
108 iMsg[8] = 'P'; |
|
109 iMsg[9] = 'I'; |
|
110 iMsg[10] = 'N'; |
|
111 iMsg[11] = 'G'; |
|
112 } |
|
113 |
|
114 void CJavaDebugAgentKeepAlive::ConstructL() |
|
115 { |
|
116 iReceiver = new(ELeave)CReceiver(&iSocket); |
|
117 User::LeaveIfError(iTimer.CreateLocal()); |
|
118 User::LeaveIfError(iSocket.Open(*iSocketServ, KAfInet, |
|
119 KSockDatagram, KProtocolInetIcmp)); |
|
120 |
|
121 SetActive(); |
|
122 iStatus = KRequestPending; |
|
123 iState = EConnecting; |
|
124 iSocket.Connect(iAddress, iStatus); |
|
125 } |
|
126 |
|
127 // CActive |
|
128 void CJavaDebugAgentKeepAlive::RunL() |
|
129 { |
|
130 TInt err = iStatus.Int(); |
|
131 switch (iState) |
|
132 { |
|
133 case EConnecting: |
|
134 if (err == KErrNone) |
|
135 { |
|
136 iReceiver->Recv(); |
|
137 Ping(); |
|
138 } |
|
139 else |
|
140 { |
|
141 iState = EFailed; |
|
142 iLog->LogFormat(_S("KeepAlive connect error %d"),err); |
|
143 } |
|
144 break; |
|
145 |
|
146 case ESending: |
|
147 if (err == KErrNone) |
|
148 { |
|
149 Wait(); |
|
150 } |
|
151 else |
|
152 { |
|
153 iState = EFailed; |
|
154 iLog->LogFormat(_S("KeepAlive send error %d"),err); |
|
155 } |
|
156 break; |
|
157 |
|
158 case EWaiting: |
|
159 if (err == KErrNone) |
|
160 { |
|
161 Ping(); |
|
162 } |
|
163 else |
|
164 { |
|
165 iState = EFailed; |
|
166 iLog->LogFormat(_S("KeepAlive wait error %d"),err); |
|
167 } |
|
168 break; |
|
169 } |
|
170 } |
|
171 |
|
172 void CJavaDebugAgentKeepAlive::Wait() |
|
173 { |
|
174 SetActive(); |
|
175 iStatus = KRequestPending; |
|
176 iState = EWaiting; |
|
177 iTimer.After(iStatus,iPeriod); |
|
178 } |
|
179 |
|
180 void CJavaDebugAgentKeepAlive::Ping() |
|
181 { |
|
182 TUint16* ptr16 = ((TUint16*)&iMsg[0]); |
|
183 ptr16[1] = 0; // checksum |
|
184 iSeq++; |
|
185 iMsg[6] = (TUint8)(iSeq >> 8); // convert to network byte order |
|
186 iMsg[7] = (TUint8)iSeq; |
|
187 |
|
188 // Checksum is calculated in native byte order, and stored natively too |
|
189 TUint16 sum = 0; |
|
190 for (TInt i=iMsg.Length()/2-1; i>=0; i--) sum += ptr16[i]; |
|
191 ptr16[1] = ~sum; |
|
192 |
|
193 // Send the message |
|
194 SetActive(); |
|
195 iStatus = KRequestPending; |
|
196 iState = ESending; |
|
197 iSocket.SendTo(iMsg, iAddress, 0, iStatus); |
|
198 } |
|
199 |
|
200 TInt CJavaDebugAgentKeepAlive::RunError(TInt /*aError*/) |
|
201 { |
|
202 iState = EFailed; |
|
203 return KErrNone; |
|
204 } |
|
205 |
|
206 void CJavaDebugAgentKeepAlive::DoCancel() |
|
207 { |
|
208 TRequestStatus *status; |
|
209 switch (iState) |
|
210 { |
|
211 case EConnecting: |
|
212 iSocket.CancelConnect(); |
|
213 break; |
|
214 case ESending: |
|
215 iSocket.CancelSend(); |
|
216 break; |
|
217 case EWaiting: |
|
218 iTimer.Cancel(); |
|
219 break; |
|
220 default: |
|
221 status = (&iStatus); |
|
222 User::RequestComplete(status, KErrCancel); |
|
223 break; |
|
224 } |
|
225 } |
|
226 |
|
227 /** |
|
228 * Local Variables: |
|
229 * c-basic-offset: 4 |
|
230 * indent-tabs-mode: nil |
|
231 * End: |
|
232 */ |