forked from seishun/node-steam
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
111 lines (86 loc) · 2.75 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
var EventEmitter = require('events').EventEmitter;
var Steam = require('../../steam_client');
var EMsg = Steam.EMsg;
var schema = Steam.Internal;
var protoMask = 0x80000000;
function SteamGameCoordinator(steamClient, appid) {
this._client = steamClient;
this._appid = appid;
this._jobs = {};
this._currentJobID = 0;
this._client.on('message', function(header, body, callback) {
if (header.msg in handlers)
handlers[header.msg].call(this, body, callback);
}.bind(this));
this._client.on('logOnResponse', function() {
this._jobs = {};
this._currentJobID = 0;
}.bind(this));
}
require('util').inherits(SteamGameCoordinator, EventEmitter);
// Methods
SteamGameCoordinator.prototype._send = function(header, body, callback) {
if (callback) {
var sourceJobID = ++this._currentJobID;
this._jobs[sourceJobID] = callback;
}
var type = header.msg;
if (header.proto) {
header.proto.job_id_source = sourceJobID;
header = new schema.MsgGCHdrProtoBuf(header);
} else {
header.sourceJobID = sourceJobID;
header = new schema.MsgGCHdr(header);
}
this._client.send({
msg: EMsg.ClientToGC,
proto: {
routing_appid: this._appid
}
}, new schema.CMsgGCClient({
msgtype: header.proto ? type | protoMask : type,
appid: this._appid,
payload: Buffer.concat([header.toBuffer(), body])
}).toBuffer());
};
SteamGameCoordinator.prototype.send = function(header, body, callback) {
// ignore any target job ID
if (header.proto)
delete header.proto.job_id_target;
else
delete header.targetJobID;
this._send(header, body, callback);
};
// Handlers
var handlers = {};
handlers[EMsg.ClientFromGC] = function(data, jobid) {
var msg = schema.CMsgGCClient.decode(data);
if (msg.appid != this._appid)
return;
var header, sourceJobID, targetJobID;
if (msg.msgtype & protoMask) {
header = schema.MsgGCHdrProtoBuf.decode(msg.payload);
header.proto = Steam._processProto(header.proto);
sourceJobID = header.proto.job_id_source;
targetJobID = header.proto.job_id_target;
} else {
header = schema.MsgGCHdr.decode(msg.payload);
header.msg = msg.msgtype;
sourceJobID = header.sourceJobID;
targetJobID = header.targetJobID;
}
if (sourceJobID != '18446744073709551615') {
var callback = function(header, body, callback) {
if (header.proto)
header.proto.job_id_target = sourceJobID;
else
header.targetJobID = sourceJobID;
this._send(header, body, callback);
}.bind(this);
}
if (targetJobID in this._jobs)
this._jobs[targetJobID](header, msg.payload.toBuffer(), callback);
else
this.emit('message', header, msg.payload.toBuffer(), callback);
};
Steam.SteamGameCoordinator = SteamGameCoordinator;