1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/RedirCli/REDIRCLI.CPP Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,242 @@
1.4 +// Copyright (c) 1999-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +//
1.18 +
1.19 +#include <e32std.h>
1.20 +#include <redircli.h>
1.21 +#include <redircliinternal.h>
1.22 +
1.23 +#ifndef EKA2
1.24 +GLDEF_C TInt E32Dll(TDllReason)
1.25 + {
1.26 + return KErrNone;
1.27 + }
1.28 +#endif
1.29 +
1.30 +// server name
1.31 +_LIT(KRedirServerName,"RedirServer");
1.32 +
1.33 +enum TRedirStream
1.34 + {
1.35 + ERedirRead,
1.36 + ERedirWrite,
1.37 + ERedirFlush
1.38 + };
1.39 +
1.40 +// A version must be specified when creating a session with the server
1.41 +const TUint KRedirServMajorVersionNumber=1;
1.42 +const TUint KRedirServMinorVersionNumber=0;
1.43 +const TUint KRedirServBuildVersionNumber=0;
1.44 +
1.45 +
1.46 +//**********************************
1.47 +// CRedirSession
1.48 +//**********************************
1.49 +// This class represents a session in the server.
1.50 +// Functions are provided to respond appropriately to client messages.
1.51 +NONSHARABLE_CLASS(CRedirSession) : public CSession2
1.52 + {
1.53 +public:
1.54 + static CRedirSession* NewL(CStreamBase2* aStream);
1.55 + //service request
1.56 + virtual void ServiceL(const RMessage2 &aMessage);
1.57 +private :
1.58 + // construct/destruct
1.59 + CRedirSession(CStreamBase2* aStream);
1.60 + ~CRedirSession();
1.61 + //services available to handle read/write requests
1.62 + TInt WriteStreamL(const RMessage2& aMessage);
1.63 +private:
1.64 + CStreamBase2* iStream;
1.65 + };
1.66 +
1.67 +// ------------------- CRedirServer2 implementation --------------------------
1.68 +
1.69 +
1.70 +/**
1.71 +Constructs and allocates memory for a new CRedirServer2 object.
1.72 +@return RedirServer
1.73 +@param aStreamFactory Stream factory.
1.74 +*/
1.75 +EXPORT_C CRedirServer2* CRedirServer2::NewL(CStreamFactoryBase2* aStreamFactory)
1.76 + {
1.77 + CRedirServer2* r=new(ELeave) CRedirServer2(aStreamFactory);
1.78 + CleanupStack::PushL(r);
1.79 + r->StartL(KRedirServerName);
1.80 + CleanupStack::Pop();
1.81 + return r;
1.82 + }
1.83 +
1.84 +/**
1.85 +Default constructor
1.86 +@param aStreamFactory Stream factory.
1.87 +*/
1.88 +CRedirServer2::CRedirServer2(CStreamFactoryBase2* aStreamFactory)
1.89 + : CServer2(EPriority)
1.90 + {
1.91 + iStreamFactory = aStreamFactory;
1.92 + }
1.93 +
1.94 +/**
1.95 +Virtual destructor
1.96 +*/
1.97 +CRedirServer2::~CRedirServer2()
1.98 + {
1.99 + }
1.100 +
1.101 +/**
1.102 +Create a new server session.
1.103 +Checks if the aVersion is the right version and make a new session.
1.104 +@leave KErrNotSupported
1.105 +@return Sharable Session for all threads within a single process.
1.106 +@param aVersion Contains version information.
1.107 +A version is defined by a set of three numbers:major,minor,build version number
1.108 +*/
1.109 +CSession2* CRedirServer2::NewSessionL(const TVersion& aVersion, const RMessage2& /*aMessage*/) const
1.110 + {
1.111 +
1.112 + TVersion v(KRedirServMajorVersionNumber,KRedirServMinorVersionNumber,
1.113 + KRedirServBuildVersionNumber);
1.114 + if (!User::QueryVersionSupported(v,aVersion))
1.115 + User::Leave(KErrNotSupported);
1.116 + // get a stream object from the current stream factory
1.117 + CStreamBase2* stream = iStreamFactory->GetStream();
1.118 + return CRedirSession::NewL(stream);
1.119 + }
1.120 +
1.121 +/**
1.122 +Sets the stream factory
1.123 +@param aStreamFactory a factory to be set
1.124 +*/
1.125 +EXPORT_C void CRedirServer2::SetStreamFactory(CStreamFactoryBase2* aStreamFactory)
1.126 + {
1.127 + // set the stream factory
1.128 + iStreamFactory = aStreamFactory;
1.129 + }
1.130 +
1.131 +// ------------------- CRedirSession2 implementation --------------------------
1.132 +CRedirSession* CRedirSession::NewL(CStreamBase2* aStream)
1.133 + {
1.134 + return new(ELeave) CRedirSession(aStream);
1.135 + }
1.136 +
1.137 +CRedirSession::CRedirSession(CStreamBase2* aStream)
1.138 + : iStream(aStream)
1.139 + {}
1.140 +
1.141 +CRedirSession::~CRedirSession()
1.142 + {
1.143 + // The stream returned by GetStream() is effectively a singleton as
1.144 + // implemented in the current redirector, so we leave deletion to
1.145 + // the factory
1.146 + }
1.147 +
1.148 +void CRedirSession::ServiceL(const RMessage2& aMessage)
1.149 + {
1.150 + TInt ret = KErrNone;
1.151 + switch (aMessage.Function())
1.152 + {
1.153 + case ERedirRead:
1.154 + // call the Stream to do the read for us
1.155 + iStream->Read(aMessage);
1.156 + break;
1.157 + case ERedirWrite:
1.158 + TRAP(ret,WriteStreamL(aMessage));
1.159 + aMessage.Complete(ret);
1.160 + break;
1.161 + case ERedirFlush:
1.162 + // TO DO: ignore flushing?
1.163 + aMessage.Complete(ret);
1.164 + break;
1.165 + default:
1.166 + aMessage.Complete(KErrNotSupported);
1.167 + break;
1.168 + }
1.169 + return;
1.170 + }
1.171 +
1.172 +TInt CRedirSession::WriteStreamL(const RMessage2& aMessage)
1.173 + {
1.174 + const TInt KMaxTransfer=256;
1.175 + //
1.176 + // retrieve the length of the buffer the client wants to write
1.177 + TInt length = aMessage.Int1();
1.178 + // retrieve the descriptor to be written
1.179 + TBuf8<KMaxTransfer> bufDes;
1.180 + for (TInt offset=0;offset<length;offset+=KMaxTransfer)
1.181 + {
1.182 + aMessage.ReadL(0, bufDes, offset);
1.183 + // send descriptor to stream for printing
1.184 + iStream->Write(bufDes);
1.185 + }
1.186 + return KErrNone;
1.187 + }
1.188 +
1.189 +// -------------- CLIENT IMPLEMENTATION -----------------------------
1.190 +
1.191 +EXPORT_C TInt RRedirSession2::Connect()
1.192 + {
1.193 + TVersion version(KRedirServMajorVersionNumber,KRedirServMinorVersionNumber,
1.194 + KRedirServBuildVersionNumber);
1.195 + return CreateSession(KRedirServerName,version,1); // only one message allowed - no concurrency
1.196 + }
1.197 +
1.198 +TInt RRedirSession2::CheckEOF(TRequestStatus& aStatus)
1.199 + {
1.200 + if (Handle()!=0)
1.201 + return KErrNone;
1.202 + TRequestStatus* aStatusPtr=&aStatus;
1.203 + User::RequestComplete(aStatusPtr,KErrEof);
1.204 + return KErrEof;
1.205 + }
1.206 +
1.207 +EXPORT_C void RRedirSession2::Read(TRequestStatus& aStatus, TDes8& aDes)
1.208 + {
1.209 + Read(aStatus, aDes, aDes.MaxLength());
1.210 + }
1.211 +
1.212 +EXPORT_C void RRedirSession2::Read(TRequestStatus& aStatus, TDes8& aDes, TInt aLength)
1.213 + {
1.214 + if (CheckEOF(aStatus))
1.215 + return;
1.216 + SendReceive(ERedirRead,TIpcArgs(&aDes,aLength),aStatus);
1.217 + }
1.218 +
1.219 +EXPORT_C void RRedirSession2::Write(TRequestStatus& aStatus, const TDesC8& aDes)
1.220 + {
1.221 + Write(aStatus, aDes, aDes.Length());
1.222 + }
1.223 +
1.224 +EXPORT_C void RRedirSession2::Write(TRequestStatus& aStatus, const TDesC8& aDes, TInt aLength)
1.225 +//
1.226 +// Write aLength bytes
1.227 +//
1.228 + {
1.229 + if (CheckEOF(aStatus))
1.230 + return;
1.231 + SendReceive(ERedirWrite,TIpcArgs(&aDes,aLength),aStatus);
1.232 + }
1.233 +
1.234 +EXPORT_C void RRedirSession2::Flush(TRequestStatus& aStatus)
1.235 +//
1.236 +// Flush output
1.237 +//
1.238 + {
1.239 + if (CheckEOF(aStatus))
1.240 + return;
1.241 + SendReceive(ERedirFlush,aStatus);
1.242 + }
1.243 +
1.244 +
1.245 +