mirror of
https://github.com/gyurix1968/guacamole-client.git
synced 2025-09-06 13:17:41 +00:00
Implemented Guacamole.Parser
This commit is contained in:
@@ -42,6 +42,131 @@
|
||||
*/
|
||||
var Guacamole = Guacamole || {};
|
||||
|
||||
/**
|
||||
* Simple Guacamole protocol parser that invokes an oninstruction event when
|
||||
* full instructions are available from data received via receive().
|
||||
*
|
||||
* @constructor
|
||||
*/
|
||||
Guacamole.Parser = function() {
|
||||
|
||||
/**
|
||||
* Reference to this parser.
|
||||
* @private
|
||||
*/
|
||||
var parser = this;
|
||||
|
||||
/**
|
||||
* Current buffer of received data. This buffer grows until a full
|
||||
* element is available. After a full element is available, that element
|
||||
* is flushed into the element buffer.
|
||||
*
|
||||
* @priate
|
||||
*/
|
||||
var buffer = "";
|
||||
|
||||
/**
|
||||
* Buffer of all received, complete elements. After an entire instruction
|
||||
* is read, this buffer is flushed, and a new instruction begins.
|
||||
*
|
||||
* @priate
|
||||
*/
|
||||
var element_buffer = [];
|
||||
|
||||
// The location of the last element's terminator
|
||||
var element_end = -1;
|
||||
|
||||
// Where to start the next length search or the next element
|
||||
var start_index = 0;
|
||||
|
||||
this.receive = function(packet) {
|
||||
|
||||
// Truncate buffer as necessary
|
||||
if (start_index > 4096) {
|
||||
|
||||
buffer = buffer.substring(start_index);
|
||||
|
||||
// Reset parse relative to truncation
|
||||
element_end -= start_index;
|
||||
start_index = 0;
|
||||
|
||||
}
|
||||
|
||||
// Append data to buffer
|
||||
buffer += packet;
|
||||
|
||||
// While search is within currently received data
|
||||
while (element_end < buffer.length) {
|
||||
|
||||
// If we are waiting for element data
|
||||
if (element_end >= start_index) {
|
||||
|
||||
// We now have enough data for the element. Parse.
|
||||
var element = buffer.substring(start_index, element_end);
|
||||
var terminator = buffer.substring(element_end, element_end+1);
|
||||
|
||||
// Add element to array
|
||||
element_buffer.push(element);
|
||||
|
||||
// If last element, handle instruction
|
||||
if (terminator == ";") {
|
||||
|
||||
// Get opcode
|
||||
var opcode = element_buffer.shift();
|
||||
|
||||
// Call instruction handler.
|
||||
if (parser.oninstruction != null)
|
||||
parser.oninstruction(opcode, element_buffer);
|
||||
|
||||
// Clear elements
|
||||
element_buffer.length = 0;
|
||||
|
||||
}
|
||||
|
||||
// Start searching for length at character after
|
||||
// element terminator
|
||||
start_index = element_end + 1;
|
||||
|
||||
}
|
||||
|
||||
// Search for end of length
|
||||
var length_end = buffer.indexOf(".", start_index);
|
||||
if (length_end != -1) {
|
||||
|
||||
// Parse length
|
||||
var length = parseInt(buffer.substring(element_end+1, length_end));
|
||||
|
||||
// Calculate start of element
|
||||
start_index = length_end + 1;
|
||||
|
||||
// Calculate location of element terminator
|
||||
element_end = start_index + length;
|
||||
|
||||
}
|
||||
|
||||
// If no period yet, continue search when more data
|
||||
// is received
|
||||
else {
|
||||
start_index = buffer.length;
|
||||
break;
|
||||
}
|
||||
|
||||
} // end parse loop
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Fired once for every complete Guacamole instruction received, in order.
|
||||
*
|
||||
* @event
|
||||
* @param {String} opcode The Guacamole instruction opcode.
|
||||
* @param {Array} parameters The parameters provided for the instruction,
|
||||
* if any.
|
||||
*/
|
||||
this.oninstruction = null;
|
||||
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Guacamole protocol client. Given a display element and {@link Guacamole.Tunnel},
|
||||
|
Reference in New Issue
Block a user