node-peercast
Version:
PeerCast client for node.js
101 lines (100 loc) • 3.58 kB
JavaScript
var __extends = this.__extends || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
__.prototype = b.prototype;
d.prototype = new __();
};
var events = require('events');
var log4js = require('log4js');
var AtomReader = require('./atomreader');
var pcp = require('./pcp');
const AGENT_NAME = 'node-peercast';
var logger = log4js.getLogger();
var PcpSocket = (function (_super) {
__extends(PcpSocket, _super);
function PcpSocket(socket) {
var _this = this;
_super.call(this);
this.socket = socket;
this.reader = new AtomReader();
var localRemote = this.localRemote;
socket.on('close', function () {
logger.info('Closed: ' + localRemote);
_this.socket = null;
_this.emit('close');
});
socket.on('end', function () {
logger.info('EOS: ' + localRemote);
});
socket.on('readable', function () {
logger.info('Incomming message: ' + localRemote);
var atom = _this.reader.read(_this.socket);
if (atom == null) {
logger.debug('wait');
return;
}
logger.info('Atom received: ' + atom.name);
_this.emit(pcp.toName(atom.name), atom);
});
logger.info('Connected: ' + localRemote);
}
PcpSocket.prototype.sendHTTPHeader = function (channelId) {
this.socket.write('GET /channel/' + channelId + ' HTTP/1.0\r\n' + 'x-peercast-pcp:1\r\n' + '\r\n');
};
PcpSocket.prototype.sendPCPHeader = function () {
this.socket.write('pcp\n');
writeInt32LE(this.socket, 4);
writeInt32LE(this.socket, 1);
};
PcpSocket.prototype.hello = function (port) {
logger.info('Send hello: ' + this.localRemote);
var sessionId = new Buffer(16);
sessionId.fill(0);
write(this.socket, pcp.createHello(AGENT_NAME, 0, sessionId, port, port, 0, sessionId));
};
PcpSocket.prototype.olleh = function () {
logger.info('Send olleh: ' + this.localRemote);
var sessionId = new Buffer(16);
sessionId.fill(0);
write(this.socket, pcp.createOlleh(AGENT_NAME, sessionId, 0, 0, 0));
};
PcpSocket.prototype.quit = function () {
// ���ɐؒf�ς݂Ȃ牽�����Ȃ�
if (this.socket == null) {
return;
}
logger.info('Send quit: ' + this.localRemote);
write(this.socket, pcp.createQuit());
};
Object.defineProperty(PcpSocket.prototype, "localRemote", {
get: function () {
return this.socket.localAddress + ':' + this.socket.localPort + ', ' + this.socket.remoteAddress + ':' + this.socket.remotePort;
},
enumerable: true,
configurable: true
});
return PcpSocket;
})(events.EventEmitter);
function write(stream, atom) {
if (atom.name.length !== 4) {
throw new Error('Invalid name: ' + atom.name);
}
logger.debug('Atom writing: ' + atom.name);
stream.write(atom.name);
if (atom.isContainer()) {
writeInt32LE(stream, 0x80000000 | atom.children.length);
atom.children.forEach(function (child) {
write(stream, child);
});
}
else {
writeInt32LE(stream, atom.content.length);
stream.write(atom.content);
}
}
function writeInt32LE(stream, value) {
var buffer = new Buffer(4);
buffer.writeInt32LE(value, 0);
stream.write(buffer);
}
module.exports = PcpSocket;