src/wire/libevent_socket.cpp
WriteState LibeventSocket::BufferWriteBytesContent(OutputPacket *pkt) {
  // the packet content to write
  ByteBuf &pkt_buf = pkt->buf;
  // the length of remaining content to write
  size_t len = pkt->len;
  // window is the size of remaining space in socket's wbuf
  size_t window = 0;

  // fill the contents
  while (len) {
    // calculate the remaining space in wbuf
    window = wbuf_.GetMaxSize() - wbuf_.buf_ptr;
    if (len <= window) {
      // contents fit in the window, range copy "len" bytes
      std::copy(std::begin(pkt_buf) + pkt->write_ptr,
                std::begin(pkt_buf) + pkt->write_ptr + len,
                std::begin(wbuf_.buf) + wbuf_.buf_ptr);

      // Move the cursor and update size of socket buffer
      wbuf_.buf_ptr += len;
      wbuf_.buf_size = wbuf_.buf_ptr;
      LOG_TRACE("Content fit in window. Write content successful");
      return WRITE_COMPLETE;
    } else {
      // contents longer than socket buffer size, fill up the socket buffer
      // with "window" bytes

      std::copy(std::begin(pkt_buf) + pkt->write_ptr,
                std::begin(pkt_buf) + pkt->write_ptr + window,
                std::begin(wbuf_.buf) + wbuf_.buf_ptr);

      // move the packet's cursor
      pkt->write_ptr += window;
      len -= window;
      // Now the wbuf is full
      wbuf_.buf_size = wbuf_.GetMaxSize();

      LOG_TRACE("Content doesn't fit in window. Try flushing");
      auto result = FlushWriteBuffer();
      // flush before write the remaining content
      if (result == WRITE_NOT_READY || result == WRITE_ERROR) {
        // need to retry or close connection
        return result;
      }
    }
  }
  return WRITE_COMPLETE;
}
// Writes a packet's content into the write buffer
// Return false when the socket is not ready for write
Writes a packet's content into the write buffer
Return false when the socket is not ready for write
Writes a packet ' s content into the write buffer Return false when the socket is not ready for write
WriteState LibeventSocket::BufferWriteBytesContent(OutputPacket *pkt) {
  ByteBuf &pkt_buf = pkt->buf;
  size_t len = pkt->len;
  size_t window = 0;
  while (len) {
    window = wbuf_.GetMaxSize() - wbuf_.buf_ptr;
    if (len <= window) {
      std::copy(std::begin(pkt_buf) + pkt->write_ptr,
                std::begin(pkt_buf) + pkt->write_ptr + len,
                std::begin(wbuf_.buf) + wbuf_.buf_ptr);
      wbuf_.buf_ptr += len;
      wbuf_.buf_size = wbuf_.buf_ptr;
      LOG_TRACE("Content fit in window. Write content successful");
      return WRITE_COMPLETE;
    } else {
      std::copy(std::begin(pkt_buf) + pkt->write_ptr,
                std::begin(pkt_buf) + pkt->write_ptr + window,
                std::begin(wbuf_.buf) + wbuf_.buf_ptr);
      pkt->write_ptr += window;
      len -= window;
      wbuf_.buf_size = wbuf_.GetMaxSize();
      LOG_TRACE("Content doesn't fit in window. Try flushing");
      auto result = FlushWriteBuffer();
      if (result == WRITE_NOT_READY || result == WRITE_ERROR) {
        return result;
      }
    }
  }
  return WRITE_COMPLETE;
}
WriteState LibeventSocket :: BufferWriteBytesContent ( OutputPacket * pkt ) { ByteBuf & pkt_buf = pkt -> buf ; size_t len = pkt -> len ; size_t window = 0 ; while ( len ) { window = wbuf_ . GetMaxSize ( ) - wbuf_ . buf_ptr ; if ( len <= window ) { std :: copy ( std :: begin ( pkt_buf ) + pkt -> write_ptr , std :: begin ( pkt_buf ) + pkt -> write_ptr + len , std :: begin ( wbuf_ . buf ) + wbuf_ . buf_ptr ) ; wbuf_ . buf_ptr += len ; wbuf_ . buf_size = wbuf_ . buf_ptr ; LOG_TRACE ( " " ) ; return WRITE_COMPLETE ; } else { std :: copy ( std :: begin ( pkt_buf ) + pkt -> write_ptr , std :: begin ( pkt_buf ) + pkt -> write_ptr + window , std :: begin ( wbuf_ . buf ) + wbuf_ . buf_ptr ) ; pkt -> write_ptr += window ; len -= window ; wbuf_ . buf_size = wbuf_ . GetMaxSize ( ) ; LOG_TRACE ( " " ) ; auto result = FlushWriteBuffer ( ) ; if ( result == WRITE_NOT_READY || result == WRITE_ERROR ) { return result ; } } } return WRITE_COMPLETE ; }
Writes a packet's content into the write buffer
Return false when the socket is not ready for write
Writes a packet ' s content into the write buffer Return false when the socket is not ready for write
// the packet content to write // the length of remaining content to write // window is the size of remaining space in socket's wbuf // fill the contents // calculate the remaining space in wbuf // contents fit in the window, range copy "len" bytes // Move the cursor and update size of socket buffer // contents longer than socket buffer size, fill up the socket buffer // with "window" bytes // move the packet's cursor // Now the wbuf is full // flush before write the remaining content // need to retry or close connection