Skip to content

Commit

Permalink
Merge pull request #4 from LuKks/master
Browse files Browse the repository at this point in the history
remoteAddress/Port/Family and setTimeout feature
  • Loading branch information
Ardalan Amini authored Jan 16, 2019
2 parents 0a68993 + 57a8981 commit d1ee74e
Show file tree
Hide file tree
Showing 5 changed files with 2,092 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,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 @@ -142,6 +146,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 @@ -57,6 +61,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 @@ -92,6 +98,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 @@ -198,6 +208,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

0 comments on commit d1ee74e

Please sign in to comment.