libraries/clogger/src/SensibleClient.cpp
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // SensibleClient.cpp
       
     2 // 
       
     3 // Copyright (c) 2006 - 2010 Accenture. All rights reserved.
       
     4 // This component and the accompanying materials are made available
       
     5 // under the terms of the "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 // Accenture - Initial contribution
       
    11 //
       
    12 
       
    13 #include "SensibleClient.h"
       
    14 #include "SensibleClient_internals.h"
       
    15 #include "cliserv.h"
       
    16 #include "SensibleCompat.h"
       
    17 
       
    18 _LIT(KClientPanic, "SensibleClient");
       
    19 #define DebugPanic() User::Panic(KClientPanic, - __LINE__)
       
    20 #define AssertPanic(x) User::Panic(KClientPanic, x)
       
    21 enum TPanic
       
    22 	{
       
    23 	EReadOffEndOfCallback, // A call to TCallbackParser::Next when there's nothing more in the callback buffer
       
    24 	EBadType, // A call to TCallbackParser::NextX where X doesn't match the type of the next thing in the buffer
       
    25 	};
       
    26 
       
    27 #ifdef EKA2
       
    28 
       
    29 static TInt StartServer()
       
    30 //
       
    31 // Start the server process. Simultaneous launching
       
    32 // of two such processes should be detected when the second one attempts to
       
    33 // create the server object, failing with KErrAlreadyExists.
       
    34 //
       
    35 	{
       
    36 	const TUidType serverUid(KNullUid,KNullUid,KServerUid3);
       
    37 	RProcess server;
       
    38 	TInt r=server.Create(KMyServerImg,KNullDesC,serverUid);
       
    39 	if (r!=KErrNone)
       
    40 		return r;
       
    41 	TRequestStatus stat;
       
    42 	server.Rendezvous(stat);
       
    43 	if (stat!=KRequestPending)
       
    44 		server.Kill(0);		// abort startup
       
    45 	else
       
    46 		server.Resume();	// logon OK - start the server
       
    47 	User::WaitForRequest(stat);		// wait for start or death
       
    48 	// we are expected to return a negative error code from this function so if the server died 
       
    49 	// with a positive (or zero) code, map it to KErrServerTerminated
       
    50 	r = (server.ExitType() != EExitPending && stat.Int() >= 0) ? KErrServerTerminated : stat.Int();
       
    51 	server.Close();
       
    52 	return r;
       
    53 	}
       
    54 
       
    55 
       
    56 
       
    57 #else
       
    58 
       
    59 #include <e32math.h>
       
    60 
       
    61 inline TServerStart::TServerStart(TRequestStatus& aStatus)
       
    62 	:iId(RThread().Id()),iStatus(&aStatus)
       
    63 	{aStatus=KRequestPending;}
       
    64 inline TPtrC TServerStart::AsCommand() const
       
    65 	{return TPtrC(reinterpret_cast<const TText*>(this),sizeof(TServerStart)/sizeof(TText));}
       
    66 
       
    67 static TInt StartServer()
       
    68 //
       
    69 // Start the server process/thread which lives in an EPOCEXE object
       
    70 //
       
    71 	{
       
    72 	TRequestStatus started;
       
    73 	TServerStart start(started);
       
    74 
       
    75 	const TUidType serverUid(KNullUid,KNullUid,KServerUid3);
       
    76 
       
    77 #ifdef __WINS__
       
    78 	//
       
    79 	// In WINS the server is a DLL, the exported entrypoint returns a TInt
       
    80 	// which represents the real entry-point for the server thread
       
    81 	//
       
    82 	RLibrary lib;
       
    83 	TInt r=lib.Load(KMyServerImg,serverUid);
       
    84 	if (r!=KErrNone)
       
    85 		return r;
       
    86 	TLibraryFunction ordinal1=lib.Lookup(1);
       
    87 	TThreadFunction serverFunc=reinterpret_cast<TThreadFunction>(ordinal1());
       
    88 	//
       
    89 	// To deal with the unique thread (+semaphore!) naming in EPOC, and that we may
       
    90 	// be trying to restart a server that has just exited we attempt to create a
       
    91 	// unique thread name for the server.
       
    92 	// This uses Math::Random() to generate a 32-bit random number for the name
       
    93 	//
       
    94 	TName name(KMyServerName);
       
    95 	name.AppendNum(Math::Random(),EHex);
       
    96 	RThread server;
       
    97 	r=server.Create(name,serverFunc,
       
    98 					KMyServerStackSize,
       
    99 					&start,&lib,NULL,
       
   100 					KMyServerInitHeapSize,KMyServerMaxHeapSize,EOwnerProcess);
       
   101 	lib.Close();	// if successful, server thread has handle to library now
       
   102 #else
       
   103 	//
       
   104 	// EPOC is easy, we just create a new server process. Simultaneous launching
       
   105 	// of two such processes should be detected when the second one attempts to
       
   106 	// create the server object, failing with KErrAlreadyExists.
       
   107 	//
       
   108 	RProcess server;
       
   109 	TInt r=server.Create(KMyServerImg,start.AsCommand(),serverUid);
       
   110 #endif
       
   111 	if (r!=KErrNone)
       
   112 		return r;
       
   113 	TRequestStatus died;
       
   114 	server.Logon(died);
       
   115 	if (died!=KRequestPending)
       
   116 		{
       
   117 		// logon failed - server is not yet running, so cannot have terminated
       
   118 		User::WaitForRequest(died);	// eat signal
       
   119 		server.Kill(0);				// abort startup
       
   120 		server.Close();
       
   121 		return died.Int();
       
   122 		}
       
   123 	//
       
   124 	// logon OK - start the server
       
   125 	server.Resume();
       
   126 	User::WaitForRequest(started,died);		// wait for start or death
       
   127 	if (started==KRequestPending)
       
   128 		{
       
   129 		// server has died, never made it to the startup signal
       
   130 		server.Close();
       
   131 		return died.Int();
       
   132 		}
       
   133 	//
       
   134 	// server started (at last). Cancel and consume the death-notification
       
   135 	// before reporting success
       
   136 	server.LogonCancel(died);
       
   137 	server.Close();
       
   138 	User::WaitForRequest(died);		// eat the signal (from the cancel)
       
   139 	return KErrNone;
       
   140 	}
       
   141 
       
   142 TInt E32Dll(TDllReason)
       
   143 	{
       
   144 	return 0;
       
   145 	}
       
   146 
       
   147 
       
   148 #endif
       
   149 
       
   150 RSensibleSessionBody::RSensibleSessionBody()
       
   151 	: iDispatcher(NULL)
       
   152 	{}
       
   153 
       
   154 TInt RSensibleSessionBody::Connect(TInt aMessageSlots, TBool aStartCallbackDispatcher)
       
   155 //
       
   156 // Connect to the server, attempting to start it if necessary
       
   157 //
       
   158 	{
       
   159 	TInt r = KErrNone;
       
   160 	TInt retry=2;
       
   161 	for (;;)
       
   162 		{
       
   163 		r=DoCreateSession(KMyServerName, TVersion(0,0,0), aMessageSlots);
       
   164 		if (r!=KErrNotFound && r!=KErrServerTerminated)
       
   165 			break;
       
   166 		if (--retry==0)
       
   167 			break;
       
   168 		r=StartServer();
       
   169 		if (r!=KErrNone && r!=KErrAlreadyExists)
       
   170 			break;
       
   171 		}
       
   172 	if (r == KErrNone && aStartCallbackDispatcher)
       
   173 		{
       
   174 		if (StartCallbackDispatcher() != KErrNone)
       
   175 			{
       
   176 			Close();
       
   177 			return KErrNoMemory;
       
   178 			}
       
   179 		}
       
   180 	return r;
       
   181 	}
       
   182 
       
   183 TInt RSensibleSessionBody::StartCallbackDispatcher()
       
   184 	{
       
   185 	if (!iDispatcher)
       
   186 		{
       
   187 		iDispatcher = new CServerCallbackDispatcher(*this);
       
   188 		}
       
   189 	else if (!iDispatcher->IsActive())
       
   190 		{
       
   191 		iDispatcher->Register();
       
   192 		}
       
   193 	return iDispatcher == NULL ? KErrNoMemory : KErrNone;
       
   194 	}
       
   195 
       
   196 void RSensibleSessionBody::StopCallbackDispatcher()
       
   197 	{
       
   198 	if (iDispatcher) iDispatcher->Cancel();
       
   199 	}
       
   200 
       
   201 void RSensibleSessionBody::DispatchCallbackL(TServerCallback& /*aCallback*/, TCallbackParser& /*aParser*/)
       
   202 	{
       
   203 	// Implemented by subclass
       
   204 	}
       
   205 
       
   206 void RSensibleSessionBody::ServerDiedL()
       
   207 	{
       
   208 	// Implemented by subclass
       
   209 	}
       
   210 
       
   211 void RSensibleSessionBody::Close()
       
   212 	{
       
   213 	delete iDispatcher;
       
   214 	iDispatcher = NULL;
       
   215 	}
       
   216 
       
   217 // Not happy this actually works or is worth it
       
   218 /*
       
   219 void CCleanupAndComplete::RunL()
       
   220 	{
       
   221 	if (iClientStatus)
       
   222 		{
       
   223 		// We use this class with a null pointer to allow non-asynchronous (ie blind) APIs to still have the ability to cleanup resource when they are completed
       
   224 		User::RequestComplete(iClientStatus, iStatus.Int());
       
   225 		}
       
   226 	delete this;
       
   227 	}
       
   228 
       
   229 void CCleanupAndComplete::DoCancel()
       
   230 	{
       
   231 	//TODO what? Probably delete this in some way
       
   232 	//delete this;
       
   233 	}
       
   234 
       
   235 CCleanupAndComplete::CCleanupAndComplete(TRequestStatus* aClientStatus)
       
   236 	: CActive(EPriorityStandard), iClientStatus(aClientStatus)
       
   237 	{
       
   238 	CActiveScheduler::Add(this);
       
   239 	if (iClientStatus)
       
   240 		{
       
   241 		*iClientStatus = KRequestPending;
       
   242 		}
       
   243 	}
       
   244 	
       
   245 CCleanupAndComplete* CCleanupAndComplete::NewLC(TRequestStatus* aClientStatus)
       
   246 	{
       
   247 	CCleanupAndComplete* self = new(ELeave) CCleanupAndComplete(aClientStatus);
       
   248 	CleanupStack::PushL(self);
       
   249 	return self;
       
   250 	}
       
   251 		
       
   252 TInt CCleanupAndComplete::Create(TRequestStatus*& aStatus, const TDesC8& aDes, TInt& aIpcArg)
       
   253 	{
       
   254 	CCleanupAndComplete* self = new CCleanupAndComplete(aStatus);
       
   255 	TInt err = KErrNone;
       
   256 	if (!self) err = KErrNoMemory;
       
   257 	HBufC8* buf = NULL;
       
   258 	if (!err)
       
   259 		{
       
   260 		buf = aDes.Alloc();
       
   261 		}
       
   262 	if (!buf) err = KErrNoMemory;
       
   263 	if (!err)
       
   264 		{
       
   265 		err = self->iWithoutDestructor.Append(buf);
       
   266 		}
       
   267 	if (err) 
       
   268 		{
       
   269 		delete buf;
       
   270 		delete self;
       
   271 		return KErrNoMemory;
       
   272 		}
       
   273 	aIpcArg = (TInt)buf;
       
   274 	aStatus = &self->iStatus;
       
   275 	self->SetActive();
       
   276 	return KErrNone;
       
   277 	}
       
   278 
       
   279 TInt CCleanupAndComplete::Create(TRequestStatus*& aStatus, const TDesC16& aDes, TInt& aIpcArg)
       
   280 	{
       
   281 	CCleanupAndComplete* self = new CCleanupAndComplete(aStatus);
       
   282 	TInt err = KErrNone;
       
   283 	if (!self) err = KErrNoMemory;
       
   284 	HBufC16* buf = NULL;
       
   285 	if (!err)
       
   286 		{
       
   287 		buf = aDes.Alloc();
       
   288 		}
       
   289 	if (!buf) err = KErrNoMemory;
       
   290 	if (!err)
       
   291 		{
       
   292 		err = self->iWithoutDestructor.Append(buf);
       
   293 		}
       
   294 	if (err) 
       
   295 		{
       
   296 		delete buf;
       
   297 		delete self;
       
   298 		return KErrNoMemory;
       
   299 		}
       
   300 	aIpcArg = (TInt)buf;
       
   301 	aStatus = &self->iStatus;
       
   302 	self->SetActive();
       
   303 	return KErrNone;
       
   304 	}
       
   305 
       
   306 void CCleanupAndComplete::AddL(CBase* aWithDestructor)
       
   307 	{
       
   308 	User::LeaveIfError(iWithDestructor.Append(aWithDestructor));
       
   309 	}
       
   310 
       
   311 void CCleanupAndComplete::AddL(TAny* aWithoutDestructor)
       
   312 	{
       
   313 	User::LeaveIfError(iWithoutDestructor.Append(aWithoutDestructor));
       
   314 	}
       
   315 
       
   316 CCleanupAndComplete::~CCleanupAndComplete()
       
   317 	{
       
   318 	Cancel();
       
   319 	iWithDestructor.ResetAndDestroy();
       
   320 	for (TInt i = 0; i < iWithoutDestructor.Count(); i++)
       
   321 		{
       
   322 		delete iWithoutDestructor[i];
       
   323 		}
       
   324 	}
       
   325 
       
   326 void CCleanupAndComplete::SetActive()
       
   327 	{
       
   328 	CActive::SetActive();
       
   329 	}
       
   330 
       
   331 */
       
   332 
       
   333 CServerCallbackDispatcher::CServerCallbackDispatcher(RSensibleSessionBody& aSession)
       
   334 	: CActive(EPriorityStandard), iSession(aSession), iContextPtr(NULL, 0)
       
   335 	{
       
   336 	CActiveScheduler::Add(this);
       
   337 	Register();
       
   338 	}
       
   339 	
       
   340 void CServerCallbackDispatcher::Register()
       
   341 	{
       
   342 	iState = EWaitingForCallback;
       
   343 	IPC(args, &iNextCallback, 0,0,0);
       
   344 	iSession.DoSendReceive(ERegisterCallbackNotifier, args, iStatus);
       
   345 	SetActive();
       
   346 	}
       
   347 
       
   348 void CServerCallbackDispatcher::RunL()
       
   349 	{
       
   350 	DISOWN(iCachedCallbackResult8);
       
   351 	DISOWN(iCachedCallbackResult16);
       
   352 
       
   353 	if (iStatus == KErrServerTerminated)
       
   354 		{
       
   355 		iSession.ServerDiedL();
       
   356 		return;
       
   357 		}
       
   358 	else if (iStatus != KErrNone)
       
   359 		{
       
   360 		//TODO Do something...
       
   361 		__DEBUGGER();
       
   362 		return;
       
   363 		}
       
   364 
       
   365 	TServerCallback cb = iNextCallback();
       
   366 	TCallbackParser p(cb);
       
   367 
       
   368 	if (iState == EWaitingForCallback && cb.iContextLength != 0)
       
   369 		{
       
   370 		iState = EWaitingForContext;
       
   371 		DISOWN(iContext);
       
   372 		iContext = HBufC8::NewL(cb.iContextLength);
       
   373 		iContextPtr = iContext->Des();
       
   374 		IPC(args, &iContextPtr, 0,0,0);
       
   375 		iSession.DoSendReceive(EGetCallbackContext, args, iStatus);
       
   376 		SetActive();
       
   377 		return;
       
   378 		}
       
   379 
       
   380 	if (iState == EWaitingForContext)
       
   381 		{
       
   382 		p.SetContext(*iContext);
       
   383 		}
       
   384 
       
   385 	Register();
       
   386 	iSession.DispatchCallbackL(cb, p);
       
   387 	}
       
   388 
       
   389 void CServerCallbackDispatcher::DoCancel()
       
   390 	{
       
   391 	IPC(args, 0,0,0,0);
       
   392 	iSession.DoSendReceive(ECancelCallbackNotifier, args);
       
   393 	}
       
   394 
       
   395 CServerCallbackDispatcher::~CServerCallbackDispatcher()
       
   396 	{
       
   397 	Cancel();
       
   398 	DISOWN(iCachedCallbackResult8);
       
   399 	DISOWN(iCachedCallbackResult16);
       
   400 	DISOWN(iContext);
       
   401 	}
       
   402 
       
   403 
       
   404 TCallbackParser::TCallbackParser(const TServerCallback& aCallback)
       
   405 	: iCallback(aCallback), iContext(NULL), iNextPtr(aCallback.iData.Ptr()), iInContext(EFalse)
       
   406 	{}
       
   407 
       
   408 void TCallbackParser::SetContext(TDesC8& aContext)
       
   409 	{
       
   410 	iContext = &aContext;
       
   411 	}
       
   412 
       
   413 #define DOGET(T, type, name) T name; TAny* res = Next(sizeof(T), #type); Mem::Copy(&name, res, sizeof(T));
       
   414 #define GET(T, type) DOGET(T, type, __result); return __result;
       
   415 
       
   416 TInt TCallbackParser::NextInt()
       
   417 	{
       
   418 	GET(TInt, i);
       
   419 	}
       
   420 
       
   421 TUint TCallbackParser::NextUint()
       
   422 	{
       
   423 	GET(TUint, u);
       
   424 	}
       
   425 
       
   426 TPoint TCallbackParser::NextPoint()
       
   427 	{
       
   428 	GET(TPoint, P);
       
   429 	}
       
   430 
       
   431 TSize TCallbackParser::NextSize()
       
   432 	{
       
   433 	GET(TSize, S);
       
   434 	}
       
   435 
       
   436 TRgb TCallbackParser::NextRgb()
       
   437 	{
       
   438 	GET(TRgb, G);
       
   439 	}
       
   440 
       
   441 TRect TCallbackParser::NextRect()
       
   442 	{
       
   443 	GET(TRect, R);
       
   444 	}
       
   445 
       
   446 TPtrC TCallbackParser::NextDesC()
       
   447 	{
       
   448 	DOGET(TInt, i, len);
       
   449 	const TUint16* ptr = reinterpret_cast<const TUint16*>(Next(len*2, "D"));
       
   450 	if (TUint(ptr)&1)
       
   451 		{
       
   452 		// Need to allow for padding
       
   453 		ptr = (TUint16*)(((TUint8*)ptr)+1);
       
   454 		iNextPtr++; // And also move iNextPtr to the right place for the next read
       
   455 		}
       
   456 	return TPtrC(ptr, len);
       
   457 	}
       
   458 
       
   459 TPtrC8 TCallbackParser::NextDesC8()
       
   460 	{
       
   461 	DOGET(TInt, i, len);
       
   462 	const TUint8* ptr = reinterpret_cast<const TUint8*>(Next(len, "8"));
       
   463 	return TPtrC8(ptr, len);
       
   464 	}
       
   465 
       
   466 void* TCallbackParser::Next(TInt aSize, char* aType)
       
   467 	{
       
   468 	const TUint8* endPtr = iInContext ? iContext->Ptr() + iContext->Size() : iCallback.iData.Ptr() + iCallback.iData.Size();
       
   469 
       
   470 	if (iNextPtr + aSize + 1 > endPtr)
       
   471 		{
       
   472 		// No room for arg and type
       
   473 		if (!iInContext && iContext)
       
   474 			{
       
   475 			// try moving to context
       
   476 			iNextPtr = (TUint8*)iContext->Ptr();
       
   477 			iInContext = ETrue;
       
   478 			}
       
   479 		else
       
   480 			{
       
   481 			// Either there's no context, or we're already in it and reading off the end
       
   482 			AssertPanic(EReadOffEndOfCallback);
       
   483 			}
       
   484 		}
       
   485 
       
   486 	TUint8 nextType = *iNextPtr; //iInContext ? iCallback.iContextTypes[iIdx] : iCallback.iDataTypes[iIdx];
       
   487 	iNextPtr++;
       
   488 	void* result = (void*)iNextPtr;
       
   489 	
       
   490 	__ASSERT_ALWAYS(nextType == *aType, AssertPanic(EBadType)); // Types must match
       
   491 
       
   492 	iNextPtr = iNextPtr + aSize;
       
   493 	return result;
       
   494 	}