This repository was archived by the owner on Sep 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathhandshake.js
More file actions
135 lines (114 loc) · 3.48 KB
/
Copy pathhandshake.js
File metadata and controls
135 lines (114 loc) · 3.48 KB
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
'use strict';
var pomelo = require('../../pomelo');
var Package = require('pomelo-protocol').Package;
var CODE_OK = 200;
var CODE_USE_ERROR = 500;
var CODE_OLD_CLIENT = 501;
/**
* Process the handshake request.
*
* @param {Object} opts option parameters
* opts.handshake(msg, cb(err, resp)) handshake callback. msg is the handshake message from client.
* opts.hearbeat heartbeat interval (level?)
* opts.version required client level
*/
var Command = function(opts) {
opts = opts || {};
this.userHandshake = opts.handshake;
if(opts.heartbeat) {
this.heartbeatSec = opts.heartbeat;
this.heartbeat = opts.heartbeat * 1000;
}
if(opts.timeout){
this.timeout = opts.timeout;
}
this.checkClient = opts.checkClient;
this.useDict = opts.useDict;
this.useProtobuf = opts.useProtobuf;
this.useCrypto = opts.useCrypto;
};
module.exports = Command;
Command.prototype.handle = function(socket, msg) {
if(typeof this.checkClient === 'function') {
if(!msg || !msg.sys || !this.checkClient(msg.sys.type, msg.sys.version)) {
processError(socket, CODE_OLD_CLIENT);
return;
}
}
var opts = {
heartbeat : setupHeartbeat(this)
};
if(this.timeout){
opts.timeout = this.timeout;
}
if(this.useDict) {
var dictVersion = pomelo.app.components.__dictionary__.getVersion();
if(!msg.sys.dictVersion || msg.sys.dictVersion !== dictVersion){
// may be deprecated in future
opts.dict = pomelo.app.components.__dictionary__.getDict();
opts.routeToCode = pomelo.app.components.__dictionary__.getDict();
opts.codeToRoute = pomelo.app.components.__dictionary__.getAbbrs();
opts.dictVersion = dictVersion;
}
opts.useDict = true;
}
if(this.useProtobuf) {
var protoVersion = pomelo.app.components.__protobuf__.getVersion();
if(!msg.sys.protoVersion || msg.sys.protoVersion !== protoVersion){
opts.protos = pomelo.app.components.__protobuf__.getProtos();
}
opts.useProto = true;
}
if(!!pomelo.app.components.__decodeIO__protobuf__) {
if(!!this.useProtobuf) {
throw new Error('protobuf can not be both used in the same project.');
}
var version = pomelo.app.components.__decodeIO__protobuf__.getVersion();
if(!msg.sys.protoVersion || msg.sys.protoVersion < version) {
opts.protos = pomelo.app.components.__decodeIO__protobuf__.getProtos();
}
opts.useProto = true;
}
if(this.useCrypto) {
pomelo.app.components.__connector__.setPubKey(socket.id, msg.sys.rsa);
}
if(typeof this.userHandshake === 'function') {
this.userHandshake(msg, function(err, resp) {
if(err) {
process.nextTick(function() {
processError(socket, CODE_USE_ERROR);
});
return;
}
process.nextTick(function() {
response(socket, opts, resp);
});
});
return;
}
process.nextTick(function() {
response(socket, opts);
});
};
var setupHeartbeat = function(self) {
return self.heartbeatSec;
};
var response = function(socket, sys, resp) {
var res = {
code: CODE_OK,
sys: sys
};
if(resp) {
res.user = resp;
}
socket.handshakeResponse(Package.encode(Package.TYPE_HANDSHAKE, new Buffer(JSON.stringify(res))));
};
var processError = function(socket, code) {
var res = {
code: code
};
socket.sendForce(Package.encode(Package.TYPE_HANDSHAKE, new Buffer(JSON.stringify(res))));
process.nextTick(function() {
socket.disconnect();
});
};