dry
This commit is contained in:
@@ -65,6 +65,20 @@ const char *api_error_to_str(APIError err) {
|
||||
return "UNKNOWN";
|
||||
}
|
||||
|
||||
// Helper method to buffer data from IOVs
|
||||
void APIFrameHelper::buffer_data_from_iov_(const struct iovec *iov, int iovcnt, size_t total_write_len) {
|
||||
// Add new data to the buffer queue
|
||||
SendBuffer buffer;
|
||||
buffer.data.reserve(total_write_len);
|
||||
|
||||
for (int i = 0; i < iovcnt; i++) {
|
||||
const uint8_t *data = reinterpret_cast<uint8_t *>(iov[i].iov_base);
|
||||
buffer.data.insert(buffer.data.end(), data, data + iov[i].iov_len);
|
||||
}
|
||||
|
||||
this->tx_buf_.push_back(std::move(buffer));
|
||||
}
|
||||
|
||||
// Common implementation for writing raw data to socket
|
||||
APIError APIFrameHelper::write_raw_(const struct iovec *iov, int iovcnt) {
|
||||
// This method writes data to socket or buffers it
|
||||
@@ -98,16 +112,7 @@ APIError APIFrameHelper::write_raw_(const struct iovec *iov, int iovcnt) {
|
||||
// If there is still data in the buffer, we can't send, buffer
|
||||
// the new data and return
|
||||
if (!this->tx_buf_.empty()) {
|
||||
// Add new data to the buffer queue
|
||||
SendBuffer buffer;
|
||||
buffer.data.reserve(total_write_len);
|
||||
|
||||
for (int i = 0; i < iovcnt; i++) {
|
||||
const uint8_t *data = reinterpret_cast<uint8_t *>(iov[i].iov_base);
|
||||
buffer.data.insert(buffer.data.end(), data, data + iov[i].iov_len);
|
||||
}
|
||||
|
||||
this->tx_buf_.push_back(std::move(buffer));
|
||||
this->buffer_data_from_iov_(iov, iovcnt, total_write_len);
|
||||
return APIError::OK; // Success, data buffered
|
||||
}
|
||||
}
|
||||
@@ -118,15 +123,7 @@ APIError APIFrameHelper::write_raw_(const struct iovec *iov, int iovcnt) {
|
||||
if (sent == -1) {
|
||||
if (errno == EWOULDBLOCK || errno == EAGAIN) {
|
||||
// Socket would block, buffer the data
|
||||
SendBuffer buffer;
|
||||
buffer.data.reserve(total_write_len);
|
||||
|
||||
for (int i = 0; i < iovcnt; i++) {
|
||||
const uint8_t *data = reinterpret_cast<uint8_t *>(iov[i].iov_base);
|
||||
buffer.data.insert(buffer.data.end(), data, data + iov[i].iov_len);
|
||||
}
|
||||
|
||||
this->tx_buf_.push_back(std::move(buffer));
|
||||
this->buffer_data_from_iov_(iov, iovcnt, total_write_len);
|
||||
return APIError::OK; // Success, data buffered
|
||||
}
|
||||
// Socket error
|
||||
|
||||
@@ -165,6 +165,9 @@ class APIFrameHelper {
|
||||
|
||||
// Try to send data from the tx buffer
|
||||
APIError try_send_tx_buf_();
|
||||
|
||||
// Helper method to buffer data from IOVs
|
||||
void buffer_data_from_iov_(const struct iovec *iov, int iovcnt, size_t total_write_len);
|
||||
};
|
||||
|
||||
#ifdef USE_API_NOISE
|
||||
|
||||
Reference in New Issue
Block a user