Skip to content

Commit

Permalink
incoming packet read
Browse files Browse the repository at this point in the history
  • Loading branch information
Yusuke Hata committed Mar 9, 2012
1 parent 9a4ad93 commit e7b58ee
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
24 changes: 17 additions & 7 deletions Resources/ti-websocket-client/lib/websocket-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -543,9 +543,24 @@ WebSocket.prototype._read_request = function(e, callback){
};
WebSocket.prototype._read_callback = function(e){
var self = this;
var streamReadCallback = function(evt){
if(0 < evt.bytesProcessed){
return self._read_request(evt, function (){
return setTimeout(nextTick, 0);
});
}
return setTimeout(nextTick, 100);
};
var nextTick = function(){
if(0 < self.bufferSize){
return self._read_buffer(nextTick);
return self._read_buffer(function (readSuccess){
if(readSuccess){
return setTimeout(nextTick, 0);
}

self._socketReadBuffer.clear();
return Ti.Stream.read(self._socket, self._socketReadBuffer, streamReadCallback);
});
}

if(null == self._socket){
Expand All @@ -554,12 +569,7 @@ WebSocket.prototype._read_callback = function(e){
}

self._socketReadBuffer.clear();
return Ti.Stream.read(self._socket, self._socketReadBuffer, function(e){
if(0 < e.bytesProcessed){
return self._read_request(e, nextTick);
}
return setTimeout(nextTick, 0);
});
return Ti.Stream.read(self._socket, self._socketReadBuffer, streamReadCallback);
};
return setTimeout(nextTick, 0);
};
Expand Down
36 changes: 29 additions & 7 deletions Resources/ti-websocket-client/ti-websocket-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1183,9 +1183,36 @@ WebSocket.prototype._read_request = function(e, callback){
};
WebSocket.prototype._read_callback = function(e){
var self = this;
var streamReadCallback = function(evt){
if(0 < evt.bytesProcessed){
return self._read_request(evt, function (){
//
// incoming data(wait:short)
//
return setTimeout(nextTick, 0);
});
}

//
// incoming data(wait:long)
//
return setTimeout(nextTick, 100);
};
var nextTick = function(){
if(0 < self.bufferSize){
return self._read_buffer(nextTick);
return self._read_buffer(function (readSuccess){
if(readSuccess){
return setTimeout(nextTick, 0);
}

//
// unknown read packet(success: false)
// then clear read buffer
//

self._socketReadBuffer.clear();
return Ti.Stream.read(self._socket, self._socketReadBuffer, streamReadCallback);
});
}

if(null == self._socket){
Expand All @@ -1194,12 +1221,7 @@ WebSocket.prototype._read_callback = function(e){
}

self._socketReadBuffer.clear();
return Ti.Stream.read(self._socket, self._socketReadBuffer, function(e){
if(0 < e.bytesProcessed){
return self._read_request(e, nextTick);
}
return setTimeout(nextTick, 0);
});
return Ti.Stream.read(self._socket, self._socketReadBuffer, streamReadCallback);
};
return setTimeout(nextTick, 0);
};
Expand Down

0 comments on commit e7b58ee

Please sign in to comment.