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

📄 engsrv.cpp

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

// SYSTEM HEADERS
#include <in_sock.h>

// PROJECT HEADERS
#include "socksrv.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*/)
    {
    // Issue read request
    iRecvAddr = KInetAddrLoop;
    iSocket->RecvFrom(iDataBuffer, iRecvAddr, 0, iStatus);
    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)
    {
    // Cancel any outstanding read request
    iSocket->CancelRecv();
    }

// Transmitter AO implementation (class CTx)
CTx::CTx(RSocket* aSocket) : CActive(CActive::EPriorityStandard), iSocket(aSocket)
    {
    iSendAddr.SetPort(KTestClientPort);
    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;

        iSocket->SendTo(iDataBuffer, iSendAddr, 0, iStatus);
        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)
    {
    // Cancel any outstanding write request
    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;

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

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

    // Using datagrams...

    // Only need one socket - because there is no need to create 
    // a connection prior to sending messages
    err = iSocket.Open(iSession, KAfInet, KSockDatagram, KProtocolInetUdp);
    User::LeaveIfError(err);

    // Bind the socket to the correct port.
    TInetAddr anyAddrOnPort(KInetAddrAny, KTestServerPort);
    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;

    // Connect to the socket server
    err = iSession.Connect();
    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 sibling AOs
    iRxAO->SetObserver(iObserver);
    iTxAO->SetObserver(iObserver);
    }

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

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

void CModel::SetAutoReply(TBool aAutoReply)
    {
    iAutoReply = aAutoReply;
    }

TBool CModel::AutoReply(void)
    {
    return iAutoReply;
    }

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

void CModel::DoCancel(void)
    {
    iRxAO->Cancel();
    iTxAO->Cancel();
    iSocket.CancelAll();
    iSocket.Close();
    }

⌨️ 快捷键说明

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