📄 socketsengine.cpp
字号:
TUint32 networkId;
commsView = commsDb->OpenTableLC(TPtrC(NETWORK));
TRAP(error,commsView->InsertRecord(networkId));
if (error!=KErrNone) return -1;
commsView->WriteTextL(TPtrC(COMMDB_NAME), KCmnetIapName);
TRAP(error,commsView->PutRecordChanges(EFalse, EFalse));
if (error!=KErrNone) return -1;
CleanupStack::PopAndDestroy(commsView);
// find Mobile LOCATION id
TInt result;
TUint32 locationId;
TUint32 mobileLocationId;
commsView = commsDb->OpenTableLC(TPtrC(LOCATION));
result = commsView->GotoFirstRecord();
TBuf<128> locationName;
while (result == KErrNone) {
commsView->ReadTextL(TPtrC(COMMDB_NAME), locationName);
commsView->ReadUintL(TPtrC(LOCATION_MOBILE), locationId);
if (locationName.Match(_L("Mobile"))!= KErrNotFound)
mobileLocationId = locationId;
result = commsView->GotoNextRecord();
}
CleanupStack::PopAndDestroy(commsView);
// create IAP
TUint32 iapId;
commsView = commsDb->OpenTableLC(TPtrC(IAP));
TRAP(error,commsView->InsertRecord(iapId));
if (error!=KErrNone) return -1;
commsView->WriteTextL(TPtrC(COMMDB_NAME), KCmnetIapName);
commsView->WriteTextL(TPtrC(IAP_SERVICE_TYPE), TPtrC(OUTGOING_GPRS));
commsView->WriteUintL(TPtrC(IAP_SERVICE), GPRSId);
commsView->WriteUintL(TPtrC(IAP_NETWORK_WEIGHTING), 0);
commsView->WriteUintL(TPtrC(IAP_NETWORK), networkId);
commsView->WriteUintL(TPtrC(IAP_BEARER), 2);
commsView->WriteTextL(TPtrC(IAP_BEARER_TYPE), TPtrC(MODEM_BEARER));
commsView->WriteUintL(TPtrC(IAP_LOCATION), mobileLocationId);
TRAP(error,commsView->PutRecordChanges());
if (error!=KErrNone) return -1;
CleanupStack::PopAndDestroy(commsView);
// create cmnet ap
TUint32 wapId;
commsView = commsDb->OpenTableLC(TPtrC(WAP_ACCESS_POINT));
TRAP(error,commsView->InsertRecord(wapId));
if (error!=KErrNone) return 2048;
commsView->WriteTextL(TPtrC(COMMDB_NAME), KCmnetIapName);
commsView->WriteTextL(TPtrC(WAP_CURRENT_BEARER), TPtrC(WAP_IP_BEARER));
TRAP(error,commsView->PutRecordChanges());
if (error!=KErrNone) return -1;
CleanupStack::PopAndDestroy(commsView);
// create WAP_IP_BEARER
TUint32 wapIPId;
commsView = commsDb->OpenTableLC(TPtrC(WAP_IP_BEARER));
TRAP(error,commsView->InsertRecord(wapIPId));
if (error!=KErrNone) return -1;
commsView->WriteUintL(TPtrC(WAP_ACCESS_POINT_ID), wapId);
commsView->WriteTextL(TPtrC(WAP_GATEWAY_ADDRESS), _L("0.0.0.0"));
commsView->WriteUintL(TPtrC(WAP_WSP_OPTION),EWapWspOptionConnectionOriented);
commsView->WriteBoolL(TPtrC(WAP_SECURITY), EFalse);
commsView->WriteUintL(TPtrC(WAP_IAP),iapId);
commsView->WriteUintL(TPtrC(WAP_PROXY_PORT), 0);
commsView->WriteTextL(TPtrC(WAP_PROXY_LOGIN_NAME), _L(""));
commsView->WriteTextL(TPtrC(WAP_PROXY_LOGIN_PASS), _L(""));
TRAP(error,commsView->PutRecordChanges(EFalse, EFalse));
if (error!=KErrNone) return -1;
CleanupStack::PopAndDestroy(commsView);
CStoreableOverrideSettings* overrides = CStoreableOverrideSettings::NewL(CStoreableOverrideSettings::EParamListFull);
CleanupStack::PushL(overrides);
CCommsDbConnectionPrefTableView::TCommDbIapConnectionPref pref;
pref.iRanking = 1;
pref.iDirection = ECommDbConnectionDirectionOutgoing;
pref.iDialogPref = ECommDbDialogPrefDoNotPrompt;
pref.iBearer.iBearerSet = ECommDbBearerGPRS;
pref.iBearer.iIapId=iapId;
TRAP(error,overrides->SetConnectionPreferenceOverride(pref));
if (error!=KErrNone) return -1;
CleanupStack::PopAndDestroy(overrides);
CleanupStack::PopAndDestroy(commsDb);
return iapId ;
#endif
}
void CSocketsEngine::ConnectSmoothCMNET()
{
#ifdef __WINSCW__
User::LeaveIfError(iSocketServ.Connect()) ;
#else
TBuf<20> iap_name_buf ;
TBuf<20> cmnet_str ;
TUint32 iap_id = -1 ; // set invalid iap first
cmnet_str.Append(KCmnetIapName) ;
// prepare conn
User::LeaveIfError(iSocketServ.Connect());
User::LeaveIfError(iConn.Open(iSocketServ)) ;
// visit commdb for iap
CCommsDatabase* const comm_db = CCommsDatabase::NewL(EDatabaseTypeIAP);
CleanupStack::PushL(comm_db);
// visit the table of comm_db
CCommsDbTableView *iap_table_view = comm_db->OpenTableLC(TPtrC(IAP));
// switch pointer to the first iap record
if(iap_table_view->GotoFirstRecord() == KErrNone) {
// search comm_db for cmnet iap
do {
// get the iap name from comm_db
iap_name_buf.SetLength(0) ;
iap_table_view->ReadTextL(TPtrC(COMMDB_NAME), iap_name_buf);
// test the cmnet iap name
if((iap_name_buf.Find(cmnet_str) != KErrNotFound)) {
iap_table_view->ReadUintL(TPtrC(COMMDB_ID), iap_id) ;
break ;
}
} while (KErrNone == iap_table_view->GotoNextRecord()) ;
}
CleanupStack::PopAndDestroy(2, comm_db); // iap_table_view, comm_db
// make sure the iap is valid
if(iap_id == -1) iap_id = CreateCmnetIap() ;
// use the iap, construct preference
TCommDbConnPref conn_pref;
conn_pref.SetDialogPreference(ECommDbDialogPrefDoNotPrompt) ;
conn_pref.SetDirection(ECommDbConnectionDirectionOutgoing) ;
conn_pref.SetBearerSet(ECommDbBearerGPRS) ;
conn_pref.SetIapId(iap_id) ;
// perform the smooth connection
iConn.Start(conn_pref);
#endif
}
void CSocketsEngine::ConnectL()
{
// Initiate connection process
if (iEngineStatus == ENotConnected)
{
TInetAddr addr;
if (addr.Input(iServerName) == KErrNone)
{
// server name is already a valid ip address
ConnectL(addr.Address());
}
else // need to look up name using dns
{
// Initiate DNS
User::LeaveIfError(iResolver.Open(iSocketServ, KAfInet, KProtocolInetUdp));
// DNS request for name resolution
iResolver.GetByName(iServerName, iNameEntry, iStatus);
ChangeStatus(ELookingUp);
// Request time out
iTimer->After(KTimeOut);
SetActive();
}
}
}
void CSocketsEngine::ConnectL(TUint32 aAddr) // <a name="ConnectL32">
{
// Initiate attempt to connect to a socket by IP address
if (iEngineStatus == ENotConnected)
{
// Open a TCP socket
User::LeaveIfError(iSocket.Open(iSocketServ, KAfInet, KSockStream, KProtocolInetTcp));
// Set up address information
iAddress.SetPort(iPort);
iAddress.SetAddress(aAddr);
// Initiate socket connection
iSocket.Connect(iAddress, iStatus);
ChangeStatus(EConnecting);
// Start a timeout
iTimer->After(KTimeOut);
SetActive();
}
}
void CSocketsEngine::Disconnect()
{
__ASSERT_ALWAYS(iEngineStatus == EConnected, User::Panic(KPanicSocketsEngine, ESocketsBadState));
// cancel all outstanding operations
// since we are connected, the only possibilities are read and write
iSocketsReader->Cancel();
iSocketsWriter->Cancel();
iSocket.Close();
ChangeStatus(ENotConnected);
}
// from CActive
void CSocketsEngine::DoCancel()
{
iTimer->Cancel();
// Cancel appropriate request to socket
switch (iEngineStatus)
{
case EConnecting:
iSocket.CancelConnect();
iSocket.Close();
break;
case ELookingUp:
// Cancel look up attempt
iResolver.Cancel();
iResolver.Close();
break;
default:
User::Panic(KPanicSocketsEngine, ESocketsBadStatus);
break;
}
ChangeStatus(ENotConnected);
}
void CSocketsEngine::WriteL(const TDesC8& aData)
{
// Write data to socket
if (iEngineStatus == EConnected)
{
iSocketsWriter->IssueWriteL(aData);
}
}
void CSocketsEngine::Read()
{
// Initiate read of data from socket
if ((iEngineStatus == EConnected) && (!iSocketsReader->IsActive()))
{
iSocketsReader->Start();
}
}
// from CActive
void CSocketsEngine::RunL()
{
// Active object request complete handler.
// iEngineStatus flags what request was made, so its
// completion can be handled appropriately
iTimer->Cancel(); // Cancel TimeOut timer before completion
switch(iEngineStatus)
{
case EConnecting:
// IP connection request
if (iStatus == KErrNone)
// Connection completed successfully
{
ChangeStatus(EConnected);
Read(); //Start CSocketsReader Active object
}
else
{
iSocket.Close();
iConsole.ErrorNotify(KErrConnectionFailed, iStatus.Int());
ChangeStatus(ENotConnected);
}
break;
case ELookingUp:
iResolver.Close();
if (iStatus == KErrNone)
{
// DNS look up successful
iNameRecord = iNameEntry();
// Extract domain name and IP address from name record
Print(KStrDomainName);
Print(iNameRecord.iName);
TBuf<15> ipAddr;
TInetAddr::Cast(iNameRecord.iAddr).Output(ipAddr);
Print(KStrIPAddr);
Print(ipAddr);
Print(KStrNewLine);
// And connect to the IP address
ChangeStatus(ENotConnected);
ConnectL(TInetAddr::Cast(iNameRecord.iAddr).Address());
}
else
{
// DNS lookup failed
iConsole.ErrorNotify(KErrDNSFailed, iStatus.Int());
ChangeStatus(ENotConnected);
}
break;
default:
User::Panic(KPanicSocketsEngine, ESocketsBadStatus);
break;
};
}
void CSocketsEngine::TimerExpired()
{
Cancel();
iConsole.ErrorNotify(KErrSocketTimeout, KErrTimedOut);
}
void CSocketsEngine::ReportError(MEngineNotifier::TErrorType aErrorType, TInt aErrorCode)
{
// No recovery or retries are attempted in this example so we just
// disconnect and inform the user
Disconnect();
switch (aErrorType)
{
case MEngineNotifier::EDisconnected:
iConsole.ErrorNotify(KErrSocketDisconnected, aErrorCode);
break;
case MEngineNotifier::EGeneralReadError:
iConsole.ErrorNotify(KErrSocketRead, aErrorCode);
break;
case MEngineNotifier::ETimeOutOnWrite:
iConsole.ErrorNotify(KErrSocketWriteTO, aErrorCode);
break;
case MEngineNotifier::EGeneralWriteError:
iConsole.ErrorNotify(KErrSocketWriteFailed, aErrorCode);
break;
default:
User::Panic(KPanicSocketsEngine, ESocketsBadStatus);
break;
}
}
void CSocketsEngine::ResponseReceived(const TDesC8& aBuffer)
{
iConsole.RecvNotify(aBuffer);
}
void CSocketsEngine::ChangeStatus(TSocketsEngineState aNewStatus)
{
// Update the status (and the status display)
switch (aNewStatus)
{
case ENotConnected:
iConsole.SetStatus(KStrNotConnected);
break;
case EConnecting:
iConsole.SetStatus(KStrConnecting);
break;
case EConnected:
iConsole.SetStatus(KStrConnected);
break;
case ELookingUp:
iConsole.SetStatus(KStrLookingUp);
break;
default:
User::Panic(KPanicSocketsEngine, ESocketsBadStatus);
break;
}
iEngineStatus = aNewStatus;
}
void CSocketsEngine::Print(const TDesC& aDes)
{
// Print some text on the console
iConsole.PrintNotify(aDes, CEikGlobalTextEditor::EItalic);
}
void CSocketsEngine::SetServerName(const TDesC& aName)
{
iServerName.Copy(aName);
}
const TDesC& CSocketsEngine::ServerName() const
{
return iServerName;
}
void CSocketsEngine::SetPort(TInt aPort)
{
iPort = aPort;
}
TInt CSocketsEngine::Port() const
{
return iPort;
}
TBool CSocketsEngine::Connected() const
{
return (iEngineStatus == EConnected);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -