turbo-net

Low level TCP library for Node.js

  • 所有者: mafintosh/turbo-net
  • 平台:
  • 許可證: MIT License
  • 分類:
  • 主題:
  • 喜歡:
    0
      比較:

Github星跟蹤圖

turbo-net

Low level TCP library for Node.js

npm install turbo-net

build status
Build status

Usage

const turbo = require('turbo-net')

// Echo server that allocates constant memory

const server = turbo.createServer(function (socket) {
  socket.read(Buffer.alloc(32 * 1024), function onread (err, buf, read) {
    if (err) throw err
    socket.write(buf, read, function (err) {
      if (err) throw err
      socket.read(buf, onread)
    })
  })
})

server.listen(8080, function () {
  const socket = turbo.connect(8080)

  socket.read(Buffer.alloc(32), function (err, buf, read) {
    if (err) throw err
    console.log(buf.toString('utf-8', 0, read))
  })
  socket.write(Buffer.from('hello world\n'))
})

Performance

Running the echo server examples in ./examples I get the following throughput on my laptop

  • echo-classic: 1.3GB/s at ~100MB of ram
  • echo-turbo: 3.4GB/s at ~35MB of ram

API

server = turbo.createServer([options], [onsocket])

Create a new TCP server. Options include:

{
  allowHalfOpen: false // set to true to allow half open TCP connections
}

server.on('connection', connection)

Emitted when a new connection is established.

server.on('listening')

Emitted when the server is listening.

server.connections

Unordered array containing the current active connections

server.listen(port, [address], [onlistening])

Listen on a port.

server.address()

Similar to net.Server.address. Useful if you are listening on port 0,
to find out which port was picked.

server.close([onclose])

Close the server.

connection = turbo.connect(port, host, [options])

Connect to a TCP server. Options include:

{
  allowHalfOpen: false // set to true to allow half open TCP connections
}

connection.on('connect')

Emitted when a client connection is fully connected.

connection.on('error', err)

Emitted when a client fails to connect.

connection.on('close')

Emitted a connection is fully closed. No other events will be emitted after.

connection.on('finish')

Emitted when the writable side is fully closed.

connection.on('end')

Emitted when the readable side is fully closed.

connection.close([callback])

Closes the connection.

connection.read(buffer, callback)

Read data from the connection. Data will be read into the buffer you pass.

The callback is called with callback(err, buffer, bytesRead).

If bytesRead is 0, then the readable side of the connection has ended.

connection.write(buffer, [length], [callback])

Write data to the connection. Optionally you specify how many bytes in the
buffer you want to write.

The callback is called with callback(err, buffer, length).

connection.writev(buffers, [lengths], [callback])

Write more than one buffer at once. Optionally you can specify how many bytes in
each buffer you want to write.

The callback is called with callback(err, buffers, lengths).

connection.end([callback])

End the writable side of the connection.

License

MIT

主要指標

概覽
名稱與所有者mafintosh/turbo-net
主編程語言JavaScript
編程語言Python (語言數: 3)
平台
許可證MIT License
所有者活动
創建於2017-02-09 15:28:35
推送於2019-09-13 09:11:30
最后一次提交2018-03-19 19:01:39
發布數11
最新版本名稱v1.4.0 (發布於 2018-03-18 19:36:31)
第一版名稱v0.0.1 (發布於 2017-02-09 16:32:08)
用户参与
星數600
關注者數21
派生數39
提交數37
已啟用問題?
問題數12
打開的問題數9
拉請求數5
打開的拉請求數5
關閉的拉請求數1
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?