applayerprotocols/telnetengine/SRC/ACTIVEIO.CPP
changeset 0 b16258d2340f
equal deleted inserted replaced
-1:000000000000 0:b16258d2340f
       
     1 // Copyright (c) 2003-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 // CActiveReader & CActiveWriter
       
    15 // Contains basic Read and Write Active object definitions for Socket I/O
       
    16 // 
       
    17 //
       
    18 
       
    19 /**
       
    20  @file
       
    21 */
       
    22 
       
    23 #include "ACTIVEIO.H"
       
    24 #include "IOBUFFER.H"
       
    25 #include "TELFSM.H"
       
    26 #include "TELDEBUG.H"
       
    27 
       
    28 CActiveWriter::CActiveWriter() : CActive(EPriorityStandard)
       
    29 /**
       
    30 Constructor
       
    31 */
       
    32 	{
       
    33 	CActiveScheduler::Add(this);
       
    34 	}
       
    35 
       
    36 CActiveWriter::~CActiveWriter()
       
    37 /**
       
    38 Destructor
       
    39 */
       
    40 	{
       
    41 	__FLOG_STATIC(KTelnetLoggingCompnt(),KTelnetLoggingTag(),_L("CActiveWriter::D'Tor"));
       
    42 	Cancel();
       
    43 	}
       
    44 
       
    45 CActiveWriter* CActiveWriter::NewL(MIONotifier* aNotifier)
       
    46 	{
       
    47 	CActiveWriter* self = new(ELeave) CActiveWriter;
       
    48 	CleanupStack::PushL(self);
       
    49 	self->ConstructL(aNotifier);
       
    50 	CleanupStack::Pop();
       
    51 	return self;
       
    52 	}
       
    53 
       
    54 void CActiveWriter::ConstructL(MIONotifier* aNotifier)
       
    55 	{	
       
    56 	iNotifier = aNotifier;
       
    57 	iSocket = NULL;
       
    58 	}
       
    59 
       
    60 void CActiveWriter::SetSocket(RSocket* aSocket)
       
    61 	{
       
    62 	iSocket = aSocket;
       
    63 	}
       
    64 
       
    65 TInt CActiveWriter::IssueWrite(const TDesC8& aBuffer)
       
    66 /**
       
    67 Standard write
       
    68 */
       
    69 	{
       
    70 	if (IsActive())
       
    71 		return KRequestPending;
       
    72 
       
    73 	iSocket->Send(aBuffer,NULL,iStatus,iXfrLength);
       
    74 	SetActive();
       
    75 	return KErrNone;
       
    76 	}
       
    77 
       
    78 TInt CActiveWriter::IssueUrgentWrite(const TDesC8& aBuffer)
       
    79 /**
       
    80 Caller wants to send data as TCP urgent
       
    81 */
       
    82 	{
       
    83 	iSocket->SetOpt(KSoTcpNextSendUrgentData, KSolInetTcp, ETrue);
       
    84 	iSocket->Send(aBuffer,NULL,iStatus,iXfrLength);
       
    85 	SetActive();
       
    86 
       
    87 	return(KErrNone);
       
    88 	}
       
    89 
       
    90 
       
    91 void CActiveWriter::RunL()
       
    92 /**
       
    93 Write completed on the socket
       
    94 */
       
    95 	{
       
    96 	if(iStatus != KErrNone)
       
    97 		iNotifier->WriteError(iStatus.Int());
       
    98 	else
       
    99 		iNotifier->WriteComplete();
       
   100 	}
       
   101 
       
   102 
       
   103 CActiveReader::CActiveReader() : CActive(EPriorityStandard)
       
   104 /**
       
   105 Constructor
       
   106 */
       
   107 	{
       
   108 	CActiveScheduler::Add(this);
       
   109 	}
       
   110 
       
   111 CActiveReader::~CActiveReader()
       
   112 /**
       
   113 Destructor
       
   114 */
       
   115 	{
       
   116 	__FLOG_STATIC(KTelnetLoggingCompnt(),KTelnetLoggingTag(),_L("CActiveReader::D'Tor"));
       
   117 	Cancel();
       
   118 	}
       
   119 
       
   120 
       
   121 CActiveReader* CActiveReader::NewL(MIONotifier* aNotifier)
       
   122 	{
       
   123 	CActiveReader* self = new(ELeave) CActiveReader;
       
   124 	CleanupStack::PushL(self);
       
   125 	self->ConstructL(aNotifier);
       
   126 	CleanupStack::Pop();
       
   127 	return self;
       
   128 	}
       
   129 
       
   130 void CActiveReader::ConstructL(MIONotifier* aNotifier)
       
   131 /**
       
   132 @internalComponent
       
   133 */
       
   134 	{	
       
   135 	iNotifier = aNotifier;
       
   136 	iSocket = NULL;
       
   137 	iClientBuffer = NULL;
       
   138 	iRequest = ERequestNone;
       
   139 	}
       
   140 
       
   141 void CActiveReader::SetSocket(RSocket* aSocket)
       
   142 	{
       
   143 	iSocket = aSocket;
       
   144 	}
       
   145 
       
   146 TInt CActiveReader::IssueRead(TDes8& aBuffer)
       
   147 /**
       
   148 Issues read using Ioctl so urgents can be picked up
       
   149 TCP test code Test_05() demonstrates this
       
   150 */
       
   151 	{
       
   152 	if (IsActive())
       
   153 		return KRequestPending;
       
   154 	
       
   155 	iClientBuffer = &aBuffer;
       
   156 	// Tell the RunL() to expect an Ioctl completion
       
   157 	iRequest = EIOctlRequest;
       
   158 	iflags() = KSockSelectRead | KSockSelectExcept;
       
   159 	iSocket->Ioctl(KIOctlSelect,iStatus, &iflags, KSOLSocket);
       
   160 	SetActive();
       
   161 
       
   162 	return KErrNone;
       
   163 	}
       
   164 
       
   165 TInt CActiveReader::RunError(TInt /*aError*/)
       
   166 	{
       
   167 	return KErrNone;
       
   168 	}
       
   169 
       
   170 void CActiveReader::RunL()
       
   171 /**
       
   172 Read completion
       
   173 Performed in 2 stages
       
   174 We get a completion on the Ioctl first where we test for urgent data, we set active again and
       
   175 Next we get a normal completion for the read following a call to RecvOneOrMore()
       
   176 */
       
   177 	{
       
   178 	TInt bytesPending;
       
   179 	TInt urgentOffset;
       
   180 	TInt ret;
       
   181 	TInt urgentData;
       
   182 
       
   183 	if(iRequest == EIOctlRequest)
       
   184 		// First completion for the Ioctl
       
   185 		{
       
   186 		iSocket->GetOpt(KSoTcpRcvAtMark, KSolInetTcp,ret);
       
   187 		iSocket->GetOpt(KSOUrgentDataOffset, KSOLSocket,urgentOffset);
       
   188 		iSocket->GetOpt(KSOReadBytesPending, KSOLSocket,bytesPending);
       
   189 		urgentData = 0;
       
   190 		// Peek check for urgent data
       
   191 		if(iSocket->GetOpt(KSoTcpPeekUrgentData, KSolInetTcp,urgentData)==KErrNone)
       
   192 			{
       
   193 			// We have urgent data
       
   194 			urgentData = 0;
       
   195 			// Read the urgent data
       
   196 			iSocket->GetOpt(KSoTcpReadUrgentData, KSolInetTcp,urgentData);
       
   197 			__FLOG_STATIC1(KTelnetLoggingCompnt(),KTelnetLoggingTag(),_L("CActiveReader::RunL() Urgent Data = %X"),urgentData);
       
   198 			// Notify that we have received urgent data
       
   199 			iNotifier->Event(CProto::EEventUrgentData,urgentData);
       
   200 			}
       
   201 		// Now set to receive data
       
   202 		iSocket->RecvOneOrMore(*iClientBuffer,NULL,iStatus,iXfrLength);
       
   203 		SetActive();
       
   204 		// Completion will be normal next time
       
   205 		iRequest = EReadRequest;
       
   206 		}
       
   207 	else if(iRequest == EReadRequest)
       
   208 		{
       
   209 		// Normal read complete
       
   210 		// iClientBuffer will contain the data
       
   211 		if(iStatus.Int() == KErrNone)
       
   212 			{
       
   213 			iNotifier->ReadCompleteL();
       
   214 			}
       
   215 		else
       
   216 			{
       
   217 			iNotifier->ReadComplete(iStatus.Int());
       
   218 			}
       
   219 		}
       
   220 	}
       
   221