Send some raw data (a byte-slice) to the host stream. Raw data can either be sent straight to the host stream, or to our TX buffer, if the former fails.
(&mut self, buf: &[u8])
| 595 | /// former fails. |
| 596 | /// |
| 597 | fn send_bytes(&mut self, buf: &[u8]) -> Result<()> { |
| 598 | // If there is data in the TX buffer, that means we're already registered for EPOLLOUT |
| 599 | // events on the underlying stream. Therefore, there's no point in attempting a write |
| 600 | // at this point. `self.notify()` will get called when EPOLLOUT arrives, and it will |
| 601 | // attempt to drain the TX buffer then. |
| 602 | if !self.tx_buf.is_empty() { |
| 603 | return self.tx_buf.push(buf); |
| 604 | } |
| 605 | |
| 606 | // The TX buffer is empty, so we can try to write straight to the host stream. |
| 607 | let written = match self.stream.write(buf) { |
| 608 | Ok(cnt) => cnt, |
| 609 | Err(e) => { |
| 610 | // Absorb any would-block errors, since we can always try again later. |
| 611 | if e.kind() == ErrorKind::WouldBlock { |
| 612 | 0 |
| 613 | } else { |
| 614 | // We don't know how to handle any other write error, so we'll send it up |
| 615 | // the call chain. |
| 616 | return Err(Error::StreamWrite(e)); |
| 617 | } |
| 618 | } |
| 619 | }; |
| 620 | // Move the "forwarded bytes" counter ahead by how much we were able to send out. |
| 621 | self.fwd_cnt += Wrapping(written as u32); |
| 622 | |
| 623 | // If we couldn't write the whole slice, we'll need to push the remaining data to our |
| 624 | // buffer. |
| 625 | if written < buf.len() { |
| 626 | self.tx_buf.push(&buf[written..])?; |
| 627 | } |
| 628 | |
| 629 | Ok(()) |
| 630 | } |
| 631 | |
| 632 | /// Check if the credit information the peer has last received from us is outdated. |
| 633 | /// |