|
1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 #include "ccontrolchannel.h" |
|
17 #include <es_sock.h> |
|
18 #include <e32const.h> |
|
19 #include <upnpconstants.h> |
|
20 |
|
21 CControlChannelHandler::CControlChannelHandler ( RSocket &aSocket, TBool aChannelType ) |
|
22 :CActive ( EPriorityNormal ), iSocket ( aSocket ), iChannelType ( aChannelType ) |
|
23 { |
|
24 CActiveScheduler::Add(this); |
|
25 } |
|
26 |
|
27 TInt CControlChannelHandler::RunError ( TInt aError ) |
|
28 { |
|
29 User::RequestComplete ( iChannelStatus, aError ); |
|
30 return KErrNone; |
|
31 } |
|
32 // ------------------------------------------------------------- |
|
33 CControlChannelReader* CControlChannelReader::NewL ( RSocket& aSocket, TBool aChannelType ) |
|
34 { |
|
35 return new ( ELeave ) CControlChannelReader ( aSocket, aChannelType ); |
|
36 } |
|
37 |
|
38 CControlChannelReader::CControlChannelReader ( RSocket& aSocket, TBool aChannelType ) |
|
39 : CControlChannelHandler ( aSocket, aChannelType ) |
|
40 { |
|
41 } |
|
42 |
|
43 CControlChannelReader::~CControlChannelReader () |
|
44 { |
|
45 Cancel (); |
|
46 } |
|
47 |
|
48 void CControlChannelReader::Read ( TControlMessage& aMessage, TRequestStatus& aStatus ) |
|
49 { |
|
50 if ( iChannelType == EFalse ) |
|
51 { |
|
52 __ASSERT_DEBUG ( !aMessage.IsDirty (), User::Panic ( KUPnPControlChannel, KErrInUse ) ); |
|
53 } |
|
54 |
|
55 iMessage = &aMessage; |
|
56 iChannelStatus = &aStatus; |
|
57 *iChannelStatus = KRequestPending; |
|
58 |
|
59 iSocket.RecvOneOrMore ( iMessage->MessagePtr(), 0, iStatus, iBytesReceived ); |
|
60 SetActive (); |
|
61 } |
|
62 |
|
63 void CControlChannelReader::RunL () |
|
64 { |
|
65 User::LeaveIfError ( iStatus.Int() ); |
|
66 TPtrC8 buffer ( iMessage->MessagePtr() ); |
|
67 TInt lenToSkip = 0; |
|
68 if ( iDataLength == 0 ) // ie; first message |
|
69 { |
|
70 // We should have more than 4 bytes ( ie; the identifier, which is of TUint, |
|
71 // size. here, sizeof (TUint) ) |
|
72 __ASSERT_DEBUG ( buffer.Length () > sizeof(TUint), User::Invariant() ); |
|
73 |
|
74 // Extract the first 4 bytes. we will have our identifier with it. |
|
75 TLex8 parser ( buffer ); |
|
76 TUint id = 0; |
|
77 User::LeaveIfError ( parser.Val ( id, EDecimal ) ); |
|
78 iMessage->SetId( id ); |
|
79 lenToSkip = sizeof (TUint); |
|
80 |
|
81 // Check how we are going to end with the message. checking the TControlMessage::MaxLength |
|
82 // or by explicitly specifying it. |
|
83 |
|
84 // Get the Maxlength and set it to the iMessage |
|
85 TInt maxLen = 0; |
|
86 User::LeaveIfError ( iSocket.GetOpt ( KCHMaxRecvLength, KCHOptionLevel, maxLen ) ); |
|
87 iMessage->SetMaxLength ( maxLen ); |
|
88 } |
|
89 TPtrC8 bufferToCopy ( buffer.Ptr() + lenToSkip, buffer.Length() - lenToSkip ); |
|
90 iMessage->MessagePtr().Copy ( bufferToCopy ); |
|
91 iDataLength += bufferToCopy.Length (); |
|
92 TInt maxLen = iMessage->MaxLength (); |
|
93 if ( maxLen == KErrUnknown ) |
|
94 { |
|
95 // We have to issue a GetOpt to see whether we complete with the message. |
|
96 TBool last = EFalse; |
|
97 User::LeaveIfError ( iSocket.GetOpt ( KCHLastMessage, KCHOptionLevel, last ) ); |
|
98 if ( last ) |
|
99 iMessage->SetLast (); |
|
100 } |
|
101 else |
|
102 { |
|
103 if ( iDataLength == maxLen ) |
|
104 { |
|
105 // We completed with the message. Set the message end flag as ETrue |
|
106 // and reset the iRecvLength |
|
107 iDataLength = 0; |
|
108 iMessage->SetLast (); |
|
109 } |
|
110 } |
|
111 |
|
112 if ( iMessage->IsLast () && iChannelType == EFalse ) |
|
113 { |
|
114 // client usage is done with the current control message |
|
115 // We have to set a dirty bit as well for not reusing this message again |
|
116 iMessage->SetDirty (); |
|
117 } |
|
118 |
|
119 User::RequestComplete ( iChannelStatus, KErrNone ); |
|
120 } |
|
121 |
|
122 void CControlChannelReader::DoCancel () |
|
123 { |
|
124 iSocket.CancelRecv (); |
|
125 User::RequestComplete ( iChannelStatus, KErrCancel ); |
|
126 } |
|
127 |
|
128 // --------------------------------------------------------------- |
|
129 |
|
130 CControlChannelWriter* CControlChannelWriter::NewL ( RSocket& aSocket, TBool aChannelType ) |
|
131 { |
|
132 return new ( ELeave ) CControlChannelWriter ( aSocket, aChannelType ); |
|
133 } |
|
134 |
|
135 CControlChannelWriter::CControlChannelWriter ( RSocket& aSocket, TBool aChannelType ) |
|
136 : CControlChannelHandler ( aSocket, aChannelType) |
|
137 { |
|
138 } |
|
139 |
|
140 CControlChannelWriter::~CControlChannelWriter () |
|
141 { |
|
142 Cancel (); |
|
143 } |
|
144 |
|
145 void CControlChannelWriter::Write ( TControlMessage& aMessage, TRequestStatus& aStatus ) |
|
146 { |
|
147 if ( iChannelType ) |
|
148 { |
|
149 __ASSERT_DEBUG ( !aMessage.IsDirty (), User::Panic ( KUPnPControlChannel, KErrInUse ) ); |
|
150 } |
|
151 |
|
152 iMessage = &aMessage; |
|
153 iChannelStatus = &aStatus; |
|
154 *iChannelStatus = KRequestPending; |
|
155 |
|
156 if ( iMessage->Id () == 0 ) |
|
157 { |
|
158 // Message ID is not set. Typically when we do the first send after connect |
|
159 // ie; Client side RControlChannel operation. |
|
160 iMessage->SetId ( iSocket.SubSessionHandle () ); |
|
161 } |
|
162 |
|
163 if ( iMessage->MaxLength() != KErrUnknown ) |
|
164 { |
|
165 TCHMessageOption option ( iMessage->Id (), iMessage->MaxLength () ); |
|
166 TPckg < TCHMessageOption > optionBuf ( option ); |
|
167 // Set the total size if set in the TControlMessage |
|
168 iSocket.SetOpt ( KCHMaxRecvLength, KCHOptionLevel, optionBuf ); |
|
169 } |
|
170 |
|
171 if ( iMessage->IsLast () && iChannelType ) |
|
172 { |
|
173 // server usage is done with the current control message |
|
174 // We have to set a dirty bit as well for not reusing this message again |
|
175 iMessage->SetDirty (); |
|
176 } |
|
177 |
|
178 iSocket.Send ( iMessage->MessageDes(), iMessage->Id(), iStatus ); |
|
179 SetActive (); |
|
180 } |
|
181 |
|
182 void CControlChannelWriter::RunL () |
|
183 { |
|
184 User::LeaveIfError ( iStatus.Int() ); |
|
185 if ( iMessage->MaxLength() == KErrUnknown && iMessage->IsLast () ) |
|
186 { |
|
187 TCHMessageOption option ( iMessage->Id (), iMessage->IsLast () ); |
|
188 TPckg < TCHMessageOption > optionBuf ( option ); |
|
189 |
|
190 // SetOpt to know whether we are sending the last message |
|
191 User::LeaveIfError ( iSocket.SetOpt( KCHLastMessage, KCHOptionLevel, optionBuf ) ); |
|
192 } |
|
193 |
|
194 User::RequestComplete ( iChannelStatus, KErrNone ); |
|
195 } |
|
196 |
|
197 void CControlChannelWriter::DoCancel () |
|
198 { |
|
199 iSocket.CancelSend (); |
|
200 User::RequestComplete ( iChannelStatus, KErrCancel ); |
|
201 } |
|
202 |
|
203 // ---------------------------------------------------------------- |
|
204 |
|
205 CControlChannel* CControlChannel::NewL ( RSocketServ& aSocketServ, RSubConnection& aSubConnection, TUint aAddrFamily, TBool aChannelType, TUint aProtocol, const TConnectionDetails& aConnInfo ) |
|
206 { |
|
207 CControlChannel* self = new (ELeave) CControlChannel ( ); |
|
208 CleanupStack::PushL ( self ); |
|
209 self->ConstructL ( aSocketServ, aSubConnection, aAddrFamily, aChannelType, aProtocol, aConnInfo ); |
|
210 CleanupStack::Pop (); // self |
|
211 return self; |
|
212 } |
|
213 |
|
214 CControlChannel::~CControlChannel () |
|
215 { |
|
216 delete iReader; |
|
217 delete iWriter; |
|
218 iSocket.Close (); |
|
219 } |
|
220 |
|
221 void CControlChannel::Recv ( TControlMessage& aMessage, TRequestStatus& aStatus ) |
|
222 { |
|
223 iReader->Read ( aMessage, aStatus ); |
|
224 } |
|
225 |
|
226 void CControlChannel::Send ( TControlMessage& aMessage, TRequestStatus& aStatus ) |
|
227 { |
|
228 iWriter->Write ( aMessage, aStatus ); |
|
229 } |
|
230 |
|
231 |
|
232 CControlChannel::CControlChannel ( ) |
|
233 { |
|
234 } |
|
235 |
|
236 void CControlChannel::ConstructL ( RSocketServ& aSocketServ, RSubConnection& aSubConnection, TUint aAddrFamily, TBool aChannelType, TUint aProtocol, const TConnectionDetails& aConnInfo ) |
|
237 { |
|
238 User::LeaveIfError ( iSocket.Open( aSocketServ, aAddrFamily, KSockStream, aProtocol, aSubConnection ) ); |
|
239 |
|
240 if ( aConnInfo.iAddr ) |
|
241 { |
|
242 TRequestStatus connectStatus; |
|
243 iSocket.Connect ( *(aConnInfo.iAddr), connectStatus ); |
|
244 User::WaitForRequest ( connectStatus ); // This is Ok to do... as in the esock server ( actually flow )will just store the |
|
245 // connect address internally and not attempting to do an actual connect. As good as |
|
246 // setopt but we need the socket to know that this is a connected socket. |
|
247 User::LeaveIfError ( connectStatus.Int() ); |
|
248 } |
|
249 |
|
250 if ( aConnInfo.iUri.Length() != 0 ) |
|
251 { |
|
252 // Do a setopt to inform the esock about our URI |
|
253 User::LeaveIfError ( iSocket.SetOpt ( KCHAbsoluteUri, KCHOptionLevel, aConnInfo.iUri ) ); |
|
254 } |
|
255 |
|
256 iReader = CControlChannelReader::NewL ( iSocket, aChannelType ); |
|
257 iWriter = CControlChannelWriter::NewL ( iSocket, aChannelType ); |
|
258 } |