examples/SFExamples/Symbian_OS_Explained_Hercules-v9_Example_Code/src/clientserver.cpp

00001 // 
00002 // Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
00003 // All rights reserved.
00004 // This component and the accompanying materials are made available
00005 // under the terms of the License "Eclipse Public License v1.0"
00006 // which accompanies this distribution, and is available
00007 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
00008 // 
00009 // Initial Contributors:
00010 // Nokia Corporation - initial contribution.
00011 // 
00012 // Contributors:
00013 // 
00014 // Description:
00015 // 
00016 //      client-server.cpp
00017 
00018 #include "client-server.h"
00019 #include <s32mem.h>
00020 
00021 EXPORT_C CHerculesData* CHerculesData::NewLC(const TDesC8& aDes1, const TDesC8& aDes2, TInt aVal)
00022         {
00023         CHerculesData* data = new (ELeave)CHerculesData(aVal);
00024         CleanupStack::PushL(data);
00025         data->ConstructL(aDes1, aDes2);
00026         return (data);
00027         }
00028 
00029 // Creates a CHerculesData initialized with the contents of the
00030 // descriptor parameter
00031 CHerculesData* CHerculesData::NewLC(const TDesC8& aStreamData)
00032         {
00033         CHerculesData* data = new (ELeave)CHerculesData();
00034         CleanupStack::PushL(data);
00035         
00036         // Open a read stream for the descriptor
00037         RDesReadStream stream(aStreamData);
00038         CleanupClosePushL(stream);
00039         data->InternalizeL(stream);
00040         CleanupStack::PopAndDestroy(&stream); // finished with the stream
00041         return (data);
00042         }
00043 
00044 
00045 EXPORT_C CHerculesData::~CHerculesData()
00046         {
00047         delete iDes1;
00048         delete iDes2;
00049         }
00050 
00051 CHerculesData::CHerculesData(TInt aVal)
00052 :       iVal(aVal){}
00053 
00054 void CHerculesData::ConstructL(const TDesC8& aDes1, const TDesC8& aDes2)
00055         {
00056         iDes1 = aDes1.AllocL();
00057         iDes2 = aDes2.AllocL();
00058         }
00059 
00060         
00061 // Creates and returns a heap descriptor which holds contents of ’this’ 
00062 EXPORT_C HBufC8* CHerculesData::MarshalDataL() const
00063         {
00064 // Create a dynamic flat buffer to hold this object's member data       
00065         const TInt KExpandSize = 128; // "Granularity" of dynamic buffer        
00066         CBufFlat* buf = CBufFlat::NewL(KExpandSize);
00067         CleanupStack::PushL(buf);
00068         RBufWriteStream stream(*buf); // Stream over the buffer
00069         CleanupClosePushL(stream);
00070 
00071         ExternalizeL(stream);
00072         CleanupStack::PopAndDestroy(&stream);
00073         
00074         // Create a heap descriptor from the buffer
00075         HBufC8* des = HBufC8::NewL(buf->Size());
00076         TPtr8 ptr(des->Des());
00077         buf->Read(0, ptr, buf->Size());
00078         
00079         CleanupStack::PopAndDestroy(buf); // Finished with the buffer
00080         return (des);
00081         }
00082 
00083 // Writes ’this’ to aStream
00084 void CHerculesData::ExternalizeL(RWriteStream& aStream) const
00085         {
00086         if (iDes1) // Write iDes1 to the stream (or a NULL descriptor)
00087                 {
00088                 aStream << *iDes1;
00089                 }
00090         else
00091                 {
00092                 aStream << KNullDesC8;
00093                 }
00094 
00095         if (iDes2) // Write iDes2 to the stream (or a NULL descriptor)
00096                 {
00097                 aStream << *iDes2;
00098                 }
00099         else
00100                 {
00101                 aStream << KNullDesC8;
00102                 }
00103 
00104         aStream.WriteInt32L(iVal); // Write iVal to the stream
00105         }
00106 
00107 // Initializes ’this’ with the contents of aStream
00108 void CHerculesData::InternalizeL(RReadStream& aStream)
00109         {
00110         const TInt KMaxReadSize = 1024; // Limits each descriptor to max 1024 bytes
00111         iDes1 = HBufC8::NewL(aStream, KMaxReadSize); // Read iDes1
00112         iDes2 = HBufC8::NewL(aStream, KMaxReadSize); // Read iDes2
00113         iVal = aStream.ReadInt32L(); // Read iVal
00114         }
00115 
00116 void CHerculesData::SetDes1L(const TDesC8& aDes)
00117         {
00118         delete iDes1;
00119         iDes1=NULL;
00120         iDes1 = aDes.AllocL();
00121         }
00122 
00123 void CHerculesData::SetDes2L(const TDesC8& aDes)
00124         {
00125         delete iDes2;
00126         iDes2=NULL;
00127         iDes2 = aDes.AllocL();
00128         }
00129 
00130 
00131 

Generated by  doxygen 1.6.2