Lines Matching full:this

35     this.socketStarted = () => {};
36 this.socketClosed = () => {};
37 this.errorReadingFile = () => {};
38 this.file = file;
39 this.id = id;
40 this.endpoint = endpoint;
41 this.ws = null;
42 this.state = NBD_STATE_UNKNOWN;
43 this.msgbuf = null;
44 this.start = function () {
45 this.ws = new WebSocket(this.endpoint, [token]);
46 this.state = NBD_STATE_OPEN;
47 this.ws.binaryType = 'arraybuffer';
48 this.ws.onmessage = this._on_ws_message.bind(this);
49 this.ws.onopen = this._on_ws_open.bind(this);
50 this.ws.onclose = this._on_ws_close.bind(this);
51 this.ws.onerror = this._on_ws_error.bind(this);
52 this.socketStarted();
54 this.stop = function () {
55 if (this.ws.readyState == 1) {
56 this.ws.close();
57 this.state = NBD_STATE_UNKNOWN;
60 this._on_ws_error = function (ev) {
64 this._on_ws_close = function (ev) {
69 this.socketClosed(ev.code);
72 this._on_ws_open = function () {
74 this.client = {
77 this._negotiate();
79 this._on_ws_message = function (ev) {
81 if (this.msgbuf == null) {
82 this.msgbuf = data;
84 const tmp = new Uint8Array(this.msgbuf.byteLength + data.byteLength);
85 tmp.set(new Uint8Array(this.msgbuf), 0);
86 tmp.set(new Uint8Array(data), this.msgbuf.byteLength);
87 this.msgbuf = tmp.buffer;
90 var handler = this.recv_handlers[this.state];
92 console.log('no handler for state ' + this.state);
93 this.stop();
96 var consumed = handler(this.msgbuf);
99 'handler[state=' + this.state + '] returned error ' + consumed,
101 this.stop();
108 if (consumed == this.msgbuf.byteLength) {
109 this.msgbuf = null;
112 this.msgbuf = this.msgbuf.slice(consumed);
116 this._negotiate = function () {
127 this.state = NBD_STATE_WAIT_CFLAGS;
128 this.ws.send(buf);
131 this._handle_cflags = function (buf) {
136 this.client.flags = data.getUint32(0);
137 this.state = NBD_STATE_WAIT_OPTION;
140 this._handle_option = function (buf) {
155 if (!(this.client.flags & NBD_FLAG_NO_ZEROES)) n += 124;
159 var size = this.file.size;
165 this.ws.send(resp);
166 this.state = NBD_STATE_TRANSMISSION;
178 this.ws.send(resp1);
182 this._create_cmd_response = function (req, rc, data = null) {
194 this._handle_cmd = function (buf) {
222 err = this._handle_cmd_read(req);
225 err = this._handle_cmd_disconnect(req);
245 var resp = this._create_cmd_response(req, err);
246 this.ws.send(resp);
248 this.errorReadingFile();
249 this.stop();
254 this._handle_cmd_read = function (req) {
261 var blob = this.file.slice(offset, offset + req.length);
267 var resp = this._create_cmd_response(req, 0, reader.result);
268 this.ws.send(resp);
269 }.bind(this);
274 var resp = this._create_cmd_response(req, EIO);
275 this.ws.send(resp);
276 }.bind(this);
280 this._handle_cmd_disconnect = function () {
281 this.stop();
284 this.recv_handlers = Object.freeze({
285 [NBD_STATE_WAIT_CFLAGS]: this._handle_cflags.bind(this),
286 [NBD_STATE_WAIT_OPTION]: this._handle_option.bind(this),
287 [NBD_STATE_TRANSMISSION]: this._handle_cmd.bind(this),