Skip to content

Commit 9c7325b

Browse files
author
Igor
committed
-
1 parent 2ce7fe5 commit 9c7325b

File tree

3 files changed

+56
-29
lines changed

3 files changed

+56
-29
lines changed

.idea/workspace.xml

+32-21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var foo;
1212

1313

1414
foo = function() {
15-
twc.gen_token(Buffer.from("lalka", 'ascii'), function (err, data) {
15+
twc.gen_token(Buffer.from("lalka", 'ascii'), 10, function (err, data) {
1616
if(!err)
1717
{
1818
//console.log(data.toString('ascii'));
@@ -58,4 +58,4 @@ benchmark();
5858

5959
function randomInt (low, high) {
6060
return Math.floor(Math.random() * (high - low) + low);
61-
}
61+
}

twc.js

+22-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var Buffer = require('buffer').Buffer;
44

55
var ClientCommands = {
66
genToken : 0x0,
7-
destroyToken : 0x1,
7+
invalidateToken : 0x1,
88
getToken : 0x2
99
};
1010

@@ -18,6 +18,7 @@ var ServerResponse = {
1818
var client = new net.Socket();
1919
exports.client = client;
2020
var tokenLength;
21+
2122
function int32ToBytes(num) {
2223
arr = new ArrayBuffer(4); // an Int32 takes 4 bytes
2324
view = new DataView(arr);
@@ -49,20 +50,35 @@ exports.get_token = function(token, handler) {
4950
requestQueue.push(handler);
5051
};
5152

52-
exports.gen_token = function(dataBuf, handler) {
53+
exports.invalidate = function(token, handler) {
5354
if(requestQueue.length > 65536)
5455
{
5556
handler(true);
5657
return;
5758
}
58-
var buff = new Buffer(dataBuf.length+1+4);
59-
dataBuf.copy(buff, 5, 0, dataBuf.length);
60-
buff.writeUInt32LE(dataBuf.length+1, 0);
61-
buff.writeUInt8(ClientCommands.genToken, 4);
59+
token += '\0';
60+
var buff = new Buffer(token.length+1+4);
61+
buff.writeUInt32LE(token.length+1, 0);
62+
buff.writeUInt8(ClientCommands.invalidateToken, 4);
63+
buff.write(token, 5, token.length, 'ascii');
6264
client.write(buff);
6365
requestQueue.push(handler);
6466
};
6567

68+
exports.gen_token = function(dataBuf, lifeTime, handler) {
69+
if(requestQueue.length > 65536)
70+
{
71+
handler(true);
72+
return;
73+
}
74+
var buff = new Buffer(dataBuf.length+1+4+4);
75+
dataBuf.copy(buff, 9, 0, dataBuf.length);
76+
buff.writeUInt32LE(dataBuf.length+1+4, 0);
77+
buff.writeUInt8(ClientCommands.genToken, 4);
78+
buff.writeUInt32LE(lifeTime, 5);
79+
client.write(buff);
80+
requestQueue.push(handler);
81+
};
6682

6783
exports.connect = function () {
6884
client.connect(10200, '127.0.0.1', function() {

0 commit comments

Comments
 (0)