classSocket : public FileDescriptor { public: // Bind a socket to a specified address with [bind(2)](\ref man2::bind), usually for listen/accept voidbind(const Address &address);
// Connect a socket to a specified peer address with [connect(2)](\ref man2::connect) voidconnect(const Address &address);
// Push data to stream, but only as much as available capacity allows. voidWriter::push(string data) { // If stream is closed, set error if (is_closed_ && !data.empty()) { set_error(); return; }
// Trim data to available capacity uint64_t cur_capacity = available_capacity(); if (data.size() > cur_capacity) data = data.substr(0, cur_capacity);
// Append data to buffer for (char c : data) buffer_.push_back(c); bytes_pushed_ += data.size();
debug("Writer::push({})", data); }
// Signal that the stream has reached its ending. Nothing more will be written. voidWriter::close() { is_closed_ = true; debug("Writer::close()"); }
// Has the stream been closed? boolWriter::is_closed()const { debug("Writer::is_closed() -> {}", is_closed_); return is_closed_; }
// How many bytes can be pushed to the stream right now? uint64_tWriter::available_capacity()const { uint64_t available_capacity = capacity_ - (bytes_pushed_ - bytes_popped_); debug("Writer::available_capacity() -> {}", available_capacity); return available_capacity; }
// Total number of bytes cumulatively pushed to the stream uint64_tWriter::bytes_pushed()const { debug("Writer::bytes_pushed() -> {}", bytes_pushed_); return bytes_pushed_; }
// Peek at the next bytes in the buffer -- ideally as many as possible. // It's not required to return a string_view of the *whole* buffer, but // if the peeked string_view is only one byte at a time, it will probably force // the caller to do a lot of extra work. string_view Reader::peek()const { debug("Reader::peek() called"); if (buffer_.empty()) { return {}; }
// Number of bytes currently buffered (pushed and not popped) uint64_tReader::bytes_buffered()const { debug("Reader::bytes_buffered() -> {}", bytes_pushed_ - bytes_popped_); return bytes_pushed_ - bytes_popped_; }
// Total number of bytes cumulatively popped from stream uint64_tReader::bytes_popped()const { debug("Reader::bytes_popped() -> {}", bytes_popped_); return bytes_popped_; }