-
Notifications
You must be signed in to change notification settings - Fork 392
Expand file tree
/
Copy pathcoder.ts
More file actions
114 lines (105 loc) · 3.05 KB
/
Copy pathcoder.ts
File metadata and controls
114 lines (105 loc) · 3.05 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
import { Message } from 'pinus-protocol';
import * as Constants from '../../util/constants';
import { getLogger } from 'pinus-logger';
import { IConnector } from '../../interfaces/IConnector';
let logger = getLogger('pinus', __filename);
let encode = function (this : IConnector , reqId : number, route : string, msg : any)
{
if (!!reqId)
{
return composeResponse(this, reqId, route, msg);
} else
{
return composePush(this, route, msg);
}
};
let decode = function (this : any , msg : any)
{
msg = Message.decode(msg.body);
let route = msg.route;
// decode use dictionary
if (!!msg.compressRoute)
{
if (!!this.connector.useDict)
{
let abbrs = this.dictionary.getAbbrs();
if (!abbrs[route])
{
logger.error('dictionary error! no abbrs for route : %s', route);
return null;
}
route = msg.route = abbrs[route];
} else
{
logger.error('fail to uncompress route code for msg: %j, server not enable dictionary.', msg);
return null;
}
}
// decode use protobuf
if (!!this.protobuf && !!this.protobuf.getProtos().client[route])
{
msg.body = this.protobuf.decode(route, msg.body);
} else if (!!this.decodeIO_protobuf && !!this.decodeIO_protobuf.check(Constants.RESERVED.CLIENT, route))
{
msg.body = this.decodeIO_protobuf.decode(route, msg.body);
} else
{
try
{
msg.body = JSON.parse(msg.body.toString('utf8'));
} catch (ex)
{
msg.body = {};
}
}
return msg;
};
let composeResponse = function (server : any, msgId : number, route : string, msgBody : any)
{
if (!msgId || !route || !msgBody)
{
return null;
}
msgBody = encodeBody(server, route, msgBody);
return Message.encode(msgId, Message.TYPE_RESPONSE, false, null, msgBody);
};
let composePush = function (server : any, route : string, msgBody : any)
{
if (!route || !msgBody)
{
return null;
}
msgBody = encodeBody(server, route, msgBody);
// encode use dictionary
let compressRoute = false;
if (!!server.dictionary)
{
let dict = server.dictionary.getDict();
if (!!server.connector.useDict && !!dict[route])
{
route = dict[route];
compressRoute = true;
}
}
return Message.encode(0, Message.TYPE_PUSH, compressRoute, route, msgBody);
};
let encodeBody = function (server : any, route : string, msgBody : any)
{
// encode use protobuf
if (!!server.protobuf && !!server.protobuf.getProtos().server[route])
{
msgBody = server.protobuf.encode(route, msgBody);
} else if (!!server.decodeIO_protobuf && !!server.decodeIO_protobuf.check(Constants.RESERVED.SERVER, route))
{
msgBody = server.decodeIO_protobuf.encode(route, msgBody);
} else
{
msgBody = new Buffer(JSON.stringify(msgBody), 'utf8');
}
return msgBody;
};
export
{
encode,
decode
};