This repository has been archived on 2023-09-20. You can view files and clone it, but cannot push or open issues or pull requests.
chromesoul/lib/txt_socket.js

91 lines
2.9 KiB
JavaScript
Raw Permalink Normal View History

2013-04-05 17:59:17 +02:00
//
2013-02-27 15:37:08 +01:00
// Copyright (c) 2013 Rodolphe Breard
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
var TxtSocket = function() {
this.socket_id = null;
this.buffer = '';
this.onError = function(infos) {};
};
TxtSocket.prototype.connect = function(host, port, callback) {
chrome.socket.create('tcp', {}, (function(elem) {
2014-11-06 18:52:14 +01:00
return function(inf) {
elem.socket_id = inf.socketId;
chrome.socket.connect(elem.socket_id, host, port, callback);
};
2013-02-27 15:37:08 +01:00
})(this));
};
TxtSocket.prototype.disconnect = function() {
if (this.socket_id !== null) {
2014-11-06 18:52:14 +01:00
chrome.socket.disconnect(this.socket_id);
chrome.socket.destroy(this.socket_id);
2013-02-27 15:37:08 +01:00
}
};
TxtSocket.prototype.read = function(callback) {
var tmp = '', offset = this.buffer.indexOf("\n");
if (offset === -1) {
2014-11-06 18:52:14 +01:00
chrome.socket.read(this.socket_id, (function(elem) {
return function(rd_inf) {
if (rd_inf.resultCode > 0) {
elem.buffer += elem.ab2str(rd_inf.data);
elem.read(callback);
} else {
elem.throwError({code: rd_inf.resultCode});
}
};
})(this));
2013-02-27 15:37:08 +01:00
} else {
2014-11-06 18:52:14 +01:00
tmp = this.buffer.substr(0, offset);
this.buffer = this.buffer.substr(offset + 1);
callback(tmp);
2013-02-27 15:37:08 +01:00
}
};
TxtSocket.prototype.write = function(str, callback) {
chrome.socket.write(this.socket_id, this.str2ab(str), (function(elem) {
2014-11-06 18:52:14 +01:00
return function(w_inf) {
if (w_inf.bytesWritten >= 0) {
callback(w_inf);
} else {
elem.throwError({code: w_inf.bytesWritten});
}
};
2013-02-27 15:37:08 +01:00
})(this));
};
TxtSocket.prototype.throwError = function(infos) {
console.error('socket error ' + infos.code + ', shutting it down');
this.disconnect();
this.onError(infos);
};
TxtSocket.prototype.ab2str = function(buff) {
return String.fromCharCode.apply(null, new Uint8Array(buff));
};
TxtSocket.prototype.str2ab = function(str) {
var i = 0, buff = new ArrayBuffer(str.length), buff_v = new Uint8Array(buff);
for (i = str.length - 1; i >= 0; --i) {
2014-11-06 18:52:14 +01:00
buff_v[i] = str.charCodeAt(i);
2013-02-27 15:37:08 +01:00
}
return buff;
}