

var message = require("./message");
var buffer = require("./buffer")

exports.Coder = Coder;
function Coder(cb){
	this.cur_buf_;
	this.cb_ = cb;
}
Coder.prototype.OnData = function(buf){
	if(!this.cur_buf_ || this.cur_buf_.length == 0){
		this.cur_buf_ = buf;
	}else{
		this.cur_buf_ = Buffer.concat([this.cur_buf_,buf]);
	//	var tmpbuf = new Buffer(this.cur_buf_.length+buf.length);
	//	this.cur_buf_.copy(tmpbuf);
	//	buf.copy(tmpbuf,this.cur_buf_.length);
	//	this.cur_buf_ = tmpbuf;
	}
	while(true){
		if(this.cur_buf_.Length == 0){
			this.cur_buf_ = null;
			break;
		}
		var bufWriter = new buffer.BufferReader(this.cur_buf_);
		var msg = message.DecodeMessage(bufWriter);
		if(msg){
			this.cur_buf_ = this.cur_buf_.slice(msg.Length());
			this.cb_(msg);
		}else{
			break;
		}
	}
}