Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 32e8795

Browse files
committed
Merge pull request mqttjs#124 from adamvr/fix-121
Connection as a Writable stream
2 parents 0d818e5 + 9b94816 commit 32e8795

File tree

5 files changed

+70
-86
lines changed

5 files changed

+70
-86
lines changed

lib/client.js

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@ function MqttClient(streamBuilder, options) {
5555

5656
this._setupStream();
5757

58-
// MqttConnection
59-
this.conn = new Connection(this.stream);
60-
6158
// Ping timer, setup in _setupPingTimer
6259
this.pingTimer = null;
6360
// Is the client connected?
@@ -83,9 +80,6 @@ function MqttClient(streamBuilder, options) {
8380
pubrel: {}
8481
};
8582

86-
// Echo connection errors
87-
this.conn.on('error', this.emit.bind(this, 'error'));
88-
8983
// Mark connected on connect
9084
this.on('connect', function() {
9185
this.connected = true;
@@ -96,11 +90,6 @@ function MqttClient(streamBuilder, options) {
9690
this.connected = false;
9791
});
9892

99-
// Handle connack
100-
this.conn.on('connack', function (packet) {
101-
that._handleConnack(packet);
102-
});
103-
10493
// Setup ping timer
10594
this.on('connect', this._setupPingTimer);
10695

@@ -119,27 +108,6 @@ function MqttClient(streamBuilder, options) {
119108
that.queue = [];
120109
});
121110

122-
// Handle incoming publish
123-
this.conn.on('publish', function (packet) {
124-
that._handlePublish(packet);
125-
});
126-
127-
// one single handleAck function
128-
var handleAck = function (packet) {
129-
that._handleAck(packet);
130-
};
131-
132-
// Handle incoming acks
133-
var acks = ['puback', 'pubrec', 'pubcomp', 'suback', 'unsuback'];
134-
135-
acks.forEach(function (event) {
136-
that.conn.on(event, handleAck);
137-
});
138-
139-
// Handle outgoing acks
140-
this.conn.on('pubrel', function (packet) {
141-
that._handlePubrel(packet);
142-
});
143111

144112
// Clear ping timer
145113
this.on('close', function () {
@@ -173,6 +141,9 @@ MqttClient.prototype._setupStream = function() {
173141

174142
this.stream = this.streamBuilder();
175143

144+
// MqttConnection
145+
this.conn = this.stream.pipe(new Connection());
146+
176147
// Suppress connection errors
177148
this.stream.on('error', nop);
178149

@@ -187,6 +158,36 @@ MqttClient.prototype._setupStream = function() {
187158
this.stream.on('secureConnect', function () {
188159
that.conn.connect(that.options);
189160
});
161+
162+
// Handle incoming publish
163+
this.conn.on('publish', function (packet) {
164+
that._handlePublish(packet);
165+
});
166+
167+
// one single handleAck function
168+
var handleAck = function (packet) {
169+
that._handleAck(packet);
170+
};
171+
172+
// Handle incoming acks
173+
var acks = ['puback', 'pubrec', 'pubcomp', 'suback', 'unsuback'];
174+
175+
acks.forEach(function (event) {
176+
that.conn.on(event, handleAck);
177+
});
178+
179+
// Handle outgoing acks
180+
this.conn.on('pubrel', function (packet) {
181+
that._handlePubrel(packet);
182+
});
183+
184+
// Handle connack
185+
this.conn.on('connack', function (packet) {
186+
that._handleConnack(packet);
187+
});
188+
189+
// Echo connection errors
190+
this.conn.on('error', this.emit.bind(this, 'error'));
190191
};
191192

192193
/**
@@ -361,9 +362,12 @@ MqttClient.prototype.end = function() {
361362
*/
362363
MqttClient.prototype._reconnect = function() {
363364

364-
this._setupStream();
365+
if (this.conn) {
366+
this.conn.removeAllListeners();
367+
delete this.conn;
368+
}
365369

366-
this.conn.reconnect(this.stream);
370+
this._setupStream();
367371
};
368372

369373
/**

lib/connection.js

Lines changed: 21 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ var events = require('events')
33
, protocol = require('./protocol')
44
, generate = require('./generate')
55
, parse = require('./parse')
6+
, Stream = require("stream").Stream
67
, Writable = require("stream").Writable
78
, delay = global.setImmediate;
89

9-
var Writable = require("stream").Writable
1010

1111
if (!Writable) {
1212
Writable = require("readable-stream").Writable;
@@ -19,30 +19,24 @@ if(!delay) {
1919
}
2020

2121
var Connection = module.exports =
22-
function Connection(stream, server) {
23-
this.server = server;
22+
function Connection() {
23+
if (!(this instanceof Connection)) {
24+
return new Connection();
25+
}
2426

2527
this.generate = generate;
26-
var that = this;
2728

28-
events.EventEmitter.call(this);
29+
Writable.call(this);
2930

30-
this.reconnect(stream);
31-
};
32-
util.inherits(Connection, events.EventEmitter);
31+
this._newPacket();
3332

34-
Connection.prototype._setupParser = function() {
35-
this.parser = this.stream.pipe(new PacketParser({
36-
connection: this
37-
}));
38-
};
39-
40-
Connection.prototype.reconnect = function(stream) {
4133
var that = this;
34+
this.on("pipe", function(source) {
4235

43-
this.stream = stream;
44-
this._setupParser();
36+
that.stream = source;
37+
});
4538
};
39+
util.inherits(Connection, Writable);
4640

4741
for (var k in protocol.types) {
4842
var v = protocol.types[k];
@@ -59,30 +53,13 @@ for (var k in protocol.types) {
5953
Connection.prototype[v] = new Function("opts", fun);
6054
}
6155

62-
function PacketParser(options) {
63-
64-
if (!(this instanceof PacketParser)) {
65-
return new PacketParser(options);
66-
}
67-
68-
Writable.call(this, options);
69-
70-
this.conn = options.connection;
71-
this.newPacket();
72-
}
73-
74-
PacketParser.prototype = Object.create(
75-
Writable.prototype,
76-
{ constructor: { value: PacketParser } }
77-
);
78-
79-
PacketParser.prototype.newPacket = function() {
56+
Connection.prototype._newPacket = function() {
8057
this.packet = {};
8158
this.tmp = { pos: 1, mul: 1, length: 0};
8259
this.partialPayload = null;
8360
}
8461

85-
PacketParser.prototype._parseHeader = function() {
62+
Connection.prototype._parseHeader = function() {
8663
// Fresh packet - parse the header
8764
if (!this.packet.cmd) {
8865
// there is at least one byte in the buffer
@@ -93,7 +70,7 @@ PacketParser.prototype._parseHeader = function() {
9370
return true;
9471
};
9572

96-
PacketParser.prototype._parseLength = function() {
73+
Connection.prototype._parseLength = function() {
9774
var result = true, data = this.data;
9875

9976
if (this.packet.length === undefined) {
@@ -130,7 +107,7 @@ PacketParser.prototype._parseLength = function() {
130107
return result;
131108
};
132109

133-
PacketParser.prototype._readPayload = function() {
110+
Connection.prototype._readPayload = function() {
134111
var result = true;
135112

136113
// Do we have a payload?
@@ -170,17 +147,17 @@ PacketParser.prototype._readPayload = function() {
170147
Object.keys(parse).forEach(function(key) {
171148
fun = fun +
172149
" case '" + key + "': \n" +
173-
" result = parse." + key + "(buf, this.packet, this.conn.encoding); \n" +
150+
" result = parse." + key + "(buf, this.packet, this.encoding); \n" +
174151
" break; \n ";
175152
});
176153

177154
fun += "} \n";
178155
fun += "return result; \n";
179156

180-
PacketParser.prototype._parsePayload = new Function("parse", fun);
157+
Connection.prototype._parsePayload = new Function("parse", fun);
181158
})();
182159

183-
PacketParser.prototype._write = function(data, encoding, done) {
160+
Connection.prototype._write = function(data, encoding, done) {
184161

185162
var byte = null, result;
186163

@@ -197,13 +174,13 @@ PacketParser.prototype._write = function(data, encoding, done) {
197174
result = this._parsePayload(parse);
198175

199176
// Clear packet state
200-
this.newPacket();
177+
this._newPacket();
201178

202179
// Emit packet or error
203180
if (result instanceof Error) {
204-
this.conn.emit("error", result);
181+
this.emit("error", result);
205182
} else {
206-
this.conn.emit(result.cmd, result);
183+
this.emit(result.cmd, result);
207184
}
208185

209186
var that = this;

lib/mqtt.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,12 @@ module.exports.createConnection = function(port, host, callback) {
165165
}
166166

167167
net_client = net.createConnection(port, host);
168-
mqtt_conn = new MqttConnection(net_client);
168+
mqtt_conn = net_client.pipe(new MqttConnection());
169169

170170
// Echo net errors
171-
mqtt_conn.stream.on('error', mqtt_conn.emit.bind(mqtt_conn, 'error'));
172-
mqtt_conn.stream.on('close', mqtt_conn.emit.bind(mqtt_conn, 'close'));
171+
net_client.on('error', mqtt_conn.emit.bind(mqtt_conn, 'error'));
172+
173+
net_client.on('close', mqtt_conn.emit.bind(mqtt_conn, 'close'));
173174

174175
net_client.on('connect', function() {
175176
mqtt_conn.emit('connected');

lib/server.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,10 @@ util.inherits(MqttSecureServer, tls.Server);
7474

7575
var MqttServerClient = module.exports.MqttServerClient =
7676
function MqttServerClient(stream, server) {
77-
Connection.call(this, stream, server);
78-
this.stream.on('error', this.emit.bind(this, 'error'));
79-
this.stream.on('close', this.emit.bind(this, 'close'));
77+
Connection.call(this);
78+
stream.on('error', this.emit.bind(this, 'error'));
79+
stream.on('close', this.emit.bind(this, 'close'));
80+
stream.pipe(this);
81+
this.server = server;
8082
};
8183
util.inherits(MqttServerClient, Connection);

test/connection.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('Connection', function() {
1414
beforeEach(function () {
1515
var that = this;
1616
this.stream = new Stream();
17-
this.conn = new Connection(this.stream);
17+
this.conn = this.stream.pipe(new Connection());
1818
});
1919

2020
describe('parsing', require('./connection.parse.js'));
@@ -34,7 +34,7 @@ describe('Connection', function() {
3434

3535
this.stream.write(new Buffer(fixture));
3636
this.conn.on('connect', function(packet) {
37-
this.parser.packet.should.eql({});
37+
this.packet.should.eql({});
3838
done();
3939
});
4040
});

0 commit comments

Comments
 (0)