Writes as many bytes as possible from the given SendBuffer chain into the write buffer and returns the number of bytes written (might be 0) (or may throw an error if the connection dies)
| 286 | // Writes as many bytes as possible from the given SendBuffer chain into the write buffer and returns the number of |
| 287 | // bytes written (might be 0) (or may throw an error if the connection dies) |
| 288 | int write(SendBuffer const* buffer, int limit) override { |
| 289 | rollRandomClose(); |
| 290 | ASSERT(limit > 0); |
| 291 | |
| 292 | int toSend = 0; |
| 293 | if (BUGGIFY && !stableConnection) { |
| 294 | toSend = std::min(limit, buffer->bytes_written - buffer->bytes_sent); |
| 295 | } else { |
| 296 | for (auto p = buffer; p; p = p->next) { |
| 297 | toSend += p->bytes_written - p->bytes_sent; |
| 298 | if (toSend >= limit) { |
| 299 | if (toSend > limit) |
| 300 | toSend = limit; |
| 301 | break; |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | ASSERT(toSend); |
| 306 | if (BUGGIFY && !stableConnection) |
| 307 | toSend = std::min(toSend, deterministicRandom()->randomInt(0, 1000)); |
| 308 | |
| 309 | if (!peer) |
| 310 | return toSend; |
| 311 | toSend = std::min(toSend, peer->availableSendBufferForPeer()); |
| 312 | ASSERT(toSend >= 0); |
| 313 | |
| 314 | int leftToSend = toSend; |
| 315 | for (auto p = buffer; p && leftToSend > 0; p = p->next) { |
| 316 | int ts = std::min(leftToSend, p->bytes_written - p->bytes_sent); |
| 317 | peer->recvBuf.insert(peer->recvBuf.end(), p->data() + p->bytes_sent, p->data() + p->bytes_sent + ts); |
| 318 | leftToSend -= ts; |
| 319 | } |
| 320 | ASSERT(leftToSend == 0); |
| 321 | peer->writtenBytes.set(peer->writtenBytes.get() + toSend); |
| 322 | return toSend; |
| 323 | } |
| 324 | |
| 325 | // Returns the network address and port of the other end of the connection. In the case of an incoming connection, |
| 326 | // this may not be an address we can connect to! |
no test coverage detected