package cn.net.cpzslibs.prot.handset;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import cn.net.cpzslibs.prot.ProtBase;
import cn.net.cpzslibs.prot.SocketCreate;

/**
 * 8.拆包操作
 * 
 * @author zwj
 * 
 *         2014-12-31
 */
public class Prot08UnPack extends ProtBase {
	private short iTaskCode = 8;

	/**
	 * 8.拆包 <br/>
	 * 2015-3-24上午11:08:01
	 * 
	 * @param dos
	 * @param dis
	 * @param iTaskCode
	 * @param iPackageId
	 * @param callBack
	 * @throws IOException
	 */
	public void dealUnPack(SocketCreate create, long iPackageId,
                           UnPackCallBack callBack) throws IOException {
		this.iSendTaskIndex = create.getiTaskIndex();
		this.callBack = callBack;
		recProt(create.getDos(), create.getDis(), iTaskCode, iPackageId);
	}

	/**
	 * 8.拆包
	 * 
	 * @param dos
	 * @param dis
	 * @param iPackageId
	 * @param mHandler
	 * @param dao
	 * @throws IOException
	 */

	private void recProt(DataOutputStream dos, DataInputStream dis,
			short iTaskCode, long iPackageId) throws IOException {
		sendUnPackaging(dos, iPackageId);
		recHeader(dis);

		recFFFF(dis, iTaskCode);
		callBack.updateData(isSuccess(),iPackageId,"拆包异常("+getiRecErrcode());

	}

	/**
	 * 8.拆包操作
	 * 
	 * @param dos
	 * @param iPackageId
	 *            包标签
	 * @throws IOException
	 */
	private void sendUnPackaging(DataOutputStream dos, long iPackageId)
			throws IOException {
		short bodySize = countBodySize(iPackageId);
		sendHeader(dos, (short) 8, bodySize);// 10
		sendiLabel(dos, iPackageId);
		sendCheckCode(dos);
	}

	private UnPackCallBack callBack;

	public interface UnPackCallBack {
		public void updateData(boolean bool, long iPackageId, String retErr);
	}
}
