📄 chatbt.cpp
字号:
default:
Panic( EChatInvalidLogicState );
break;
};
}
}
// ----------------------------------------------------------------------------
// CChatBt::ConnectL()
// Connect to a service.
// ----------------------------------------------------------------------------
//
void CChatBt::ConnectL()
{
if ( State() == EWaitingToGetDevice && !IsActive() )
{
SetState( EGettingDevice );
iServiceSearcher->SelectDeviceByDiscoveryL( iStatus );
SetActive();
}
else
{
HBufC* errClientBusy = StringLoader::LoadLC ( R_CHAT_CLIENT_BUSY );
iLog.LogL( *errClientBusy );
CleanupStack::PopAndDestroy( errClientBusy );
User::Leave( KErrInUse );
}
}
// ----------------------------------------------------------------------------
// CChatBt::DisconnectL()
// Disconnects from the remote machine.
// ----------------------------------------------------------------------------
//
void CChatBt::DisconnectL()
{
if ( ( State() == EConnected )||( State() == ESendingMessage ) )
{
DisconnectFromServerL();
SetState( EDisconnecting );
}
else
{
HBufC* errNoConn = StringLoader::LoadLC ( R_CHAT_ERR_NO_CONN );
iLog.LogL( *errNoConn );
CleanupStack::PopAndDestroy( errNoConn );
User::Leave( KErrDisconnected );
}
}
// ----------------------------------------------------------------------------
// CChatBt::DisconnectFromServerL()
// Disconnects from the service
// ----------------------------------------------------------------------------
//
void CChatBt::DisconnectFromServerL()
{
// Terminate all operations
iSocket.CancelAll();
Cancel();
HBufC* strReleasingConn = StringLoader
::LoadLC ( R_CHAT_STR_RELEASING_CONN );
iLog.LogL( *strReleasingConn );
CleanupStack::PopAndDestroy( strReleasingConn );
iSocket.Shutdown( RSocket::ENormal, iStatus );
SetActive();
}
// ----------------------------------------------------------------------------
// CChatBt::ConnectToServerL()
// Connect to the server.
// ----------------------------------------------------------------------------
//
void CChatBt::ConnectToServerL()
{
HBufC* strConnecting = StringLoader
::LoadLC ( R_CHAT_STR_CONNECTING );
iLog.LogL( *strConnecting );
CleanupStack::PopAndDestroy( strConnecting );
User::LeaveIfError( iSocket.Open( iSocketServer, KStrRFCOMM ) );
TBTSockAddr address;
address.SetBTAddr( iServiceSearcher->BTDevAddr() );
address.SetPort( iServiceSearcher->Port() );
iSocket.Connect( address, iStatus );
iActiveSocket = &iSocket;
#ifdef __WINS__
// Fix to allow emulator client to connect to server
User::After( 1 );
#endif
SetActive();
}
// ----------------------------------------------------------------------------
// CChatBt::SendMessageL()
// Send a message to a service on a remote machine.
// ----------------------------------------------------------------------------
//
void CChatBt::SendMessageL( TDes& aText )
{
if ( State() != EConnected )
{
User::Leave( KErrDisconnected );
}
// stop reading socket
if ( iActiveSocket )
{
iActiveSocket->CancelRead();
}
if ( IsActive() )
{
Cancel();
}
TBufC<KChatTextBufLength> message ( aText );
SetState( ESendingMessage );
HBufC* tempString = HBufC::NewL( message.Length() );
CleanupStack::PushL( tempString );
tempString->Des().Copy( message );
iMessage = HBufC8::NewL(tempString->Length());
CleanupStack::Pop( tempString );
iMessage->Des().Copy( *tempString );
if ( iActiveSocket )
{
iActiveSocket->Write( *iMessage, iStatus );
}
delete tempString;
SetActive();
}
// ----------------------------------------------------------------------------
// CChatBt::StartL()
// Starts the server.
// ----------------------------------------------------------------------------
//
void CChatBt::StartL()
{
if ( State() != EWaitingToGetDevice )
{
User::Leave( KErrInUse );
}
TInt result( 0 );
result = iSocket.Open( iSocketServer, KStrRFCOMM );
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, channel );
iAdvertiser->StartAdvertisingL( channel );
iAdvertiser->UpdateAvailabilityL( ETrue );
SetServer( ETrue );
}
// ----------------------------------------------------------------------------
// CChatBt::SetSecurityWithChannelL( )
// Sets the security on the channel port and returns the available port.
// ----------------------------------------------------------------------------
//
void CChatBt::SetSecurityWithChannelL(
TBool aAuthentication,
TBool aEncryption,
TBool aAuthorisation,
TBool aDenied,
TInt& aChannel )
{
// Local variable to channel to listen to.
TInt channel;
User::LeaveIfError( iSocket.GetOpt(
KRFCOMMGetAvailableServerChannel,
KSolBtRFCOMM,
channel ) );
TBTSockAddr listeningAddress;
// Set the Port to listen to.
listeningAddress.SetPort( channel );
// Write Log events
HBufC* strGetPort = StringLoader::LoadLC ( R_CHAT_STR_GET_PORT );
iLog.LogL( *strGetPort, channel );
CleanupStack::PopAndDestroy( strGetPort );
User::LeaveIfError( iSocket.Bind( listeningAddress ) );
User::LeaveIfError( iSocket.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.
SetState( EConnecting );
iSocket.Accept( iAcceptedSocket, iStatus );
iActiveSocket = &iAcceptedSocket;
// Set the Active Object Active again,
SetActive();
// Write Log events
HBufC* acceptNextConn = StringLoader::LoadLC (
R_CHAT_ACCEPT_NEXT_CONN );
iLog.LogL( *acceptNextConn );
CleanupStack::PopAndDestroy( acceptNextConn );
// Set the security according to.
TBTServiceSecurity serviceSecurity;
serviceSecurity.SetUid ( KUidChatApp );
serviceSecurity.SetAuthentication ( aAuthentication );
serviceSecurity.SetEncryption ( aEncryption );
serviceSecurity.SetAuthorisation ( aAuthorisation );
serviceSecurity.SetDenied( aDenied );
// Attach the security settings.
listeningAddress.SetSecurity(serviceSecurity);
// Return the port to listen to.
aChannel = channel;
}
// ----------------------------------------------------------------------------
// CChatBt::StopL()
// Stops the server.
// ----------------------------------------------------------------------------
//
void CChatBt::StopL()
{
if ( State() != EDisconnected )
{
if ( iAdvertiser->IsAdvertising() )
{
iAdvertiser->StopAdvertisingL();
}
// Close() will wait forever for Read to complete
if ( State() == EConnected )
{
if ( iActiveSocket )
{
iActiveSocket->CancelRead();
}
}
iAcceptedSocket.Close();
iSocket.Close();
}
SetState( EWaitingToGetDevice );
}
// ----------------------------------------------------------------------------
// CChatBt::RequestData()
// Request data from the client.
// ----------------------------------------------------------------------------
//
void CChatBt::RequestData()
{
if ( iActiveSocket )
{
iActiveSocket->RecvOneOrMore( iBuffer, 0, iStatus, iLen );
}
SetActive();
}
// End of File
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -