|
1 /* |
|
2 * Copyright (c) 2008 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: CCommsSession handles communication with client side |
|
15 * |
|
16 */ |
|
17 |
|
18 #include <memory> |
|
19 |
|
20 #include "logger.h" |
|
21 |
|
22 #include "common.h" |
|
23 #include "commssession.h" |
|
24 #include "ipcserver.h" |
|
25 |
|
26 namespace java |
|
27 { |
|
28 namespace comms |
|
29 { |
|
30 using java::util::ScopedLock; |
|
31 |
|
32 CCommsSession::CCommsSession(IpcListener& aListener, int aPermissions) |
|
33 : mListener(aListener), mClientPermissions(aPermissions) |
|
34 { |
|
35 } |
|
36 |
|
37 CCommsSession::~CCommsSession() |
|
38 { |
|
39 while (!mMessageQueue.empty()) |
|
40 { |
|
41 std::auto_ptr<HBufC8> tmp(mMessageQueue.front()); |
|
42 mMessageQueue.pop(); |
|
43 } |
|
44 } |
|
45 |
|
46 void CCommsSession::ServiceL(const RMessage2& aMessage) |
|
47 { |
|
48 TInt function = aMessage.Function(); |
|
49 switch (function) |
|
50 { |
|
51 case ESend: |
|
52 handleSendL(aMessage); |
|
53 break; |
|
54 |
|
55 case EReceive: |
|
56 handleReceiveL(aMessage); |
|
57 break; |
|
58 |
|
59 case ECancelReceive: |
|
60 handleCancelReceive(aMessage); |
|
61 break; |
|
62 |
|
63 default: |
|
64 aMessage.Complete(KErrNotSupported); |
|
65 break; |
|
66 } |
|
67 } |
|
68 |
|
69 int CCommsSession::send(ipcMessage_t* aMsg) |
|
70 { |
|
71 ScopedLock lock(mMutex); |
|
72 |
|
73 // messages from server are considered to be always trusted |
|
74 aMsg->ipcHeader.permissions = -1; |
|
75 HBufC8* msg = messageToDes(*aMsg); |
|
76 mMessageQueue.push(msg); |
|
77 |
|
78 if (isReceivePending()) |
|
79 { |
|
80 HBufC8* msg = mMessageQueue.front(); |
|
81 |
|
82 TInt rc = doSend(mReceiveMessage, *msg); |
|
83 if (rc == KErrNone) |
|
84 { |
|
85 mMessageQueue.pop(); |
|
86 delete msg; |
|
87 } |
|
88 else |
|
89 { |
|
90 WLOG2(EJavaComms, "%s failed, err = %d", __PRETTY_FUNCTION__, rc); |
|
91 } |
|
92 } |
|
93 return 0; |
|
94 } |
|
95 |
|
96 void CCommsSession::handleSendL(const RMessage2& aMessage) |
|
97 { |
|
98 TInt len = aMessage.GetDesLengthL(0); |
|
99 int rc = KErrNone; |
|
100 if (len < sizeof(ipcHeader_t)) |
|
101 { |
|
102 WLOG2(EJavaComms, "%s invalid message size, len = %d", __PRETTY_FUNCTION__, len); |
|
103 rc = KErrArgument; |
|
104 } |
|
105 else |
|
106 { |
|
107 RBuf8 buffer; |
|
108 |
|
109 buffer.CleanupClosePushL(); |
|
110 buffer.CreateL(len); |
|
111 |
|
112 aMessage.ReadL(0,buffer); |
|
113 |
|
114 char* buf = desToMessage(buffer); |
|
115 ipcMessage_t* msg = reinterpret_cast<ipcMessage_t*>(buf); |
|
116 if (msg->ipcHeader.length == len) |
|
117 { |
|
118 msg->ipcHeader.sender = getId(); |
|
119 msg->ipcHeader.permissions = mClientPermissions; |
|
120 mListener.processMessage(msg); |
|
121 } |
|
122 else |
|
123 { |
|
124 WLOG3(EJavaComms, "%s message size does not match to header, len = %d header = %d", |
|
125 __PRETTY_FUNCTION__, len, msg->ipcHeader.length); |
|
126 rc = KErrArgument; |
|
127 } |
|
128 |
|
129 delete[] buf; |
|
130 CleanupStack::PopAndDestroy(); |
|
131 } |
|
132 aMessage.Complete(rc); |
|
133 } |
|
134 |
|
135 void CCommsSession::handleReceiveL(const RMessage2& aMessage) |
|
136 { |
|
137 ScopedLock lock(mMutex); |
|
138 |
|
139 if (mMessageQueue.empty()) |
|
140 { |
|
141 mReceiveMessage = aMessage; |
|
142 } |
|
143 else |
|
144 { |
|
145 HBufC8* msg = mMessageQueue.front(); |
|
146 TInt rc = doSend(aMessage, *msg); |
|
147 if (rc == KErrNone) |
|
148 { |
|
149 mMessageQueue.pop(); |
|
150 delete msg; |
|
151 } |
|
152 else |
|
153 { |
|
154 WLOG2(EJavaComms, "%s failed, err = %d", __PRETTY_FUNCTION__, rc); |
|
155 } |
|
156 } |
|
157 } |
|
158 |
|
159 void CCommsSession::handleCancelReceive(const RMessage2& aMessage) |
|
160 { |
|
161 ScopedLock lock(mMutex); |
|
162 |
|
163 if (isReceivePending()) |
|
164 { |
|
165 mReceiveMessage.Complete(KErrCancel); |
|
166 } |
|
167 aMessage.Complete(KErrNone); |
|
168 } |
|
169 |
|
170 bool CCommsSession::isReceivePending() const |
|
171 { |
|
172 return !mReceiveMessage.IsNull(); |
|
173 } |
|
174 |
|
175 int CCommsSession::getId() const |
|
176 { |
|
177 // to be used as unique id to identify session - not as pointer |
|
178 return (int)this; |
|
179 } |
|
180 |
|
181 TInt CCommsSession::doSend(const RMessagePtr2& aMessage, HBufC8& aData) |
|
182 { |
|
183 TInt allocatedLength = aMessage.GetDesMaxLength(0); |
|
184 TInt neededLength = aData.Length(); |
|
185 |
|
186 TPckgBuf<TInt> lengthPckg(neededLength); |
|
187 TInt rc = aMessage.Write(1, lengthPckg); |
|
188 |
|
189 if (rc == KErrNone && allocatedLength >= neededLength) |
|
190 { |
|
191 rc = aMessage.Write(0, aData.Des()); |
|
192 } |
|
193 else |
|
194 { |
|
195 rc = KErrOverflow; |
|
196 WLOG3(EJavaComms, "%s: allocated length for message is too small (required size=%d, allocated size=%d)", |
|
197 __PRETTY_FUNCTION__, neededLength, allocatedLength); |
|
198 } |
|
199 |
|
200 aMessage.Complete(rc); |
|
201 return rc; |
|
202 } |
|
203 |
|
204 } // namespace comms |
|
205 } // namespace java |