www.pudn.com > DTDaemon.rar > DTSocket.cpp


/* 
============================================================================ 
 Name        : CDTSocket.cpp 
 Author      :  
 Version     : 
 Copyright   : Your copyright notice 
 Description : CDTSocket.cpp - source file 
============================================================================ 
*/ 
 
#include "DTSocket.h" 
 
CDTSocket::CDTSocket(TInt aPort) 
{ 
	iPort = aPort; 
} 
 
CDTSocket::~CDTSocket() 
{ 
	CleanupStack::PopAndDestroy(); 
} 
 
CDTSocket* CDTSocket::NewLC(TInt aPort) 
{ 
	CDTSocket* self = new(ELeave) CDTSocket(aPort); 
	CleanupStack::PushL(self); 
	self->ConstructL(); 
	return self; 
} 
 
void CDTSocket::ConstructL() 
{ 
	TInetAddr addr(KInetAddrLoop, iPort); 
	User::LeaveIfError(iSocketServ.Connect()); 
	CleanupClosePushL(iSocketServ);//if function leaves ensure socket serv session closes 
	User::LeaveIfError(iListener.Open(iSocketServ, KAfInet, KSockStream, KProtocolInetTcp)); 
	User::LeaveIfError(iListener.Bind(addr));   
	User::LeaveIfError(iListener.Listen(1)); 
} 
 
void CDTSocket::Accept() 
{ 
	TRequestStatus status; 
	TSockXfrLength dummyLength; 
	iBlank.Open(iSocketServ); 
	iListener.Accept(iBlank, status); 
	User::WaitForRequest(status); 
	if(status != KErrNone) User::Leave(KErrGeneral); 
} 
 
TInt CDTSocket::Read(TDes8& aBuffer, TInt aLength) 
{ 
	TRequestStatus status; 
	TSockXfrLength dummyLength; 
	iBlank.RecvOneOrMore(aBuffer, 0, status, dummyLength); 
	aBuffer.SetLength( dummyLength.Length() ); 
	User::WaitForRequest(status);	 
	if(status != KErrNone) User::Leave(KErrGeneral); 
	return dummyLength.Length(); 
}