📄 sockstream.h
字号:
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Jabber
* Copyright (C) 2004 Xie Tian Lu http://sabber.jabberstudio.org/
*/
#ifndef __SOCKSTREAM_H__
#define __SOCKSTREAM_H__
#include <es_sock.h>
#include <in_sock.h>
#include "Notify.h"
class CSymSocket;
class CTimeOutTimer: public CTimer
{
public:
static CTimeOutTimer* NewL(const TInt aPriority, MTimeOutNotify& aTimeOutNotify);
~CTimeOutTimer();
protected:
CTimeOutTimer(const TInt aPriority);
void ConstructL(MTimeOutNotify& aTimeOutNotify);
virtual void RunL();
private:
MTimeOutNotify* iNotify;
};
struct CSockPack
{
TInt iPos;
TInt iLen;
TInetAddr iAddr; // take effort only using udp
};
const TInt KCacheBufSize = 16384;
const TInt KMaxPackNum = 64;
class CSockBuffer
{
public:
CSockBuffer( TInt s ) : S( s ), iHead( 0 ), iRear( 0 ), iPackHead( 0 ), iPackRear( 0 )
{
iBuf = new TUint8[ S ];
}
~CSockBuffer() {
delete[] iBuf;
}
void Reset() {
iHead = 0;
iRear = 0;
}
TBool IsEmpty() {
return ( iHead == iRear );
}
TBool IsFull() {
return ( ( iRear + 1 ) % S == iHead );
}
TBool WillFull( TInt len ) {
if ( len + 1 > S )
return ETrue;
if ( iRear >= iHead ) {
if ( iRear + len + 1 - S >= iHead )
return ETrue;
} else {
if ( iRear + len + 1 > iHead )
return ETrue;
}
return EFalse;
}
TBool HasMorePack() {
return ( iPackHead != iPackRear );
}
TBool HasEmptyPack() {
return ( ( iPackRear + 1 ) % S != iPackHead );
}
int GetPack( char* buf, int len, TInetAddr& addr ) {
if ( !HasMorePack() )
return 0;
if ( len < iPack[ iPackHead ].iLen )
return 0;
addr = iPack[ iPackHead ].iAddr;
TInt l = iPack[ iPackHead ].iLen;
Get( buf, l );
iPackHead = ( iPackHead + 1 ) % KMaxPackNum;
return l;
}
int PutPack( const char* buf, int len, TInetAddr addr ) {
if ( !HasEmptyPack() )
return 0;
if ( WillFull( len ) )
return 0;
iPack[ iPackRear ].iPos = iRear;
iPack[ iPackRear ].iLen = len;
iPack[ iPackRear ].iAddr = addr;
Put( buf, len );
iPackRear = ( iPackRear + 1 ) % KMaxPackNum;
return len;
}
int GetPack( TDes8& buf, TInt len, TInetAddr& addr ) {
if ( !HasMorePack() )
return 0;
if ( len < iPack[ iPackHead ].iLen )
return 0;
addr = iPack[ iPackHead ].iAddr;
Get( buf, iPack[ iPackHead ].iLen );
TInt l = iPack[ iPackHead ].iLen;
iPackHead = ( iPackHead + 1 ) % KMaxPackNum;
return l;
}
int PutPack( TDes8& buf, TInt len, TInetAddr addr ) {
if ( !HasEmptyPack() )
return 0;
if ( WillFull( len ) )
return 0;
iPack[ iPackRear ].iPos = iRear;
iPack[ iPackRear ].iLen = len;
iPack[ iPackRear ].iAddr = addr;
Put( buf, len );
iPackRear = ( iPackRear + 1 ) % KMaxPackNum;
return len;
}
int Get( char* buf, int len ) {
int i;
for ( i = 0; i < len && !IsEmpty(); i++ ) {
buf[ i ] = iBuf[ iHead ];
iHead = ( iHead + 1 ) % S;
}
return i;
}
int Put( const char* buf, int len ) {
int i;
for ( i = 0; i < len && !IsFull(); i++ ) {
iBuf[ iRear ] = buf[ i ];
iRear = ( iRear + 1 ) % S;
}
return i;
}
TInt Get( TDes8& buf, TInt len ) {
int i;
buf.SetLength( 0 );
for ( i = 0; i < len && i < buf.MaxSize() && !IsEmpty(); i++ ) {
buf.Append( iBuf[ iHead ] );
iHead = ( iHead + 1 ) % S;
}
return i;
}
TInt Put( TDes8& buf, TInt len ) {
int i;
for ( i = 0; ( i < len ) && ( i < buf.Length() ) && !IsFull(); i++ ) {
iBuf[ iRear ] = buf[ i ];
buf[ i ] = 0;
iRear = ( iRear + 1 ) % S;
}
buf.SetLength( 0 );
return i;
}
TUint8* BuffAddr() {
return iBuf;
}
TInt BuffSize() {
return ( iRear - iHead );
}
private:
CSockBuffer( const CSockBuffer& sb );
CSockBuffer& operator= ( const CSockBuffer& sb );
protected:
TInt S;
TInt iHead;
TInt iRear;
TUint8* iBuf;
TInt iPackHead;
TInt iPackRear;
CSockPack iPack[ KMaxPackNum ];
};
const TInt KReadBufSize = 4096;
class CSockReader : public CActive
{
public:
static CSockReader* NewL ( RSocket* aSocket, CSymSocket* aOwner, TUint aCacheSize );
~CSockReader();
int GetCacheSize();
char* GetCacheAddr();
void ResetCache();
void StopRecv();
int Recv( char* buf, int len );
int RecvFrom( TInetAddr& addr, char* buf, int len );
void IssueRead();
//Implemented functions from CActive
void DoCancel();
void RunL();
protected:
static CSockReader* NewLC( RSocket* aSocket, CSymSocket* aOwner, TUint aCacheSize );
void ConstructL( RSocket* aSocket, CSymSocket* aOwner, TUint aCacheSize );
CSockReader();
private:
//MSockOwner* iOwner;
CSymSocket* iOwner;
RSocket* iSocket;
CSockBuffer* iCache;
TInetAddr iFrom;
TSockXfrLength iLen;
TInt iRecv;
TBuf8<KReadBufSize> iBuffer;
};
const TInt KWriteBufSize = 4096;
class CSockWriter : public CActive, public MTimeOutNotify
{
public:
static CSockWriter* NewL ( RSocket* aSocket, CSymSocket* aOwner, TUint aCacheSize );
~CSockWriter();
void ResetCache();
int Send( const char* buf, int len );
int SendTo( TInetAddr addr, const char* buf, int len );
void IssueWrite();
//Implemented functions from CActive
void DoCancel();
void RunL();
//Implemented functions from MNTimeOutNotify
void TimerExpired();
protected:
static CSockWriter* NewLC( RSocket* aSocket, CSymSocket* aOwner, TUint aCacheSize );
void ConstructL( RSocket* aSocket, CSymSocket* aOwner, TUint aCacheSize );
CSockWriter();
private:
//MSockOwner* iOwner;
CSymSocket* iOwner;
CTimeOutTimer* iTimer;
TInt iTimeOut;
RSocket* iSocket;
CSockBuffer* iCache;
TInetAddr iDest;
TSockXfrLength iLen;
TSymSocketState iWriteStatus;
TBuf8<KWriteBufSize> iBuffer;
};
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -