Skip to content

Commit

Permalink
Исправил persistent connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Shirokov committed Nov 6, 2018
1 parent f033e0a commit fb0d6df
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions limestone.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ exports.SphinxClient = function() {

server_conn = tcp.createConnection(port, host);
server_conn.on('error', function(x){
console.log('Error: '+x);
console.log('Error: ' + x);
server_conn.end();
callback(x);
});
Expand Down Expand Up @@ -166,33 +166,40 @@ exports.SphinxClient = function() {
status_code = protocol_version_raw.int16();
version = protocol_version_raw.int16();
server_message = protocol_version_raw.lstring();
if(status_code == Sphinx.statusCode.ERROR){
if (status_code == Sphinx.statusCode.ERROR){
errmsg = 'Server issued ERROR: '+server_message;
}
if(status_code == Sphinx.statusCode.RETRY){
if (status_code == Sphinx.statusCode.RETRY){
errmsg = 'Server issued RETRY: '+server_message;
}
console.log('Protocol version is here');

if(errmsg){
callback(new Error(errmsg));
}
}// if !protocol_version_raw.empty()
var data_unpacked = {'': protocol_version};

if (data_unpacked[""] >= 1) {
} // if !protocol_version_raw.empty()
if (protocol_version >= 1) {

if (persistent){
var pers_req = Buffer.makeWriter();
pers_req.push.int16(Sphinx.command.PERSIST);
pers_req.push.int16(0);
pers_req.push.int32(4);
pers_req.push.int32(1);
server_conn.write(pers_req.toBuffer());
server_conn.once('drain', function(){
server_conn.on('data', readResponseData);
const persistenceWritten = server_conn.write(pers_req.toBuffer());
if (!persistenceWritten) {
server_conn.once('drain', function(){
server_conn.on('readable', readResponseData);
_connected = true;
server_conn.emit('sphinx.connected');
callback(null);
});
} else {
server_conn.on('readable', readResponseData);
_connected = true;
server_conn.emit('sphinx.connected');
callback(null);
}
server_conn.once('drain', function(){
});
} else {
server_conn.on('readable', readResponseData);
Expand Down Expand Up @@ -546,7 +553,7 @@ exports.SphinxClient = function() {
// Got response!
response_output.append(data);
if (_queue.length > 0) {
response_output.runCallbackIfDone(_queue[0]['search_command']);
response_output.runCallbackIfDone(_queue[0]['search_command']);
}
}

Expand All @@ -558,24 +565,22 @@ exports.SphinxClient = function() {
data : new Buffer(0),
parseHeader : function() {
if (this.status === null && this.data.length >= 8) {
var decoder = this.data.toReader();
var decoder = this.data.toReader();

this.status = decoder.int16();
this.version = decoder.int16();
this.length = decoder.int32();

this.data = this.data.slice(8, this.data.length);
this.data = this.data.slice(8, this.data.length);
// this.data = decoder.string(this.data.length - 8);
}
},
append : function(data) {
append : function(data) {
if (data) {
//this.data.write(data.toString('utf-8'), 'utf-8');
var new_buffer = new Buffer(this.data.length + data.length);
this.data.copy(new_buffer, 0, 0);
data.copy(new_buffer, this.data.length, 0);
this.data = new_buffer;
// console.log('Data length after appending: ' + this.data.length);
this.parseHeader();
}
},
Expand Down

0 comments on commit fb0d6df

Please sign in to comment.