nodejs-local-ipc
Advanced tools
+1
-1
| { | ||
| "name": "nodejs-local-ipc", | ||
| "version": "1.0.4", | ||
| "version": "1.0.5", | ||
| "description": "实现nodejs中兄弟进程的通信", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
+3
-3
| const net = require('net'); | ||
| const os = require('os'); | ||
| const { EventEmitter } = require('events'); | ||
| const { Parser } = require('./lib/protocol'); | ||
| const { FSM } = require('./lib/protocol'); | ||
| const { port, path } = require('./config'); | ||
@@ -12,3 +12,3 @@ | ||
| this.socket = null; | ||
| this.parser = new Parser({ | ||
| this.fsm = new FSM({ | ||
| cb: (packet) => { | ||
@@ -30,3 +30,3 @@ this.emit('message', packet); | ||
| this.socket = net.connect({allowHalfOpen: true, ...this.options}); | ||
| this.socket.on('data', this.parser.parse.bind(this.parser)); | ||
| this.socket.on('data', this.fsm.run.bind(this.fsm)); | ||
| this.socket.on('end', () => { | ||
@@ -33,0 +33,0 @@ // 触发end事件 |
@@ -22,3 +22,3 @@ // 开始标识符 | ||
| } | ||
| // 解析器状态 | ||
| // 状态集 | ||
| const PARSE_STATE = { | ||
@@ -31,3 +31,3 @@ PARSE_INIT: 0, | ||
| class FSM { | ||
| class StateSwitcher { | ||
| constructor(options) { | ||
@@ -111,7 +111,7 @@ this.options = options; | ||
| class Parser { | ||
| class FSM { | ||
| constructor(options) { | ||
| this.options = options; | ||
| // 状态处理机,定义了状态转移集合 | ||
| this.fsm = new FSM({cb: options.cb}); | ||
| this.stateSwitcher = new StateSwitcher({cb: options.cb}); | ||
| // 当前状态 | ||
@@ -125,3 +125,3 @@ this.state = PARSE_STATE.PARSE_INIT; | ||
| parse(data) { | ||
| run(data) { | ||
| // 没有数据或者解析结束了直接返回 | ||
@@ -136,3 +136,3 @@ if (this.state === this.endState || !data || !data.length) { | ||
| // 执行状态处理函数,返回[下一个状态, 剩下的数据] | ||
| const result = this.fsm[this.state](this.buffer); | ||
| const result = this.stateSwitcher[this.state](this.buffer); | ||
| // 如果下一个状态是NEED_MORE_DATA则说明需要更多的数据才能继续解析,并保持当前状态 | ||
@@ -150,5 +150,5 @@ if (result[0] === NEED_MORE_DATA) { | ||
| module.exports = { | ||
| Parser, | ||
| FSM, | ||
| packet, | ||
| seq, | ||
| }; |
+3
-3
@@ -5,3 +5,3 @@ const fs = require('fs'); | ||
| const os = require('os'); | ||
| const { Parser } = require('./lib/protocol'); | ||
| const { FSM } = require('./lib/protocol'); | ||
| const { port, path } = require('./config'); | ||
@@ -43,3 +43,3 @@ | ||
| typeof connectionListener === 'function' && connectionListener(_client); | ||
| const parser = new Parser({ | ||
| const fsm = new FSM({ | ||
| cb: function(packet) { | ||
@@ -49,3 +49,3 @@ _client.emit('message', packet); | ||
| }) | ||
| client.on('data', parser.parse.bind(parser)); | ||
| client.on('data', fsm.run.bind(fsm)); | ||
| client.on('end', () => { | ||
@@ -52,0 +52,0 @@ // 触发end事件 |
| const { Client, packet, seq } = require('../../'); | ||
| const client = new Client({port: 80, path: '/tmp/unix.sock'}) | ||
| client.end(packet('hello', seq())); | ||
| client.send(packet('hello', seq())); | ||
| client.send(packet('hello', seq())); | ||
| client.on('message', function(res) { | ||
| console.log('receive', res); | ||
| }) |
@@ -7,2 +7,3 @@ const { Server, packet, seq } = require('../../'); | ||
| client.send(packet('world', data.seq)); | ||
| client.send(packet('world', data.seq)); | ||
| }); | ||
@@ -9,0 +10,0 @@ client.on('end', (data) => { |
14049
0.57%360
0.56%