Skip to content
Snippets Groups Projects
Commit e9d069bb authored by Nicolas Pope's avatar Nicolas Pope
Browse files

#18 Increase msgpack buffer if needed

parent 9da2399c
No related branches found
No related tags found
Loading
...@@ -288,21 +288,24 @@ void Peer::data() { ...@@ -288,21 +288,24 @@ void Peer::data() {
int rc = 0; int rc = 0;
// Only need to lock and reserve buffer if there isn't enough // Only need to lock and reserve buffer if there isn't enough
if (recv_buf_.buffer_capacity() < kMaxMessage) { if (recv_buf_.buffer_capacity() < recv_buf_max_) {
UNIQUE_LOCK(recv_mtx_, lk); UNIQUE_LOCK(recv_mtx_, lk);
recv_buf_.reserve_buffer(kMaxMessage); recv_buf_.reserve_buffer(recv_buf_max_);
} }
int cap = static_cast<int>(recv_buf_.buffer_capacity()); size_t cap = recv_buf_.buffer_capacity();
try { try {
rc = sock_->recv(recv_buf_.buffer(), recv_buf_.buffer_capacity()); rc = sock_->recv(recv_buf_.buffer(), recv_buf_.buffer_capacity());
if (rc >= cap - 1) { if (rc >= static_cast<int>(cap - 1)) {
net_->_notifyError(this, Error::kBufferSize, "Too much data received"); net_->_notifyError(this, Error::kBufferSize, "Too much data received");
// TODO(Nick): Increase the buffer size next time // Increase buffer size
if (recv_buf_max_ < kMaxMessage) {
recv_buf_max_ += 512 * 1024;
}
} }
if (cap < (kMaxMessage / 10)) { if (cap < (recv_buf_max_ / 10)) {
net_->_notifyError(this, Error::kBufferSize, "Buffer is at capacity"); net_->_notifyError(this, Error::kBufferSize, "Buffer is at capacity");
} }
} catch (std::exception& ex) { } catch (std::exception& ex) {
......
...@@ -213,7 +213,8 @@ class Peer { ...@@ -213,7 +213,8 @@ class Peer {
void data(); void data();
public: public:
static const int kMaxMessage = 2*1024*1024; // 10Mb currently static const int kMaxMessage = 4*1024*1024; // 4Mb currently
static const int kDefaultMessage = 512*1024; // 0.5Mb currently
private: // Functions private: // Functions
bool socketError(); // Process one error from socket bool socketError(); // Process one error from socket
...@@ -266,6 +267,7 @@ class Peer { ...@@ -266,6 +267,7 @@ class Peer {
std::atomic_flag recv_checked_ = ATOMIC_FLAG_INIT; std::atomic_flag recv_checked_ = ATOMIC_FLAG_INIT;
msgpack::unpacker recv_buf_; msgpack::unpacker recv_buf_;
size_t recv_buf_max_ = kDefaultMessage;
MUTEX recv_mtx_; MUTEX recv_mtx_;
// Send buffers // Send buffers
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment