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

📄 clieng.cpp

📁 如题 就是 这东西 为什么非要我 说到 20个 字 呢 看看这回 够 不
💻 CPP
字号:
// clieng.cpp
//
// Copyright (c) 1999-2007 Symbian Software Ltd.  All rights reserved.
//
// $Change: 937687 $
//
// client server exercise

// SYSTEM HEADERS
#include <in_sock.h>

// PROJECT HEADERS
#include "cliui.h"

// Receiver AO implementation (class CRx)
CRx::CRx(RSocket* aSocket) : CActive(CActive::EPriorityStandard), iRecvLen(MAX_MSG_LEN), iSocket(aSocket)
    {
    }

CRx* CRx::NewL(RSocket* aSocket)
    {
    CRx* self = new(ELeave) CRx(aSocket);
    CleanupStack::PushL(self);
    self->ConstructL();
    CleanupStack::Pop(self);
    CActiveScheduler::Add(self);
    return self;
    }

CRx::~CRx()
    {
    Cancel();   // cancel ANY outstanding request at time of destruction
    }

void CRx::RxL(TInt /*aSocketType*/)
    {
    //TODO
    // Issue a request on your socket (iSocket) to receive an incoming datagram.

    // You want the request to complete when a datagram has been received - not necessarily when
    // the incoming buffer has been filled. This requirement should help you choose the correct
    // RSocket method.

    // Note that the CRx class has a pointer to the socket you created in the CModel class
    // and that a buffer (iDataBuffer) has already been set up for you to receive the
    // incoming data.

    // Finally, note that the iRecvAddr data member can be used to hold the source address
    // of any incoming datagrams.
    iSocket->RecvFrom(iDataBuffer, iRecvAddr, 0, iStatus);

    // Having issued the request for incoming data, ensure you mark this active object 
    // as having an outstanding request.
    SetActive();
    }

void CRx::SetObserver(MSocketObserver* aObserver)
    {
    iObserver = aObserver;
    }

TDesC* CRx::GetDataIn(void)
    {
    return &iDataBuffer16;
    }

void CRx::ConstructL(void)
    {
    }

void CRx::NotifyEvent(TInt aEvent)
    {
    iObserver->HandleSocketEvent(CModel::ESourceRx, aEvent);
    }

void CRx::RunL(void)
    {
    if (iStatus == KErrNone)    // received some data
        {
        // covert binary received to unicode
        TPtrC16 myPtr(reinterpret_cast<const TUint16*>(iDataBuffer.Ptr()),
                        (iDataBuffer.Size()/2));
        iDataBuffer16 = myPtr;

        NotifyEvent(CRx::ERxReceiveOK);
        }
    else
        {
        // some error condition
        NotifyEvent(CRx::ERxReceiveFailed);
        }
    }

void CRx::DoCancel(void)
    {
    //TODO
    // Cancel any outstanding read request on the socket.
    iSocket->CancelRecv();
    }

// Transmitter AO implementation (class CTx)
CTx::CTx(RSocket* aSocket) : CActive(CActive::EPriorityStandard), iSocket(aSocket)
    {
    iSendAddr.SetPort(KTestServerPort);
    iSendAddr.SetAddress(KInetAddrLoop);
    }

CTx* CTx::NewL(RSocket* aSocket)
    {
    CTx* self = new(ELeave) CTx(aSocket);
    CleanupStack::PushL(self);
    self->ConstructL();
    CleanupStack::Pop(self);
    CActiveScheduler::Add(self);
    return self;
    }

CTx::~CTx()
    {
    Cancel();   // cancel ANY outstanding request at time of destruction
    }

void CTx::TxL(TDesC16& aData, TInt /*aSocketType*/)
    {
    if (!IsActive())
        {
        // Take a copy of the unicode data to be sent as binary.
        TPtrC8 myPtr(reinterpret_cast<const TUint8*>(aData.Ptr()), aData.Size());
        iDataBuffer = myPtr;

        //TODO
        // Issue a datagram transmit request on your socket (iSocket).

        // Note that the CTx class has a pointer to the socket you created in the CModel class
        // and that a copy of the data to be transmitted has been taken 
        // in the iDataBuffer member.
        // Copying the data in this way means the calling code does not have to 
        // worry about persisting the data until the transmission has completed.

        // You should send your datagram to the address defined for you in the data member
        // iSendAddr.

        // Issue write request (user initiated)
        iSocket->SendTo(iDataBuffer, iSendAddr, 0, iStatus);

        // Having issued the request for incoming data, ensure you mark this active object 
        // as having an outstanding request.
        SetActive();
        }
    }

void CTx::SetObserver(MSocketObserver* aObserver)
    {
    iObserver = aObserver;
    }

void CTx::ConstructL(void)
    {
    }

void CTx::NotifyEvent(TInt aEvent)
    {
    iObserver->HandleSocketEvent(CModel::ESourceTx, aEvent);
    }

void CTx::RunL(void)
    {
    if (iStatus == KErrNone)    // transmit ok
        {
        NotifyEvent(CTx::ETxTransmitOK);
        }
    else
        {
        // some error condition
        NotifyEvent(CTx::ETxTransmitFailed);
        }
    }

void CTx::DoCancel(void)
    {
    //TODO
    // Cancel any outstanding write request on iSocket at this time.
    iSocket->CancelSend();
    }

// Main model implementation (class CModel - also an AO)
CModel::CModel() : CActive(CActive::EPriorityStandard)
    {
    }

CModel* CModel::NewL(void)
    {
    CModel* self = new(ELeave) CModel;
    CleanupStack::PushL(self);
    self->ConstructL();
    CleanupStack::Pop(self);
    CActiveScheduler::Add(self);
    return self;
    }

CModel::~CModel()
    {
    // Cancel ANY outstanding request - including these requests owned by the sibling AOs
    Cancel();

    // Destroy the sibling active objects
    delete iRxAO;
    delete iTxAO;

    //TODO
    // Shut down the connection to the socket server.
    iSession.Close();
    }

void CModel::StartEngineL(void)
    {
    TInt err;
    // We have already created a session with the socket server.
    // The RSocketServ class cannot be used to exchange data with another application.
    // To be able to communicate, we need to create a socket.

    //TODO
    // Add a line of code to create a socket for use by your application.
    // Note that CModel contains a RSocket data member for this purpose.
    // Refer to the RSocket class documentation in the SDK to determine how you create
    // a socket.
    // You should create a socket with the following properties:
    //        Socket type          = KSockDatagram
    //        Protocol             = KProtocolInetUdp
    //        Addressing family    = KAfInet

    // Ensure that this method leaves if you fail to create a socket for any reason.
    err = iSocket.Open(iSession, KAfInet, KSockDatagram, KProtocolInetUdp);
    User::LeaveIfError(err);

    // ******************************************************
    // DO NOT MODIFY THE CODE IN THIS METHOD BELOW THIS POINT
    // ******************************************************

    // Bind the socket to the correct port.
    TInetAddr anyAddrOnPort(KInetAddrAny, KTestClientPort);
    iSocket.Bind(anyAddrOnPort);

    // Now need to start the receiver AO. (Transmit AO fires on user demand)
    iRxAO->RxL(iSocketType);
    }

void CModel::SendMessageL(TDesC& aMsg)
    {
    // Pass message on to transmit AO
    if (!iTxAO->IsActive())
        {
        iTxAO->TxL(aMsg, iSocketType);
        }
    }

void CModel::SetObserver(MSocketObserver* aObserver)
    {
    iObserver = aObserver;
    }

void CModel::RequestMessage(void)
    {
    if (!iRxAO->IsActive())
        {
        iRxAO->RxL(iSocketType);
        }
    }

TDesC* CModel::GetDataIn(void)
    {
    return iRxAO->GetDataIn();
    }

void CModel::ConstructL(void)
    {
    TInt err;

    //TODO
    // Write a line of code to create a session with the socket server 
    // for your client application.
    // This class contains a RSocketServ data member for this purpose.

    err = iSession.Connect();

    // Note that this function should leave if you fail to create a session 
    // with the socket server.
    User::LeaveIfError(err);
    }

void CModel::PreStartEngineL(void)
    {
    // Create the receiver AO
    iRxAO = CRx::NewL(&iSocket);

    // Create the transmitter AO
    iTxAO = CTx::NewL(&iSocket);

    // Set observers on the two sibling AOs
    iRxAO->SetObserver(iObserver);
    iTxAO->SetObserver(iObserver);
    }

void CModel::SetSocketType(TInt aSockType)
    {
    iSocketType = aSockType;
    }

TInt CModel::SocketType(void)
    {
    return iSocketType;
    }

RSocketServ& CModel::Session(void)
    {
    return iSession;
    }

void CModel::RunL(void)
    {
    // Not called in this implementation!
    }

void CModel::DoCancel(void)
    {
    iRxAO->Cancel();
    iTxAO->Cancel();

    //TODO
    // Ensure that any outstanding requests are cancelled
    // then close the socket.
    iSocket.CancelAll();
    iSocket.Close();
    }

⌨️ 快捷键说明

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