HESSIAN协议symbian简用版

  1 #ifndef CHESSION_H
  2 #define CHESSION_H
  3 #include <e32std.h>
  4 class CCHession
  5 {
  6 public:
  7     HBufC8 * sendBytes;
  8 public:
  9     CCHession(void);
 10     ~CCHession(void);
 11     void WirteInt(TInt tint);
 12     void WirteHBufC8(HBufC8* hbufc8);
 13     void WriteLong(TInt tint);
 14     void WriteByte(HBufC8* hbufc8);
 15     void WriteTBuf(TBuf<50> &tbuf);
 16     TInt ReadInt(HBufC8* response);
 17     //void WirteInt(TInt tint);
 18 };
 19 #endif
 20 
 21 =============================================
 22 #include "CHession.h"
 23 #include <e32std.h>
 24 #include <e32base.h>
 25 CCHession::CCHession(void)
 26 {
 27     sendBytes=HBufC8::NewLC(1);
 28 
 29 }
 30 
 31 CCHession::~CCHession(void)
 32 {
 33     CleanupStack::Pop();
 34     delete sendBytes;
 35 }
 36 
 37 
 38 
 39 
 40 TInt CCHession::ReadInt(HBufC8* response)
 41 {
 42 //TInt result=1;
 43 TInt b32 = (*response)[1]& 0xFF;
 44 TInt b24 = (*response)[2]& 0xFF;
 45 TInt b16 = (*response)[3]& 0xFF;
 46 TInt b8 = (*response)[4]& 0xFF;
 47 return ((b32 << 24+ (b24 << 16+ (b16 << 8+ b8)& 0x00000000FFFFFFFF;
 48 }
 49 
 50 void CCHession::WriteByte(HBufC8* hbufc8)
 51 {
 52     TInt length=hbufc8->Length();
 53     if(length==0)
 54     {
 55         sendBytes=sendBytes->ReAllocL(sendBytes->Length()+1);
 56         sendBytes->Des().Append(TChar('N'));
 57     }else
 58     {
 59         sendBytes=sendBytes->ReAllocL(sendBytes->Length()+3+length);
 60         sendBytes->Des().Append(TChar('B'));
 61         TInt b32 = length >> 24;
 62         TInt b24 = (length >> 16);//& 0x000000FF;
 63         TInt b16 = (length >> 8);//& 0x000000FF;
 64         TInt b8 = length;//& 0x000000FF;
 65         /*
 66         TBuf8<1> tb16;
 67         tb16.Format(_L8("%d"),b16);
 68         TBuf8<1> tb8;
 69         tb8.Format(_L8("%d"),b8);
 70         */
 71         //sendBytes->Des().Append(TChar(b32));
 72         //sendBytes->Des().Append(TChar(b24));
 73         sendBytes->Des().Append(TChar(b16));
 74         sendBytes->Des().Append(TChar(b8));
 75         sendBytes->Des().Append(*hbufc8);
 76     }
 77 
 78 
 79 }
 80 void CCHession::WirteInt(TInt tint)
 81 {
 82         TInt b32 = tint >> 24;
 83         TInt b24 = (tint >> 16);// & 0x000000FF;
 84         TInt b16 = (tint >> 8);// & 0x000000FF;
 85         TInt b8 = tint;// & 0x000000FF;
 86     if(sendBytes->Length()==1){
 87         sendBytes=sendBytes->ReAllocL(5);
 88     }else
 89     {
 90         sendBytes=sendBytes->ReAllocL(sendBytes->Length()+5);
 91     }
 92         sendBytes->Des().Append(TChar('I'));
 93         /*
 94         TBuf8<1> tb32;
 95         tb32.Format(_L8("%d"),b32);
 96         TBuf8<1> tb24;
 97         tb24.Format(_L8("%d"),b24);
 98         TBuf8<1> tb16;
 99         tb16.Format(_L8("%d"),b16);
100         TBuf8<1> tb8;
101         tb8.Format(_L8("%d"),b8);
102         */
103         sendBytes->Des().Append(TChar(b32));
104         sendBytes->Des().Append(TChar(b24));
105         sendBytes->Des().Append(TChar(b16));
106         sendBytes->Des().Append(TChar(b8));
107 }
108 
109 void CCHession::WirteHBufC8(HBufC8* hbufc8)
110 {
111     if(hbufc8->Length()==0)
112     {
113         sendBytes=sendBytes->ReAllocL(sendBytes->Length()+1);
114         sendBytes->Des().Append(TChar(TChar('N')));
115     }else
116     {
117         TInt length=hbufc8->Length();
118         sendBytes=sendBytes->ReAllocL(sendBytes->Length()+3);
119         TInt b32 = length >> 24;
120         TInt b24 = (length >> 16);//& 0x000000FF;
121         TInt b16 = (length >> 8);// & 0x000000FF;
122         TInt b8 = length;// & 0x000000FF;
123 /*
124         TBuf8<1> tb32;
125         tb32.Format(_L8("%d"),b32);
126         TBuf8<1> tb24;
127         tb24.Format(_L8("%d"),b24);
128         TBuf8<1> tb16;
129         tb16.Format(_L8("%d"),b16);
130         TBuf8<1> tb8;
131         tb8.Format(_L8("%d"),b8);
132 */
133 
134         sendBytes->Des().Append(TChar('S'));
135         sendBytes->Des().Append(TChar(b16));
136         sendBytes->Des().Append(TChar(b8));
137         sendBytes=sendBytes->ReAllocL(sendBytes->Length()+hbufc8->Length());
138         sendBytes->Des().Append(*hbufc8);
139         
140     }
141 
142 }
143 
144 
145 void CCHession::WriteLong(TInt tint)
146     {
147 
148         TInt64 i64=TInt64(tint);
149                 
150         TInt b64 = (tint >> 56);//& 0x00000000000000FF;
151         TInt b56 = (tint >> 48);//& 0x00000000000000FF;
152         TInt b48 = (tint >> 40);//& 0x00000000000000FF;
153         TInt b40 = (tint >> 32);//& 0x00000000000000FF;
154         TInt b32 = (tint >> 24);//& 0x00000000000000FF;
155         TInt b24 = (tint >> 16);//& 0x00000000000000FF;
156         TInt b16 = (tint >> 8);//& 0x00000000000000FF;
157         TInt b8 = tint;//t& 0x00000000000000FF;
158         
159         sendBytes=sendBytes->ReAllocL(sendBytes->Length()+9);
160         sendBytes->Des().Append('L');
161         sendBytes->Des().Append(TChar(b64));
162         sendBytes->Des().Append(TChar(b56));
163         sendBytes->Des().Append(TChar(b48));
164         sendBytes->Des().Append(TChar(b40));
165         sendBytes->Des().Append(TChar(b32));
166         sendBytes->Des().Append(TChar(b24));
167         sendBytes->Des().Append(TChar(b16));
168         sendBytes->Des().Append(TChar(b8));
169         //WirteInt(tint);
170     }
171 
172     void CCHession::WriteTBuf(TBuf<50> &tbuf)
173     {
174     if(tbuf.Length()==0)
175     {
176         sendBytes=sendBytes->ReAllocL(sendBytes->Length()+1);
177         sendBytes->Des().Append(TChar('N'));
178     }else
179     {
180         
181         TInt length=tbuf.Length();
182         /*for(TInt i=0;i<tbuf.Length();i++)
183         {
184             //TChar ch =TChar(tbuf[i]);
185             if (tbuf[i] < 0x80){
186                 length+=1;
187             }else if (tbuf[i] < 0x800) {
188                 length+=2;
189             }else {
190                 length+=3;
191             }
192         }
193         */
194         sendBytes=sendBytes->ReAllocL(sendBytes->Length()+3);
195         TInt b32 = length >> 24;
196         TInt b24 = (length >> 16);//& 0x000000FF;
197         TInt b16 = (length >> 8);// & 0x000000FF;
198         TInt b8 = length;// & 0x000000FF;
199 
200         sendBytes->Des().Append(TChar('S'));
201         sendBytes->Des().Append(TChar(b16));
202         sendBytes->Des().Append(TChar(b8));
203         //sendBytes=sendBytes->ReAllocL(sendBytes->Length()+length);
204         for(TInt i=0;i<tbuf.Length();i++)
205         {
206             if (tbuf[i] < 0x80){
207                 sendBytes=sendBytes->ReAllocL(sendBytes->Length()+1);
208                 sendBytes->Des().Append(TChar(tbuf[i]));
209             }else if (tbuf[i] < 0x800) {
210                 sendBytes=sendBytes->ReAllocL(sendBytes->Length()+2);
211                 sendBytes->Des().Append(TChar(0xc0 + ((tbuf[i] >> 6& 0x1f)));
212                 sendBytes->Des().Append(TChar(0x80 + (tbuf[i] & 0x3f)));
213             }else {
214                 sendBytes=sendBytes->ReAllocL(sendBytes->Length()+3);
215                 sendBytes->Des().Append(TChar(0xe0 + ((tbuf[i] >> 12& 0xf)));
216                 sendBytes->Des().Append(TChar(0x80 + ((tbuf[i] >> 6& 0x3f)));
217                 sendBytes->Des().Append(TChar(0x80 + (tbuf[i] & 0x3f)));
218             }
219         }
220         
221     }
222 
223     }
//HessionReadInput
/*
============================================================================
 Name        : HessionReadInput.h
 Author      :
 Version     :
 Copyright   : Your copyright notice
 Description : CHessionReadInput declaration
============================================================================
*/

#ifndef HESSIONREADINPUT_H
#define HESSIONREADINPUT_H

// INCLUDES
#include <e32std.h>
#include <e32base.h>
#include <s32strm.h>
#include <s32mem.h>
// CLASS DECLARATION

/**
*  CHessionReadInput
*
*/
class CHessionReadInput : public CBase
{
public: // Constructors and destructor

 /**
        * Destructor.
        */
 ~CHessionReadInput();

        /**
        * Two-phased constructor.
        */
 static CHessionReadInput* NewL(HBufC8 * acontent);

        /**
        * Two-phased constructor.
        */
 static CHessionReadInput* NewLC(HBufC8 * acontent);

private:

 /**
        * Constructor for performing 1st stage construction
        */
 CHessionReadInput();

 /**
        * EPOC default constructor for performing 2nd stage construction
        */
 void ConstructL(HBufC8 * acontent);
private:
 //RReadStream * iInputStream;
 RDesReadStream* iInputStream;
public :
 TInt ReadInt();
 HBufC8* ReadBytes();
 TBool ReadBoolean();
 HBufC8* ReadString();
 TInt64 ReadLongContent();

};

#endif // HESSIONREADINPUT_H
====================================================================================
/*
============================================================================
 Name        : HessionReadInput.cpp
 Author      :
 Version     :
 Copyright   : Your copyright notice
 Description : CHessionReadInput implementation
============================================================================
*/

#include "HessionReadInput.h"
#include <s32strm.h>
#include <s32buf.h>
#include <s32mem.h>
CHessionReadInput::CHessionReadInput()
{
 // No implementation required
}


CHessionReadInput::~CHessionReadInput()
{
 
 iInputStream->Close();
 //CleanupStack::PopAndDestroy(iInputStream);
 delete iInputStream;
 //iInputStream->Pop();

}

CHessionReadInput* CHessionReadInput::NewLC(HBufC8 * acontent)
{
 CHessionReadInput* self = new (ELeave)CHessionReadInput();
 CleanupStack::PushL(self);
 self->ConstructL(acontent);
 return self;
}

CHessionReadInput* CHessionReadInput::NewL(HBufC8 * acontent)
{
 CHessionReadInput* self=CHessionReadInput::NewLC(acontent);
 CleanupStack::Pop(); // self;
 return self;
}

void CHessionReadInput::ConstructL(HBufC8 * acontent)
{
 //this->iInputStream=new (ELeave)RReadStream();
 iInputStream =new (ELeave)RDesReadStream(*acontent);
 //CleanupClosePushL(*iInputStream);
 //iInputStream->PushL();
 //iInputStream->

 //MStreamBuf * aSource;
 //aSource->WriteL(acontent,acontent->Length());
 //this->iInputStream=new (ELeave)RReadStream(aSource);
}


TInt CHessionReadInput::ReadInt()
{
 TInt tag=iInputStream->ReadInt8L();
  if (tag == 'I')
  {
  TInt b32 = iInputStream->ReadInt8L();
  TInt b24 = iInputStream->ReadInt8L();
  TInt b16 = iInputStream->ReadInt8L();
  TInt8 b8 = iInputStream->ReadInt8L();
  if(b8<0)
  {
   b8=b8+256;
  }
  TInt len=(b32<<24)+(b24<<16)+(b16 << 8)+b8;
  return len;
  }
}

HBufC8* CHessionReadInput::ReadBytes()
{
 TChar aChar=iInputStream->ReadUint8L();
 if(aChar=='N')
 {
  return NULL;
 }
 if(aChar=='B')
 {
  TInt b16=iInputStream->ReadInt8L();
  TInt8 b8=iInputStream->ReadInt8L();
  if(b8<0)
  {
   b8=256+b8;
  }
  TInt len=(b16 << 8)+b8;
  HBufC8 * aBytes=HBufC8::NewLC(1);
  CleanupStack::Pop();
  aBytes=aBytes->ReAllocL(len);
  iInputStream->ReadL(aBytes->Des(),len);
  return aBytes;
 }

}

TBool CHessionReadInput::ReadBoolean()
{
 TInt tag = iInputStream->ReadInt8L();
    switch (tag) {
    case 'T': return ETrue;
    case 'F': return EFalse;
    default:
      break;
    }

}

HBufC8* CHessionReadInput::ReadString()
{
 TInt tag = iInputStream->ReadInt8L();
    if (tag == 'N')
      return NULL;
    if (tag == 'S')
 {
  TInt b16=iInputStream->ReadInt8L();
  TInt b8=iInputStream->ReadInt8L();
  if(b8<0)
  {
   b8=256+b8;
  }
  TInt len=(b16 << 8) + b8;
  HBufC8* aString=HBufC8::NewLC(1);
  CleanupStack::Pop();
  aString=aString->ReAllocL(len);
  iInputStream->ReadL(aString->Des(),len);
  return aString;
 }
}

TInt64 CHessionReadInput::ReadLongContent()
{
 TInt tag = iInputStream->ReadInt8L();
 if (tag == 'L'){
  TInt64 b64 = iInputStream->ReadInt8L();
  TInt64 b56 = iInputStream->ReadInt8L();
  TInt64 b48 = iInputStream->ReadInt8L();
  TInt64 b40 = iInputStream->ReadInt8L();
  TInt64 b32 = iInputStream->ReadInt8L();
  TInt64 b24 = iInputStream->ReadInt8L();
  TInt64 b16 = iInputStream->ReadInt8L();
  TInt64 b8 = iInputStream->ReadInt8L();
  if(b8<0)
  {
   b8=b8+256;
  }
  TInt64 aInt=(b64 << 56) +(b56 << 48) +(b48 << 40) +(b40 << 32) +(b32 << 24) +(b24 << 16) +(b16 << 8) + b8;
  return aInt; 
 }
}
//end cpp

适用于文件的分块上传~~~~~~转载请注明出处!

你可能感兴趣的:(Symbian)