⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 socketwriter.cpp

📁 基于SIP协议的即时消息聊天功能设计,Symbian平台下实现
💻 CPP
字号:
/*
* ==============================================================================
*  Name        : SocketWriter.cpp
*  Part of     : SocketEngine
*  Interface   : 
*  Description : 
*  Version     : 
*
*  Copyright (c) 2004-2006 Nokia Corporation.
*  This material, including documentation and any related 
*  computer programs, is protected by copyright controlled by 
*  Nokia Corporation.
* ==============================================================================
*/

// INCLUDES
#include    "SocketWriter.h"
#include    "WriterNotifier.h"
#include    <libc/string.h>

// -----------------------------------------------------------------------------
// CSocketWriter::NewL
// Static constructor
// -----------------------------------------------------------------------------
//
CSocketWriter* CSocketWriter::NewL( 
    RSocket& aSocket, 
    MWriterNotifier& aNotifier,
	TProtocolType aProtocolType, 
	TInetAddr& aRemoteAddr)
    {
    CSocketWriter* self = NewLC( aSocket, aNotifier, aProtocolType, aRemoteAddr);
    CleanupStack::Pop(self);
    return self;
    }

// -----------------------------------------------------------------------------
// CSocketWriter::NewLC
// Static constructor
// -----------------------------------------------------------------------------
//    
CSocketWriter* CSocketWriter::NewLC( 
    RSocket& aSocket, 
    MWriterNotifier& aNotifier,
	TProtocolType aProtocolType, 
	TInetAddr& aRemoteAddr)
    {
    CSocketWriter* self = new (ELeave) CSocketWriter( aSocket, aNotifier, aProtocolType, aRemoteAddr);
    CleanupStack::PushL(self);
    self->ConstructL();
    return self;
    }

// -----------------------------------------------------------------------------
// CSocketWriter::CSocketWriter
// Constructor
// -----------------------------------------------------------------------------
//
CSocketWriter::CSocketWriter( 
    RSocket& aSocket, 
    MWriterNotifier& aNotifier,
	TProtocolType aProtocolType, 
	TInetAddr& aRemoteAddr)
    : CActive(CActive::EPriorityStandard), 
      iNotifier(aNotifier), 
      iSocket(aSocket),
	  iProtocolType(aProtocolType),
	  iRemoteAddr(aRemoteAddr)
    {
    //CActiveScheduler::Add(this);
    }

// -----------------------------------------------------------------------------
// CSocketWriter::~CSocketWriter
// Destructor
// -----------------------------------------------------------------------------
//
CSocketWriter::~CSocketWriter()
    {
    Cancel();
    delete iBuffer;
    }

// -----------------------------------------------------------------------------
// CSocketWriter::ConstructL
// 2nd phase constructor
// -----------------------------------------------------------------------------
//
void CSocketWriter::ConstructL()
    {
    }

// -----------------------------------------------------------------------------
// CSocketWriter::DoCancel
// From CActive
// -----------------------------------------------------------------------------
//
void CSocketWriter::DoCancel()
    {
    iSocket.CancelSend();
    }

// -----------------------------------------------------------------------------
// CSocketWriter::RunL
// From CActive
// -----------------------------------------------------------------------------
//
void CSocketWriter::RunL()
    {
    if( iBuffer )
        {
        delete iBuffer;
        iBuffer = NULL;
        }

    if( iStatus.Int() == KErrNone )
        {
        iNotifier.WriteDone();
        }
    else
        {
        iNotifier.ErrorInWriting( iStatus.Int() );
        }
    }

// -----------------------------------------------------------------------------
// CSocketWriter::Write
// Asynchronous
// Copy the parameter to the member variable and start asynchronous writing.
// -----------------------------------------------------------------------------
//
void CSocketWriter::Write( const TDesC8& aBuffer )
    {
		if(iProtocolType == EUDP) // UDP MODE
		{
			if( iBuffer )
			{
				delete iBuffer;
				iBuffer = NULL;
			}

			TRAPD( err, iBuffer = aBuffer.AllocL() );
			if( !IsActive() && err == KErrNone )
			{
				iSocket.SendTo(*iBuffer, iRemoteAddr, 0, iStatus);
				User::WaitForRequest(iStatus);
			}
			else
				iNotifier.ErrorInWriting( iStatus.Int() );
		}
		else // TCP MODE
		{
			if( iBuffer )
			{
				delete iBuffer;
				iBuffer = NULL;
			}

			TRAPD( err, iBuffer = aBuffer.AllocL() );

			if( !IsActive() && err == KErrNone )
			{
				iSocket.Write(*iBuffer, iStatus);
				User::WaitForRequest(iStatus);
			}
		}
    }

// End of file

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -