title: How to tell if SSL_read has received and processed all the records from single message
body: <p>Following is the dilemma,
  SSL_read, on success returns number of bytes read, SSL_pending is used to tell if the processed record has more that to be read, that means probably buffer provided is not sufficient to contain the record.</p>
  
  <p>SSL_read may return n > 0, but what if this happens when first records has been processed and message effectively is multi record communication.</p>
  
  <p>Question: I am using epoll to send/receive messages, which means I have to queue up event in case I expect more data. What check will ensure that all the records have been read from single message and it's time to remove this event and queue up an response event that will write the response back to client?</p>
  
  <p>PS: This code hasn't been tested so it may be incorrect. Purpose of the code is to share the idea that I am trying to implement.</p>
  
  <p>Following is code snippet for the read -</p>
  
  <pre><code>        //read whatever is available.
          while (1)
          {
              auto n = SSL_read(ssl_, ptr_ + tail_, sz_ - tail_);
  
              if (n &lt;= 0)
              {
                  int ssle = SSL_get_error(ch-&gt;ssl_, rd);
                  auto old_ev = evt_.events;
                  if (ssle == SSL_ERROR_WANT_READ)
                  {
                      //need more data to process, wait for epoll notification again
                      evt_.events = EPOLLIN | EPOLLERR;
                  }
                  else if (err == SSL_ERROR_WANT_WRITE)
                  {
                      evt_.events = EPOLLOUT | EPOLLERR;
                  }
                  else
                  {
                      /*  connection closed by peer, or
                          some irrecoverable error */
                      done_ = true;
                      tail_ = 0; //invalidate the data
                      break;
                  }
  
                  if (old_ev != evt_.events)
                      if (epoll_ctl(epoll_fd_, EPOLL_CTL_MOD, socket_fd_, &amp;evt_) &lt; 0)
                      {
                          perror("handshake failed at EPOLL_CTL_MOD");
                          SSL_free(ssl_);
                          ssl_ = nullptr;
                          return false;
                      }
              }
              else //some data has been read
              {
                  tail_ = n;
                  if (SSL_pending(ssl_) &gt; 0)
                  //buffer wasn't enough to hold the content. resize and reread
                      resize();
                  else
                      break;
              }
          }
      ```
      enter code here
  </code></pre>