src/wire/libevent_socket.cpp
WriteState LibeventSocket::BufferWriteBytesHeader(OutputPacket *pkt) {
  // If we should not write
  if (pkt->skip_header_write) {
    return WRITE_COMPLETE;
  }

  size_t len = pkt->len;
  unsigned char type = static_cast<unsigned char>(pkt->msg_type);
  int len_nb;  // length in network byte order

  // check if we have enough space in the buffer
  if (wbuf_.GetMaxSize() - wbuf_.buf_ptr < 1 + sizeof(int32_t)) {
    // buffer needs to be flushed before adding header
    auto result = FlushWriteBuffer();
    if (result == WRITE_NOT_READY || result == WRITE_ERROR) {
      // Socket is not ready for write
      return result;
    }
  }

  // assuming wbuf is now large enough to fit type and size fields in one go
  if (type != 0) {
    // type shouldn't be ignored
    wbuf_.buf[wbuf_.buf_ptr++] = type;
  }

  // make len include its field size as well
  len_nb = htonl(len + sizeof(int32_t));

  // append the bytes of this integer in network-byte order
  std::copy(reinterpret_cast<uchar *>(&len_nb),
            reinterpret_cast<uchar *>(&len_nb) + 4,
            std::begin(wbuf_.buf) + wbuf_.buf_ptr);

  // move the write buffer pointer and update size of the socket buffer
  wbuf_.buf_ptr += sizeof(int32_t);
  wbuf_.buf_size = wbuf_.buf_ptr;

  // Header is written to socket buf. No need to write it in the future
  pkt->skip_header_write = true;
  return WRITE_COMPLETE;
}
// Writes a packet's header (type, size) into the write buffer.
// Return false when the socket is not ready for write
Writes a packet's header (type, size) into the write buffer.
Return false when the socket is not ready for write
Writes a packet ' s header ( type size ) into the write buffer . Return false when the socket is not ready for write
WriteState LibeventSocket::BufferWriteBytesHeader(OutputPacket *pkt) {
  if (pkt->skip_header_write) {
    return WRITE_COMPLETE;
  }
  size_t len = pkt->len;
  unsigned char type = static_cast<unsigned char>(pkt->msg_type);
  int len_nb;  
  if (wbuf_.GetMaxSize() - wbuf_.buf_ptr < 1 + sizeof(int32_t)) {
    auto result = FlushWriteBuffer();
    if (result == WRITE_NOT_READY || result == WRITE_ERROR) {
      return result;
    }
  }
  if (type != 0) {
    wbuf_.buf[wbuf_.buf_ptr++] = type;
  }
  len_nb = htonl(len + sizeof(int32_t));
  std::copy(reinterpret_cast<uchar *>(&len_nb),
            reinterpret_cast<uchar *>(&len_nb) + 4,
            std::begin(wbuf_.buf) + wbuf_.buf_ptr);
  wbuf_.buf_ptr += sizeof(int32_t);
  wbuf_.buf_size = wbuf_.buf_ptr;
  pkt->skip_header_write = true;
  return WRITE_COMPLETE;
}
WriteState LibeventSocket :: BufferWriteBytesHeader ( OutputPacket * pkt ) { if ( pkt -> skip_header_write ) { return WRITE_COMPLETE ; } size_t len = pkt -> len ; unsigned char type = static_cast < unsigned char > ( pkt -> msg_type ) ; int len_nb ; if ( wbuf_ . GetMaxSize ( ) - wbuf_ . buf_ptr < 1 + sizeof ( int32_t ) ) { auto result = FlushWriteBuffer ( ) ; if ( result == WRITE_NOT_READY || result == WRITE_ERROR ) { return result ; } } if ( type != 0 ) { wbuf_ . buf [ wbuf_ . buf_ptr ++ ] = type ; } len_nb = htonl ( len + sizeof ( int32_t ) ) ; std :: copy ( reinterpret_cast < uchar * > ( & len_nb ) , reinterpret_cast < uchar * > ( & len_nb ) + 4 , std :: begin ( wbuf_ . buf ) + wbuf_ . buf_ptr ) ; wbuf_ . buf_ptr += sizeof ( int32_t ) ; wbuf_ . buf_size = wbuf_ . buf_ptr ; pkt -> skip_header_write = true ; return WRITE_COMPLETE ; }
Writes a packet's header (type, size) into the write buffer.
Writes a packet ' s header ( type size ) into the write buffer .
// If we should not write // length in network byte order // check if we have enough space in the buffer // buffer needs to be flushed before adding header // Socket is not ready for write // assuming wbuf is now large enough to fit type and size fields in one go // type shouldn't be ignored // make len include its field size as well // append the bytes of this integer in network-byte order // move the write buffer pointer and update size of the socket buffer // Header is written to socket buf. No need to write it in the future