📄 socksrv.cpp
字号:
// socksrv.cpp
//
// Copyright (c) 1999-2007 Symbian Software Ltd. All rights reserved.
//
// $Change: 937687 $
// cs_serv exercise
// SYSTEM HEADERS
#include <eikstart.h>
#include <txtetext.h>
// PROJECT HEADERS
#include "socksrv.h"
LOCAL_C CApaApplication* NewApplication()
{
return new CSocketsApplication;
}
GLDEF_C TInt E32Main()
{
return EikStart::RunApplication(NewApplication);
}
////////////////////////////////////////////////////////////////
//
// Application class, CExampleApplication
//
////////////////////////////////////////////////////////////////
TUid CSocketsApplication::AppDllUid() const
{
return KUidSocketsApp;
}
CApaDocument* CSocketsApplication::CreateDocumentL()
{
return CSocketsDocument::NewL(*this); // Construct the document
}
////////////////////////////////////////////////////////////////
//
// Document class, CExampleDocument
//
////////////////////////////////////////////////////////////////
// C++ constructor
CSocketsDocument::CSocketsDocument(CEikApplication& aApp)
: CEikDocument(aApp)
{
}
// Implement two-phase construction for document
CSocketsDocument* CSocketsDocument::NewL(CEikApplication& aApp)
{
CSocketsDocument* self = new(ELeave) CSocketsDocument(aApp);
CleanupStack::PushL(self);
self->ConstructL();
CleanupStack::Pop(self);
return self;
}
void CSocketsDocument::ConstructL()
{
iModel = CModel::NewL(); // Construct the model
}
CSocketsDocument::~CSocketsDocument()
{
delete iModel; // delete the model
}
CEikAppUi* CSocketsDocument::CreateAppUiL()
{
return new(ELeave) CSocketsAppUi;
}
////////////////////////////////////////////////////////////////
//
// App UI class, CExampleAppUi
//
////////////////////////////////////////////////////////////////
void CSocketsAppUi::ConstructL()
{
BaseConstructL();
iModel = STATIC_CAST(CSocketsDocument*, iDocument)->Model();
iAppView = new(ELeave) CSocketsAppView;
iAppView->ConstructL(ClientRect(), iModel);
AddToStackL(iAppView);
// Configure the app.
iModel->SetSocketType(CModel::EDatagram);
iModel->SetAutoReply(ETrue);
iModel->PreStartEngineL();
// We are using datagrams: there is no connection established
// between endpoints. Therefore we can send messages immediately,
// so set focus on the current msg edwin.
iAppView->SetCurrentMsgFocus(ETrue);
// Also need to create our socket...
iModel->StartEngineL();
}
CSocketsAppUi::~CSocketsAppUi()
{
RemoveFromStack(iAppView);
delete iAppView;
}
void CSocketsAppUi::HandleCommandL(TInt aCommand)
{
switch (aCommand)
{
case ESendCurrentMsg:
iAppView->SendMessageL();
break;
case EClearRx:
iEikonEnv->InfoMsg(R_CLEAR_RX);
iAppView->ClearRxL();
break;
case EEikCmdExit:
Exit();
break;
default:
break;
}
}
////////////////////////////////////////////////////////////////
//
// Application view class, CSocketsAppView
//
////////////////////////////////////////////////////////////////
CSocketsAppView::~CSocketsAppView()
{
delete iHistory;
delete iMsg;
delete iHistoryLabel;
delete iMsgLabel;
}
void CSocketsAppView::ConstructL(const TRect& aRect, CModel* aModel)
{
// Take copy of pointer to model.
iModel = aModel;
iModel->SetObserver(this);
CreateWindowL();
SetRect(aRect);
// Construct the sub-views - those contained within this view.
_LIT(KMsgLabel, "Message to send:");
_LIT(KHistoryLabel, "Received messages:");
// Fetch font for use with labels.
iFont = iEikonEnv->LegendFont();
// Create label for history edwin
TInt offset = aRect.iBr.iX / 2-80;
TPoint historyLabelPos(offset, 20);
iHistoryLabel = new(ELeave) CEikLabel;
iHistoryLabel->SetFont(iFont);
iHistoryLabel->SetTextL(KHistoryLabel);
iHistoryLabel->SetContainerWindowL(*this);
iHistoryLabel->SetEmphasis(CEikLabel::ENoEmphasis);
iHistoryLabel->iAlignment.SetHAlignment(TGulHAlignment(EHLeft + EHCenter));
iHistoryLabel->iAlignment.SetVAlignment(TGulVAlignment(EVTop + EVCenter));
iHistoryLabel->SetExtent(historyLabelPos, iHistoryLabel->MinimumSize());
// Create label for msg edwin
TPoint msgLabelPos(offset, 180);
iMsgLabel = new(ELeave) CEikLabel;
iMsgLabel->SetFont(iFont);
iMsgLabel->SetTextL(KMsgLabel);
iMsgLabel->SetContainerWindowL(*this);
iMsgLabel->SetEmphasis(CEikLabel::ENoEmphasis);
iMsgLabel->iAlignment.SetHAlignment(TGulHAlignment(EHLeft + EHCenter));
iMsgLabel->iAlignment.SetVAlignment(TGulVAlignment(EVTop + EVCenter));
iMsgLabel->SetExtent(msgLabelPos, iMsgLabel->MinimumSize());
TPoint historyPos(offset,
iHistoryLabel->Position().iY + iHistoryLabel->Size().iHeight + 10);
iHistory = new(ELeave) CEikEdwin;
iHistory->SetContainerWindowL(*this);
iHistory->SetObserver(this);
iHistory->ConstructL(CEikEdwin::EWidthInPixels + CEikEdwin::EAllowPictures+CEikEdwin::EInclusiveSizeFixed,150,0,6);
iHistory->SetExtent(historyPos, iHistory->MinimumSize());
iHistory->SetReadOnly(ETrue);
iHistory->CreateScrollBarFrameL();
iHistory->ScrollBarFrame()->SetScrollBarVisibilityL(CEikScrollBarFrame::EOff, CEikScrollBarFrame::EOn);
TPoint msgPos(offset, iMsgLabel->Position().iY + iMsgLabel->Size().iHeight + 10);
iMsg = new(ELeave) CEikEdwin;
iMsg->SetContainerWindowL(*this);
iMsg->SetObserver(this);
iMsg->ConstructL(CEikEdwin::EWidthInPixels + CEikEdwin::EAllowPictures+EEikEdwinNoWrap,150,0,1);
iMsg->SetExtent(msgPos, iMsg->MinimumSize());
iFocusControl = iMsg;
iFocusControl->SetFocus(EFalse); // set focus on the current msg edwin.
ActivateL();
}
TInt CSocketsAppView::CountComponentControls() const
{
return 4;
}
CCoeControl* CSocketsAppView::ComponentControl(TInt aIndex) const
{
switch (aIndex)
{
case 0: return iHistoryLabel;
case 1: return iMsgLabel;
case 2: return iHistory;
case 3: return iMsg;
default: return 0;
}
}
TKeyResponse CSocketsAppView::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
{
TKeyResponse response = EKeyWasNotConsumed;
if (aType == EEventKey)
{
if (aKeyEvent.iModifiers & EModifierCtrl)
{
switch (aKeyEvent.iCode)
{
case CTRL('e'):
CActiveScheduler::Stop();
response = EKeyWasConsumed;
break;
default:
;
}
}
response = iFocusControl->OfferKeyEventL(aKeyEvent, aType);
}
return(response);
}
void CSocketsAppView::Draw(const TRect& aRect) const
{
CWindowGc& gc = SystemGc();
gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
gc.SetBrushColor(KRgbYellow);
gc.DrawRect(aRect);
}
void CSocketsAppView::SendMessageL(void)
{
// Need to extract the text from the iMsg member edwin first.
HBufC* toSend = NULL;
toSend = iMsg->GetTextInHBufL();
if (toSend)
{
// Got some data to send.
iModel->SendMessageL(*toSend);
delete toSend;
}
// Now clear the text in the message edwin
// actually, should make this a configurable option
iMsg->SetTextL('\0');
iMsg->HandleTextChangedL();
}
void CSocketsAppView::SendReplyL(void)
{
TDesC* msgIn = NULL;
msgIn = iModel->GetDataIn();
if (msgIn)
{
// Got some data to send
TBuf<MAX_MSG_LEN> modMsgIn = *msgIn;
ReverseDescriptor(modMsgIn);
iModel->SendMessageL(modMsgIn);
}
}
void CSocketsAppView::ReverseDescriptor(TDes& aData)
{
TInt dataLen = aData.Length();
TText copy;
for (TInt i = 0; i < dataLen / 2; i++)
{
copy = aData[i];
aData[i] = aData[dataLen-i-1];
aData[dataLen-i-1] = copy;
}
}
void CSocketsAppView::ClearRxL(void)
{
iHistory->SetTextL('\0');
iHistory->HandleTextChangedL();
}
void CSocketsAppView::SetCurrentMsgFocus(TBool aFocus)
{
iFocusControl->SetFocus(aFocus); // set focus on the current msg edwin.
}
// Implementation of MCoeControlObserver
void CSocketsAppView::HandleControlEventL(CCoeControl* /*aControl*/, TCoeEvent /*aEventType*/)
{
}
// Implementation of MSocketObserver
void CSocketsAppView::HandleSocketEvent(TInt aSource, TInt aReason)
{
switch (aSource)
{
case CModel::ESourceRx:
HandleRxSocketEvent(aReason);
break;
case CModel::ESourceTx:
HandleTxSocketEvent(aReason);
break;
default:
break;
}
}
void CSocketsAppView::HandleMsgIn(void)
{
// Need to query the models active object to get the data...
TDesC* msgIn;
msgIn = iModel->GetDataIn();
TInt msgLength = msgIn->Length();
TInt reallocationLength = iHistory->TextLength() + msgLength + 2; // allow for 16-bit carriage return.
if (msgLength)
{
HBufC* newHistory;
newHistory = iHistory->GetTextInHBufL();
// Now expand newHistory so it can hold the expanded data.
if (newHistory)
{
CleanupStack::PushL(newHistory);
newHistory = newHistory->ReAllocL(reallocationLength);
CleanupStack::Pop(); // previous newHistory
CleanupStack::PushL(newHistory); // reallocated newHistory.
TPtr newHistoryPtr = newHistory->Des();
#ifdef __WINS__
#pragma warning(disable : 4239) // disable nonstandad extension used warning
newHistoryPtr.Set(newHistory->Des()); // fixes VC6 bug
#pragma warning(default : 4239) // re-enable nonstandard extension used warning
#endif
newHistoryPtr.Append(CEditableText::EParagraphDelimiter);
newHistoryPtr.Append(*msgIn);
iHistory->SetTextL(newHistory);
iHistory->HandleTextChangedL();
CleanupStack::PopAndDestroy(newHistory);// newHistory
}
else
{
// First time through, just set the history to the msg to be sent.
iHistory->SetTextL(msgIn);
iHistory->HandleTextChangedL();
}
}
}
void CSocketsAppView::HandleRxSocketEvent(TInt aReason)
{
switch (aReason)
{
case CRx::ERxReceiveOK:
HandleMsgIn();
if (iModel->AutoReply())
{
SendReplyL();
}
iModel->RequestMessage();
break;
case CRx::ERxReceiveFailed:
iEikonEnv->BusyMsgL(R_RX_EVENT_FAIL);
break;
default:
break;
}
}
void CSocketsAppView::HandleTxSocketEvent(TInt aReason)
{
switch (aReason)
{
case CTx::ETxTransmitOK:
iEikonEnv->InfoMsg(R_TX_EVENT_OK);
break;
case CTx::ETxTransmitFailed:
iEikonEnv->InfoMsg(R_TX_EVENT_FAIL);
break;
default:
break;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -