
var util = require("util");
var coder = require("../coder");


exports.RpcRequest = RpcRequest;
exports.RpcResponce = RpcResponce;
function RpcRequest(methodname,seq,type) {
	coder.Message.call(this,coder.TAG.ADCLOUD_MESSAGE_TAG_RPC_REQ);
	this.seq_ = seq;
	this.methodname_ = methodname;
	this.SetType(type);
}
util.inherits(RpcRequest, coder.Message);
RpcRequest.prototype.SetType = function(type){
	this.type_ = type;
	if(this.type_ == null){
		this.type_ = new coder.TypeNull();
	}
}
RpcRequest.prototype.GetType = function(type){
	if(this.type_ instanceof coder.TypeNull){
		return null;
	}
	return this.type_;
}
RpcRequest.prototype.BodyLength = function(){
	var size = Buffer.byteLength(this.methodname_)  + 1 + this.type_.Length() + 4;
	return size;
}

RpcRequest.prototype.EncodeBody= function(buf_writer){
	buf_writer.WriteUInt32(this.seq_);
	buf_writer.WriteStringAscii(this.methodname_);
	this.type_.Encode(buf_writer);
	return 0;
}

RpcRequest.prototype.DecodeBody = function(buf_reader){
	this.seq_ = buf_reader.ReadUInt32();
	this.methodname_ = buf_reader.ReadStringAscii();
	this.type_ = coder.DecodeType(buf_reader);
	return 0;
}


function RpcResponce(seq,type) {
	coder.Message.call(this,coder.TAG.ADCLOUD_MESSAGE_TAG_RPC_RSP);
	this.seq_ = seq;
	this.SetType(type);
}
util.inherits(RpcResponce, coder.Message);
RpcResponce.prototype.SetType = function(type){
	this.type_ = type;
	if(this.type_ == null){
		this.type_ = new coder.TypeNull();
	}
}
RpcResponce.prototype.GetType = function(type){
	if(this.type_ instanceof coder.TypeNull){
		return null;
	}
	return this.type_;
}
RpcResponce.prototype.BodyLength = function(){
	var size = this.type_.Length() + 4;
	return size;
}
RpcResponce.prototype.EncodeBody= function(buf_writer){
	buf_writer.WriteUInt32(this.seq_);
	this.type_.Encode(buf_writer);
	return 0;
}

RpcResponce.prototype.DecodeBody = function(buf_reader){
	this.seq_ = buf_reader.ReadUInt32();
	this.type_ = coder.DecodeType(buf_reader);
	return 0;
}


coder.RegistMessage(coder.TAG.ADCLOUD_MESSAGE_TAG_RPC_REQ,RpcRequest);
coder.RegistMessage(coder.TAG.ADCLOUD_MESSAGE_TAG_RPC_RSP,RpcResponce);