📄 socketengine.cpp
字号:
/*
* ==============================================================================
* Name : SocketEngine.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 "SocketEngine.h"
#include "SocketWriter.h"
#include "SocketReader.h"
#include "TimeOutTimer.h"
#include <e32svr.h>
#include <CommDbConnPref.h>
#include <es_enum.h>
// CONSTANTS
// -----------------------------------------------------------------------------
// CSocketEngine::NewL
// Creates the instance of class and returns it.
// -----------------------------------------------------------------------------
//
CSocketEngine* CSocketEngine::NewL(
MSocketEngineObserver& aObserver,
TProtocolType aProtocolType,
TInt aLocalPort,
const TDesC& aRemoteAddr,
TInt aRemotePort)
{
CSocketEngine* self = CSocketEngine::NewLC(
aObserver,
aProtocolType,
aLocalPort,
aRemoteAddr,
aRemotePort);
CleanupStack::Pop(self);
return self;
}
// -----------------------------------------------------------------------------
// CSocketEngine::NewLC
// Creates the instance of class and pushes it to the CleanupStack and return
// it.
// -----------------------------------------------------------------------------
//
CSocketEngine* CSocketEngine::NewLC(
MSocketEngineObserver& aObserver,
TProtocolType aProtocolType,
TInt aLocalPort,
const TDesC& aRemoteAddr,
TInt aRemotePort)
{
CSocketEngine* self = new (ELeave) CSocketEngine(
aObserver,
aProtocolType,
aLocalPort,
aRemoteAddr,
aRemotePort);
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
// -----------------------------------------------------------------------------
// CSocketEngine::CSocketEngine
// Calls base classes constructor with priority value. Add class to the
// active sheduler.
// -----------------------------------------------------------------------------
//
CSocketEngine::CSocketEngine(
MSocketEngineObserver& aObserver,
TProtocolType aProtocolType,
TInt aLocalPort,
const TDesC& aRemoteAddr,
TInt aRemotePort)
:CActive( EPriorityStandard),
iObserver( aObserver),
iProtocolType(aProtocolType),
iLocalPort(aLocalPort)
{
iRemoteAddr.Input(aRemoteAddr);
iRemoteAddr.SetPort(aRemotePort);
iLocalAddr.Input(KAddrAny);
CActiveScheduler::Add( this );
}
// -----------------------------------------------------------------------------
// CSocketEngine::~CSocketEngine
// Cancels any outstanding requests and deletes members.
// -----------------------------------------------------------------------------
//
CSocketEngine::~CSocketEngine()
{
Cancel();
if ( iTimer )
{
iTimer->Cancel();
delete iTimer;
iTimer = NULL;
}
if ( iWriter )
{
iWriter->Cancel();
delete iWriter;
iWriter = NULL;
}
if ( iReader )
{
iReader->Cancel();
delete iReader;
iReader = NULL;
}
if (iListenSocket.SubSessionHandle() != 0)
{
iListenSocket.Close();
}
if (iDataSocket.SubSessionHandle() != 0)
{
iDataSocket.Close();
}
if (iConnection.SubSessionHandle() != 0)
{
iConnection.Close();
}
if (iSocketServer.Handle() != 0)
{
iSocketServer.Close();
}
}
// -----------------------------------------------------------------------------
// CSocketEngine::ConstructL
// The socketserver is connected and members created.
// -----------------------------------------------------------------------------
//
void CSocketEngine::ConstructL()
{
iState = ENotConnected;
iTimer = CTimeOutTimer::NewL( *this );
// Open channel to Socket Server
User::LeaveIfError(iSocketServer.Connect());
if(iProtocolType == EUDP)
{
InitUDP();
}
else
{
InitTCP();
}
}
// -----------------------------------------------------------------------------
// CSocketEngine::DoCancel
// From CActive. Cancels any outstanding request according the engine state.
// -----------------------------------------------------------------------------
//
void CSocketEngine::DoCancel()
{
iTimer->Cancel();
// Cancel appropriate request to socket
switch ( iState )
{
case EConnecting:
if (iDataSocket.SubSessionHandle() != 0)
{
// iDataSocket.CancelConnect();
iDataSocket.CancelAll();
iDataSocket.Close();
}
break;
case EListening:
if (iListenSocket.SubSessionHandle() != 0)
{
// iListenSocket.CancelAccept();
iListenSocket.CancelAll();
iListenSocket.Close();
}
break;
default:
break;
}
if (iConnection.SubSessionHandle() != 0)
{
iConnection.Close();
}
}
// -----------------------------------------------------------------------------
// CSocketEngine::RunL
// From CActive. Handles the state changes and notifing the observer.
// -----------------------------------------------------------------------------
//
void CSocketEngine::RunL()
{
iTimer->Cancel();
switch( iState )
{
case EConnecting:
{
if( iStatus.Int() == KErrNone )
{
ChangeStateAndNotify( EConnected );
#ifdef _DEBUG
iObserver.ShowMessage(1);
#endif
Read();
}
else
{
ChangeStateAndNotify( ENotConnected );
#ifdef _DEBUG
iObserver.ShowMessage(2);
#endif
}
}
break;
case EListening:
{
iListenSocket.Close();
if( iStatus.Int() == KErrNone )
{
ChangeStateAndNotify( EConnected );
Read();
}
else
{
ChangeStateAndNotify( ENotConnected );
}
}
break;
case ETimedOut:
{
if( iStatus.Int() == KErrNone )
{
iObserver.SocketState( ETimedOut );
iState = ENotConnected;
}
else
{
ChangeStateAndNotify( ENotConnected );
}
}
break;
default:
iObserver.SocketState( iStatus.Int() );
}
}
// -----------------------------------------------------------------------------
// CSocketEngine::ConnectL
// Connects the socket to the remote host. Asynchronous.
// -----------------------------------------------------------------------------
//
void CSocketEngine::ConnectL(const TUint32 aIapId, const TDesC& aRemoteAddr, TInt aPort)
{
if( iState != ENotConnected )
{
#ifdef _DEBUG
iObserver.ShowMessage(5);
#endif
return;
}
// Make first sure that underlying interface is active
StartInterfaceL( aIapId );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -