This commit is contained in:
you21979
2017-07-10 20:09:32 +09:00
parent 62d4cf636d
commit d8e59aa7af
3 changed files with 76 additions and 68 deletions

View File

@@ -1,56 +1,7 @@
'use strict'
const EventEmitter = require('events').EventEmitter;
const tls = require('tls');
const net = require('net');
const util = require('./util');
class MessageParser{
constructor(callback){
this.buffer = ''
this.callback = callback
}
run(chunk){
this.buffer += chunk
while(true){
const res = util.recursiveParser(0, this.buffer, this.callback)
this.buffer = res.buffer
if(res.code === 0){
break;
}
}
}
}
const getSocket = (protocol, options) => {
switch(protocol){
case 'tcp':
return new net.Socket();
case 'tls':
case 'ssl':
return new tls.TLSSocket(options);
}
throw new Error('unknown protocol')
}
const initSocket = (self, protocol, options) => {
const conn = getSocket(protocol, options);
conn.setEncoding('utf8')
conn.setKeepAlive(true, 0)
conn.setNoDelay(true)
conn.on('connect', () => {
self.onConnect()
})
conn.on('close', () => {
self.onClose()
})
conn.on('data', (chunk) => {
self.onRecv(chunk)
})
conn.on('end', () => {
self.onEnd()
})
return conn
}
const EventEmitter = require('events').EventEmitter
const util = require('./util')
const initSocket = require('./init_socket')
class Client{
constructor(port, host, protocol = 'tcp', options = void 0){
@@ -60,9 +11,9 @@ class Client{
this.callback_message_queue = {}
this.subscribe = new EventEmitter()
this.conn = initSocket(this, protocol, options)
this.mp = new MessageParser((body, n) => {
this.mp = new util.MessageParser((body, n) => {
this.onMessage(body, n)
});
})
}
connect(){
@@ -74,8 +25,8 @@ class Client{
}
close(){
this.conn.end();
this.conn.destroy();
this.conn.end()
this.conn.destroy()
}
request(method, params){
@@ -96,6 +47,21 @@ class Client{
}else{
callback(null, msg.result)
}
}else{
; // can't get callback
}
}
onMessage(body, n){
const msg = JSON.parse(body)
if(msg instanceof Array){
; // don't support batch request
} else {
if(msg.id !== void 0){
this.response(msg)
}else{
this.subscribe.emit(msg.method, msg.params)
}
}
}
@@ -116,18 +82,6 @@ class Client{
onEnd(){
}
onMessage(body, n){
const msg = JSON.parse(body)
if(msg instanceof Array){
; // don't support batch request
} else {
if(msg.id !== void 0){
this.response(msg)
}else{
this.subscribe.emit(msg.method, msg.params)
}
}
}
}
module.exports = Client

36
lib/init_socket.js Normal file
View File

@@ -0,0 +1,36 @@
'use strict'
const tls = require('tls');
const net = require('net');
const getSocket = (protocol, options) => {
switch(protocol){
case 'tcp':
return new net.Socket();
case 'tls':
case 'ssl':
return new tls.TLSSocket(options);
}
throw new Error('unknown protocol')
}
const initSocket = (self, protocol, options) => {
const conn = getSocket(protocol, options);
conn.setEncoding('utf8')
conn.setKeepAlive(true, 0)
conn.setNoDelay(true)
conn.on('connect', () => {
self.onConnect()
})
conn.on('close', () => {
self.onClose()
})
conn.on('data', (chunk) => {
self.onRecv(chunk)
})
conn.on('end', () => {
self.onEnd()
})
return conn
}
module.exports = initSocket

View File

@@ -34,3 +34,21 @@ const createPromiseResult = exports.createPromiseResult = (resolve, reject) => {
}
}
class MessageParser{
constructor(callback){
this.buffer = ''
this.callback = callback
}
run(chunk){
this.buffer += chunk
while(true){
const res = recursiveParser(0, this.buffer, this.callback)
this.buffer = res.buffer
if(res.code === 0){
break;
}
}
}
}
exports.MessageParser = MessageParser