mtpfws/mtpfw/src/cmtpconnection.cpp
changeset 38 48c22c726cf9
parent 22 a5c0bb5018eb
child 47 63cf70d3ecd8
--- a/mtpfws/mtpfw/src/cmtpconnection.cpp	Tue Jul 06 15:13:34 2010 +0300
+++ b/mtpfws/mtpfw/src/cmtpconnection.cpp	Wed Aug 18 10:38:51 2010 +0300
@@ -82,18 +82,16 @@
     __FLOG(_L8("~CMTPConnection - Entry"));
     CloseAllSessions();
     
-    // Remove any events not associated 
-    // with a session
-    TSglQueIter<CMTPEventLink> iter(iEventQ);
-    iter.SetToFirst();
-    CMTPEventLink* link = NULL;
+	//remove all events
+    DequeueAllEvents();
+	
+    iSessions.ResetAndDestroy();
     
-    while ((link = iter++) != NULL)
-    	{
-    	delete link;
-    	}
+    if (iTransportConnection != NULL)
+        {
+        iTransportConnection->Unbind(*this);
+        }
     
-    iSessions.ResetAndDestroy();
     //close the property
     iProperty.Close();
     // delete the ‘name?property
@@ -357,7 +355,7 @@
             CompleteCloseConnection();
             ret = ETrue;
             }
-        
+          
         SetState(EStateShutdown);
         PublishConnState(EDisconnectedFromHost);   
         }
@@ -372,10 +370,6 @@
     
     CloseAllSessions();
     iSessions.Reset();
-	if (iTransportConnection != NULL)
-		{
-		iTransportConnection->Unbind(*this);
-		}
     
     //notify ConnectionMgr and corresponding transports of completion of connection close
     iSingletons.ConnectionMgr().ConnectionCloseComplete(iConnectionId);    
@@ -405,8 +399,12 @@
         */
         SessionOpenedL(KMTPSessionNone);            
         
+        if (iTransportConnection != NULL)
+            {
+            iTransportConnection->Unbind(*this);
+            }
         iTransportConnection = &aTransportConnection;
-        iTransportConnection->BindL(*this); 
+        iTransportConnection->BindL(*this);
         SetState(EStateOpen); 
          
         }
@@ -661,9 +659,16 @@
     DequeueEvent(iEventQ.First());       
    	if (iPendingEventCount > 0)
    		{
-   		// Forward the event to the transport connection layer.
-   		__FLOG(_L8("Sending queued event"));
- 	    iTransportConnection->SendEventL(iEventQ.First()->iEvent);
+		if (NULL != iTransportConnection)
+			{
+			// Forward the event to the transport connection layer.
+			__FLOG(_L8("Sending queued event"));
+			iTransportConnection->SendEventL(iEventQ.First()->iEvent);
+			}
+		else
+			{
+			DequeueAllEvents();
+			}
    		}
     
     __FLOG(_L8("SendEventCompleteL - Exit"));
@@ -707,7 +712,7 @@
     iEventQ(_FOFF(CMTPEventLink, iLink)),
     iTransportConnection(&aTransportConnection)
     {
-    
+    iTransportConnection->BindL(*this);
     }
     
 /**
@@ -1040,6 +1045,19 @@
     __FLOG(_L8("RemoveEventsForSession - Exit"));
 	}
 
+void CMTPConnection::DequeueAllEvents()
+	{
+	TSglQueIter<CMTPEventLink> iter(iEventQ);
+    iter.SetToFirst();
+    CMTPEventLink* link = NULL;
+    
+    while ((link = iter++) != NULL)
+    	{
+    	delete link;
+    	}
+	iPendingEventCount = 0;
+	}
+	
 void CMTPConnection::DequeueEvent(CMTPEventLink* aLink)
 	{
 	iEventQ.Remove(*aLink);