📄 messageserver.cpp
字号:
/* Copyright (c) 2004, Nokia. All rights reserved */
// INCLUDE FILES
#include <StringLoader.h>
#include <BTPointToPoint.rsg>
#include "MessageServer.h"
#include "MessageProtocolConstants.h"
#include "MessageServiceAdvertiser.h"
#include "Log.h"
#include "BTPointToPoint.pan"
// ============================ MEMBER FUNCTIONS ==============================
// ----------------------------------------------------------------------------
// CMessageServer::NewL()
// Two-phased constructor.
// ----------------------------------------------------------------------------
//
CMessageServer* CMessageServer::NewL( MLog& aLog )
{
CMessageServer* self = NewLC( aLog );
CleanupStack::Pop( self );
return self;
}
// ----------------------------------------------------------------------------
// CMessageServer::NewLC()
// Two-phased constructor.
// ----------------------------------------------------------------------------
//
CMessageServer* CMessageServer::NewLC( MLog& aLog )
{
CMessageServer* self = new ( ELeave ) CMessageServer( aLog );
CleanupStack::PushL( self );
self->ConstructL();
return self;
}
// ----------------------------------------------------------------------------
// CMessageServer::CMessageServer()
// Constructor.
// ----------------------------------------------------------------------------
//
CMessageServer::CMessageServer( MLog& aLog )
: CActive( CActive::EPriorityStandard ),
iLog( aLog ),
iState( EDisconnected )
{
CActiveScheduler::Add( this );
}
// ----------------------------------------------------------------------------
// CMessageServer::~CMessageServer()
// Destructor.
// ----------------------------------------------------------------------------
//
CMessageServer::~CMessageServer()
{
TRAPD( err,StopL() );
if ( err != KErrNone )
{
Panic( EBTPointToPointServerStop );
}
delete iAdvertiser;
iAdvertiser = NULL;
Cancel();
}
// ----------------------------------------------------------------------------
// CMessageServer::ConstructL()
// Perform second phase construction of this object.
// ----------------------------------------------------------------------------
//
void CMessageServer::ConstructL()
{
iAdvertiser = CMessageServiceAdvertiser::NewL();
}
// ----------------------------------------------------------------------------
// CMessageServer::DoCancel()
// Cancel any outstanding requests.
// ----------------------------------------------------------------------------
//
void CMessageServer::DoCancel()
{
}
// ----------------------------------------------------------------------------
// CMessageServer::RunL()
// Respond to an event.
// ----------------------------------------------------------------------------
//
void CMessageServer::RunL()
{
if ( iStatus == KErrDisconnected )
{
// Disconnected so go back to listening
HBufC* strDisconnected = StringLoader
::LoadLC ( R_BTPO_STR_DISCONNECTED );
iLog.LogL( *strDisconnected );
CleanupStack::PopAndDestroy( strDisconnected );
StopL();
return;
}
else if ( iStatus == KErrAbort )
{
HBufC* strDisconnected = StringLoader
::LoadLC ( R_BTPO_STR_DISCONNECTED );
iLog.LogL( *strDisconnected );
CleanupStack::PopAndDestroy( strDisconnected );
StopL();
return;
}
else if ( iStatus != KErrNone )
{
HBufC* strRecvErr = StringLoader
::LoadLC ( R_BTPO_STR_RECV_ERR );
iLog.LogL( *strRecvErr, iStatus.Int() );
CleanupStack::PopAndDestroy( strRecvErr );
StopL();
return;
}
switch ( iState )
{
HBufC* textResource;
case EConnecting:
textResource = StringLoader::LoadLC ( R_BTPO_CONNECTED );
iLog.LogL( *textResource );
CleanupStack::PopAndDestroy( textResource );
// do not accept any more connections
iAdvertiser->UpdateAvailabilityL( EFalse );
RequestData();
iState = EWaitingForMessage;
break;
case EWaitingForMessage:
{
HBufC* text = HBufC::NewLC( iBuffer.Length() );
text->Des().Copy( iBuffer );
iLog.LogL( *text );
CleanupStack::PopAndDestroy( text );
RequestData(); // Get more data
}
break;
default:
Panic( EBTPointToPointReceiverInvalidState );
break;
}
}
// ----------------------------------------------------------------------------
// CMessageServer::StartL()
// Starts the server.
// ----------------------------------------------------------------------------
//
void CMessageServer::StartL()
{
if ( iState != EDisconnected )
{
User::Leave( KErrInUse );
}
User::LeaveIfError( iSocketServer.Connect() );
TInt result( 0 );
result = iListeningSocket.Open( iSocketServer, KServerTransportName );
if ( result != KErrNone )
{
iSocketServer.Close();
User::Leave( result );
}
//
// Set the Socket's security with parameters,
// Authentication, Encryption, Authorisation and Denied
// Method also return the channel available to listen to.
TInt channel ( SetSecurityWithChannelL( EFalse, EFalse, ETrue, EFalse ) );
iAdvertiser->StartAdvertisingL( channel );
iAdvertiser->UpdateAvailabilityL( ETrue );
}
// ----------------------------------------------------------------------------
// CMessageServer::SetSecurityWithChannelL()
// Sets the security on the channel port and returns the available port.
// ----------------------------------------------------------------------------
//
TInt CMessageServer::SetSecurityWithChannelL( TBool aAuthentication,
TBool aEncryption,
TBool aAuthorisation,
TBool aDenied )
{
// Local variable to channel to listen to.
TInt channel;
User::LeaveIfError( iListeningSocket.GetOpt( KRFCOMMGetAvailableServerChannel, KSolBtRFCOMM, channel ) );
TBTSockAddr listeningAddress;
// Set the Port to listen to.
listeningAddress.SetPort( channel );
// Write Log events
HBufC* strGetPort = StringLoader::LoadLC ( R_BTPO_STR_GET_PORT );
iLog.LogL( *strGetPort, channel );
CleanupStack::PopAndDestroy( strGetPort );
User::LeaveIfError( iListeningSocket.Bind( listeningAddress ) );
User::LeaveIfError( iListeningSocket.Listen( KListeningQueSize ) );
// close old connection - if any
iAcceptedSocket.Close();
// Open abstract socket
User::LeaveIfError( iAcceptedSocket.Open( iSocketServer ) );
// Set the Active Object's State to Connecting indicated.
iState = EConnecting;
iListeningSocket.Accept( iAcceptedSocket, iStatus );
// Set the Active Object Active again,
SetActive();
// Write Log events
HBufC* acceptNextConn = StringLoader::LoadLC ( R_BTPO_ACCEPT_NEXT_CONN );
iLog.LogL( *acceptNextConn );
CleanupStack::PopAndDestroy( acceptNextConn );
// Set the security according to.
TBTServiceSecurity serviceSecurity;
serviceSecurity.SetUid ( KUidBTPointToPointApp );
serviceSecurity.SetAuthentication ( aAuthentication );
serviceSecurity.SetEncryption ( aEncryption );
serviceSecurity.SetAuthorisation ( aAuthorisation );
serviceSecurity.SetDenied( aDenied );
// Attach the security settings.
listeningAddress.SetSecurity(serviceSecurity);
// Retrurn the port to listen to.
return channel;
}
// ----------------------------------------------------------------------------
// CMessageServer::StopL()
// Stops the server.
// ----------------------------------------------------------------------------
//
void CMessageServer::StopL()
{
if ( iState != EDisconnected )
{
if ( iAdvertiser->IsAdvertising() )
{
iAdvertiser->StopAdvertisingL();
}
iAcceptedSocket.Close();
iListeningSocket.Close();
iSocketServer.Close();
}
iState = EDisconnected;
}
// ----------------------------------------------------------------------------
// CMessageServer::RequestData()
// Request data from the client.
// ----------------------------------------------------------------------------
//
void CMessageServer::RequestData()
{
iAcceptedSocket.RecvOneOrMore( iBuffer, 0, iStatus, iLen );
SetActive();
}
// ----------------------------------------------------------------------------
// CMessageServer::IsConnected()
// Results true if the server is connected.
// ----------------------------------------------------------------------------
//
TBool CMessageServer::IsConnected()
{
return !( iState == EDisconnected );
}
// End of File
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -