hi
i tried to use active objects, but I still have some problem...
After connecting to the server with RSocket::Connect, my program crashes....(this happens when I put SetActive() command after RSocket::Connect). The same problem happens when I use CSecureSocket::StartClientHandshake...
I paste my .h and .cpp files....I only try to setup a SSL connection without sending/receiving file...
sslEngine.h:
Code:
//sslEngine.h
#ifndef __SSLENGINE_H__
#define __SSLENGINE_H__
// Include Files
#include <e32base.h>
#include <in_sock.h>
#include <e32def.h>
#include <es_sock.h>
#include <e32std.h>
#include <e32cons.h>
#include <x509cert.h>
#include <securesocket.h>
//#include <>
// Function Prototypes
GLDEF_C TInt E32Main();
class CSslEngine : public CActive {
public:
//Costruttore
static CSslEngine *NewL();
//Distruttore
~CSslEngine();
// ConnectL
void ConnectL(TUint32 anAddress, TInt aPort);
// setConsole
void setConsole(CConsoleBase* aConsole);
private:
//costruttore
CSslEngine();
//ConstructL
void ConstructL();
// RunL
void RunL();
// DoCancel
void DoCancel();
// makeSecureConL
void makeSecureConL();
// CloseCoL()
void CloseConL();
// RunError
TInt RunError(TInt aError);
private:
// oggetti della classe
RSocket sockEng;
RSocketServ sockServEng;
TInetAddr servAddr;
TRequestStatus iStatus;
CConsoleBase* iConsole;
CSecureSocket* iTlsSock;
// engine state
enum eSslState
{
eNotCon, // non connesso
eConnected, // connesso al Rsocket
eSecConnected, // socket protetto con ssl
eDataTransfered, // invio/Ricezione dati terminato
};
// variabile indicante lo stato dell' engine
TInt iSslState;
};
#endif // __SSLENGINE_H__
sslEngine.cpp
Code:
// sslEngine.cpp
// Include Files
#include "sslEngine.h"
// COSTANTI
_LIT(KErrOpen,"Error opening socket...\n");
_LIT(KNerrOpen,"Socket opened...\n");
_LIT(KErrCon,"Error connecting to the server...\n");
_LIT(KNerrCon,"Socket Connected...\n");
_LIT(KErrHand,"Handshake Failed...\n");
_LIT(KNerrHand,"Handshake Done...\n");
_LIT(KErrRecv,"Error receiving data...\n");
_LIT(KNerrRecv,"Data Received");
_LIT(KErrSend,"Error sending data...\n");
_LIT(KNerrSend,"Data Sent...\n");
_LIT(KCloseConn,"Connection Closed...\n");
// CLASS CSSLENGINE
// costruttore pubblico
CSslEngine* CSslEngine::NewL()
{
CSslEngine* me = new(ELeave) CSslEngine;
CleanupStack::PushL(me);
me->ConstructL();
CleanupStack::Pop();
return me;
}
// costruttore privato
CSslEngine::CSslEngine()
: CActive(0)
{
//CActiveScheduler::Add( this );
}
// distruttore
CSslEngine::~CSslEngine()
{
sockServEng.Close();
}
// ConstructL
void CSslEngine::ConstructL()
{
User::LeaveIfError(sockServEng.Connect());
CActiveScheduler::Add( this );
}
// RunL
void CSslEngine::RunL()
{
switch(iSslState)
{
case eConnected:
makeSecureConL();
break;
//case eSecConnected:
// invio-ricezione
//break;
//case eDataTransfered:
//closeConL();
//break;
}
}
//DoCancel
void CSslEngine::DoCancel()
{
}
// ConnectL
void CSslEngine::ConnectL(TUint32 anAddress, TInt aPort)
{
TInt errorCode;
servAddr.SetAddress(anAddress);
servAddr.SetPort(aPort);
errorCode = sockEng.Open(sockServEng, KAfInet, KSockStream,KProtocolInetTcp);
if(errorCode != KErrNone)
{
iConsole->Write(KErrOpen);
}
else
{
iConsole->Write(KNerrOpen);
}
//SetActive();
sockEng.Connect(servAddr, iStatus);
if(iStatus != KErrNone)
{
iConsole->Write(KErrCon);
iSslState = eConnected;
}
else
{
iConsole->Write(KNerrCon);
}
SetActive();
}
void CSslEngine::setConsole(CConsoleBase* aConsole)
{
iConsole = aConsole;
}
void CSslEngine::makeSecureConL()
{
_LIT(KSerTls, "TLS1.0");
iTlsSock = CSecureSocket::NewL(sockEng, KSerTls);
iTlsSock->FlushSessionCache();
//SetActive();
iTlsSock->StartClientHandshake(iStatus);
iSslState = eSecConnected;
SetActive();
}
void CSslEngine::CloseConL()
{
iTlsSock->CancelAll();
iTlsSock->Close();
delete iTlsSock;
sockEng.CancelAll();
sockEng.Close();
}
TInt CSslEngine::RunError(TInt aError)
{
TInt y = aError;
return KErrNone;
}
//==============================================================
//==============================================================
//==============================================================
// Constants
_LIT(KTextConsoleTitle, "Console");
_LIT(KTextFailed, " failed, leave code = %d");
_LIT(KTextPressAnyKey, " [press any key]\n");
// Global Variables
LOCAL_D CConsoleBase* console;
// Local Functions
//LOCAL_C void MainL()
// {
// console->Write(_L("Hello, world!\n"));
// }
LOCAL_C void DoStartL()
{
// Create active scheduler (to run active objects)
CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();
CleanupStack::PushL(scheduler);
CActiveScheduler::Install(scheduler);
//CActiveScheduler::Start();
TInt tipo = 1;
TUint32 serv;
//porta
TInt port = 10000;
//indirizzo ip
if(tipo == 1){
serv = INET_ADDR(10,20,10,22);
}
else{
serv = INET_ADDR(192,168,1,2);
}
CSslEngine* pippo;
pippo = CSslEngine::NewL();
pippo->setConsole(console);
pippo->ConnectL(serv, port);
pippo->~CSslEngine();
//CActiveScheduler::Stop();
// Delete active scheduler
CleanupStack::PopAndDestroy(scheduler);
}
// Global Functions
GLDEF_C TInt E32Main()
{
// Create cleanup stack
__UHEAP_MARK;
CTrapCleanup* cleanup = CTrapCleanup::New();
// Create output console
TRAPD(createError, console = Console::NewL(KTextConsoleTitle, TSize(KConsFullScreen,KConsFullScreen)));
if (createError)
return createError;
// Run application code inside TRAP harness, wait keypress when terminated
TRAPD(mainError, DoStartL());
if (mainError)
console->Printf(KTextFailed, mainError);
console->Printf(KTextPressAnyKey);
console->Getch();
delete console;
delete cleanup;
__UHEAP_MARKEND;
return KErrNone;
}