-
Notifications
You must be signed in to change notification settings - Fork 62
/
peer.js
82 lines (71 loc) · 2.12 KB
/
peer.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
var assert = require('assert');
var net = require('net');
var protocol = require('./protocol');
function Peer(id, port, localId) {
this.buf = '';
if (typeof id === 'number') {
this.id = id;
this.remotePort = port;
this.localId = localId;
this.socket = net.connect(port, this.onConnected_.bind(this));
} else if (typeof id === 'object') {
this.socket = id;
this.localId = port;
this.socket.setEncoding('utf8');
this.socket.on('data', this.onData_.bind(this));
this.socket.on('end', this.onClose_.bind(this));
} else {
assert(false);
}
}
Peer.prototype.send = function(msg) {
var data = JSON.stringify(msg);
// console.log(this.localId + ' >> [' + this.id + ']', data);
this.socket.write('' + data.length);
this.socket.write('\n');
this.socket.write(data);
}
Peer.prototype.getId = function() {
return this.id;
}
Peer.prototype.setMessageCb = function(cb) {
assert(typeof cb === 'function');
this.messageCb = cb;
}
Peer.prototype.close = function() {
this.socket.end();
}
Peer.prototype.onConnected_ = function() {
console.log('peer ' + this.id + ' connected with ' + this.localId);
this.socket.setEncoding('utf8');
this.socket.on('data', this.onData_.bind(this));
this.send(protocol.initMessage(this.localId));
}
Peer.prototype.onData_ = function(data) {
this.buf += data;
var start = 0;
var pos = this.buf.indexOf('\n', start);
var end = 0;
while (pos !== -1) {
var len = parseInt(this.buf.substring(start, pos));
if (this.buf.length - pos - 1 < len) {
break;
}
var body = this.buf.substr(pos + 1, len);
end = pos + 1 + len;
var msg = JSON.parse(body);
if (msg.type === protocol.MessageType.Init) {
this.id = msg.id;
console.log('peer ' + this.id + ' accpeted on ' + this.localId);
}
// console.log(this.localId + ' << ' + this.id, body);
this.messageCb(this, msg);
start = pos + 1 + len;
pos = this.buf.indexOf('\n', start);
}
this.buf = this.buf.substring(end);
}
Peer.prototype.onClose_ = function() {
console.log('connection ' + this.id + ' -> ' + this.localId + ' closed');
}
module.exports = Peer;