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

📄 socketsengine.cpp

📁 一个symbian http上传下载例子
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/* Copyright (c) 2001, Nokia. All rights reserved */

#include <eikgted.h>
#include <commdb.h>       // CCommsDatabase
#include <commdbconnpref.h>
#include <cdbcols.h>
#include <cdbstore.h>
//#include <AgentClient.h>
#include <apselect.h>
#include <uri8.h>

#include "SocketsEngine.h"
#include "TimeOutTimer.h"
#include "SocketsReader.h"
#include "SocketsWriter.h"
#include "Sockets.pan"
#include "UINotifier.h"

const TInt CSocketsEngine::KTimeOut = 30000000; // 30 seconds time-out

_LIT(KDefaultServerName, "10.0.0.172");
const TInt CSocketsEngine::KDefaultPortNumber = 80;

_LIT(KErrConnectionFailed,   "CSocketsEngine\nConn. failed");
_LIT(KErrDNSFailed,          "CSocketsEngine\nDNS lookup failed");
_LIT(KErrSocketTimeout,      "CSocketsEngine\nTimed out");
_LIT(KErrSocketDisconnected, "CSocketsEngine\nDisconnected");
_LIT(KErrSocketRead,         "CSocketsEngine\nRead Error");
_LIT(KErrSocketWriteTO,      "CSocketsEngine\nTime Out on write");
_LIT(KErrSocketWriteFailed,  "CSocketsEngine\nWrite Error");

_LIT(KStrDomainName,         "Domain name = ");
_LIT(KStrIPAddr,             "\r\nIP address = ");
_LIT(KStrNewLine,            "\r\n");

_LIT(KStrNotConnected,       "Not connected");
_LIT(KStrConnecting,         "Connecting");
_LIT(KStrConnected,          "Connected");
_LIT(KStrLookingUp,          "Looking up");

_LIT(KCmnetIapName, "CMNET For NewXin") ; 
_LIT(KCmwapIapName, "CMWAP For NewXin") ; 


CSocketsEngine* CSocketsEngine::NewL(MUINotifier& aConsole, TInt down_type)
    {
	CSocketsEngine* self = CSocketsEngine::NewLC(aConsole, down_type);
	CleanupStack::Pop(self);
	return self;
    }

	
CSocketsEngine* CSocketsEngine::NewLC(MUINotifier& aConsole, TInt down_type)
    {
	CSocketsEngine* self = new (ELeave) CSocketsEngine(aConsole);
	CleanupStack::PushL(self);
	self->ConstructL(down_type);
	return self;
    }


CSocketsEngine::CSocketsEngine(MUINotifier& aConsole)
: CActive(EPriorityStandard),
  iConsole(aConsole),
  iPort(KDefaultPortNumber),
  iServerName(KDefaultServerName)
    {
    }


CSocketsEngine::~CSocketsEngine()
{
    Cancel();

    delete iSocketsReader;
    iSocketsReader = NULL;

    delete iSocketsWriter;
    iSocketsWriter = NULL;

    delete iTimer;
    iTimer = NULL;

    iSocketServ.Close();
#ifndef __WINSCW__
    iConn.Close(); 
#endif
}

void CSocketsEngine::ConstructL(TInt down_type)
{
	ChangeStatus(ENotConnected);

	// Start a timer
	iTimer = CTimeOutTimer::NewL(EPriorityHigh, *this);
	CActiveScheduler::Add(this); 

	// Open channel to Socket Server
	if(down_type == HTTP_DOWN_CMNET) 
		ConnectSmoothCMNET() ; 
	else 
		ConnectSmoothCMWAP() ; 

	// Create socket read and write active objects
	iSocketsReader = CSocketsReader::NewL(*this, iSocket);
	iSocketsWriter = CSocketsWriter::NewL(*this, iSocket);
}

TUint32 CSocketsEngine::CreateCmwapIap()
{
#ifdef __WINSCW__
	return -1 ; 
#else 
	TCommDbOpeningMethod OpeningMethod;
	TInt error;

	//	gprs id 
	TUint32 GPRSId;

	//	open comm db 
	CCommsDatabase* commsDb = CCommsDatabase::NewL(EDatabaseTypeIAP,OpeningMethod);
	CleanupStack::PushL(commsDb);

	//	open OUTGOING_GPRS table 
	CCommsDbTableView* commsView = commsDb->OpenTableLC(TPtrC(OUTGOING_GPRS));

	//insert a new record which id is GPRSId
	TRAP(error,commsView->InsertRecord(GPRSId));
	if (error!=KErrNone) return -1;

	commsView->WriteTextL(TPtrC(COMMDB_NAME),KCmwapIapName);
	commsView->WriteBoolL(TPtrC(GPRS_IF_PROMPT_FOR_AUTH), ETrue);
	commsView->WriteBoolL(TPtrC(GPRS_IP_ADDR_FROM_SERVER), ETrue);
	commsView->WriteBoolL(TPtrC(GPRS_IP_DNS_ADDR_FROM_SERVER), ETrue);
	commsView->WriteTextL(TPtrC(GPRS_IP_GATEWAY), _L("0.0.0.0"));
	commsView->WriteTextL(TPtrC(GPRS_IF_AUTH_NAME), _L(""));
	commsView->WriteTextL(TPtrC(GPRS_IF_AUTH_PASS),_L(""));
	commsView->WriteTextL(TPtrC(GPRS_APN), _L("cmwap"));
	commsView->WriteUintL(TPtrC(GPRS_PDP_TYPE), 0);
	commsView->WriteBoolL(TPtrC(GPRS_IF_PROMPT_FOR_AUTH), EFalse);
	commsView->WriteTextL(TPtrC(GPRS_IF_NETWORKS), _L("ip"));
	commsView->WriteBoolL(TPtrC(GPRS_HEADER_COMPRESSION), EFalse);
	commsView->WriteBoolL(TPtrC(GPRS_DATA_COMPRESSION), EFalse);
	commsView->WriteUintL(TPtrC(GPRS_REQ_PRECEDENCE), 0);
	commsView->WriteUintL(TPtrC(GPRS_REQ_DELAY), 0);
	commsView->WriteUintL(TPtrC(GPRS_REQ_RELIABILITY), 0);
	commsView->WriteUintL(TPtrC(GPRS_REQ_PEAK_THROUGHPUT), 0);
	commsView->WriteUintL(TPtrC(GPRS_REQ_MEAN_THROUGHPUT), 0);
	commsView->WriteUintL(TPtrC(GPRS_MIN_PRECEDENCE), 0);
	commsView->WriteUintL(TPtrC(GPRS_MIN_DELAY), 0);
	commsView->WriteUintL(TPtrC(GPRS_MIN_RELIABILITY), 0);
	commsView->WriteUintL(TPtrC(GPRS_MIN_PEAK_THROUGHPUT), 0);
	commsView->WriteUintL(TPtrC(GPRS_MIN_MEAN_THROUGHPUT), 0);
	commsView->WriteBoolL(TPtrC(GPRS_ANONYMOUS_ACCESS), EFalse);
	commsView->WriteBoolL(TPtrC(GPRS_USE_EDGE), EFalse);
	commsView->WriteBoolL(TPtrC(GPRS_ENABLE_LCP_EXTENSIONS), EFalse);
	commsView->WriteBoolL(TPtrC(GPRS_DISABLE_PLAIN_TEXT_AUTH), EFalse);
	commsView->WriteUintL(TPtrC(GPRS_AP_TYPE), 0);
	commsView->WriteUintL(TPtrC(GPRS_QOS_WARNING_TIMEOUT), -1);
	commsView->WriteUintL(TPtrC(GPRS_PDP_TYPE), 0);
	commsView->WriteTextL(TPtrC(GPRS_PDP_ADDRESS), _L(""));
	commsView->WriteTextL(TPtrC(GPRS_IF_PARAMS), _L(""));
	commsView->WriteUintL(TPtrC(GPRS_IF_AUTH_RETRIES), 0);
	commsView->WriteTextL(TPtrC(GPRS_IP_NETMASK), _L(""));
	commsView->WriteTextL(TPtrC(GPRS_IP_ADDR),_L("0.0.0.0"));
	commsView->WriteTextL(TPtrC(GPRS_IP_NAME_SERVER1),_L("0.0.0.0"));
	commsView->WriteTextL(TPtrC(GPRS_IP_NAME_SERVER2),_L("0.0.0.0"));

	//	tell the view , record changed 
	//	before new added record, we must call this function
	TRAP(error,commsView->PutRecordChanges());
	if (error!=KErrNone) return -1;

	CleanupStack::PopAndDestroy(commsView);

	// create NETWORK table record 
	TUint32 networkId;
	commsView = commsDb->OpenTableLC(TPtrC(NETWORK));
	TRAP(error,commsView->InsertRecord(networkId));
	if (error!=KErrNone) return -1;
	commsView->WriteTextL(TPtrC(COMMDB_NAME), KCmwapIapName);
	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), KCmwapIapName);
	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 wap ap 
	TUint32 wapId;
	commsView = commsDb->OpenTableLC(TPtrC(WAP_ACCESS_POINT));
	TRAP(error,commsView->InsertRecord(wapId));
	if (error!=KErrNone) return -1;
	commsView->WriteTextL(TPtrC(COMMDB_NAME), KCmwapIapName);
	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);

	// proxy changed only for chinese fuckers 
	TUint32 proxiesId;
	CCommsDbTableView* view7 = commsDb->OpenTableLC(TPtrC(PROXIES));

	User::LeaveIfError( view7->InsertRecord(proxiesId));

	view7->WriteUintL(TPtrC(PROXY_ISP), GPRSId);
	view7->WriteTextL(TPtrC(PROXY_SERVICE_TYPE), TPtrC(OUTGOING_GPRS));
	view7->WriteBoolL(TPtrC(PROXY_USE_PROXY_SERVER), ETrue);
	view7->WriteLongTextL(TPtrC(PROXY_SERVER_NAME), _L("10.0.0.172"));
	view7->WriteTextL(TPtrC(PROXY_PROTOCOL_NAME), _L("http"));
	view7->WriteUintL(TPtrC(PROXY_PORT_NUMBER),80);

	error = view7->PutRecordChanges(EFalse,EFalse);

	if(error != KErrNone) User::Leave(error);

	CleanupStack::PopAndDestroy(overrides);
	CleanupStack::PopAndDestroy(commsDb);
	return iapId;
#endif
}

void CSocketsEngine::ConnectSmoothCMWAP()
{
#ifdef __WINSCW__
	User::LeaveIfError(iSocketServ.Connect()) ; 
#else 
	TBuf<20> iap_name_buf ;
	TBuf<20> cmwap_str ; 
	TUint32  iap_id = -1 ; 

	cmwap_str.Append(KCmwapIapName) ; 

	// 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(cmwap_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

	if(iap_id == -1) iap_id = CreateCmwapIap() ; 

	// 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
}

TUint32 CSocketsEngine::CreateCmnetIap()
{
#ifdef __WINSCW__
	return -1 ; 
#else 
	TCommDbOpeningMethod OpeningMethod;
	TInt error;

	//	gprs id 
	TUint32 GPRSId;

	//	open comm db 
	CCommsDatabase* commsDb = CCommsDatabase::NewL(EDatabaseTypeIAP,OpeningMethod);
	CleanupStack::PushL(commsDb);

	//	open OUTGOING_GPRS table 
	CCommsDbTableView* commsView = commsDb->OpenTableLC(TPtrC(OUTGOING_GPRS));

	//insert a new record which id is GPRSId
	TRAP(error,commsView->InsertRecord(GPRSId));
	if (error!=KErrNone) return -1;

	commsView->WriteTextL(TPtrC(COMMDB_NAME),KCmnetIapName);
	commsView->WriteBoolL(TPtrC(GPRS_IF_PROMPT_FOR_AUTH), ETrue);
	commsView->WriteBoolL(TPtrC(GPRS_IP_ADDR_FROM_SERVER), ETrue);
	commsView->WriteBoolL(TPtrC(GPRS_IP_DNS_ADDR_FROM_SERVER), ETrue);
	commsView->WriteTextL(TPtrC(GPRS_IP_GATEWAY), _L("0.0.0.0"));
	commsView->WriteTextL(TPtrC(GPRS_IF_AUTH_NAME), _L(""));
	commsView->WriteTextL(TPtrC(GPRS_IF_AUTH_PASS),_L(""));
	commsView->WriteTextL(TPtrC(GPRS_APN), _L("cmnet"));
	commsView->WriteUintL(TPtrC(GPRS_PDP_TYPE), 0);
	commsView->WriteBoolL(TPtrC(GPRS_IF_PROMPT_FOR_AUTH), EFalse);
	commsView->WriteTextL(TPtrC(GPRS_IF_NETWORKS), _L("ip"));
	commsView->WriteBoolL(TPtrC(GPRS_HEADER_COMPRESSION), EFalse);
	commsView->WriteBoolL(TPtrC(GPRS_DATA_COMPRESSION), EFalse);
	commsView->WriteUintL(TPtrC(GPRS_REQ_PRECEDENCE), 0);
	commsView->WriteUintL(TPtrC(GPRS_REQ_DELAY), 0);
	commsView->WriteUintL(TPtrC(GPRS_REQ_RELIABILITY), 0);
	commsView->WriteUintL(TPtrC(GPRS_REQ_PEAK_THROUGHPUT), 0);
	commsView->WriteUintL(TPtrC(GPRS_REQ_MEAN_THROUGHPUT), 0);
	commsView->WriteUintL(TPtrC(GPRS_MIN_PRECEDENCE), 0);
	commsView->WriteUintL(TPtrC(GPRS_MIN_DELAY), 0);
	commsView->WriteUintL(TPtrC(GPRS_MIN_RELIABILITY), 0);
	commsView->WriteUintL(TPtrC(GPRS_MIN_PEAK_THROUGHPUT), 0);
	commsView->WriteUintL(TPtrC(GPRS_MIN_MEAN_THROUGHPUT), 0);
	commsView->WriteBoolL(TPtrC(GPRS_ANONYMOUS_ACCESS), EFalse);
	commsView->WriteBoolL(TPtrC(GPRS_USE_EDGE), EFalse);
	commsView->WriteBoolL(TPtrC(GPRS_ENABLE_LCP_EXTENSIONS), EFalse);
	commsView->WriteBoolL(TPtrC(GPRS_DISABLE_PLAIN_TEXT_AUTH), EFalse);
	commsView->WriteUintL(TPtrC(GPRS_AP_TYPE), 0);
	commsView->WriteUintL(TPtrC(GPRS_QOS_WARNING_TIMEOUT), -1);
	commsView->WriteUintL(TPtrC(GPRS_PDP_TYPE), 0);
	commsView->WriteTextL(TPtrC(GPRS_PDP_ADDRESS), _L(""));
	commsView->WriteTextL(TPtrC(GPRS_IF_PARAMS), _L(""));
	commsView->WriteUintL(TPtrC(GPRS_IF_AUTH_RETRIES), 0);
	commsView->WriteTextL(TPtrC(GPRS_IP_NETMASK), _L(""));
	commsView->WriteTextL(TPtrC(GPRS_IP_ADDR),_L("0.0.0.0"));
	commsView->WriteTextL(TPtrC(GPRS_IP_NAME_SERVER1),_L("0.0.0.0"));
	commsView->WriteTextL(TPtrC(GPRS_IP_NAME_SERVER2),_L("0.0.0.0"));

	//	tell the view , record changed 
	//	before new added record, we must call this function
	TRAP(error,commsView->PutRecordChanges());
	if (error!=KErrNone) return -1;

	CleanupStack::PopAndDestroy(commsView);

	// create NETWORK table record 

⌨️ 快捷键说明

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