datacommsserver/esockserver/test/protocols/ipc/IPC_PROT.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 <es_ver.h>
       
    17 #include "IPC_MAIN.H"
       
    18 #include "ES_IPC.H"
       
    19 
       
    20 CIpcProtocolHolder::CIpcProtocolHolder()
       
    21 	:iSAPs(_FOFF(CIpcProvdBase,iLink))
       
    22 	{
       
    23 	}
       
    24 
       
    25 void CIpcProtocolHolder::SocketRemoved(TInt aPort)
       
    26 //
       
    27 // Called from a Service providers destructor so we can keep a count of the sockets.
       
    28 //
       
    29 	{
       
    30 	iNumSockets--;
       
    31 	if (aPort)
       
    32 		iPortNumbers->Free(aPort);
       
    33 	}
       
    34 
       
    35 TInt CIpcProtocolHolder::GetNextFreePort()
       
    36 //
       
    37 // The guts of AutoBind
       
    38 //
       
    39 	{
       
    40 	return iPortNumbers->Alloc();
       
    41 	}
       
    42 
       
    43 CIpcProtocolHolder* CIpcProtocolHolder::NewL()
       
    44 	{
       
    45 	CIpcProtocolHolder* p=new(ELeave)CIpcProtocolHolder;
       
    46 	CleanupStack::PushL(p);
       
    47 	p->iPortNumbers=CBitMapAllocator::NewL(KIPSNumberSockets+1);
       
    48 	p->iPortNumbers->Alloc();	// we reserve port 0 to indicate unset.
       
    49 	CleanupStack::Pop();
       
    50 	return p;
       
    51 	}
       
    52 
       
    53 CIpcProtocolHolder::~CIpcProtocolHolder()
       
    54 	{
       
    55 	if (iPortNumbers)
       
    56 		{
       
    57 		// Check that there is only one socket allocated (we always allocate 0 in CIpcProtocolHolder::NewL above)
       
    58 		__ASSERT_DEBUG(iPortNumbers->Avail()==iPortNumbers->Size()-1,Panic(ECloseWithoutDeleteingAllSockets));
       
    59 		delete iPortNumbers;
       
    60 		}
       
    61 	}
       
    62 
       
    63 TInt CIpcProtocolHolder::CheckAndAllocatePortNumber(TInt aPort)
       
    64 	{
       
    65 
       
    66 	if(aPort<1 || aPort>KIPSNumberSockets)
       
    67 		return KErrTooBig;
       
    68 
       
    69 	if (iPortNumbers->IsFree(aPort))
       
    70 		{
       
    71 		iPortNumbers->AllocAt(aPort);
       
    72 		return KErrNone;
       
    73 		}
       
    74 	else
       
    75 		return KErrInUse;
       
    76 	}
       
    77 
       
    78 void CIpcProtocolHolder::Add(CIpcProvdBase* aSAP)
       
    79 	{
       
    80 	iNumSockets++;
       
    81 	iSAPs.AddFirst(*aSAP);
       
    82 	}
       
    83 
       
    84 CIpcProvdBase* CIpcProtocolHolder::FindPeerForConnection(TInt aPort)
       
    85 	{
       
    86 	TDblQueIter<CIpcProvdBase> i(iSAPs);
       
    87 	CIpcProvdBase* p;
       
    88 	while (p=i++,p!=NULL)
       
    89 		{
       
    90 		if (p->iLocalAddr==aPort)
       
    91 			return p;
       
    92 		}
       
    93 	return NULL;
       
    94 	}
       
    95 
       
    96 
       
    97 CIpcProtocol::~CIpcProtocol()
       
    98 	{
       
    99 	delete iStreamProtocolSAPs;
       
   100 	}
       
   101 
       
   102 CIpcProtocol::CIpcProtocol(TInt aProtocol)
       
   103 	{
       
   104 	__DECLARE_NAME(_S("CIpcProtocol"));
       
   105 	
       
   106 	iProtocol=aProtocol;
       
   107 	}
       
   108 
       
   109 CIpcProtocol* CIpcProtocol::NewL(TInt aProtocol)
       
   110 	{
       
   111 	CIpcProtocol* p=new(ELeave)CIpcProtocol(aProtocol);
       
   112 	CleanupStack::PushL(p);
       
   113 	p->iStreamProtocolSAPs=CIpcProtocolHolder::NewL();
       
   114 	CleanupStack::Pop(p);
       
   115 	return p;
       
   116 	}
       
   117 
       
   118 CServProviderBase *CIpcProtocol::NewSAPL(TUint /*aProtocol*/)
       
   119 //
       
   120 // Socket server asking for a host resolver
       
   121 //
       
   122 	{
       
   123 
       
   124 	CIpcProvdBase* s=NULL;
       
   125 	switch(iProtocol)
       
   126 		{
       
   127 	case KSockStream:
       
   128 		if (iStreamProtocolSAPs->iNumSockets==KIPSNumberSockets)
       
   129 			User::Leave(KErrTooBig);
       
   130 		s=CIpcStreamProvd::NewL(iStreamProtocolSAPs);
       
   131 		iStreamProtocolSAPs->Add(s);
       
   132 		break;
       
   133 	case KSockDatagram:
       
   134 		User::Leave(KErrNotSupported);
       
   135 		break;
       
   136 		}
       
   137 	
       
   138 	return s;
       
   139 	}
       
   140 
       
   141 TBool CIpcProtocol::CanCreateSockets()
       
   142 //
       
   143 // Very rude question from the socket server. Doesn't even say Excuse me or please.
       
   144 //
       
   145 	{
       
   146 	return ETrue;
       
   147 	}
       
   148 
       
   149 void CIpcProtocol::InitL(TDesC& /*aTag*/)
       
   150 //
       
   151 // InitL call from socket server. Do nothing
       
   152 //
       
   153 	{
       
   154 	}
       
   155 
       
   156 void CIpcProtocol::BindL(CProtocolBase* /*aProtocol*/, TUint /*anId*/)
       
   157 //
       
   158 // BindL call from peer protocol
       
   159 //
       
   160 	{
       
   161 	Panic(ECantBind);
       
   162 	}
       
   163 
       
   164 void CIpcProtocol::StartL(void)
       
   165 //
       
   166 // StartL call from socket server. Do nothing
       
   167 //
       
   168 	{
       
   169 	
       
   170 	}
       
   171 
       
   172 void CIpcProtocol::Identify(TServerProtocolDesc *aDesc)const
       
   173 //
       
   174 // Identify request from SOCKET server
       
   175 //
       
   176 	{
       
   177 	_LIT(ipcStream,"IPC Stream");
       
   178 	_LIT(ipcDatagramm,"IPC Datagramm");
       
   179 	switch(iProtocol)
       
   180 		{
       
   181 	case KSockStream:
       
   182 
       
   183 		aDesc->iName=ipcStream;
       
   184 		aDesc->iAddrFamily=KIPCAddrFamily;
       
   185 		aDesc->iSockType=KSockStream;
       
   186 		aDesc->iProtocol=KIPCStreamProtocol;
       
   187 
       
   188 		aDesc->iVersion=TVersion(KES32MajorVersionNumber,KES32MinorVersionNumber,KES32BuildVersionNumber);
       
   189 		aDesc->iByteOrder=ELittleEndian;
       
   190 		aDesc->iServiceInfo=0;
       
   191 		aDesc->iNamingServices=0;
       
   192 		aDesc->iSecurity=KSocketNoSecurity;
       
   193 		aDesc->iMessageSize=KSocketMessageSizeIsStream;
       
   194 		aDesc->iServiceTypeInfo=0;
       
   195 		aDesc->iNumSockets=KIPSNumberSockets;
       
   196 		return;
       
   197 	case KSockDatagram:
       
   198 		aDesc->iName=ipcDatagramm;
       
   199 		aDesc->iAddrFamily=KIPCAddrFamily;
       
   200 		aDesc->iSockType=KSockDatagram;
       
   201 		aDesc->iProtocol=KIPCDatagramProtocol;
       
   202 
       
   203 		aDesc->iVersion=TVersion(KES32MajorVersionNumber,KES32MinorVersionNumber,KES32BuildVersionNumber);
       
   204 		aDesc->iByteOrder=ELittleEndian;
       
   205 		aDesc->iServiceInfo=KIPCDatagramServiceInfo;
       
   206 		aDesc->iNamingServices=0;
       
   207 		aDesc->iSecurity=KSocketNoSecurity;
       
   208 		aDesc->iMessageSize=KSocketMessageSizeNoLimit;
       
   209 		aDesc->iServiceTypeInfo=0;
       
   210 		aDesc->iNumSockets=KIPSNumberSockets;
       
   211 		return;
       
   212 		}
       
   213 
       
   214 	}
       
   215 
       
   216 void CIpcProtocol::BindToL(CProtocolBase* /*protocol*/)
       
   217 //
       
   218 // BindL call from socket server
       
   219 //
       
   220 	{
       
   221 	Panic(ECantBindTo);
       
   222 	}
       
   223 
       
   224 TInt CIpcProtocol::Send(RMBufChain &,CProtocolBase* /*aSourceProtocol*/)
       
   225 //
       
   226 // Send Down call from bindee
       
   227 //
       
   228 	{
       
   229 	Panic(ESendCallCantBind);
       
   230 	return 0;
       
   231 	}
       
   232 
       
   233 void CIpcProtocol::Process(RMBufChain &,CProtocolBase* /*aSourceProtocol*/)
       
   234 //
       
   235 // Process up call from bindee
       
   236 //
       
   237 	{
       
   238 	Panic(EProcessCallCantBind);
       
   239 	}
       
   240 
       
   241 TInt CIpcProtocol::Send(TDes8 &, TSockAddr* /*to*/,TSockAddr* /*from*/,CProtocolBase* /*aSourceProtocol*/)
       
   242 //
       
   243 // Send down call from bindee
       
   244 //
       
   245 	{
       
   246 	Panic(ESendCallCantBind);
       
   247 	return KErrNone;
       
   248 	}
       
   249 
       
   250 void CIpcProtocol::Process(TDes8 & ,TSockAddr* /*from*/,TSockAddr* /*to*/,CProtocolBase* /*aSourceProtocol*/)
       
   251 //
       
   252 // Process up call from bindee
       
   253 //
       
   254 	{
       
   255 	Panic(EProcessCallCantBind);
       
   256 	}
       
   257 
       
   258 
       
   259 TInt CIpcProtocol::GetOption(TUint /*level*/,TUint /*name*/,TDes8 & /*anOption*/,CProtocolBase* /*aSourceProtocol*/)
       
   260 //
       
   261 // GetOption Down call from bindee
       
   262 //
       
   263 	{
       
   264 	Panic(EGetOptionCallCantBind);
       
   265 	return KErrNone;
       
   266 	}
       
   267 
       
   268 TInt CIpcProtocol::SetOption(TUint /*level*/,TUint /*name*/,const TDesC8& /*option*/,CProtocolBase* /*aSourceProtocol*/)
       
   269 //
       
   270 // SetOption Down call from bindee
       
   271 //
       
   272 	{
       
   273 	Panic(ESetOptionCallCantBind);
       
   274 	return KErrNone;
       
   275 	}
       
   276 
       
   277 void CIpcProtocol::Error(TInt /*anError*/,CProtocolBase* /*aSourceProtocol*/)
       
   278 //
       
   279 // Error up call from bindee
       
   280 //
       
   281 	{
       
   282 	Panic(EErrorUpCallCantBind);
       
   283 	}
       
   284 
       
   285 CHostResolvProvdBase *CIpcProtocol::NewHostResolverL()
       
   286 //
       
   287 // Socket server asking for a host resolver
       
   288 //
       
   289 	{
       
   290 	Panic(ECantCreateHostResolver);
       
   291 	return NULL;
       
   292 	}
       
   293 
       
   294 CServiceResolvProvdBase *CIpcProtocol::NewServiceResolverL()
       
   295 //
       
   296 // Socket server asking for a service resolver
       
   297 //
       
   298 	{
       
   299 	Panic(ECantCreateServiceResolver);
       
   300 	return NULL;
       
   301 	}
       
   302 
       
   303 CNetDBProvdBase* CIpcProtocol::NewNetDatabaseL()
       
   304 //
       
   305 // Socket server asking for a net data base
       
   306 //
       
   307 	{
       
   308 	Panic(ECantCreateNetDatabase);
       
   309 	return NULL;
       
   310 	}