email/imap4mtm/imaptransporthandler/src/csocket.cpp
changeset 31 ebfee66fde93
parent 0 72b543305e3a
equal deleted inserted replaced
30:6a20128ce557 31:ebfee66fde93
       
     1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "csocket.h"
       
    17 #include "csecuresocketcontroller.h"
       
    18 #include "mcommsinfoprovider.h"
       
    19 #include "imappaniccodes.h"
       
    20 
       
    21 // Flags to pass to protocol in RecvOneOrMore call for socket
       
    22 const TInt KDefaultFlags = 0;
       
    23 
       
    24 /**
       
    25 The factory constructor.
       
    26 
       
    27 @param		aCommsInfoProvider	The comms info provider object.
       
    28 @param		aSocketType			The type of the socket.
       
    29 @return		A pointer to a fully constructed object.
       
    30 */
       
    31 CSocket* CSocket::NewL(MCommsInfoProvider& aCommsInfoProvider, TSocketType aSocketType)
       
    32 	{
       
    33 	CSocket* self = new (ELeave) CSocket(aCommsInfoProvider);
       
    34 	CleanupStack::PushL(self);
       
    35 	self->ConstructL(aSocketType);
       
    36 	CleanupStack::Pop(self);
       
    37 	return self;
       
    38 	}
       
    39 
       
    40 /**
       
    41 Destructor.
       
    42 */
       
    43 CSocket::~CSocket()
       
    44 	{
       
    45 	// Close socket and notify the socket status observer
       
    46 	if( iSecureSocketController )
       
    47 		{
       
    48 		delete iSecureSocketController;
       
    49 		}
       
    50 	else
       
    51 		{
       
    52 		iSocket.Close();
       
    53 		}
       
    54 	}
       
    55 
       
    56 /**
       
    57 Constructor.
       
    58 
       
    59 @param	aCommsInfoProvider	The comms info provider object.
       
    60 */
       
    61 CSocket::CSocket(MCommsInfoProvider& aCommsInfoProvider)
       
    62 	: iCommsInfoProvider(aCommsInfoProvider)
       
    63 	{
       
    64 	}
       
    65 
       
    66 /**
       
    67 Second phase constructor.
       
    68 
       
    69 @param		aSocketType	The type of the socket.
       
    70 */
       
    71 void CSocket::ConstructL(TSocketType aSocketType)
       
    72 	{
       
    73 	switch( aSocketType )
       
    74 		{
       
    75 	case EProtocolSocket:
       
    76 		{
       
    77 		// Open a protocol socket
       
    78 		User::LeaveIfError(iSocket.Open(
       
    79 									iCommsInfoProvider.SocketServer(),
       
    80 									iCommsInfoProvider.ProtocolDescription().iAddrFamily, 
       
    81 									iCommsInfoProvider.ProtocolDescription().iSockType, 
       
    82 									iCommsInfoProvider.ProtocolDescription().iProtocol,
       
    83 									iCommsInfoProvider.Connection()
       
    84 									));
       
    85 		} break;
       
    86 	case EBlankSocket:
       
    87 		{
       
    88 		// Open a blank socket
       
    89 		User::LeaveIfError(iSocket.Open(iCommsInfoProvider.SocketServer()));
       
    90 		} break;
       
    91 	default:
       
    92 		{
       
    93 		User::Invariant();
       
    94 		} break;
       
    95 		}
       
    96 	}
       
    97 
       
    98 /**
       
    99 Start asynchronous connect service. The address contains the IP address and
       
   100 port with which a tcp connection should be established with. The request 
       
   101 status is completed either when a connection has been established or an error
       
   102 has occurred - this is reflected in the value of the request status.
       
   103 
       
   104 @param	aAddr	The IP address and port of the remote host.
       
   105 @param	aStatus	The request status that is completed when the connect 
       
   106 		service completes.
       
   107 */
       
   108 void CSocket::Connect(TInetAddr& aAddr, TRequestStatus& aStatus)
       
   109 	{
       
   110 	iSocket.Connect(aAddr, aStatus);
       
   111 	}
       
   112 
       
   113 /**
       
   114 Cancel the connect service.
       
   115 */
       
   116 void CSocket::CancelConnect()
       
   117 	{
       
   118 	iSocket.CancelConnect();
       
   119 	}
       
   120 
       
   121 /**
       
   122 Receive data from socket asynchronously. Any data received by the socket is 
       
   123 placed in the buffer supplied by aBuffer. The request status is completed 
       
   124 either when data has been received or an error has occurred - this is 
       
   125 reflected in the value of the request status.
       
   126 
       
   127 @param		aBuffer	The buffer where any received data is placed.	
       
   128 @param		aStatus	The request status that is completed when the receive 
       
   129 					service completes.
       
   130 */
       
   131 void CSocket::RecvOneOrMore(TDes8& aBuffer, TRequestStatus& aStatus)
       
   132 	{
       
   133 	if( iSecureSocketController )
       
   134 		{
       
   135 		// aFlags not used for secure sockets
       
   136 		iSecureSocketController->RecvOneOrMore(aBuffer, aStatus, iBytesReceived);
       
   137 		}
       
   138 	else
       
   139 		{
       
   140 		iSocket.RecvOneOrMore(aBuffer, KDefaultFlags, aStatus, iBytesReceived);
       
   141 		}
       
   142 	}
       
   143 
       
   144 /**
       
   145 Cancel the receive service.
       
   146 */
       
   147 void CSocket::CancelRecv()
       
   148 	{
       
   149 	if( iSecureSocketController )
       
   150 		{
       
   151 		iSecureSocketController->CancelRecv();
       
   152 		}
       
   153 	else
       
   154 		{
       
   155 		iSocket.CancelRecv();
       
   156 		}
       
   157 	}
       
   158 
       
   159 /**
       
   160 Send data to the socket asynchronously. The data in the supplied buffer is 
       
   161 sent to the socket. The request status is completed either when data has 
       
   162 been sent or an error has occurred - this is reflected in the value of the
       
   163 request status.
       
   164 
       
   165 @param		aBuffer	The buffer containing the data to be sent.
       
   166 @param		aStatus	The request status that is completed when the send 
       
   167 					service completes.
       
   168 */
       
   169 void CSocket::Send(const TDesC8& aBuffer, TRequestStatus& aStatus)
       
   170 	{
       
   171 	if( iSecureSocketController )
       
   172 		{
       
   173 		iSecureSocketController->Send(aBuffer, aStatus);
       
   174 		}
       
   175 	else
       
   176 		{
       
   177 		iSocket.Write(aBuffer, aStatus);
       
   178 		}
       
   179 	}
       
   180 
       
   181 /**
       
   182 Cancel the send service.
       
   183 */
       
   184 void CSocket::CancelSend()
       
   185 	{
       
   186 	if( iSecureSocketController )
       
   187 		{
       
   188 		iSecureSocketController->CancelSend();
       
   189 		}
       
   190 	else
       
   191 		{
       
   192 		iSocket.CancelWrite();
       
   193 		}
       
   194 	}
       
   195 
       
   196 /**
       
   197 Get the remote host name. The IP address and port of the remote host is set
       
   198 in the output argument.
       
   199 
       
   200 @param	aAddr	The output argument where the IP address and port of the
       
   201 				remote host is placed.
       
   202 */
       
   203 void CSocket::RemoteName(TInetAddr& aAddr)
       
   204 	{
       
   205 	iSocket.RemoteName(aAddr);
       
   206 	}
       
   207 
       
   208 /**
       
   209 Get the local socket name. The IP address and port of the local socket is 
       
   210 set in the output argument.
       
   211 
       
   212 @param	aAddr	The output argument where the IP address and port of the
       
   213 				local socket is placed.
       
   214 */
       
   215 void CSocket::LocalName(TInetAddr& aAddr)
       
   216 	{
       
   217 	iSocket.LocalName(aAddr);
       
   218 	}
       
   219 
       
   220 /**
       
   221 Upgrade the socket connection to a secure connection.
       
   222 
       
   223 @param	aStatus		The request status.
       
   224 @param	aSSLDomainName SSL domain name.
       
   225 */
       
   226 void CSocket::UpgradeToSecureL(TRequestStatus& aStatus, const TDesC8& aSSLDomainName)
       
   227 	{
       
   228 	// Check that the socket is already a secure one.
       
   229 	if( iSecureSocketController )
       
   230 		{
       
   231 		// The socket connection is already secure, simply complete the request
       
   232 		aStatus=KRequestPending;
       
   233 		TRequestStatus* pStat = &aStatus;
       
   234 		User::RequestComplete(pStat, KErrNone);
       
   235 		}
       
   236 	else
       
   237 		{
       
   238 		// Create the secure socket controller and start the secure handshake
       
   239 		iSecureSocketController = CSecureSocketController::NewL(iSocket, iCommsInfoProvider);
       
   240 		iSecureSocketController->StartSecureHandshakeL(aStatus, aSSLDomainName);
       
   241 		}
       
   242 	}
       
   243 
       
   244 /**
       
   245 Cancel the upgrade to a secure socket.
       
   246 */
       
   247 void CSocket::CancelUpgradeToSecure()
       
   248 	{
       
   249 	if( iSecureSocketController )
       
   250 		{
       
   251 		iSecureSocketController->CancelHandshake();
       
   252 		}
       
   253 	}