Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remoteAddress/Port/Family and setTimeout feature #15

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ Emitted when the writable side is fully closed.

Emitted when the readable side is fully closed.

#### `connection.on('timeout')`

Emitted when the timeout it's reached. Is only a notification, you should end the connection.

#### `connection.close([callback])`

Closes the connection.
Expand Down Expand Up @@ -141,6 +145,22 @@ The callback is called with `callback(err, buffers, lengths)`.

End the writable side of the connection.

#### `connection.setTimeout(millis, [callback])`

Set timeout on the connection. Optionally you can provide an callback. If millis is setted on 0, then it disabled.

#### `connection.remoteAddress`

The IP address of the connection. For example, '172.217.28.163'.

#### `connection.remoteFamily`

The IP family of the connection. Before the event "connect" or "connection" it's a empty string and after it's always "IPv4".

#### `connection.remotePort`

The remote port. For example, 63750.

## License

MIT
9 changes: 9 additions & 0 deletions examples/wait.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const turbo = require('../')

const server = turbo.createServer(function (socket) {
console.log(socket.remoteFamily, socket.remoteAddress, socket.remotePort)

socket.close()
})

server.listen(8080)
19 changes: 19 additions & 0 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class Connection extends events.EventEmitter {
this.finished = false
this.ended = false
this.allowHalfOpen = false
this.remoteFamily = ''
this.remoteAddress = ''
this.remotePort = 0
this.writable = false
this.readable = false

Expand All @@ -22,6 +25,7 @@ class Connection extends events.EventEmitter {
this._handle = Buffer.alloc(binding.sizeof_turbo_net_tcp_t)
this._reads = new RequestQueue(8, 0)
this._writes = new RequestQueue(16, binding.sizeof_uv_write_t)
this._timeoutId = false

this._finishing = []
this._closing = []
Expand Down Expand Up @@ -56,6 +60,8 @@ class Connection extends events.EventEmitter {
}

_onclose () {
clearTimeout(this._timeoutId)

if (this._server) unordered.remove(this._server.connections, this)
this.closed = true
this._closing = callAll(this._closing, null)
Expand Down Expand Up @@ -91,6 +97,10 @@ class Connection extends events.EventEmitter {
return
}

this.remoteFamily = 'IPv4'
this.remoteAddress = binding.turbo_net_tcp_remote_address(this._handle)
this.remotePort = binding.turbo_net_tcp_remote_port(this._handle)

this.readable = true
this.writable = true
if (this._queued) this._unqueue()
Expand Down Expand Up @@ -196,6 +206,15 @@ class Connection extends events.EventEmitter {
binding.turbo_net_tcp_write(this._handle, writing.handle, writing.buffer, len)
}

setTimeout (millis, cb) {
clearTimeout(this._timeoutId)

if (millis > 0) {
this._timeoutId = setTimeout(this.emit.bind(this), millis, 'timeout')
if (cb) this.once('timeout', cb)
} else if (cb) this.removeListener('timeout', cb)
}

close (cb) {
if (!cb) cb = noop
if (this.closed) return process.nextTick(cb)
Expand Down
Loading