Update contrib.
1 // Copyright (c) 1999-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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
18 #include <redircliinternal.h>
21 GLDEF_C TInt E32Dll(TDllReason)
28 _LIT(KRedirServerName,"RedirServer");
37 // A version must be specified when creating a session with the server
38 const TUint KRedirServMajorVersionNumber=1;
39 const TUint KRedirServMinorVersionNumber=0;
40 const TUint KRedirServBuildVersionNumber=0;
43 //**********************************
45 //**********************************
46 // This class represents a session in the server.
47 // Functions are provided to respond appropriately to client messages.
48 NONSHARABLE_CLASS(CRedirSession) : public CSession2
51 static CRedirSession* NewL(CStreamBase2* aStream);
53 virtual void ServiceL(const RMessage2 &aMessage);
56 CRedirSession(CStreamBase2* aStream);
58 //services available to handle read/write requests
59 TInt WriteStreamL(const RMessage2& aMessage);
61 CStreamBase2* iStream;
64 // ------------------- CRedirServer2 implementation --------------------------
68 Constructs and allocates memory for a new CRedirServer2 object.
70 @param aStreamFactory Stream factory.
72 EXPORT_C CRedirServer2* CRedirServer2::NewL(CStreamFactoryBase2* aStreamFactory)
74 CRedirServer2* r=new(ELeave) CRedirServer2(aStreamFactory);
75 CleanupStack::PushL(r);
76 r->StartL(KRedirServerName);
83 @param aStreamFactory Stream factory.
85 CRedirServer2::CRedirServer2(CStreamFactoryBase2* aStreamFactory)
88 iStreamFactory = aStreamFactory;
94 CRedirServer2::~CRedirServer2()
99 Create a new server session.
100 Checks if the aVersion is the right version and make a new session.
101 @leave KErrNotSupported
102 @return Sharable Session for all threads within a single process.
103 @param aVersion Contains version information.
104 A version is defined by a set of three numbers:major,minor,build version number
106 CSession2* CRedirServer2::NewSessionL(const TVersion& aVersion, const RMessage2& /*aMessage*/) const
109 TVersion v(KRedirServMajorVersionNumber,KRedirServMinorVersionNumber,
110 KRedirServBuildVersionNumber);
111 if (!User::QueryVersionSupported(v,aVersion))
112 User::Leave(KErrNotSupported);
113 // get a stream object from the current stream factory
114 CStreamBase2* stream = iStreamFactory->GetStream();
115 return CRedirSession::NewL(stream);
119 Sets the stream factory
120 @param aStreamFactory a factory to be set
122 EXPORT_C void CRedirServer2::SetStreamFactory(CStreamFactoryBase2* aStreamFactory)
124 // set the stream factory
125 iStreamFactory = aStreamFactory;
128 // ------------------- CRedirSession2 implementation --------------------------
129 CRedirSession* CRedirSession::NewL(CStreamBase2* aStream)
131 return new(ELeave) CRedirSession(aStream);
134 CRedirSession::CRedirSession(CStreamBase2* aStream)
138 CRedirSession::~CRedirSession()
140 // The stream returned by GetStream() is effectively a singleton as
141 // implemented in the current redirector, so we leave deletion to
145 void CRedirSession::ServiceL(const RMessage2& aMessage)
148 switch (aMessage.Function())
151 // call the Stream to do the read for us
152 iStream->Read(aMessage);
155 TRAP(ret,WriteStreamL(aMessage));
156 aMessage.Complete(ret);
159 // TO DO: ignore flushing?
160 aMessage.Complete(ret);
163 aMessage.Complete(KErrNotSupported);
169 TInt CRedirSession::WriteStreamL(const RMessage2& aMessage)
171 const TInt KMaxTransfer=256;
173 // retrieve the length of the buffer the client wants to write
174 TInt length = aMessage.Int1();
175 // retrieve the descriptor to be written
176 TBuf8<KMaxTransfer> bufDes;
177 for (TInt offset=0;offset<length;offset+=KMaxTransfer)
179 aMessage.ReadL(0, bufDes, offset);
180 // send descriptor to stream for printing
181 iStream->Write(bufDes);
186 // -------------- CLIENT IMPLEMENTATION -----------------------------
188 EXPORT_C TInt RRedirSession2::Connect()
190 TVersion version(KRedirServMajorVersionNumber,KRedirServMinorVersionNumber,
191 KRedirServBuildVersionNumber);
192 return CreateSession(KRedirServerName,version,1); // only one message allowed - no concurrency
195 TInt RRedirSession2::CheckEOF(TRequestStatus& aStatus)
199 TRequestStatus* aStatusPtr=&aStatus;
200 User::RequestComplete(aStatusPtr,KErrEof);
204 EXPORT_C void RRedirSession2::Read(TRequestStatus& aStatus, TDes8& aDes)
206 Read(aStatus, aDes, aDes.MaxLength());
209 EXPORT_C void RRedirSession2::Read(TRequestStatus& aStatus, TDes8& aDes, TInt aLength)
211 if (CheckEOF(aStatus))
213 SendReceive(ERedirRead,TIpcArgs(&aDes,aLength),aStatus);
216 EXPORT_C void RRedirSession2::Write(TRequestStatus& aStatus, const TDesC8& aDes)
218 Write(aStatus, aDes, aDes.Length());
221 EXPORT_C void RRedirSession2::Write(TRequestStatus& aStatus, const TDesC8& aDes, TInt aLength)
223 // Write aLength bytes
226 if (CheckEOF(aStatus))
228 SendReceive(ERedirWrite,TIpcArgs(&aDes,aLength),aStatus);
231 EXPORT_C void RRedirSession2::Flush(TRequestStatus& aStatus)
236 if (CheckEOF(aStatus))
238 SendReceive(ERedirFlush,aStatus);