📄 securesockets.cpp
字号:
}
else
{ // read failed..
iSecureSocket->Close();
delete(iSecureSocket);
iSecureSocket=NULL;
iSocket.Close();
iState=ESeNotConnected;
iObserver->LogInfo(KEngRunL,KReadFailed);
}
break;
case ESeLookingUp: // name look up has completed
iHostResolver.Close();
iState=ESeNotConnected;
if (iStatus==KErrNone)
{ // DNS look up successful - attempt to connect to that IP addr
Connect(TInetAddr::Cast(iNameEntry().iAddr).Address());
}
else
{
TBuf<32> bb;
bb.Format(KResolverErr,iStatus.Int());
iObserver->LogInfo(KEngRunL,bb);
}
break;
default:
break;
}
}
void CSecureSocketEngine::Connect(const TUint32 aAddr)
//
// Attempt to connect to the port at the IP address
//
{
// Open a TCP socket
TInt err=iSocket.Open(iSocketServer,KAfInet,KSockStream,KProtocolInetTcp);
if (err==KErrNone)
{
// Set up address information
iAddress.SetPort(iPort);
iAddress.SetAddress(aAddr);
// Initiate socket connection
iSocket.Connect(iAddress,iStatus);
TBuf<32>bb;
TBuf<64>addr;
iAddress.Output(addr); // get a 10.0.0.1 type addr
_LIT(KAddrPort,"Addr:%S,port:%d");
bb.Format(KAddrPort,&addr,iPort);
_LIT(KConnectingTo,"Connecting to");
iObserver->LogInfo(KConnectingTo,bb);
}
else
{ // deal with errors in RunL()
TRequestStatus* q=(&iStatus);
User::RequestComplete(q,err);
_LIT(KConnect,"Connect");
_LIT(KSocketOpenErr,"SocketOpenErr");
iObserver->LogInfo(KConnect,KSocketOpenErr);
}
iState=ESeConnecting;
SetActive();
}
void CSecureSocketEngine::SetServerName(const TDesC& aName)
// Set the name of the server to connect to
{
iServerName=aName;
}
const TDesC& CSecureSocketEngine::GetServerName() const
// Report the currently defined server name
{
return(iServerName);
}
void CSecureSocketEngine::SetPort(const TInt aPort)
// Set the server port to connect to
{
iPort=aPort;
}
TInt CSecureSocketEngine::GetPort() const
// Report the currently defined port
{
return(iPort);
}
void CSecureSocketEngine::Connect()
//
// Attempt to connect to the server defined by iServerName
//
{
TInetAddr addr;
if (addr.Input(iServerName)==KErrNone)
Connect(addr.Address()); // server name already a valid IP addr
else
{ // need to look up IP addr using DNS
TInt err=iHostResolver.Open(iSocketServer,KAfInet,KProtocolInetUdp);
if (err==KErrNone)
iHostResolver.GetByName(iServerName,iNameEntry,iStatus);
else
{ // deal with all errors in the RunL()
TRequestStatus* q=(&iStatus);
User::RequestComplete(q,err);
_LIT(KConnectErr,"ConnectErr");
iObserver->LogInfo(KConnectErr,iServerName);
}
iState=ESeLookingUp; // now started request
SetActive();
}
}
//////////////////////////////////////////////////////////////////////////////////
class CAppSpecificListView : public CQikViewBase, public MSocketEngineObserver
{
protected:
// from CQikViewBase
TVwsViewId ViewId() const;
void HandleCommandL(CQikCommand& aCommand);
void ViewConstructL();
void ViewDeactivated();
void ViewActivatedL(const TVwsViewId& aPrevViewId,const TUid aCustomMessageId,const TDesC8& aCustomMessage);
// from MSocketEngineObserver
void LogInfo(const TDesC& aBuf,const TDesC& aBuf2);
void DataReceived(const TDesC8& aData);
void DataSent(const TInt aError);
// new methods
void RebuildListbox();
public:
// new methods
~CAppSpecificListView();
CAppSpecificListView(CAppSpecificUi& aAppUi);
protected:
RSocketServ iSocketServ;
CSecureSocketEngine* iSocketEngine;
CLogText* iLogText;
};
CAppSpecificListView::~CAppSpecificListView()
{
delete(iSocketEngine);
delete(iLogText);
iSocketServ.Close();
}
CAppSpecificListView::CAppSpecificListView(CAppSpecificUi& aAppUi) :
CQikViewBase(aAppUi,KNullViewId)
{}
TVwsViewId CAppSpecificListView::ViewId() const
//
// All views are uniquely identified within the entire system. A TVwsViewId consists of
// the application uid (uid3) and app specific view uid
//
{
return(KViewIdListView);
}
void CAppSpecificListView::HandleCommandL(CQikCommand& aCommand)
//
// Handle the commands coming in from the controls that can deliver cmds..
//
{
switch (aCommand.Id())
{
case EAppCmdConnect:
{
// any previous one - cancels connections etc
delete(iSocketEngine);
iSocketEngine=NULL;
// reset log and display to blank
if (!iLogText)
{
iLogText=new(ELeave)CLogText;
iLogText->ConstructL();
}
iLogText->ResetText();
RebuildListbox();
// a new socket
iSocketEngine=new(ELeave)CSecureSocketEngine(iSocketServ,this);
iSocketEngine->ConstructL();
// Port 443 is where standard secure web servers are listening
_LIT(KServername,"www.uiq.com");
iSocketEngine->SetServerName(KServername);
iSocketEngine->SetPort(443);
// asynchronously attempt to connect to a server
iSocketEngine->Connect();
break;
}
default: // e.g. the back button...
CQikViewBase::HandleCommandL(aCommand);
break;
}
}
void CAppSpecificListView::RebuildListbox()
//
// Rebuild the listbox from scratch
//
{
TRAPD(err,
CQikListBox* listbox=LocateControlByUniqueHandle<CQikListBox>(EAppSpecificListViewListId);
listbox->RemoveAllItemsL();
MQikListBoxModel& model(listbox->Model());
model.ModelBeginUpdateLC();
TInt count=iLogText->Count();
for (TInt i=0;i<count;i+=2)
{
MQikListBoxData* lbData=model.NewDataL(MQikListBoxModel::EDataNormal);
CleanupClosePushL(*lbData);
lbData->AddTextL(iLogText->At(i),EQikListBoxSlotText1);
lbData->AddTextL(iLogText->At(i+1),EQikListBoxSlotText2);
CleanupStack::PopAndDestroy(lbData);
}
model.ModelEndUpdateL();
);
}
void CAppSpecificListView::LogInfo(const TDesC& aBuf,const TDesC& aBuf2)
//
// Called back by the engine to add some log text to output log so operations are visible
//
{
// This is an example app. Logging content to the list box in this fashion cannot be
// recommended for commerical quality applications
iLogText->LogText(aBuf2);
iLogText->LogText(aBuf);
RebuildListbox();
}
void CAppSpecificListView::DataReceived(const TDesC8& aData)
//
// Some data has arrived from the remote device. In this app we assume its text and simply
// display on scn.
//
{
TBuf<64> bb;
if (aData.Length()>64)
{ // take first 64 chars
TPtrC8 q(aData.Ptr(),64);
bb.Copy(q);
}
else
bb.Copy(aData);
iLogText->LogText(bb);
RebuildListbox();
}
void CAppSpecificListView::DataSent(const TInt aError)
//
// The last write request completed ok.
// If we have a Queue of frames to write we can send the next one here.
//
{
_LIT(KMsgSentOk,"Msg Sent Ok");
if (aError==KErrNone)
iEikonEnv->InfoMsg(KMsgSentOk);
else
{ // report a send failure.. if due to a comms failure the read will fail soon as well
TBuf<32> bb;
_LIT(KSendError,"Send error %d");
bb.Format(KSendError,aError);
iEikonEnv->InfoMsg(bb);
}
}
void CAppSpecificListView::ViewConstructL()
{
// Loads information about the UI configurations this view supports
// together with definition of each view.
ViewConstructFromResourceL(R_APP_VIEW_CONFIGURATIONS);
// obtain IPC connected to the Symbian socket server process that does lots on our behalf
User::LeaveIfError(iSocketServ.Connect());
TBuf<64> bb;
iEikonEnv->ReadResourceL(bb,R_STR_APP_TITLE);
ViewContext()->AddTextL(1,bb);
}
void CAppSpecificListView::ViewDeactivated()
{
}
void CAppSpecificListView::ViewActivatedL(
const TVwsViewId& aPrevViewId,
const TUid aCustomMessageId,
const TDesC8& aCustomMessage)
{
}
//////////////////////////////////////////////////////////////////////////////
CAppSpecificUi::~CAppSpecificUi()
{
}
void CAppSpecificUi::ConstructL()
//
// Normal primary entry point to a Symbian App
//
{
CQikAppUi::ConstructL();
// We create and add a list view.
CAppSpecificListView* list=new(ELeave)CAppSpecificListView(*this);
CleanupStack::PushL(list);
list->ConstructL();
AddViewL(*list); // takes ownership
CleanupStack::Pop(list);
SetDefaultViewL(*list);
}
/////////////////////////////////////////////////////////////////////////////////////////////
// Standard Symbian application framework code when creating an application
class CAppSpecificDocument : public CQikDocument
{
protected:
CQikAppUi* CreateAppUiL();
public:
CAppSpecificDocument(CQikApplication& aApp);
static CAppSpecificDocument* NewL(CQikApplication& aApp);
protected:
};
CAppSpecificDocument::CAppSpecificDocument(CQikApplication& aApp) :
CQikDocument(aApp)
{
__DECLARE_NAME(_S("CAppSpecificDocument"));
}
CAppSpecificDocument* CAppSpecificDocument::NewL(CQikApplication& aApp)
{
return(new(ELeave)CAppSpecificDocument(aApp));
}
CQikAppUi* CAppSpecificDocument::CreateAppUiL()
{
return(new(ELeave)CAppSpecificUi);
}
//////////////////////////////////////////////////////////////////////////////////
// Standard Symbian application framework code when creating an application
class CAppSpecificApplication : public CQikApplication
{
protected:
TUid AppDllUid() const;
CApaDocument* CreateDocumentL();
};
TUid CAppSpecificApplication::AppDllUid() const
{
return(KAppSpecificUid);
}
CApaDocument* CAppSpecificApplication::CreateDocumentL()
{
return(CAppSpecificDocument::NewL(*this));
}
//////////////////////////////////////////////////////////////////////////////////
// Standard Symbian application start up code
LOCAL_C CApaApplication* NewApplication()
{
return(new CAppSpecificApplication);
}
GLDEF_C TInt E32Main()
{
return(EikStart::RunApplication(NewApplication));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -