diff --git a/deps/evcom/evcom.c b/deps/evcom/evcom.c index 0849f8ad0f..ed9e2b5215 100644 --- a/deps/evcom/evcom.c +++ b/deps/evcom/evcom.c @@ -306,6 +306,10 @@ stream_recv__close (evcom_stream *stream) static int stream_send__drain (evcom_stream *stream) { + if (stream->on_drain) { + stream->on_drain(stream); + } + if (!GOT_CLOSE(stream)) { stream->send_action = stream_send__wait_for_buf; return OKAY; @@ -1094,6 +1098,7 @@ evcom_stream_init (evcom_stream *stream) stream->on_connect = NULL; stream->on_timeout = NULL; stream->on_read = NULL; + stream->on_drain = NULL; stream->on_close = NULL; } diff --git a/deps/evcom/evcom.h b/deps/evcom/evcom.h index 644ec9737c..fd03a5bf05 100644 --- a/deps/evcom/evcom.h +++ b/deps/evcom/evcom.h @@ -144,6 +144,7 @@ typedef struct evcom_stream { void (*on_connect) (struct evcom_stream *); void (*on_timeout) (struct evcom_stream *); void (*on_read) (struct evcom_stream *, const void* buf, size_t len); + void (*on_drain) (struct evcom_stream *); void (*on_close) (struct evcom_stream *); void *data; } evcom_stream; diff --git a/doc/api.txt b/doc/api.txt index cdf3dc672c..fe50a816e8 100644 --- a/doc/api.txt +++ b/doc/api.txt @@ -1172,6 +1172,8 @@ socket for +tcp.Server+. |+"timeout"+ | | Emitted if the connection times out from inactivity. The +"close"+ event will be emitted immediately following this event. +|+"drain"+ | | Emitted when the write buffer becomes + empty. Can be used to throttle uploads. |+"close"+ | +had_error+ | Emitted once the connection is fully closed. The argument +had_error+ is a boolean which says if the connection diff --git a/src/node_net.cc b/src/node_net.cc index a5197d0f28..78b1718885 100644 --- a/src/node_net.cc +++ b/src/node_net.cc @@ -144,6 +144,7 @@ void Connection::Init() { stream_.on_read = Connection::on_read; stream_.on_close = Connection::on_close; stream_.on_timeout = Connection::on_timeout; + stream_.on_drain = Connection::on_drain; stream_.data = this; } @@ -469,6 +470,11 @@ void Connection::OnTimeout() { Emit("timeout", 0, NULL); } +void Connection::OnDrain() { + HandleScope scope; + Emit("drain", 0, NULL); +} + void Connection::OnEOF() { HandleScope scope; Emit("eof", 0, NULL); diff --git a/src/node_net.h b/src/node_net.h index 4b35444c34..08115c7c6a 100644 --- a/src/node_net.h +++ b/src/node_net.h @@ -82,6 +82,7 @@ class Connection : public EventEmitter { virtual void OnEOF(); virtual void OnClose(); virtual void OnTimeout(); + virtual void OnDrain(); v8::Local GetProtocol(); @@ -129,6 +130,11 @@ class Connection : public EventEmitter { connection->OnTimeout(); } + static void on_drain(evcom_stream *s) { + Connection *connection = static_cast(s->data); + connection->OnDrain(); + } + void Init(); // constructor helper. static int Resolve(eio_req *req);