multimediacommsengine/mmcefloorctrlplugin/src/fcsender.cpp
author Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
Fri, 19 Feb 2010 23:12:20 +0200
branchRCL_3
changeset 3 513a8b745b2f
parent 0 1bce908db942
permissions -rw-r--r--
Revision: 201003 Kit: 201007

/*
* Copyright (c) 2005-2006 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description:    fcsender.cpp
*
*/




#include <e32base.h>
#include <in_sock.h>
#include "fcsender.h"
#include "fcconnectioncontainer.h"
#include "fcnotifier.h"

const TInt KSendBufferLen = 10;

// -----------------------------------------------------------------------------
// CFCSender::NewL
// -----------------------------------------------------------------------------
//
CFCSender* CFCSender::NewL(RSocket& aSocket, MFCNotifier& aNotifier)
	{
	CFCSender* self = new (ELeave) CFCSender(aSocket, aNotifier);
	CleanupStack::PushL(self);
	CleanupStack::Pop(self);
	return self;
	}

// -----------------------------------------------------------------------------
// CFCSender::CFCSender
// -----------------------------------------------------------------------------
//
CFCSender::CFCSender(RSocket& aSocket, MFCNotifier& aNotifier) :
CActive(EPriorityStandard),
iFCNotifier( aNotifier )
	{
	iSocket = &aSocket;
	CActiveScheduler::Add(this);
	}
	
// -----------------------------------------------------------------------------
// CFCSender::~CFCSender
// -----------------------------------------------------------------------------
//
CFCSender::~CFCSender()
	{
	Cancel();
	delete iData;
	iSendBuffer.ResetAndDestroy();
	iSendBuffer.Close();
	}

// -----------------------------------------------------------------------------
// CFCSender::SendL
// -----------------------------------------------------------------------------
//
void CFCSender::SendL (HBufC8* aData,
		        TInetAddr& aAddr)
	{
	if (!IsActive() && iConnected )
		{
		iData = aData;
		iOutgoingData.Set(*iData);
		iSocket->SendTo(iOutgoingData, aAddr, 0, iStatus);
		SetActive();
		}
	else
		{
		User::LeaveIfError( iSendBuffer.Count() <= KSendBufferLen ?
		    KErrNone : KErrNotReady );
		iRemoteAddr = aAddr;
		iSendBuffer.AppendL(aData);	
		}
	}

// -----------------------------------------------------------------------------
// CFCSender::ConnectedL
// -----------------------------------------------------------------------------
//
void CFCSender::ConnectedL()
    {
    iConnected = ETrue;
    SendFromBufferL();
    }

// -----------------------------------------------------------------------------
// CFCSender::RunL
// -----------------------------------------------------------------------------
//
void CFCSender::RunL ()
	{
	TInt err = iStatus.Int();
	if (err == KErrNoMemory)
		{
		// This will leave to RunError-function. See below.
		User::Leave(err);
		}			
	if (err != KErrNone)
		{
		iFCNotifier.ErrorNotify(iStatus.Int());
		}
	delete iData;
	iData = 0;
	
	SendFromBufferL();

	}

// -----------------------------------------------------------------------------
// CFCSender::DoCancel 
// -----------------------------------------------------------------------------
//
void CFCSender::DoCancel ()
	{
	iSocket->CancelSend();
	}

// -----------------------------------------------------------------------------
// CFCSender::RunError 
// -----------------------------------------------------------------------------
//
TInt CFCSender::RunError (TInt aError)
	{
	if(aError != KErrNoMemory)
		{
		return KErrNone;
		}
	return aError;
	}

// -----------------------------------------------------------------------------
// CFCSender::RunError 
// -----------------------------------------------------------------------------
//
void CFCSender::SendFromBufferL()
    {
    if (iSendBuffer.Count() > 0)
        {
        HBufC8* data = iSendBuffer[0];
        iSendBuffer.Remove(0);
        SendL(data, iRemoteAddr);   
        }
    }