Skip to content

Commit

Permalink
WIP: chrome-udp transport for Chrome extension
Browse files Browse the repository at this point in the history
  • Loading branch information
jinroh committed Feb 17, 2013
1 parent 1ec243d commit 06dc3fe
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
85 changes: 85 additions & 0 deletions lib/network/transport/chrome-udp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
var StateEventEmitter = require('../../util/state-eventemitter'),
csock = (chrome.socket || chrome.experimental.socket);

var UDPChrome = module.exports = StateEventEmitter.extend({

initialize: function(server_name, options) {
this.supr();
this._listening = false;
this._socket = null
this._addr = '127.0.0.1';
this._port = parseInt(options.port, 10) || 8000;
},

connect: function() {
this._socket = csock.create('udp', { onEvent: this.listener.bind(this) }, this.onCreate.bind(this));
},

onCreate: function(createInfo) {
this._socket = createInfo;
if (createInfo.socketId > 0) {
csock.connect(createInfo.socketId, this._addr, this._port, this.onConnect.bind(this));
}
},

onConnect: function() {
this.iam = this._socket.peerAddress + ':' + this._socket.peerPort;
this.setState('connected', this.iam);
},

disconnect: function() {
this._listening = false;
this._handler = null;
this._context = null;
csock.disconnect(this._socket.socketId);
this.setState('disconnected');
},

send: function(dst, message) {
if (this.stateIsNot('connected')) {
throw new Error('ChromeUDP transport layer not connected.');
}
var addr = dst.split(':'),
self = this;
this._stringToArrayBuffer(message, function(buff) {
csock.sendTo(self._socket.socketId, buff, addr[1], addr[0], function() {});
});
},

listen: function(fn, context) {
if (this.stateIsNot('connected')) {
throw new Error('ChromeUDP transport layer not connected.');
}
this._listening = true;
this._handler = fn;
this._context = context;
},

listener: function() {
if (!this._listening) { return; }
var self = this;
csock.recvFrom(this._socket.socketId, function(infos) {
self._arrayBufferToString(infos.data, function(msg) {
self._handler.call(self._context || self, {
dst : this.iam,
src : infos.address + ':' + infos.port,
msg : msg
});
});
});
},

_arrayBufferToString = function(buf, callback) {
var bb = new Blob([new Uint8Array(buf)]);
var f = new FileReader();
f.onload = function(e) { callback(e.target.result); };
f.readAsText(bb);
},

_stringToArrayBuffer = function(str, callback) {
var bb = new Blob([str]);
var f = new FileReader();
f.onload = function(e) { callback(e.target.result); };
f.readAsArrayBuffer(bb);
}
});
2 changes: 1 addition & 1 deletion lib/network/transport/udp.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var UDPTransport = module.exports = StateEventEmitter.extend({

send: function(dst, message) {
if (this.stateIsNot('connected')) {
throw new Error('SimUDP transport layer not connected.');
throw new Error('UDP transport layer not connected.');
}
if (!(message instanceof Buffer)) {
message = new Buffer(message);
Expand Down

2 comments on commit 06dc3fe

@alexstrat
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@jinroh
Copy link
Owner Author

@jinroh jinroh commented on 06dc3fe Feb 17, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

je n'ai pas encore vraiment testé...
par contre j'ai bien testé mainline avec kadoh-bittorrent, et ca fonctionne pas mal ! 😄

Please sign in to comment.