commsfwsupport/commselements/serverden/src/sd_safemessage.cpp
changeset 0 dfb7c4ff071f
equal deleted inserted replaced
-1:000000000000 0:dfb7c4ff071f
       
     1 // Copyright (c) 2006-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 /**
       
    17  @file
       
    18  @internalTechnology
       
    19 */
       
    20 
       
    21 #include <e32base.h>
       
    22 #include "sd_log.h"
       
    23 #include "sd_roles.h"
       
    24 #include "sd_std.h"
       
    25 
       
    26 using namespace Den;
       
    27 
       
    28 /** Because we strive to maintain a single owner for a message and to pass by reference when ownership
       
    29 it's generally an error for the handle to be set upon destruction.
       
    30 */
       
    31 
       
    32 EXPORT_C RSafeMessage::~RSafeMessage()
       
    33 	{
       
    34 #ifdef _DEBUG
       
    35 	if(Handle() != KNullHandle)
       
    36 		{
       
    37 		COMMONLOG((CommsFW::KInvalidWorkerId, KECommonErrorTag, _L8("RSafeMessage(%08x) - WARNING! handle still set in destructor, may be a leak, session %08x"), Handle(), Session()) );
       
    38 		}
       
    39 #endif
       
    40 	}
       
    41 
       
    42 
       
    43 /** Complete the message with the given reason code, with detailed logging
       
    44 */
       
    45 EXPORT_C void RSafeMessage::Complete(TInt aReason) const
       
    46 	{
       
    47 	//TODO	LOG(ESockLog::Printf(KESockServerTag, _L8("RSafeMessage(%08x)::Complete(%d) - session %08x"), Handle(), aReason, Session()) );
       
    48 	COMMONLOG((CommsFW::KInvalidWorkerId, KECommonSessDetailTag, _L8("RSafeMessage(%08x)::Complete(%08x) with %d"), this, Handle(), aReason) );
       
    49 	RMessage2::Complete(aReason);
       
    50 	}
       
    51 
       
    52 /** Take over a message. Beyond being a simple copy this resets the source's handle so that double-ownership is avoided
       
    53 */
       
    54 EXPORT_C void RSafeMessage::Adopt(RSafeMessage& aSrc)
       
    55 	{
       
    56 	*this = aSrc;
       
    57 	aSrc.ResetHandle();
       
    58 	}
       
    59 
       
    60 /** Duplicate a message, including aliasing the handle & so raising the risk of a double-complete
       
    61 */
       
    62 EXPORT_C void RSafeMessage::Duplicate(const RSafeMessage& aSrc)
       
    63 	{
       
    64 	*this = aSrc;
       
    65 	}
       
    66 
       
    67 /** Null the handle, so this safe message no longer references a client request. This is normally done automatically
       
    68 by completing or panicking the client or transferring message ownership, so this will seldom need to be called
       
    69 explicitly
       
    70 */
       
    71 EXPORT_C void RSafeMessage::ResetHandle()
       
    72 	{
       
    73 	iHandle = KNullHandle;
       
    74 	}
       
    75 
       
    76 RSafeMessage& RSafeMessage::operator=(const RSafeMessage& aSrc)
       
    77 	{
       
    78 	RMessage2::operator=(aSrc);
       
    79 	return *this;
       
    80 	}
       
    81 
       
    82 EXPORT_C void RSafeMessage::PanicClient(const TDesC& aCategory, TInt aReason) const
       
    83 	{
       
    84 	RThread t;
       
    85 	RMessage2::Client(t);
       
    86 	TUint tid = t.Id();
       
    87 	TExitType exittype=t.ExitType();
       
    88 	t.Close();
       
    89 	if(exittype == EExitPending) // is client alive?
       
    90 		{
       
    91 #ifdef SYMBIAN_TRACE_ENABLE
       
    92 		TBuf8<64> messBuf;
       
    93 		Logging::IPCMessName((TInt) RMessage2::Function(), messBuf);
       
    94 		COMMONLOG((CommsFW::KInvalidWorkerId, KECommonErrorTag, _L8("RSafeMessage:\tPanicClient, Reason %d, Tid %d, RMessage2 %08x [%S], Args %08x %08x %08x %08x, ClientStatus %08x"),
       
    95 				aReason, tid, RMessage2::Handle(), &messBuf, RMessage2::Int0(), RMessage2::Int1(), RMessage2::Int2(), RMessage2::Int3(), RMessage2::ClientStatus()));
       
    96 #endif
       
    97 		RMessage2::Panic(aCategory, aReason);
       
    98 		}
       
    99 	}
       
   100 
       
   101 /**
       
   102 Perform a read and kill the client if it has supplied a bad descriptor
       
   103 */
       
   104 EXPORT_C void RSafeMessage::ReadL(TInt aSrcParamIndex, TDes8& aDes, TInt anOffset) const
       
   105 	{
       
   106 	User::LeaveIfError(Read(aSrcParamIndex, aDes, anOffset));
       
   107 	}
       
   108 
       
   109 EXPORT_C TInt RSafeMessage::Read(TInt aSrcParamIndex, TDes8& aDes, TInt anOffset) const
       
   110 /**
       
   111  Perform a read and kill the client if it has supplied a bad descriptor
       
   112 */
       
   113 	{
       
   114 	TInt res= RMessage2::Read(aSrcParamIndex,aDes,anOffset);
       
   115 	__ASSERT_DEBUG(res!=KErrArgument, User::Panic(KDenFaultPanic, ECommonBadState));
       
   116 	if (res!=KErrNone)
       
   117 		{
       
   118 		PanicClient(KWorkerClientPanic, ECommonBadDescriptorRead);
       
   119 		}
       
   120 	return res;
       
   121 	}
       
   122 
       
   123 /**
       
   124 Perform a write and kill the client if it has supplied a bad descriptor
       
   125 */
       
   126 EXPORT_C void RSafeMessage::WriteL(TInt aDstParamIndex, const TDesC8& aDes, TInt anOffset) const
       
   127 	{
       
   128 	User::LeaveIfError(Write(aDstParamIndex, aDes, anOffset));
       
   129 	}
       
   130 
       
   131 /**
       
   132 Perform a write and kill the client if it has supplied a bad descriptor
       
   133 */
       
   134 EXPORT_C TInt RSafeMessage::Write(TInt aDstParamIndex, const TDesC8& aDes, TInt anOffset) const
       
   135 	{
       
   136 	TInt res = RMessage2::Write(aDstParamIndex,aDes,anOffset);
       
   137 	__ASSERT_DEBUG(res!=KErrArgument,User::Panic(KDenFaultPanic, ECommonBadState));
       
   138 	if (res == KErrBadDescriptor)
       
   139 		{
       
   140 		PanicClient(KWorkerClientPanic,ECommonBadDescriptorWrite);
       
   141 		}
       
   142 	return res;
       
   143 	}
       
   144 
       
   145 /**
       
   146 Perform a write and kill the client if it has supplied a bad descriptor
       
   147 */
       
   148 EXPORT_C void RSafeMessage::WriteL(TInt aDstParamIndex, const TPckg<TUint32>& aDes, TInt anOffset) const
       
   149 	{
       
   150 	User::LeaveIfError(Write(aDstParamIndex, aDes, anOffset));
       
   151 	}
       
   152 
       
   153 /**
       
   154 Perform a write and kill the client if it has supplied a bad descriptor
       
   155 */
       
   156 EXPORT_C TInt RSafeMessage::Write(TInt aDstParamIndex, const TPckg<TUint32>& aDes, TInt anOffset) const
       
   157 	{
       
   158 	TInt res = RMessage2::Write(aDstParamIndex,aDes,anOffset);
       
   159 	__ASSERT_DEBUG(res!=KErrArgument,User::Panic(KDenFaultPanic, ECommonBadState));
       
   160 	if (res == KErrBadDescriptor)
       
   161 		{
       
   162 		PanicClient(KWorkerClientPanic,ECommonBadDescriptorWrite);
       
   163 		}
       
   164 	return res;
       
   165 	}
       
   166 
       
   167 /**
       
   168 Perform a read and kill the client if it has supplied a bad descriptor
       
   169 */
       
   170 EXPORT_C void RSafeMessage::ReadL(TInt aSrcParamIndex,TDes16& aDes,TInt anOffset) const
       
   171 	{
       
   172 	User::LeaveIfError(Read(aSrcParamIndex, aDes, anOffset));
       
   173 	}
       
   174 
       
   175 /**
       
   176 Perform a read and kill the client if it has supplied a bad descriptor
       
   177 */
       
   178 EXPORT_C TInt RSafeMessage::Read(TInt aSrcParamIndex, TDes16& aDes, TInt anOffset) const
       
   179 	{
       
   180 #ifdef _UNICODE
       
   181 	TInt res= RMessage2::Read(aSrcParamIndex,aDes,anOffset);
       
   182 	__ASSERT_DEBUG(res!=KErrArgument, User::Panic(KDenFaultPanic, ECommonBadState));
       
   183 	if (res!=KErrNone)
       
   184 		{
       
   185 		PanicClient(KWorkerClientPanic, ECommonBadDescriptorRead);
       
   186 		}
       
   187 	return res;
       
   188 #else
       
   189 	return KErrNotSupported;
       
   190 #endif
       
   191 	}
       
   192 
       
   193 /**
       
   194 Perform a write and kill the client if it has supplied a bad descriptor
       
   195 */
       
   196 EXPORT_C void RSafeMessage::WriteL(TInt aDstParamIndex,const TDesC16& aDes,TInt anOffset) const
       
   197 	{
       
   198 	User::LeaveIfError(Write(aDstParamIndex, aDes, anOffset));
       
   199 	}
       
   200 
       
   201 /**
       
   202  Perform a write and kill the client if it has supplied a bad descriptor
       
   203 */
       
   204 EXPORT_C TInt RSafeMessage::Write(TInt aDstParamIndex,const TDesC16& aDes,TInt anOffset) const
       
   205 	{
       
   206 #ifdef _UNICODE
       
   207 	TInt res = RMessage2::Write(aDstParamIndex,aDes,anOffset);
       
   208 	__ASSERT_DEBUG(res!=KErrArgument,User::Panic(KDenFaultPanic, ECommonBadState));
       
   209 	if (res == KErrBadDescriptor)
       
   210 		{
       
   211 		PanicClient(KWorkerClientPanic,ECommonBadDescriptorWrite);
       
   212 		}
       
   213 	return res;
       
   214 #else
       
   215 	return KErrNotSupported;
       
   216 #endif
       
   217 	}
       
   218 
       
   219 /**
       
   220 Perform a GetDesLength and kill the client if it has supplied a bad descriptor
       
   221 */
       
   222 EXPORT_C TInt RSafeMessage::GetDesLengthL(TInt aParam) const
       
   223 	{
       
   224 	TInt res = RMessage2::GetDesLength(aParam);
       
   225 	__ASSERT_DEBUG(res!=KErrArgument, User::Panic(KDenFaultPanic, ECommonBadState));
       
   226 	if (res<0)
       
   227 		{
       
   228 		PanicClient(KWorkerClientPanic, ECommonBadDescriptorLength);
       
   229 		User::Leave(res);
       
   230 		}
       
   231 	return res;
       
   232 	}
       
   233 
       
   234 
       
   235 EXPORT_C TInt RSafeMessage::GetDesMaxLengthL(TInt aParam) const
       
   236 	{
       
   237 	TInt res = RMessage2::GetDesMaxLength(aParam);
       
   238 	__ASSERT_DEBUG(res!=KErrArgument, User::Panic(KDenFaultPanic, ECommonBadState));
       
   239 	if (res < 0)
       
   240 		{
       
   241 		PanicClient(KWorkerClientPanic, ECommonBadDescriptorLength);
       
   242 		User::Leave(res);
       
   243 		}
       
   244 	return res;		
       
   245 	}
       
   246