?? securesocketcore.cpp
字號:
/*
============================================================================
Name : SecureSocketCore.cpp
based on : http://www.symbian.com/developer/techlib/v9.2docs/doc_source/examples/NetworkingEx/SSLExampleCode.guide.html#NetworkingEx%2eSSLExampleCode
Author : Konstantine; Company: Fishnest Ukraine Sebastopol
Version :
Support : bluspan@gmail.com
Description : CSecureSocketCore implementation
============================================================================
*/
#include "SecureSocketCore.h"
// HTTP messages
_LIT8(KSimpleGet, "GET ");
_LIT8(KNewLine, "\n");
CSecureSocketCore::CSecureSocketCore() : CActive( EPriorityStandard ), iSndBuffer(0,0), iRcvBuffer(0,0)
{
}
CSecureSocketCore* CSecureSocketCore::NewLC()
{
CSecureSocketCore* self = new ( ELeave ) CSecureSocketCore();
CleanupStack::PushL( self );
self->ConstructL();
return self;
}
CSecureSocketCore* CSecureSocketCore::NewL()
{
CSecureSocketCore* self = CSecureSocketCore::NewLC();
CleanupStack::Pop(); // self;
return self;
}
void CSecureSocketCore::ConstructL()
{
//== konstantine_entrance : mycode myinitialization
iPort=443;
User::LeaveIfError( iSocketServer.Connect() );
iSndBuffer.Set((TUint8*)User::AllocL(256),0,256);
iRcvBuffer.Set((TUint8*)User::AllocL(256),0,256);
//== <- my
User::LeaveIfError( iTimer.CreateLocal() ); // Initialize timer
CActiveScheduler::Add( this ); // Add to scheduler
}
CSecureSocketCore::~CSecureSocketCore()
{
Cancel(); // Cancel any request, if outstanding
iTimer.Close(); // Destroy the RTimer object
delete (void*)iSndBuffer.Ptr();
delete (void*)iRcvBuffer.Ptr();
//CActiveScheduler::Stop();
// Delete instance variables if any
}
void CSecureSocketCore::DoCancel()
{
iTimer.Cancel();
}
void CSecureSocketCore::StartL( TTimeIntervalMicroSeconds32 aDelay )
{
Cancel(); // Cancel any request, just to be sure
//iState = EUninitialized;
iState = EMakingSecureConnection;
iTimer.After( iStatus, aDelay ); // Set for later
SetActive(); // Tell scheduler a request is active
}
void CSecureSocketCore::RunL()
{
if ( iState == EMakingSecureConnection) {
MakeSecureConnectionL();
iState = EMakingPageRequest;
iRBufConsoleObserver->AppendToConsole(_L("Making Secure Connection\n"));
}
else if ( iState == EMakingPageRequest) {
MakePageRequestL();
iState = EGettingServerResponse;
iRBufConsoleObserver->AppendToConsole(_L("Making Page Request\n"));
}
else if ( iState == EGettingServerResponse) {
GetServerResponseL();
iState = EDataReceived;
iRBufConsoleObserver->AppendToConsole(_L("Get Server Response\n"));
}
else if ( iState == EDataReceived) {
ReadServerResponseL();
}
else if ( iState == EStopping) {
ConnectionStop();
iState = EStopped;
iRBufConsoleObserver->AppendToConsole(_L("Stop Connection\n"));
iRBufConsoleObserver->ShowConsole();
}
else if ( iState != EError )
{
// Do something
}
//iTimer.After( iStatus, 1000000 ); // Set for 1 sec later
if (iState != EStopped)
SetActive(); // Tell scheduler a request is active
}
TInt CSecureSocketCore::RunError( TInt aError )
{
return aError;
}
// ---! Secure connection !---
void CSecureSocketCore::ConnectL()
{
iSndBuffer.SetLength( 0 );
iRcvBuffer.SetLength( 0 );
iTotalBytesRead = 0;
_LIT( KIPAddress, " 63.245.209.11");
//==konstantine_entrance : mycode
// from Chat example
TInetAddr addr;
TUint32 aAddr;
iState = EMakingSecureConnection;
if ( addr.Input( KIPAddress ) == KErrNone )
{
// server name is already a valid ip address
aAddr = addr.Address();
iAddress.SetPort( iPort );
iAddress.SetAddress( aAddr );
iAddress.SetFamily( KAfInet );
// Open a TCP socket
User::LeaveIfError( iSocket.Open( iSocketServer,
KAfInet,
KSockStream,
KProtocolInetTcp ) );
// Initiate socket connection
iSocket.Connect( iAddress, iStatus );
SetActive();
//CActiveScheduler::Start();
//User::LeaveIfError(iStatus.Int()); // errors caught by RunError()
}
}
void CSecureSocketCore::MakeSecureConnectionL()
{
User::LeaveIfError(iStatus.Int()); // errors caught by RunError()
iTlsSocket = CSecureSocket::NewL( iSocket, _L("SSL3.0"));
iTlsSocket->FlushSessionCache();
// start the handshake
iTlsSocket->StartClientHandshake( iStatus );
//SetActive();
}
void CSecureSocketCore::MakePageRequestL()
{
// The secure connection has now been made.
// Send a get request for the page.
User::LeaveIfError(iStatus.Int());
_LIT8( KPage , "https://mozilla.org");
// Create a GET request
iSndBuffer+=KSimpleGet;
iSndBuffer+=KPage();
iSndBuffer+=KNewLine;
// Send the request
iTlsSocket->Send( iSndBuffer, iStatus, iBytesSent );
}
void CSecureSocketCore::GetServerResponseL()
{
// The get request has been sent, can now try and receive the data
User::LeaveIfError(iStatus.Int());
TBuf8<2> buf;
User::LeaveIfError(iTlsSocket->CurrentCipherSuite( buf ));
// Print the protocol version string
TBuf<32> protocol;
User::LeaveIfError(iTlsSocket->Protocol( protocol ));
// Print info about the server's certificate
const CX509Certificate *servCert = iTlsSocket->ServerCert();
// Read asynchonously-returns when buffer full
iTlsSocket->Recv( iRcvBuffer, iStatus );
}
void CSecureSocketCore::ReadServerResponseL()
{
// Any error other than KErrEof means the test is a failure
if (iStatus!=KErrEof) User::LeaveIfError(iStatus.Int());
// Put the received data in the output file & reset the receive buffer
iTotalBytesRead += iRcvBuffer.Length();
// Case 1: error is KErrEof (message complete) or no data received, so stop
if ( ( iStatus==KErrEof ) || ( iRcvBuffer.Length() == 0 ) )
{
// Close the socket neatly
iState = EStopping;
iTimer.After( iStatus, 1000000 );
return;
}
// Case 2: there's more data to get from the server
iRcvBuffer.SetLength( 0 );
iState = EDataReceived;
iTlsSocket->Recv( iRcvBuffer, iStatus );
}
void CSecureSocketCore::ConnectionStop()
{
iSocket.CancelAll();
iTlsSocket->CancelAll();
iTlsSocket->Close();
delete iTlsSocket;
//iTlsSocket =0;
iSocket.Close();
//Cancel();
//iTimer.Cancel();
iSocketServer.Close();
//CActiveScheduler::Stop();
User::After( 1000000 );
}
void CSecureSocketCore::SetRBufConsoleObserver(CRBufConsoleObserver *aObserver)
{
iRBufConsoleObserver = aObserver;
}
//== <- my
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -