Procházet zdrojové kódy

Factoring out ProcessSendBuffer

Source commit: 48e8642d9b3be79faf90d366021e4f91e058d12d
Martin Prikryl před 2 roky
rodič
revize
01b2a64d2c

+ 50 - 63
source/filezilla/AsyncSslSocketLayer.cpp

@@ -153,26 +153,12 @@ void CAsyncSslSocketLayer::OnReceive(int nErrorCode)
       }
       }
     }
     }
 
 
-    if (m_pRetrySendBuffer)
+    if (m_pRetrySendBuffer != NULL)
     {
     {
-      int numwrite = BIO_write(m_sslbio, m_pRetrySendBuffer, m_nRetrySendBufferLen);
-      if (numwrite >= 0)
-      {
-        BIO_ctrl(m_sslbio, BIO_CTRL_FLUSH, 0, NULL);
-        delete [] m_pRetrySendBuffer;
-        m_pRetrySendBuffer = 0;
-      }
-      else if (numwrite == -1)
+      if (ProcessSendBuffer() == -2)
       {
       {
-        if (!BIO_should_retry(m_sslbio))
-        {
-          delete [] m_pRetrySendBuffer;
-          m_pRetrySendBuffer = 0;
-
-          ::SetLastError(WSAECONNABORTED);
-          TriggerEvent(FD_CLOSE, 0, TRUE);
-          return;
-        }
+        TriggerEvent(FD_CLOSE, 0, TRUE);
+        return;
       }
       }
     }
     }
 
 
@@ -211,6 +197,34 @@ void CAsyncSslSocketLayer::OnReceive(int nErrorCode)
   }
   }
 }
 }
 
 
+int CAsyncSslSocketLayer::ProcessSendBuffer()
+{
+  int numwrite = BIO_write(m_sslbio, m_pRetrySendBuffer, m_nRetrySendBufferLen);
+  if (numwrite >= 0)
+  {
+    BIO_ctrl(m_sslbio, BIO_CTRL_FLUSH, 0, NULL);
+    delete [] m_pRetrySendBuffer;
+    m_pRetrySendBuffer = 0;
+    return numwrite;
+  }
+  else
+  {
+    DebugAssert(numwrite == -1);
+    if (!BIO_should_retry(m_sslbio))
+    {
+      delete [] m_pRetrySendBuffer;
+      m_pRetrySendBuffer = 0;
+
+      ::SetLastError(WSAECONNABORTED);
+      return -2;
+    }
+    else
+    {
+      return -1;
+    }
+  }
+}
+
 void CAsyncSslSocketLayer::OnSend(int nErrorCode)
 void CAsyncSslSocketLayer::OnSend(int nErrorCode)
 {
 {
   if (m_bUseSSL)
   if (m_bUseSSL)
@@ -311,26 +325,12 @@ void CAsyncSslSocketLayer::OnSend(int nErrorCode)
       }
       }
     }
     }
 
 
-    if (m_pRetrySendBuffer)
+    if (m_pRetrySendBuffer != NULL)
     {
     {
-      int numwrite = BIO_write(m_sslbio, m_pRetrySendBuffer, m_nRetrySendBufferLen);
-      if (numwrite >= 0)
-      {
-        BIO_ctrl(m_sslbio, BIO_CTRL_FLUSH, 0, NULL);
-        delete [] m_pRetrySendBuffer;
-        m_pRetrySendBuffer = 0;
-      }
-      else if (numwrite == -1)
+      if (ProcessSendBuffer() == -2)
       {
       {
-        if (!BIO_should_retry(m_sslbio))
-        {
-          delete [] m_pRetrySendBuffer;
-          m_pRetrySendBuffer = 0;
-
-          ::SetLastError(WSAECONNABORTED);
-          TriggerEvent(FD_CLOSE, 0, TRUE);
-          return;
-        }
+        TriggerEvent(FD_CLOSE, 0, TRUE);
+        return;
       }
       }
     }
     }
 
 
@@ -405,45 +405,32 @@ int CAsyncSslSocketLayer::Send(const void* lpBuf, int nBufLen, int nFlags)
     m_nRetrySendBufferLen = nBufLen;
     m_nRetrySendBufferLen = nBufLen;
     memcpy(m_pRetrySendBuffer, lpBuf, nBufLen);
     memcpy(m_pRetrySendBuffer, lpBuf, nBufLen);
 
 
-    int numwrite = BIO_write(m_sslbio, m_pRetrySendBuffer, m_nRetrySendBufferLen);
-    if (numwrite >= 0)
+    int ProcessResult = ProcessSendBuffer();
+    if (ProcessResult == -2)
     {
     {
-      BIO_ctrl(m_sslbio, BIO_CTRL_FLUSH, 0, NULL);
-      delete [] m_pRetrySendBuffer;
-      m_pRetrySendBuffer = 0;
+      return SOCKET_ERROR;
     }
     }
-    else if (numwrite == -1)
+    else if (ProcessResult == -1)
     {
     {
-      if (BIO_should_retry(m_sslbio))
+      if (GetLayerState() == closed)
       {
       {
-        if (GetLayerState() == closed)
-        {
-          return 0;
-        }
-        else if (GetLayerState() != connected)
-        {
-          SetLastError(m_nNetworkError);
-          return SOCKET_ERROR;
-        }
-
-        TriggerEvents();
-
-        return nBufLen;
+        return 0;
       }
       }
-      else
+      else if (GetLayerState() != connected)
       {
       {
-        delete [] m_pRetrySendBuffer;
-        m_pRetrySendBuffer = 0;
-
-        ::SetLastError(WSAECONNABORTED);
+        SetLastError(m_nNetworkError);
+        return SOCKET_ERROR;
       }
       }
-      return SOCKET_ERROR;
+
+      TriggerEvents();
+
+      return nBufLen;
     }
     }
 
 
     m_mayTriggerWriteUp = true;
     m_mayTriggerWriteUp = true;
     TriggerEvents();
     TriggerEvents();
 
 
-    return numwrite;
+    return ProcessResult;
   }
   }
   else
   else
   {
   {

+ 1 - 0
source/filezilla/AsyncSslSocketLayer.h

@@ -162,6 +162,7 @@ private:
   void UnloadSSL();
   void UnloadSSL();
   void PrintLastErrorMsg();
   void PrintLastErrorMsg();
   bool HandleSession(SSL_SESSION * Session);
   bool HandleSession(SSL_SESSION * Session);
+  int ProcessSendBuffer();
 
 
   void TriggerEvents();
   void TriggerEvents();