Skip to content

Commit

Permalink
Ping (User Control) request are displayed by dumpMessageStream.js
Browse files Browse the repository at this point in the history
  • Loading branch information
nalply committed Oct 27, 2011
1 parent 0c90faf commit 5b71dca
Show file tree
Hide file tree
Showing 10 changed files with 327 additions and 174 deletions.
101 changes: 0 additions & 101 deletions examples/ChunkStreamDumper.js

This file was deleted.

25 changes: 25 additions & 0 deletions examples/DumpTools.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"use strict";

var dump8 = exports.dump8 = function(data) {
function b(i) { return i < data.length ? hex2(data.readUInt8(i)) : ' '; }

return b(0) + b(1) + b(2) + b(3) + ' ' + b(4) + b(5) + b(6) + b(7);
}

var hex2 = exports.hex2 = function(byte) {
function hex1(nybble) { return "0123456789abcdef"[nybble & 0xf]; }
return hex1(byte >> 4) + hex1(byte);
}

var hex6 = exports.hex6 = function(int24) {
return hex2(int24 >> 16) + hex2(int24 >> 8) + hex2(int24);
}

var ascii8 = exports.ascii8 = function(data) {
function asChar(b) { return String.fromCharCode(b); }
function ascii(b) { return b > 31 && b < 128 ? asChar(b).yellow : '·'.black; }
function b(i) { return i < data.length ? ascii(data.readUInt8(i)) : '·'.white; }

return b(0) + b(1) + b(2) + b(3) + ' ' + b(4) + b(5) + b(6) + b(7);
}

55 changes: 0 additions & 55 deletions examples/MessageStreamDumper.js

This file was deleted.

98 changes: 98 additions & 0 deletions examples/dumpChunkStream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
"use strict";

require('colors');
var net = require('net');
var mtrude = require('mtrude');
var ChunkStream = mtrude.rtmp.ChunkStream;
var asSocket = mtrude.asSocket;

function main() {
var optimist = require('optimist')
.usage('Usage: $0 [--debug] [--debugchain] [in [out]]')
.boolean('debug')
.boolean('debugchain')
.boolean('help')
.alias('h', 'help')
.describe('debug', 'Set ChunkStream.DBG = true')
.describe('debugchain', 'Set BufferChain.DBG = true')
;
var argv = optimist.argv;

if (argv.help || argv._.length > 2) {
optimist.showHelp();
return;
}

console.log(''
+ 'NOTE! ChunkStream is not complete without MessageStream and might get\n'
+ 'stuck because of missing coordination with peer. Dump format:\n'
+ 'CHUNK: %s %s %s %s %s:%s %s:%s,%s\n',
'data-1st -8-bytes'.blue, 'asci i-8c'.yellow,
'tstamp'.blue, 'ti'.green, 'msg-id'.blue, 'chs-id'.blue,
'msglen'.magenta, 'chklen'.blue, 'rest'.blue
);

if (argv.debug) ChunkStream.DBG = true;

if (argv.chain) mtrude.BufferChain.DBG = true;

if (argv._.length == 0) {
var server = net.createServer();
server.on('connection', function(socket) {
console.log('CONN : ' + 'Connection from %s'.cyan, socket.remoteAddress);
dumpChunkStream(new ChunkStream(socket));
});
server.listen(1935);
console.log('LISTN: ' + 'Listening on port 1935'.cyan);
}
else {
var iFile = argv._[0];
var time36 = new Date().getTime().toString(36)
var oFile = argv._[1] || 'out-' + time36 + '.raw';
dumpChunkStream(new ChunkStream(asSocket(iFile, oFile
, function() { console.log('FILE : ' + 'Reading %s'.cyan, iFile); }
, function() { console.log('FILE : ' + 'Writing %s'.cyan, oFile); }
)));
}
}


var DumpTools = require('./DumpTools');
var dump8 = DumpTools.dump8;
var hex2 = DumpTools.hex2;
var hex6 = DumpTools.hex6;
var ascii8 = DumpTools.ascii8;

var dumpChunkStream = function(chunkStream) {
chunkStream.on('error', function(errorMessage) {
console.log('ERROR:', ('ChunkStream: ' + errorMessage).red);
chunkStream.close();
});
chunkStream.on('end', function(graceful) {
var msg = 'ChunkStream ' + (graceful ? '' : 'not ') + 'gracefully ended';
console.log('END :', graceful ? msg.green : msg.red);
});
chunkStream.on('handshake', function() {
console.log('HANDS: %s', 'Handshake completed successfully'.green);
});
chunkStream.on('warn', function(message) {
console.log('WARN : %s', ('ChunkStream: ' + message).red);
});
chunkStream.on('chunk', function(chunk) {
console.log(
'CHUNK: %s %s %s %s %s:%s %s:%s,%s',
dump8(chunk.data).blue, ascii8(chunk.data),
hex6(chunk.timestamp).blue, hex2(chunk.typeid).green,
hex6(chunk.csid).blue, hex6(chunk.msid).blue,
chunk.length.toString().magenta, chunk.data.length.toString().blue,
chunk.rest.toString().blue);
if (chunk.typeid == 1) {
var chunkSize = chunk.data.readUInt32LE(0);
chunkStream.warn('Setting chunk size to ' + chunkSize);
chunkStream.chunkSize = chunkSize;
}
});
}

if (require.main === module) main();
module.exports = dumpChunkStream;
94 changes: 94 additions & 0 deletions examples/dumpMessageStream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
"use strict";

require('colors');
var net = require('net');
var mtrude = require('mtrude');
var rtmp = mtrude.rtmp;
var ChunkStream = rtmp.ChunkStream;
var MessageStream = rtmp.MessageStream;
var asSocket = mtrude.asSocket;

function main() {
var optimist = require('optimist')
.usage('Usage: $0 [--debug] [--chunks] [in [out]]')
.boolean('debug')
.boolean('chunks')
.boolean('help')
.alias('h', 'help')
.describe('debug', 'Set MessageStream.DBG = true')
.describe('chunks', 'Also dump chunks')
;
var argv = optimist.argv;

if (argv.help || argv._.length > 2) {
optimist.showHelp();
return;
}

console.log('Dump format:\n'
+ 'MESSAGE: ....\n');

if (argv.debug) MessageStream.DBG = true;

if (argv._.length == 0) {
var server = net.createServer();
server.on('connection', function(socket) {
console.log('CONN : ' + 'Connection from %s'.cyan, socket.remoteAddress);
var chunkStream = new ChunkStream();
});
server.listen(1935);
console.log('LISTN: ' + 'Listening on port 1935'.cyan);
}
else {
var iFile = argv._[0];
var time36 = new Date().getTime().toString(36)
var oFile = argv._[1] || 'out-' + time36 + '.raw';
var chunkStream = new ChunkStream(asSocket(iFile, oFile
, function() { console.log('FILE : ' + 'Reading %s'.cyan, iFile); }
, function() { console.log('FILE : ' + 'Writing %s'.cyan, oFile); }
));
}

if (argv.chunks) require('./dumpChunkStream')(chunkStream);

var messageStream = new MessageStream(chunkStream);
dumpMessageStream(messageStream);
}

var DumpTools = require('./DumpTools');
var dump8 = DumpTools.dump8;
var hex2 = DumpTools.hex2;
var hex6 = DumpTools.hex6;
var ascii8 = DumpTools.ascii8;

function dumpMessageStream(messageStream) {
messageStream.on('error', function(errorMessage) {
console.log('ERROR:', ('MessageStream: ' + errorMessage).red);
messageStream.close();
});
messageStream.on('warn', function(warnMessage) {
console.log('WARN :', ('MessageStream: ' + warnMessage).red);
});
messageStream.on('end', function(graceful) {
var msg = 'MessageStream ' + (graceful ? '' : 'not ') + 'gracefully ended';
console.log('END :', graceful ? msg.green : msg.red);
});
messageStream.on('message', function(message) {
console.log('MSG : %s %s %s %s %s:%s %s',
dump8(message.data).blue, ascii8(message.data),
hex6(message.timestamp).blue, hex2(message.typeid).green,
hex6(message.csid).blue, hex6(message.msid).blue,
message.data.length.toString().magenta);
});
messageStream.on('ping', function(ping) {
var id = ping.id == null ? '-' : ping.id;
var timestamp = ping.timestamp == null ? ping.buflen : ping.timestamp;
console.log('PING : %s (%s) %s %s',
hex2(ping.type).green, rtmp.pingNames[ping.type].green,
id.toString().blue, (timestamp || '-').toString().blue);
});
}

if (require.main === module) main();
exports.dumpMessageStream = dumpMessageStream;

Loading

0 comments on commit 5b71dca

Please sign in to comment.