Skip to content

Commit 9b3abea

Browse files
committed
в поисках чуда
1 parent 19c0d0e commit 9b3abea

File tree

1 file changed

+70
-7
lines changed

1 file changed

+70
-7
lines changed

lib/connection.js

Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var net = require('net');
44
var _ = require('underscore');
55
var EventEmitter = require('events');
66
var msgpack = require('msgpack');
7+
var vow = require('vow');
78

89
var states = {
910
CONNECTING: 0,
@@ -52,6 +53,7 @@ function TarantoolConnection (options){
5253
this.socket.on('connect', this.onConnect.bind(this));
5354
this.socket.on('error', this.onError.bind(this));
5455
this.socket.on('data', this.onData.bind(this));
56+
this.responseEnded = true;
5557
}
5658

5759
TarantoolConnection.prototype.onData = function(data){
@@ -70,11 +72,51 @@ TarantoolConnection.prototype.onData = function(data){
7072
this.state = states.CONNECTED;
7173
break;
7274
case states.CONNECTED:
73-
new TarantoolResponse(data);
75+
//new TarantoolResponse(data);
76+
this.responseEnded = false;
77+
this.curResponseLength = buffer.readUIntBE(1, 4);
78+
console.log('len', this.curResponseLength);
79+
this.buffer = buffer.slice(5);
80+
if (this.buffer.length >= this.curResponseLength)
81+
{
82+
this.ended = true;
83+
if (this.buffer.length > length){
84+
var plusResponse = new TarantoolResponse(this.buffer.slice(this.curResponseLength));
85+
if (plusResponse.ended)
86+
{
87+
88+
}
89+
this.buffer = this.buffer.slice(0, length);
90+
}
91+
this.processing();
92+
}
7493
break;
7594
}
7695
};
7796

97+
TarantoolConnection.prototype._responseBufferTrack = function(){
98+
99+
};
100+
101+
TarantoolConnection.prototype._processResponse = function(buffer){
102+
try{
103+
var success = buffer[1] == 0x00;
104+
console.log(this.buffer);
105+
if (this.success){
106+
// add fixarraymap with 2 objects before main object
107+
var dataBuffer = Buffer.concat([new Buffer([0x92]), this.buffer.slice(5)]);
108+
console.log(dataBuffer);
109+
var obj = msgpack.unpack(dataBuffer);
110+
console.log(obj);
111+
}
112+
else{
113+
console.log('its error');
114+
}
115+
} catch(e){
116+
console.log(e, e.stack);
117+
}
118+
}
119+
78120
TarantoolConnection.prototype.onConnect = function(){
79121
this.state = states.PREHELLO;
80122
};
@@ -95,19 +137,23 @@ TarantoolConnection.prototype.connect = function(){
95137
};
96138

97139
TarantoolConnection.prototype.ping = function(){
140+
var dfd = vow.defer();
98141
console.log('start ping');
99-
var header = this._header(tarantoolConstants.RequestCode.rqPing);
142+
var reqId = requestId.getId();
143+
var header = this._header(tarantoolConstants.RequestCode.rqPing, reqId);
100144
console.log('header', header);
101145
var body = new Buffer(0);
102146
this._request(header, body);
147+
this.commandsQueue.push([tarantoolConstants.RequestCode.rqPing, reqId, dfd]);
148+
return dfd.promise();
103149
};
104150

105151

106-
TarantoolConnection.prototype._header = function(command){
152+
TarantoolConnection.prototype._header = function(command, reqId){
107153
try {
108154
var header = new Buffer([0x82, tarantoolConstants.KeysCode.code, command,
109155
tarantoolConstants.KeysCode.sync, 0xce, 0, 0, 0, 0]);
110-
header.writeUIntBE(requestId.getId(), 5, 4);
156+
header.writeUIntBE(reqId, 5, 4);
111157
return header;
112158
} catch(e){
113159
console.log(e, e.stack);
@@ -144,22 +190,39 @@ TarantoolConnection.prototype.destroy = function(interupt){
144190
//@InputType(Buffer)
145191
function TarantoolResponse(buffer){
146192
this.ended = false;
193+
this.currentBufferLength = 0;
147194
var length = buffer.readUIntBE(1, 4);
148195
console.log('len', length);
149-
this.buffer = buffer;
150-
if (this.buffer.length - 5 == length)
196+
this.buffer = buffer.slice(5);
197+
if (this.buffer.length >= length)
151198
{
152199
this.ended = true;
200+
if (this.buffer.length > length){
201+
var plusResponse = new TarantoolResponse(this.buffer.slice(length));
202+
if (plusResponse.ended)
203+
{
204+
205+
}
206+
this.buffer = this.buffer.slice(0, length);
207+
}
153208
this.processing();
154209
}
155210
}
156211

212+
TarantoolResponse.prototype.add = function(buffer){
213+
if (this.ended)
214+
{
215+
216+
}
217+
};
218+
157219
TarantoolResponse.prototype.processing = function(){
158220
try{
159221
this.success = this.buffer[6] == 0x00;
160222
console.log(this.buffer);
161223
if (this.success){
162-
var dataBuffer = this.buffer.slice(13);
224+
// add fixarraymap with 2 objects before main object
225+
var dataBuffer = Buffer.concat([new Buffer([0x92]), this.buffer.slice(5)]);
163226
console.log(dataBuffer);
164227
var obj = msgpack.unpack(dataBuffer);
165228
console.log(obj);

0 commit comments

Comments
 (0)