A small memory footprint AMQP implimentation

Dependents:   iothub_client_sample_amqp remote_monitoring simplesample_amqp

Revision:
19:000ab4e6a2c1
Parent:
17:923575db8b2d
Child:
20:206846c14c80
--- a/link.c	Fri Feb 10 17:00:55 2017 -0800
+++ b/link.c	Fri Feb 24 14:00:58 2017 -0800
@@ -11,6 +11,7 @@
 #include "azure_uamqp_c/amqp_definitions.h"
 #include "azure_uamqp_c/amqpalloc.h"
 #include "azure_uamqp_c/amqp_frame_codec.h"
+#include "azure_c_shared_utility/optimize_size.h"
 #include "azure_c_shared_utility/xlogging.h"
 #include "azure_c_shared_utility/singlylinkedlist.h"
 
@@ -73,7 +74,7 @@
 
 	if (flow == NULL)
 	{
-		result = __LINE__;
+		result = __FAILURE__;
 	}
 	else
 	{
@@ -81,13 +82,13 @@
 			(flow_set_handle(flow, link->handle) != 0) ||
 			(flow_set_delivery_count(flow, link->delivery_count) != 0))
 		{
-			result = __LINE__;
+			result = __FAILURE__;
 		}
 		else
 		{
 			if (session_send_flow(link->link_endpoint, flow) != 0)
 			{
-				result = __LINE__;
+				result = __FAILURE__;
 			}
 			else
 			{
@@ -108,7 +109,7 @@
 	DISPOSITION_HANDLE disposition = disposition_create(link_instance->role, delivery_number);
 	if (disposition == NULL)
 	{
-		result = __LINE__;
+		result = __FAILURE__;
 	}
 	else
 	{
@@ -116,13 +117,13 @@
 			(disposition_set_settled(disposition, true) != 0) ||
 			((delivery_state != NULL) && (disposition_set_state(disposition, delivery_state) != 0)))
 		{
-			result = __LINE__;
+			result = __FAILURE__;
 		}
 		else
 		{
 			if (session_send_disposition(link_instance->link_endpoint, disposition) != 0)
 			{
-				result = __LINE__;
+				result = __FAILURE__;
 			}
 			else
 			{
@@ -144,25 +145,25 @@
 	detach_performative = detach_create(0);
 	if (detach_performative == NULL)
 	{
-		result = __LINE__;
+		result = __FAILURE__;
 	}
 	else
 	{
 		if ((error_handle != NULL) &&
 			(detach_set_error(detach_performative, error_handle) != 0))
 		{
-			result = __LINE__;
+			result = __FAILURE__;
 		}
         else if (close &&
             (detach_set_closed(detach_performative, true) != 0))
         {
-            result = __LINE__;
+            result = __FAILURE__;
         }
         else
 		{
 			if (session_send_detach(link_instance->link_endpoint, detach_performative) != 0)
 			{
-				result = __LINE__;
+				result = __FAILURE__;
 			}
 			else
 			{
@@ -189,7 +190,7 @@
 
     if (attach == NULL)
     {
-        result = __LINE__;
+        result = __FAILURE__;
     }
     else
     {
@@ -208,7 +209,7 @@
         {
             if (attach_set_initial_delivery_count(attach, link->delivery_count) != 0)
             {
-                result = __LINE__;
+                result = __FAILURE__;
             }
         }
 
@@ -217,7 +218,7 @@
             if ((attach_set_max_message_size(attach, link->max_message_size) != 0) ||
                 (session_send_attach(link->link_endpoint, attach) != 0))
             {
-                result = __LINE__;
+                result = __FAILURE__;
             }
             else
             {
@@ -479,6 +480,25 @@
         {
             bool closed = false;
             ERROR_HANDLE error;
+
+            /* Received a detach while attached */
+            if (link_instance->link_state == LINK_STATE_ATTACHED)
+            {
+                /* Respond with ack */
+                (void)send_detach(link_instance, closed, NULL);
+            }
+
+            /* Received a closing detach after we sent a non-closing detach. */
+            else if (closed &&
+                (link_instance->link_state == LINK_STATE_HALF_ATTACHED) &&
+                !link_instance->is_closed)
+            {
+
+                /* In this case, we MUST signal that we closed by reattaching and then sending a closing detach.*/
+                (void)send_attach(link_instance, link_instance->name, 0, link_instance->role);
+                (void)send_detach(link_instance, true, NULL);
+            }
+
             if (detach_get_error(detach, &error) == 0)
             {
                 error_destroy(error);
@@ -492,24 +512,6 @@
                 set_link_state(link_instance, LINK_STATE_DETACHED);
             }
 
-            /* Received a detach while attached */
-            if (link_instance->previous_link_state == LINK_STATE_ATTACHED)
-            {
-                /* Respond with ack */
-                (void)send_detach(link_instance, closed, NULL);
-            }
-
-            /* Received a closing detach after we sent a non-closing detach. */
-            else if (closed &&
-                (link_instance->previous_link_state == LINK_STATE_HALF_ATTACHED) &&
-                !link_instance->is_closed)
-            {
-
-                /* In this case, we MUST signal that we closed by reattaching and then sending a closing detach.*/
-                (void)send_attach(link_instance, link_instance->name, 0, link_instance->role);
-                (void)send_detach(link_instance, true, NULL);
-            }
-
             detach_destroy(detach);
         }
     }
@@ -735,7 +737,7 @@
 
 	if (link == NULL)
 	{
-		result = __LINE__;
+		result = __FAILURE__;
 	}
 	else
 	{
@@ -753,7 +755,7 @@
 	if ((link == NULL) ||
 		(snd_settle_mode == NULL))
 	{
-		result = __LINE__;
+		result = __FAILURE__;
 	}
 	else
 	{
@@ -771,7 +773,7 @@
 
 	if (link == NULL)
 	{
-		result = __LINE__;
+		result = __FAILURE__;
 	}
 	else
 	{
@@ -789,7 +791,7 @@
 	if ((link == NULL) ||
 		(rcv_settle_mode == NULL))
 	{
-		result = __LINE__;
+		result = __FAILURE__;
 	}
 	else
 	{
@@ -806,7 +808,7 @@
 
 	if (link == NULL)
 	{
-		result = __LINE__;
+		result = __FAILURE__;
 	}
 	else
 	{
@@ -824,7 +826,7 @@
 	if ((link == NULL) ||
 		(initial_delivery_count == NULL))
 	{
-		result = __LINE__;
+		result = __FAILURE__;
 	}
 	else
 	{
@@ -841,7 +843,7 @@
 
 	if (link == NULL)
 	{
-		result = __LINE__;
+		result = __FAILURE__;
 	}
 	else
 	{
@@ -859,7 +861,7 @@
 	if ((link == NULL) ||
 		(max_message_size == NULL))
 	{
-		result = __LINE__;
+		result = __FAILURE__;
 	}
 	else
 	{
@@ -876,14 +878,14 @@
 
     if (link == NULL)
     {
-        result = __LINE__;
+        result = __FAILURE__;
     }
     else
     {
         link->attach_properties = amqpvalue_clone(attach_properties);
         if (link->attach_properties == NULL)
         {
-            result = __LINE__;
+            result = __FAILURE__;
         }
         else
         {
@@ -901,7 +903,7 @@
 	if ((link == NULL) ||
         (link->is_closed))
 	{
-		result = __LINE__;
+		result = __FAILURE__;
 	}
 	else
 	{
@@ -914,7 +916,7 @@
 
 			if (session_begin(link->session) != 0)
 			{
-				result = __LINE__;
+				result = __FAILURE__;
 			}
 			else
 			{
@@ -922,7 +924,7 @@
 
 				if (session_start_link_endpoint(link->link_endpoint, link_frame_received, on_session_state_changed, on_session_flow_on, link) != 0)
 				{
-					result = __LINE__;
+					result = __FAILURE__;
 				}
 				else
 				{
@@ -948,7 +950,7 @@
     if ((link == NULL) ||
         (link->is_closed))
     {
-		result = __LINE__;
+		result = __FAILURE__;
 	}
 	else
 	{
@@ -959,7 +961,7 @@
             /* Sending detach when remote is not yet attached */
             if (send_detach(link, close, NULL) != 0)
             {
-                result = __LINE__;
+                result = __FAILURE__;
             }
             else
             {
@@ -972,7 +974,7 @@
             /* Send detach and wait for remote to respond */
             if (send_detach(link, close, NULL) != 0)
             {
-                result = __LINE__;
+                result = __FAILURE__;
             }
             else
             {
@@ -989,7 +991,7 @@
         default:
         case LINK_STATE_ERROR:
             /* Already detached and in error state */
-            result = __LINE__;
+            result = __FAILURE__;
             break;
         }
 	}