⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 socketengine.cpp

📁 基于SIP协议的即时消息聊天功能设计,Symbian平台下实现
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	// Open a TCP socket
	User::LeaveIfError( iDataSocket.Open(
	    iSocketServer, KAfInet, KSockStream, KProtocolInetTcp, iConnection ) );
    
    iRemoteAddr.Input(aRemoteAddr);
	iRemoteAddr.SetPort(aPort);

    // The Connect() allocate local address automatically if 
    // not bind yet.
    iDataSocket.Connect( iRemoteAddr, iStatus );
        
	iLocalPort = iDataSocket.LocalPort();
    iState = EConnecting;
    SetActive();
    iTimer->After( KTimeOut );
    }

// -----------------------------------------------------------------------------
// CSocketEngine::Write
// Starts the asynchronous writing
// -----------------------------------------------------------------------------
//
void CSocketEngine::Write( const TDesC8& aTxt )
    {
		if(iProtocolType == ETCP)
		{
			if( iState == EConnected && !iWriter->IsActive() )
	        {
		    iWriter->Write( aTxt );
			}
		}
		else
		{
			//if(!iWriter->IsActive())//1
			//{
				iWriter->Write( aTxt );
			//}
		}
    }

// -----------------------------------------------------------------------------
// CSocketEngine::Read
// Starts the asynchronous reading.
// -----------------------------------------------------------------------------
//
void CSocketEngine::Read()
    {
    	if (iProtocolType == ETCP)
		{
			if( iState == EConnected && !iReader->IsActive() )
			{
				iReader->Read();
			}
		}
		else
		{
			if( !iReader->IsActive() )
				iReader->Read();
		}
    }

// -----------------------------------------------------------------------------
// CSocketEngine::Stop
// Cancelling any outstanding read/write request.
// -----------------------------------------------------------------------------
//
void CSocketEngine::Stop()
    {
    switch( iState )
        {
        case EConnected:
			{
            iWriter->Cancel();
            iReader->Cancel();
            TRequestStatus status;
            iDataSocket.Shutdown( RSocket::EImmediate, status );
            User::WaitForRequest( status);
            iConnection.Close();
            ChangeStateAndNotify( ENotConnected );
			}
            break;
        case EListening:
        case EConnecting:
            Cancel();
            break;
        default:
            break;
        }
    }

// -----------------------------------------------------------------------------
// CSocketEngine::StartListeningL
// Resolves the local address and starts listening game port. Asynchronous.
// -----------------------------------------------------------------------------
//
TInetAddr& CSocketEngine::StartListeningL( const TUint32 aIapId)
    {
	
	// 开始获得本机IP,填充到iRemoteAddr
//    StartInterfaceL( aIapId );
//    ResolveLocalIPAddressL();

    iLocalAddr.SetPort( 0 );
    User::LeaveIfError( iListenSocket.Open( 
            iSocketServer, KAfInet, KSockStream, KProtocolInetTcp ) );
    User::LeaveIfError( iDataSocket.Open( iSocketServer ) );
    User::LeaveIfError( iListenSocket.Bind( iLocalAddr ) );
    iLocalPort = iListenSocket.LocalPort();
    User::LeaveIfError( iListenSocket.Listen( 1 ) );

    iListenSocket.Accept( iDataSocket, iStatus );    
    iState = EListening;
    SetActive();

    return iLocalAddr;
    }

TInt& CSocketEngine::LocalPort()
	{
		return iLocalPort;
	}
    
// -----------------------------------------------------------------------------
// CSocketEngine::StartInterfaceL
// Activates PDP ctx
// -----------------------------------------------------------------------------
//
void CSocketEngine::StartInterfaceL( const TUint32 aIapId )
    {
		if( !iConnection.SubSessionHandle() )
		{ 
			User::LeaveIfError( iConnection.Open( iSocketServer ) );
		}

#ifdef _DEBUG
		TCommDbConnPref prefs;
		prefs.SetDialogPreference( ECommDbDialogPrefDoNotPrompt );
		prefs.SetDirection( ECommDbConnectionDirectionOutgoing );
		prefs.SetIapId( aIapId );
		User::LeaveIfError( iConnection.Start( prefs ) ); 		
#else

		TUint connectionCount = 0;
		User::LeaveIfError(iConnection.EnumerateConnections(connectionCount));

		if(connectionCount > 0)   
		{   
			TPckgBuf<TConnectionInfo>   connectionInfo; 
			iConnection.GetConnectionInfo(1, connectionInfo);
			TInt err = iConnection.Attach(connectionInfo, RConnection::EAttachTypeNormal);
		}
		else
		{
			TCommDbConnPref prefs;
			prefs.SetDialogPreference( ECommDbDialogPrefDoNotPrompt );
			prefs.SetDirection( ECommDbConnectionDirectionOutgoing );
			prefs.SetIapId( aIapId );
			User::LeaveIfError( iConnection.Start( prefs ) );
		}
#endif

    }

// -----------------------------------------------------------------------------
// CSocketEngine::ResolveLocalIPAddressL
// Resolves the local IP address.
// -----------------------------------------------------------------------------
//
void CSocketEngine::ResolveLocalIPAddressL()
    {
    RHostResolver hostResolver;
    TNameEntry entry;

    User::LeaveIfError( hostResolver.Open( iSocketServer, KAfInet, 
                                          KProtocolInetTcp, iConnection ));
    CleanupClosePushL( hostResolver );
 
    User::LeaveIfError( hostResolver.GetByName( TPtrC(), entry ) );
    if ( !TInetAddr::Cast( entry().iAddr ).IsWildAddr() )
         {
         iLocalAddr = TInetAddr::Cast( entry().iAddr );
         }
         
    CleanupStack::PopAndDestroy(); // hostResolver
    }

// -----------------------------------------------------------------------------
// CSocketEngine::TimerExpired
// Callback from the timeout notifier. Completes the request with error code
// KErrNone and state ETimedOut.
// -----------------------------------------------------------------------------
//
void CSocketEngine::TimerExpired()
    {
    Cancel();
	iState = ETimedOut;
	
    TRequestStatus* status = &iStatus;		
    SetActive();
	User::RequestComplete( status, KErrNone );
    }

// -----------------------------------------------------------------------------
// CSocketEngine::MessageReceived
// Notifies the observer.
// -----------------------------------------------------------------------------
//
void CSocketEngine::MessageReceived( TDesC8& aBuffer )
    {
    iObserver.SocketData( aBuffer );
    }

// -----------------------------------------------------------------------------
// CSocketEngine::ErrorInReading
// Completes the request with error code.
// -----------------------------------------------------------------------------
//
void CSocketEngine::ErrorInReading( TInt aError )
    {
    TRequestStatus* status = &iStatus;		
    SetActive();
	User::RequestComplete( status, aError );
    }

// From MWriterNotifier
// -----------------------------------------------------------------------------
// CSocketEngine::WriteDone
// 
// -----------------------------------------------------------------------------
//
void CSocketEngine::WriteDone()
    {
    // Nothing to do
    }

// -----------------------------------------------------------------------------
// CSocketEngine::ErrorInWriting
// Completes the request with error code.
// -----------------------------------------------------------------------------
//
void CSocketEngine::ErrorInWriting( TInt aError )
    {
    TRequestStatus* status = &iStatus;		
    SetActive();
	User::RequestComplete( status, aError );
    }

// -----------------------------------------------------------------------------
// CSocketEngine::ChangeStateAndNotify
// Changes new state and notifies observer.
// -----------------------------------------------------------------------------
//
void CSocketEngine::ChangeStateAndNotify( TSocketEngineState aNewState )
    {
    iState = aNewState;
    iObserver.SocketState( iState );
    }

TInt& CSocketEngine::State()
{
	return (TInt&)iState;
}

void CSocketEngine::InitUDP()
{
	StartInterfaceL(3);
	ResolveLocalIPAddressL();

	User::LeaveIfError(iDataSocket.Open(iSocketServer, KAfInet, KSockDatagram, KProtocolInetUdp));

	TInetAddr addr;
	addr.SetPort( iLocalPort );
	addr.SetAddress( KInetAddrAny );
	addr.SetFamily( KAfInet );

	User::LeaveIfError(iDataSocket.Bind( addr ));

	iReader = CSocketReader::NewL(iDataSocket, *this, EUDP);
	iWriter = CSocketWriter::NewL(iDataSocket, *this, EUDP, iRemoteAddr);

	iReader->Read();	
}

TInetAddr& CSocketEngine::LocalIPAddress()
{
	return iLocalAddr;
}

void CSocketEngine::InitTCP()
{
	iReader = CSocketReader::NewL(iDataSocket, *this, ETCP);
	iWriter = CSocketWriter::NewL(iDataSocket, *this, ETCP, iRemoteAddr);
}
// End of file

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -