package cn.net.cpzslibs.prot;

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

public class Prot02Logout extends ProtBase {

	private final short iTaskCode = 2;
	private String sInspectorId;

	/**
	 * 2.退出
	 * 
	 * @author zwj<br/>
	 *         2015-4-24 下午10:22:38<br/>
	 * @param create
	 */
	public void dealLogout(SocketCreate create) {
		if(null==create){
			errLog(iTaskCode, "create:" + null);
			return;
		}
		this.iSendTaskIndex = create.getiTaskIndex();
		this.sInspectorId = create.getsInspectorId();
		Socket socket = create.getSocket();
		DataOutputStream dos = create.getDos();
		DataInputStream dis = create.getDis();
		if (socket == null) {
			return;
		}
		if (dos != null) {
			try {
				Thread.sleep(100);
				sendProt(dos, sInspectorId);
				dos.close();
			} catch (Exception e) {
				e.printStackTrace();
				errLog(iTaskCode, "dos:" + e.toString());
			}
			if (dis != null) {
				try {
					dis.close();
				} catch (IOException e) {
					e.printStackTrace();
					errLog(iTaskCode, "dis:" + e.toString());
				}
			}
			if (socket != null) {
				try {
					socket.close();
				} catch (IOException e) {
					e.printStackTrace();
					errLog(iTaskCode, "socket:" + e.toString());
				}
			}
		}
		debugLog(iTaskCode, "Logout");
	}

	/**
	 * 2.退出
	 * 
	 * @param dos
	 * @param sUserID
	 *            用户id,这里传手机号码上来
	 * @throws IOException
	 */
	private void sendProt(DataOutputStream dos, String sUserID)
			throws IOException {
		byte[] sUserTags = sUserID.getBytes();
		byte[] sendUserTags = new byte[32];
		System.arraycopy(sUserTags, 0, sendUserTags, 0, sUserTags.length);
		short bodySize = countBodySize(sendUserTags);
		sendHeader(dos, iTaskCode, bodySize);
		dos.write(sendUserTags);
		sendCheckCode(dos);
	}

}
