private void SyncLocalStateWithRemote()

in src/Proton/Engine/Implementation/ProtonConnection.cs [670:720]


      private void SyncLocalStateWithRemote()
      {
         if (engine.IsWritable)
         {
            // When the engine state changes or we have read an incoming AMQP header etc we need to check
            // if we have pending work to send and do so
            if (headerSent)
            {
               ConnectionState state = ConnectionState;

               // Once an incoming header arrives we can emit our open if locally opened and also send close if
               // that is what our state is already.
               if (state != ConnectionState.Idle && remoteHeader != null)
               {
                  bool resourceSyncNeeded = false;

                  if (!localOpenSent && !engine.IsShutdown)
                  {
                     engine.FireWrite(localOpen, 0);
                     engine.RecomputeEffectiveFrameSizeLimits();
                     localOpenSent = true;
                     resourceSyncNeeded = true;
                  }

                  if (IsLocallyClosed && !localCloseSent && !engine.IsShutdown)
                  {
                     Close localClose = new()
                     {
                        Error = ErrorCondition
                     };
                     engine.FireWrite(localClose, 0);
                     localCloseSent = true;
                     resourceSyncNeeded = false;  // Session resources can't write anything now
                  }

                  if (resourceSyncNeeded)
                  {
                     foreach (ProtonSession session in AllSessions())
                     {
                        session.TrySyncLocalStateWithRemote();
                     }
                  }
               }
            }
            else if (remoteHeader != null || ConnectionState == ConnectionState.Active || remoteHeaderHandler != null)
            {
               headerSent = true;
               engine.FireWrite(HeaderEnvelope.AMQP_HEADER_ENVELOPE);
            }
         }
      }