-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
137 lines (120 loc) · 3.68 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const net = require('net');
const { isObject, extend, isString, isFunction, parseServerString, parseClientString } = require('./util');
function Rpc() {
if(!(this instanceof Rpc != Rpc)) {
return new Rpc();
}
this.registers = {};
this.clientEvent = {}
}
Rpc.prototype.listen = function(port, callback) {
const server = net.createServer((socket) => {
this.socket = socket;
socket.on('data', handleConnectionData(this))
})
this.server = server;
server.listen(port, callback);
}
Rpc.prototype.register = function(moduleName, module) {
if(!isObject(module)) {
console.log('module only support object');
return;
}
if(this.registers[moduleName]) {
extend(module, this.registers[moduleName]);
}
this.registers[moduleName] = module;
}
Rpc.prototype.connect = function(port, host, callback) {
if(!callback) {
callback = host;
host = 'localhost';
}
const connection = net.createConnection(port, host);
connection.setKeepAlive(true);
this.connection = connection;
connection.on('data', handleResultData(this));
callback(this, connection);
}
Rpc.prototype.call = function() {
// client 端
const args = [...arguments];
const callArgs = args.slice(0, args.length - 1);
this.clientEvent[args[0]] = args.pop();
this.connection.write(JSON.stringify(callArgs), 'utf8', (err) => {
if(err){
console.log('call function error');
}
})
}
Rpc.connect = function() {
const rpc = new Rpc();
return rpc.connect.apply(rpc, arguments);
}
function handleResultData(rpc) {
return function(data) {
const { args, multi } = parseClientString(data.toString())
if(!multi) {
handleResult(rpc, args)
} else {
args.forEach((item, index) => {
handleResult(rpc, item)
})
}
}
}
function handleResult(rpc, result) {
rpc.clientEvent[result.moduleName](result.result);
}
function handleConnectionData(rpc) {
return function(data) {
// Node底层会存在批量写的过程,所以需要对传过来的data进行预处理
const { args, multi } = parseServerString(data.toString())
if(!multi) {
handleCall(rpc, args)
} else {
args.forEach((item, index) => {
handleCall(rpc, item)
})
}
}
}
function handleCall(rpc, args) {
const moduleName = args[0];
const callArgs = args.slice(1, args.length);
const regx = /([^\.\[\]])+/g
if(!isString(moduleName)) {
// 出错处理 call函数第一个参数为调用对象名 最后一个参数是返回值
console.log('call函数第一个参数为调用对象名 最后一个参数是返回值');
return;
}
const match = moduleName.match(regx);
let length = match.length;
if(match && length) {
let target;
let index = 0;
while(index != length) {
target = target ? target[match[index]] : rpc.registers[match[index]]
if(target) {
index++;
} else {
// TODO 不存在该函数调用
console.log('rpc不存在该方法');
return;
}
}
// 调用该方法
target.call(null, ...callArgs, (res) => {
const data = {};
data.moduleName = moduleName;
data.result = res;
rpc.socket.write(JSON.stringify(data), 'utf8', (err) => {
if(err) {
console.log('回调执行失败');
}
});
});
}
}
Rpc.listen = Rpc.listen;
module.exports = Rpc;