Use demuxer::dispatch() to invoke wrapped handlers.

This commit is contained in:
chris 2004-05-07 07:16:30 +00:00
parent 92414c7e63
commit 0e338b7762
3 changed files with 45 additions and 33 deletions

View File

@ -143,8 +143,9 @@ public:
class fill_handler
{
public:
fill_handler(Buffer& buffer, size_t previous_size, Handler handler)
: buffer_(buffer),
fill_handler(buffered_recv_stream<Next_Layer, Buffer>& stream,
size_t previous_size, Handler handler)
: stream_(stream),
previous_size_(previous_size),
handler_(handler)
{
@ -153,12 +154,13 @@ public:
template <typename Error>
void operator()(const Error& e, size_t bytes_recvd)
{
buffer_.resize(previous_size_ + bytes_recvd);
handler_(e, bytes_recvd);
stream_.recv_buffer().resize(previous_size_ + bytes_recvd);
stream_.demuxer().dispatch(
detail::bind_handler(handler_, e, bytes_recvd));
}
private:
Buffer& buffer_;
buffered_recv_stream<Next_Layer, Buffer>& stream_;
size_t previous_size_;
Handler handler_;
};
@ -171,7 +173,7 @@ public:
buffer_.resize(buffer_.capacity());
next_layer_.async_recv(buffer_.begin() + previous_size,
buffer_.size() - previous_size,
fill_handler<Handler>(buffer_, previous_size, handler));
fill_handler<Handler>(*this, previous_size, handler));
}
/// Receive some data from the peer. Returns the number of bytes received or
@ -197,9 +199,9 @@ public:
class recv_handler
{
public:
recv_handler(Buffer& buffer, void* data, size_t max_length,
Handler handler)
: buffer_(buffer),
recv_handler(buffered_recv_stream<Next_Layer, Buffer>& stream, void* data,
size_t max_length, Handler handler)
: stream_(stream),
data_(data),
max_length_(max_length),
handler_(handler)
@ -209,25 +211,25 @@ public:
template <typename Error>
void operator()(const Error& e, size_t bytes_recvd)
{
if (e || buffer_.empty())
if (e || stream_.recv_buffer().empty())
{
size_t length = 0;
handler_(e, length);
stream_.demuxer().dispatch(detail::bind_handler(handler_, e, length));
}
else
{
using namespace std; // For memcpy.
size_t bytes_avail = buffer_.size();
size_t bytes_avail = stream_.recv_buffer().size();
size_t length = (max_length_ < bytes_avail)
? max_length_ : bytes_avail;
memcpy(data_, buffer_.begin(), length);
buffer_.pop(length);
handler_(e, length);
memcpy(data_, stream_.recv_buffer().begin(), length);
stream_.recv_buffer().pop(length);
stream_.demuxer().dispatch(detail::bind_handler(handler_, e, length));
}
}
private:
Buffer& buffer_;
buffered_recv_stream<Next_Layer, Buffer>& stream_;
void* data_;
size_t max_length_;
Handler handler_;

View File

@ -73,8 +73,8 @@ public:
/// The buffer type for this buffering layer.
typedef Buffer buffer_type;
/// Get the recv buffer used by this buffering layer.
buffer_type& recv_buffer()
/// Get the send buffer used by this buffering layer.
buffer_type& send_buffer()
{
return buffer_;
}
@ -114,8 +114,9 @@ public:
class flush_handler
{
public:
flush_handler(Buffer& buffer, Handler handler)
: buffer_(buffer),
flush_handler(buffered_send_stream<Next_Layer, Buffer>& stream,
Handler handler)
: stream_(stream),
handler_(handler)
{
}
@ -124,12 +125,13 @@ public:
void operator()(const Error& e, size_t last_bytes_sent,
size_t total_bytes_sent)
{
buffer_.pop(total_bytes_sent);
handler_(e, last_bytes_sent);
stream_.send_buffer().pop(total_bytes_sent);
stream_.demuxer().dispatch(
detail::bind_handler(handler_, e, last_bytes_sent));
}
private:
Buffer& buffer_;
buffered_send_stream<Next_Layer, Buffer>& stream_;
Handler handler_;
};
@ -164,9 +166,9 @@ public:
class send_handler
{
public:
send_handler(Buffer& buffer, const void* data, size_t length,
Handler handler)
: buffer_(buffer),
send_handler(buffered_send_stream<Next_Layer, Buffer>& stream,
const void* data, size_t length, Handler handler)
: stream_(stream),
data_(data),
length_(length),
handler_(handler)
@ -178,22 +180,24 @@ public:
{
if (e || bytes_sent == 0)
{
handler_(e, bytes_sent);
size_t length = 0;
stream_.demuxer().dispatch(detail::bind_handler(handler_, e, length));
}
else
{
using namespace std; // For memcpy.
size_t orig_size = buffer_.size();
size_t bytes_avail = buffer_.capacity() - orig_size;
size_t orig_size = stream_.send_buffer().size();
size_t bytes_avail = stream_.send_buffer().capacity() - orig_size;
size_t bytes_copied = (length_ < bytes_avail) ? length_ : bytes_avail;
buffer_.resize(orig_size + bytes_copied);
memcpy(buffer_.begin() + orig_size, data_, bytes_copied);
handler_(e, bytes_copied);
stream_.send_buffer().resize(orig_size + bytes_copied);
memcpy(stream_.send_buffer().begin() + orig_size, data_, bytes_copied);
stream_.demuxer().dispatch(
detail::bind_handler(handler_, e, bytes_copied));
}
}
private:
Buffer& buffer_;
buffered_send_stream<Next_Layer, Buffer>& stream_;
const void* data_;
size_t length_;
Handler handler_;

View File

@ -77,6 +77,12 @@ public:
return stream_impl_.recv_buffer();
}
/// Get the send buffer used by this buffering layer.
buffer_type& send_buffer()
{
return stream_impl_.next_layer().send_buffer();
}
/// Close the stream.
void close()
{