var coder = require("./coder");
var util = require("util");
exports.MClientReg = MClientReg;
exports.MClientMessage = MClientMessage;
exports.MClientKeepAlive = MClientKeepAlive;
coder.RegistMessage(coder.TAG.ADCLOUD_MESSAGE_TAG_CLIENT_REGIST,MClientReg);
coder.RegistMessage(coder.TAG.ADCLOUD_MESSAGE_TAG_CLIENT_REGIST_BACK,MClientRegBack);
coder.RegistMessage(coder.TAG.ADCLOUD_MESSAGE_TAG_CLIENT_MESSAGE,MClientMessage);
coder.RegistMessage(coder.TAG.ADCLOUD_MESSAGE_TAG_CLIENT_KEEPALIVE,MClientKeepAlive);

function MClientReg(appid,plat,buf){
	coder.Message.call(this,coder.TAG.ADCLOUD_MESSAGE_TAG_CLIENT_REGIST);
	this.appid = appid;
	this.plat = plat;
	this.buf_ = buf;
}
util.inherits(MClientReg, coder.Message);
MClientReg.prototype.BodyLength = function(){
	var size = Buffer.byteLength(this.appid)  + 1 + Buffer.byteLength(this.plat)  + 1;
	if(this.buf_){
		size += this.buf_.length;
	}
	return size;
}

MClientReg.prototype.EncodeBody= function(buf_writer){
	buf_writer.WriteStringAscii(this.appid);
	buf_writer.WriteStringAscii(this.plat);
	if(this.buf_)
		buf_writer.WriteBuffer(this.buf_);
}

MClientReg.prototype.DecodeBody = function(buf_reader){
	this.appid = buf_reader.ReadStringAscii();
	this.plat = buf_reader.ReadStringAscii();
	if(buf_reader.DataSize()){
		this.buf_ = buf_reader.GetDataBuffer();
	}
}

function MClientRegBack(appid){
	coder.Message.call(this,coder.TAG.ADCLOUD_MESSAGE_TAG_CLIENT_REGIST_BACK);
	this.code = 0;
}
util.inherits(MClientRegBack, coder.Message);
MClientRegBack.prototype.BodyLength = function(){
	return 1;
}

MClientRegBack.prototype.EncodeBody= function(buf_writer){
	buf_writer.WriteUInt8(this.code);
}

MClientRegBack.prototype.DecodeBody = function(buf_reader){
	this.code = buf_reader.ReadUInt8();
}
function MClientKeepAlive(){
	coder.Message.call(this,coder.TAG.ADCLOUD_MESSAGE_TAG_CLIENT_KEEPALIVE);
}
util.inherits(MClientKeepAlive, coder.Message);


function MClientMessage(buf){
	coder.Message.call(this,coder.TAG.ADCLOUD_MESSAGE_TAG_CLIENT_MESSAGE);
	this.buf_ = buf;
}
util.inherits(MClientMessage, coder.Message);
MClientMessage.prototype.BodyLength = function(){
	var size = this.buf_.length;

	return size;
}

MClientMessage.prototype.EncodeBody= function(buf_writer){
	if(this.buf_){
				buf_writer.WriteBuffer(this.buf_);
	}
}

MClientMessage.prototype.DecodeBody = function(buf_reader){
	var buf = buf_reader.GetDataBuffer();
	if(buf.length  == 0){
		return;
	}
	this.buf_ = new Buffer(buf.length);
	buf.copy(this.buf_);
}