Browse Source

Add 'drain' event to tcp.Connection

v0.7.4-release
Ryan Dahl 15 years ago
parent
commit
2ca788e6e2
  1. 5
      deps/evcom/evcom.c
  2. 1
      deps/evcom/evcom.h
  3. 2
      doc/api.txt
  4. 6
      src/node_net.cc
  5. 6
      src/node_net.h

5
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;
}

1
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;

2
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

6
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);

6
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<v8::Object> GetProtocol();
@ -129,6 +130,11 @@ class Connection : public EventEmitter {
connection->OnTimeout();
}
static void on_drain(evcom_stream *s) {
Connection *connection = static_cast<Connection*>(s->data);
connection->OnDrain();
}
void Init(); // constructor helper.
static int Resolve(eio_req *req);

Loading…
Cancel
Save