📄 iapconnectdemoengine.cpp
字号:
iResolver.Cancel();
iResolver.Close();
break;
}
case EWaitingData:
case EConnected:
iSocket.Close();
break;
default:
break;
}
// ChangeStatusL does not actually leave with this particular argument
TRAPD(err, ChangeStatusL( ENotConnected ));
}
// ---------------------------------------------------------------------------
// CIAPConnectDemoEngine::WriteL()
// Writes data to socket.
// ---------------------------------------------------------------------------
//
void CIAPConnectDemoEngine::WriteL( const TDesC8& aData )
{
if ( iEngineStatus == EConnected )
{
iSocketsWriter->IssueWriteL( aData );
}
}
// ---------------------------------------------------------------------------
// CIAPConnectDemoEngine::ReadL()
// Initiates read of data from socket.
// ---------------------------------------------------------------------------
//
void CIAPConnectDemoEngine::ReadL()
{
if ( ( iEngineStatus == EConnected ) && ( !iSocketsReader->IsActive() ) )
{
iSocketsReader->StartL();
}
}
// ---------------------------------------------------------------------------
// CIAPConnectDemoEngine::RunL()
// Called when operation completes.
// ---------------------------------------------------------------------------
//
void CIAPConnectDemoEngine::RunL()
{
// Active object request complete handler.
// iEngineStatus flags what request was made, so its
// completion can be handled appropriately
switch( iEngineStatus )
{
case EWaitingData:
break;
case EOpenConnection:
if (iStatus == KErrNone)
{
ResolveAddressL();
}
else
{
ReportErrorL( EConnectionFailed, iStatus.Int() );
}
break;
case EConnecting:
// IP connection request
if ( iStatus == KErrNone )
// Connection completed successfully
{
ChangeStatusL( EConnected );
ReadL(); //Start CIAPConnectDemoSocketsReader Active object
if (iSentDataNotification)
{
delete iSentDataNotification;
iSentDataNotification = NULL;
}
TInt data = 0;
if ( iSettings -> GetSetting(EIAPConnectDemoSentData, data)
!= KErrNone )
{
User::Leave( KErrNotFound );
}
iSentDataNotification =
CIAPConnectDemoDataNotification::NewL(*this, iConnection);
iSentDataNotification -> Start( ESentDataNotification, data);
if (iReceivedDataNotification)
{
delete iReceivedDataNotification;
iReceivedDataNotification = NULL;
}
data = 0;
if ( iSettings -> GetSetting(EIAPConnectDemoReceivedData, data)
!= KErrNone )
{
User::Leave( KErrNotFound );
}
iReceivedDataNotification =
CIAPConnectDemoDataNotification::NewL(*this, iConnection);
iReceivedDataNotification -> Start( EReceivedDataNotification,
data);
HBufC* server = NULL;
if ( iSettings -> GetSetting( EIAPConnectDemoServerName,
server))
{
User::Leave( KErrNotFound );
}
TBuf8<KMaxRequestLength> request;
request.Append(KHttpGet);
request.Append( *server );
request.Append( KHttpVersion );
TRAPD (error, WriteL( request ));
if ( error )
{
ReportErrorL( EWriteFailed, KErrCouldNotConnect );
break;
}
ChangeStatusL( EWaitingData );
}
else
{
iSocket.Close();
CleanupStack::PopAndDestroy(); // connectionError
ChangeStatusL( ENotConnected );
}
break;
case ELookingUp:
iResolver.Close();
if ( iStatus == KErrNone )
{
// DNS look up successful
iNameRecord = iNameEntry();
TBuf<KMaxIpAddrLength> ipAddr;
TInetAddr::Cast( iNameRecord.iAddr ).Output( ipAddr );
// And connect to the IP address
ChangeStatusL( ENotConnected );
ConnectL( TInetAddr::Cast( iNameRecord.iAddr ).Address() );
}
else
{
// DNS lookup failed
CleanupStack::PopAndDestroy(); // dnsError
ChangeStatusL( ENotConnected );
}
break;
default:
break;
};
}
// ---------------------------------------------------------------------------
// CIAPConnectDemoEngine::ReportErrorL()
// Reports a communication error.
// ---------------------------------------------------------------------------
//
void CIAPConnectDemoEngine::ReportErrorL(
const MIAPConnectDemoSocketsObserver::TDemoErrorType& aErrorType,
const TInt& aErrorCode )
{
// No recovery or retries are attempted in this example so we just
// disconnect and inform the user
DisconnectL();
switch ( aErrorType )
{
case MIAPConnectDemoSocketsObserver::EDisconnected:
iObserver->ErrorL( KErrorDisconnected, aErrorCode );
break;
case MIAPConnectDemoSocketsObserver::EConnectionFailed:
iObserver->ErrorL( KErrorConnectionFailed, aErrorCode );
break;
case MIAPConnectDemoSocketsObserver::ETimeOut:
iObserver->ErrorL( KErrorTimeOut, aErrorCode );
break;
case MIAPConnectDemoSocketsObserver::EWriteFailed:
iObserver->ErrorL( KErrorSocketError, aErrorCode );
break;
default:
break;
}
}
// ---------------------------------------------------------------------------
// CIAPConnectDemoEngine::ResponseReceived()
// Data has been received on the socket and read into a buffer.
// ---------------------------------------------------------------------------
//
void CIAPConnectDemoEngine::ResponseReceivedL( const TDesC8& aBuffer )
{
TBuf<KBufSize> text;
CnvUtfConverter::ConvertToUnicodeFromUtf8( text, aBuffer );
iObserver->SetStatusL( text );
}
// ---------------------------------------------------------------------------
// CIAPConnectDemoEngine::ChangeStatus()
// Handles a change in this object's status.
// ---------------------------------------------------------------------------
//
void CIAPConnectDemoEngine::ChangeStatusL(
const TIAPConnectDemoEngineStates& aNewStatus )
{
// Update the status ( and the status display )
switch ( aNewStatus )
{
case ENotConnected:
break;
case EConnecting:
iObserver->SetStatusL( KStatusConnecting );
break;
case EConnected:
iObserver->SetStatusL( KStatusConnected );
break;
case ELookingUp:
iObserver->SetStatusL( KStatusLookingUp );
break;
default:
break;
}
iEngineStatus = aNewStatus;
}
// ---------------------------------------------------------------------------
// CIAPConnectDemoEngine::Connected()
// Checks if socket is fully connected.
// ---------------------------------------------------------------------------
//
TBool CIAPConnectDemoEngine::Connected() const
{
return ( iEngineStatus == EConnected );
}
// ---------------------------------------------------------------------------
// CIAPConnectDemoEngine::TimerExpiredL()
// From MTimeoutNotifier send error note to the event list
// ---------------------------------------------------------------------------
//
void CIAPConnectDemoEngine::TimerExpiredL()
{
ReportErrorL( ETimeOut, KErrTimedOut );
}
// End of File
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -