📄 sockets1.cpp
字号:
//
// Sockets1.cpp - Sockets1 example
//
// Copyright (C) UIQ Technology AB, 2007
//
// This material is provided "as is" without any warranty to its performance or functionality.
// In no event shall UIQ Technology be liable for any damages whatsoever arising out of the
// use or inabilty to use this material.
//
#include "Sockets1.h"
#include "Sockets1.hrh"
#include <Sockets1.rsg>
#include <QikViewBase.h>
#include <QikCommand.h>
#include <QikListBoxModel.h>
#include <QikListBox.h>
#include <MQikListBoxData.h>
#include <QikListBoxData.h>
#include <eikstart.h>
#include <es_sock.h>
#include <in_sock.h>
//////////////////////////////////////////////////////////////////////////////
// The application Uid here MUST match UID3 defined in the MMP file
// This is a development UID and must NOT be used in production type software
const TUid KAppSpecificUid={0xEDEAD011};
// internal secondary view id, must be unique amongst this applications views
const TUid KUidListView={0x00000001};
// views within applications are fully identified by the app Uid and secondary view id
#define KViewIdListView TVwsViewId(KAppSpecificUid,KUidListView)
//////////////////////////////////////////////////////////////////////////////////
class CLogText : public CBase
{
public:
// new methods
~CLogText();
void ConstructL();
TInt Count();
const TDesC& At(const TInt aIndex);
void LogText(const TDesC& aBuf);
void ResetText();
protected:
CArrayFixFlat<HBufC*>* iText;
};
CLogText::~CLogText()
{
ResetText();
delete(iText);
}
void CLogText::ConstructL()
{
iText=new(ELeave)CArrayFixFlat<HBufC*>(32);
}
TInt CLogText::Count()
// Report number of logged messages
{
return(iText->Count());
}
const TDesC& CLogText::At(const TInt aIndex)
// report 'n'th text message
{
return(*iText->At(aIndex));
}
void CLogText::ResetText()
{
TInt count=Count();
for (TInt i=0;i<count;i++)
delete(iText->At(i));
iText->Reset();
}
void CLogText::LogText(const TDesC& aBuf)
{ // add new text item to front of the list
TRAPD(err,
TBuf<128>bb;
if (aBuf.Length()>120)
bb=aBuf.Left(120);
else
bb=aBuf;
HBufC* q=bb.AllocLC();
iText->InsertL(0,q); // at front of list
CleanupStack::Pop(q);
if (iText->Count()>100)
{ // restrict to last 100 log msgs
delete(iText->At(100));
iText->Delete(100);
}
);
}
//////////////////////////////////////////////////////////////////////////////////
class MSocketEngineObserver
{
public:
virtual void LogInfo(const TDesC& aBuf,const TDesC& aBuf2)=0;
};
//////////////////////////////////////////////////////////////////////////////////
const TInt KMaxServerNameLen=256;
enum TSocketEngineStatus
{
ESeNotConnected=0, // must be 0
ESeConnecting, // were attempting to connect to a server
ESeLookingUp, // translating name to IP
ESeConnected // were connected + talking
};
class CSocketEngine : public CActive
{
protected:
// from CActive
void DoCancel();
void RunL();
// new methods
void Connect(const TUint32 aAddr);
public:
~CSocketEngine();
CSocketEngine(RSocketServ& aServer,MSocketEngineObserver* aObserver);
void ConstructL();
void SetServerName(const TDesC& aName);
const TDesC& GetServerName() const;
void SetPort(const TInt aPort);
TInt GetPort() const;
void Connect();
protected:
RSocketServ& iSocketServer; // connection to the Symbian OS socket server process
MSocketEngineObserver* iObserver;
RHostResolver iHostResolver; // host name resolver
TSocketEngineStatus iState; // our state machine status
TInetAddr iAddress; // address were attempting to connect to
TInt iPort; // which port to connect to
TBuf<KMaxServerNameLen> iServerName; // which server we are to talk to
TNameEntry iNameEntry; // resolved name
RSocket iSocket; // socket though which were communicating
};
CSocketEngine::CSocketEngine(RSocketServ& aServer,MSocketEngineObserver* aObserver) :
CActive(CActive::EPriorityStandard),iSocketServer(aServer),iObserver(aObserver)
{
CActiveScheduler::Add(this);
}
CSocketEngine::~CSocketEngine()
{
Cancel(); // any NameLook up or connect requests
iSocket.Close(); // if we are non active but have an open socket, close it
}
void CSocketEngine::ConstructL()
{
// real apps will probably want Read/Write AOs associated with the SocketEngine..
}
void CSocketEngine::DoCancel()
//
// Cancel whatever is currently running
//
{
// Cancel appropriate request to socket
switch (iState)
{
case ESeConnecting: // cancel the tcp connect request
iSocket.CancelConnect();
iSocket.Close();
break;
case ESeLookingUp: // cancel the name look up
iHostResolver.Cancel();
iHostResolver.Close();
default:
break;
}
iState=ESeNotConnected;
}
_LIT(KSocketEngRunL,"SocketEng::RunL");
_LIT(KESeConnecting,"ESeConnecting");
_LIT(KConnectOK,"ConnectOK");
_LIT(KConnectFailed,"ConnectFailed");
_LIT(KESeLookingUp,"ESeLookingUp");
_LIT(KResolverErr,"ResolverErr:%d");
void CSocketEngine::RunL()
//
// One of our requests has completed
//
{
switch (iState)
{
case ESeConnecting: // IP connect request has completed
iObserver->LogInfo(KSocketEngRunL,KESeConnecting);
if (iStatus==KErrNone)
{ // sucessfully connected.. typically do comms now...
iState=ESeConnected;
iObserver->LogInfo(KSocketEngRunL,KConnectOK);
}
else
{ // connect attempt failed, allow retry or different addr
iSocket.Close();
iState=ESeNotConnected;
iObserver->LogInfo(KSocketEngRunL,KConnectFailed);
}
break;
case ESeLookingUp: // name look up has completed
iObserver->LogInfo(KSocketEngRunL,KESeLookingUp);
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(KSocketEngRunL,bb);
}
break;
default:
break;
}
}
void CSocketEngine::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 CSocketEngine::SetServerName(const TDesC& aName)
// Set the name of the server to connect to
{
iServerName=aName;
}
const TDesC& CSocketEngine::GetServerName() const
// Report the currently defined server name
{
return(iServerName);
}
void CSocketEngine::SetPort(const TInt aPort)
// Set the server port to connect to
{
iPort=aPort;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -