bluetooth/btcomm/src/sockservconnector.cpp
changeset 0 29b1cd4cb562
equal deleted inserted replaced
-1:000000000000 0:29b1cd4cb562
       
     1 // Copyright (c) 2004-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 // sockservconnector.cpp - all the active connector code
       
    15 // 
       
    16 //
       
    17 
       
    18 #include <e32math.h>
       
    19 
       
    20 #include "btcomm.h"
       
    21 #include "btcommactive.h"
       
    22 #include "btcommutil.h"
       
    23 
       
    24 
       
    25 TInt StartConnector(RSocketServ& aSockServ)
       
    26 	{
       
    27 	TInt err = aSockServ.Connect();
       
    28 	if(err == KErrNone)
       
    29 		{
       
    30 		err = aSockServ.ShareAuto();
       
    31 		if(err != KErrNone)
       
    32 			{
       
    33 			aSockServ.Close();
       
    34 			}
       
    35 		}
       
    36 		
       
    37 	return err;
       
    38 	}
       
    39 
       
    40 TInt RunConnectorThread(TAny* aArg)
       
    41 	{
       
    42 	// Add the thread id to the end of the the thread name
       
    43 	// to ensure it is unique.
       
    44 	TName threadName(KConnectorThreadName);
       
    45 	RThread thread;
       
    46 	threadName.AppendNum(static_cast<TUint>(thread.Id()), EHex);
       
    47 
       
    48 	// We can carry on even if this errors
       
    49 	User::RenameThread(threadName);
       
    50 	
       
    51 	return StartConnector(*reinterpret_cast<RSocketServ*>(aArg));
       
    52 	}
       
    53 	
       
    54 //==============================================================================
       
    55 
       
    56 CSockServConnector* CSockServConnector::NewL(RSocketServ& aSockServ)
       
    57 	{
       
    58 	CSockServConnector* self = new(ELeave) CSockServConnector(aSockServ);
       
    59 	CleanupStack::PushL(self);
       
    60 	self->ConstructL();
       
    61 	CleanupStack::Pop();
       
    62 	return self;
       
    63 	}
       
    64 
       
    65 void CSockServConnector::ConstructL()
       
    66 	{
       
    67 	}
       
    68 
       
    69 CSockServConnector::~CSockServConnector()
       
    70 	{
       
    71 	Cancel();
       
    72 	}
       
    73 
       
    74 void CSockServConnector::SockServConnect(TRequestStatus& aStatus)
       
    75 	{
       
    76 	TThreadFunction connectorThreadFunction(RunConnectorThread);
       
    77 	iClientStatus = &aStatus;
       
    78 	*iClientStatus = KRequestPending;
       
    79 
       
    80 	// Once we've connected this thread it is renamed to ensure
       
    81 	// its name is unique.
       
    82 	TInt err = iConnectorThread.Create(KConnectorThreadName, connectorThreadFunction,KDefaultStackSize,KMinHeapSize,1024*1000, &iSockServ, EOwnerThread);
       
    83 
       
    84 	if(err != KErrNone)
       
    85 		{
       
    86 		User::RequestComplete(iClientStatus, err);
       
    87 		}
       
    88 	else
       
    89 		{
       
    90 		// Set up the configuration of the thread
       
    91 		iConnectorThread.SetPriority(RThread().Priority());
       
    92 		
       
    93 		// Stick ourselves on the scheduler jit so we're not
       
    94 		// cluttering up the queue when we don't want to be 
       
    95 		// run.
       
    96 		CActiveScheduler::Add(this);
       
    97 		
       
    98 		// Set ourselves to be notified on death of connector thread.
       
    99 		iConnectorThread.Logon(iStatus);
       
   100 		SetActive();
       
   101 
       
   102 		// Mark connector thread ready to run
       
   103 		iConnectorThread.Resume();
       
   104 		}
       
   105 	}
       
   106 
       
   107 void CSockServConnector::RunL()
       
   108 	{
       
   109 	// Remove ourselves from the active scheduler so we
       
   110 	// don't get in the way of other active objects
       
   111 	Deque();
       
   112 	
       
   113 	// if the thread returned something other than
       
   114 	// KErrNone then the Esock connect & share didn't
       
   115 	// succeed.  Go to RunError to notify client and
       
   116 	// tidy up.
       
   117 	User::LeaveIfError(iStatus.Int());
       
   118 	
       
   119 	// The exit reason was zero, but the thread 
       
   120 	// terminated abnormally.
       
   121 	if(iConnectorThread.ExitType() != EExitKill)
       
   122 		{
       
   123 		User::Leave(KErrGeneral);
       
   124 		}
       
   125 
       
   126 	NotifyAndClose(KErrNone);
       
   127 	}
       
   128 	
       
   129 TInt CSockServConnector::RunError(TInt aError)
       
   130 	{
       
   131 	// let client know about result, and gulp
       
   132 	NotifyAndClose(aError);
       
   133 	return KErrNone;
       
   134 	}
       
   135 	
       
   136 void CSockServConnector::NotifyAndClose(TInt aError)
       
   137 	{
       
   138 	// tell client the result
       
   139 	User::RequestComplete(iClientStatus, aError);
       
   140 	// close connector thread
       
   141 	iConnectorThread.Close();	
       
   142 	}
       
   143 
       
   144 void CSockServConnector::DoCancel()
       
   145 	{
       
   146 	BTCommUtil::Panic(EBTCommESockSessionAttachNotDone);
       
   147 	}
       
   148 
       
   149 // We make this ao high priority because we want to get the session connected asap so we can
       
   150 // make some progress servicing other requests.
       
   151 CSockServConnector::CSockServConnector(RSocketServ& aSockServ) : CActive (EPriorityHigh), iSockServ(aSockServ)
       
   152 	{
       
   153 	}