datacommsserver/esockserver/test/protocols/ipc/IPC_PRVD.CPP
changeset 0 dfb7c4ff071f
equal deleted inserted replaced
-1:000000000000 0:dfb7c4ff071f
       
     1 // Copyright (c) 1997-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 <e32std.h>
       
    17 #include "IPC_MAIN.H"
       
    18 #include "ES_IPC.H"
       
    19 
       
    20 CIpcProvdBase::CIpcProvdBase(CIpcProtocolHolder* aProtocol)
       
    21 	{
       
    22 	__DECLARE_NAME(_S("CIpcProvdBase"));
       
    23 	iProtocol=aProtocol;
       
    24 	}
       
    25 
       
    26 CIpcProvdBase::~CIpcProvdBase()
       
    27 	{
       
    28 	// ensure that another CIpcProvdBase doesn't end up with a dangling pointer to this CIpcProvdBase
       
    29 	if (iConnection && iConnection->iConnection == this)
       
    30 		{
       
    31 		iConnection->iConnection = NULL;
       
    32 		}
       
    33 	
       
    34 	iProtocol->SocketRemoved(iLocalAddr);
       
    35 	}
       
    36 
       
    37 void CIpcProvdBase::RemName(TSockAddr &anAddr)const
       
    38 //
       
    39 // Get the remote name we would connect to
       
    40 //
       
    41 	{
       
    42 	anAddr.SetPort(iRemoteAddr);
       
    43 	}
       
    44 
       
    45 TInt  CIpcProvdBase::SetRemName(TSockAddr& anAddr)
       
    46 //
       
    47 // Set the remote name prior to connecting (or as part of a send to)
       
    48 //
       
    49 	{
       
    50 	iRemoteAddr=anAddr.Port();
       
    51 	return KErrNone;
       
    52 	}
       
    53 
       
    54 void CIpcProvdBase::LocalName(TSockAddr &anAddr)const
       
    55 //
       
    56 //  Get hte local name
       
    57 //
       
    58 	{
       
    59 	anAddr.SetPort(iLocalAddr);
       
    60 	}
       
    61 
       
    62 TInt CIpcProvdBase::SetLocalName(TSockAddr &anAddr)
       
    63 //
       
    64 // Set the local name prior to accepting
       
    65 //
       
    66 	{
       
    67 	TInt ret=iProtocol->CheckAndAllocatePortNumber(anAddr.Port());
       
    68 	if (ret==KErrNone)
       
    69 		iLocalAddr=anAddr.Port();
       
    70 	return ret;
       
    71 	}
       
    72 
       
    73 void CIpcProvdBase::AutoBind( void )
       
    74 //
       
    75 // Automatically set the local name
       
    76 //
       
    77 	{
       
    78 	iLocalAddr=iProtocol->GetNextFreePort();
       
    79 	}
       
    80 
       
    81 TInt CIpcProvdBase::GetOption(TUint /*level*/,TUint /*name*/,TDes8& /*anOption*/)const
       
    82 //
       
    83 //
       
    84 //
       
    85 	{
       
    86 	return KErrNotSupported;
       
    87 	}
       
    88 
       
    89 TInt CIpcProvdBase::SetOption(TUint /*level*/,TUint /*name*/,const TDesC8& /*anOption*/)
       
    90 //
       
    91 //
       
    92 //
       
    93 	{
       
    94 	return KErrNotSupported;
       
    95 	}
       
    96 
       
    97 
       
    98 
       
    99 void CIpcProvdBase::Ioctl(TUint /*level*/,TUint /*name*/,TDes8 * /*anOption*/)
       
   100 //
       
   101 //
       
   102 //
       
   103 	{
       
   104 	iSocket->Error(KErrNotSupported,MSocketNotify::EErrorIoctl);
       
   105 	}
       
   106 
       
   107 void CIpcProvdBase::CancelIoctl(TUint /*aLevel*/,TUint /*aName*/)
       
   108 	{
       
   109 	}
       
   110 
       
   111 TInt CIpcProvdBase::PassiveOpen(TUint aQue,const TDesC8 &/*aConnectionData*/)
       
   112 //
       
   113 //
       
   114 //
       
   115 	{
       
   116 	return PassiveOpen(aQue);
       
   117 	}
       
   118 
       
   119 void CIpcProvdBase::Shutdown(TCloseType option,const TDesC8 &/*aDisconnectData*/)
       
   120 //
       
   121 // Shutdown with disconnect data
       
   122 //
       
   123 	{
       
   124 	Shutdown(option);
       
   125 	}
       
   126 
       
   127 void CIpcProvdBase::ActiveOpen(const TDesC8 &/*aConnectionData*/)
       
   128 //
       
   129 // Active open with connection data - pass it on
       
   130 //
       
   131 	{
       
   132 	ActiveOpen();
       
   133 	}
       
   134 
       
   135 void CIpcProvdBase::Start()
       
   136 //
       
   137 // Start the provider - do nothing
       
   138 //
       
   139 	{
       
   140 	}
       
   141 
       
   142 
       
   143 void CIpcProvdBase::ActiveOpen(void)
       
   144 //
       
   145 // Actively connect to a peer socket
       
   146 //
       
   147 	{
       
   148 
       
   149 	CIpcProvdBase* peer=iProtocol->FindPeerForConnection(iRemoteAddr);
       
   150 	CIpcProvdBase* newSocket=NULL;
       
   151 	if (peer)
       
   152 		newSocket=peer->GetCloneSocket();
       
   153 	if (newSocket)
       
   154 		{
       
   155 		iConnection=newSocket;
       
   156 		newSocket->iConnection=this;
       
   157 		newSocket->iRemoteAddr=iLocalAddr;
       
   158 		iSocket->ConnectComplete();
       
   159 		peer->Connected(*newSocket);
       
   160 		}
       
   161 	else
       
   162 		iSocket->Error(KErrCouldNotConnect);
       
   163 	}
       
   164 
       
   165 void CIpcProvdBase::Shutdown(TCloseType aType)
       
   166 	{
       
   167 	if (iConnection)
       
   168 		iConnection->Disconnect();
       
   169 	iConnection=NULL;
       
   170 	if (aType!=EImmediate)
       
   171 		iSocket->CanClose();
       
   172 	}
       
   173 
       
   174 void CIpcProvdBase::Disconnect()
       
   175 	{
       
   176 
       
   177 	if (iConnection)
       
   178 		{
       
   179 		if (iSocket)
       
   180 			{
       
   181 			iSocket->Disconnect();
       
   182 			}
       
   183 		iConnection=NULL;
       
   184 		}
       
   185 
       
   186 	iState=EDisconnected;
       
   187 	}
       
   188 
       
   189 TInt CIpcProvdBase::PassiveOpen(TUint /*aQue*/)
       
   190 	{
       
   191 	iState=EWaitingConnect;
       
   192 	return KErrNone;
       
   193 	}
       
   194 
       
   195 void CIpcProvdBase::Connected(CIpcProvdBase& aSocket)
       
   196 	{
       
   197 	iSocket->ConnectComplete(aSocket);
       
   198 	}
       
   199 
       
   200 //
       
   201 
       
   202 CIpcStreamProvd* CIpcStreamProvd::NewL(CIpcProtocolHolder* aProtocol)
       
   203 	{
       
   204 	CIpcStreamProvd* p=new(ELeave)CIpcStreamProvd(aProtocol);
       
   205 	CleanupStack::PushL(p);
       
   206 	p->iBuffer.SetLengthL(KIPCStreamDefaultBufferSize);
       
   207 	CleanupStack::Pop();
       
   208 	return p;
       
   209 	}
       
   210 
       
   211 CIpcStreamProvd::CIpcStreamProvd(CIpcProtocolHolder* aProtocol)
       
   212 	:CIpcProvdBase(aProtocol)
       
   213 	{
       
   214 	__DECLARE_NAME(_S("CIpcStreamProvd"));
       
   215 	}
       
   216 
       
   217 CIpcProvdBase* CIpcStreamProvd::GetCloneSocket()
       
   218 	{
       
   219 	CIpcStreamProvd* newSock=NULL;
       
   220 	if (iState!=EWaitingConnect)
       
   221 		return newSock;
       
   222 
       
   223 	TRAPD(ret,newSock=CIpcStreamProvd::NewL(iProtocol));
       
   224 	if (ret!=KErrNone)
       
   225 		{
       
   226 		iSocket->Error(ret,MSocketNotify::EErrorConnect);
       
   227 		return NULL;
       
   228 		}
       
   229 
       
   230 	newSock->AutoBind();
       
   231 	iProtocol->Add(newSock);
       
   232 	return newSock;
       
   233 	}
       
   234 
       
   235 
       
   236 TUint CIpcStreamProvd::Write(const TDesC8& aDesc,TUint /*options*/, TSockAddr* /*anAddr*/)
       
   237 	{
       
   238 	__ASSERT_DEBUG(iSocket,Panic(EBadWriteCall));
       
   239 	if (!iConnection)
       
   240 		{
       
   241 		iSocket->Error(KErrDisconnected);
       
   242 		// error occured, the only possible error for this protocol is KErrNoMemory
       
   243 		return 0;
       
   244 		}
       
   245 	TInt len=Min(aDesc.Size(),iBuffer.Length()-iBuffer.Count());
       
   246 	if (len)
       
   247 		{
       
   248 		iBuffer.Add(aDesc.Ptr(),len);
       
   249 		iConnection->NewData(len);
       
   250 		}
       
   251 	else
       
   252 		{
       
   253 		// error occured on attempt to send data, the only possible error for 
       
   254 		// this protocol is KErrNoMemory
       
   255 		return 0;
       
   256 		}
       
   257 		
       
   258 	return len;
       
   259 	}
       
   260 
       
   261 void CIpcStreamProvd::GetData(TDes8 &aDesc,TUint /*options*/,TSockAddr* /*anAddr*/)
       
   262 	{
       
   263 	__ASSERT_DEBUG(iConnection,Panic(EBadWriteCall));
       
   264 	__ASSERT_DEBUG(iSocket,Panic(EBadWriteCall));
       
   265 
       
   266 	CCirBuffer& buf=((CIpcStreamProvd *)iConnection)->iBuffer;
       
   267 
       
   268 	__ASSERT_DEBUG(aDesc.Length()<=buf.Count(),Panic(EReadGetTooMuch));
       
   269 
       
   270 	buf.Remove((TUint8 *)aDesc.Ptr(),aDesc.Length());
       
   271 	iConnection->CanSend();
       
   272 	}
       
   273 
       
   274 void CIpcStreamProvd::NewData(TInt aLen)
       
   275 //
       
   276 // New data has arrived at our oppo.
       
   277 //
       
   278 	{
       
   279 	iSocket->NewData(aLen);
       
   280 	}
       
   281 
       
   282 void CIpcStreamProvd::CanSend()
       
   283 //
       
   284 // New space has been made (in our buffer) by at our oppo.
       
   285 //
       
   286 	{
       
   287 	iSocket->CanSend();
       
   288 	}