Commit e8a25b29 by s_guodong

代码整理

parent 1689dc97
......@@ -13,113 +13,111 @@ import java.net.Socket;
public class ShortSocketHandle implements Runnable {
private Object[] args;
private Socket socket;
private String interfaceName;
private String transName;
private boolean has_head = true;// 是否有报文头
private String head_len_type = IServerInstance.HEAD_LEN_TYPE_10;// 报文头长度是二进制还是十进制
private int head_len = 0;// 报文头长度
private boolean is_contain_head_len = false;// 报文头长度是否包含报文头
private int fill_len = 0;// 默认为0
private boolean is_contain_fill_len = false;// 报文头长度是否包含报文头和报文体之间数据的长度 true
private int body_offset = 0;// 报文体位移量 默认为0, -1为减去1个长度,+1为增加1个长度
private String encoding = "UTF-8";
private Logger logger;
public ShortSocketHandle(Socket socket, String interfaceName, String transName, boolean has_head, String head_len_type, int head_len,
boolean is_contain_head_len, int fill_len, boolean is_contain_fill_len, int body_offset, Object[] args, String encoding, Logger logger) {
this.socket = socket;
this.interfaceName = interfaceName;
this.transName = transName;
this.has_head = has_head;
this.head_len_type = head_len_type;
this.head_len = head_len;
this.is_contain_head_len = is_contain_head_len;
this.fill_len = fill_len;
this.is_contain_fill_len = is_contain_fill_len;
this.body_offset = body_offset;
this.args = args;
this.encoding = encoding;
this.logger = logger;
}
@Override
public void run() {
if (has_head) {
handleWithHead();
} else {
handleWithNoHead();
}
}
public void handleWithHead() {
byte[] headLenBytes = new byte[head_len];
int headLen = 0;
int returnLen = 0;
try {
IOUtils.readFully(socket.getInputStream(), headLenBytes);
if (head_len_type.equals(IServerInstance.HEAD_LEN_TYPE_10)) {
headLen = Integer.parseInt(new String(headLenBytes));
} else if (head_len_type.equals(IServerInstance.HEAD_LEN_TYPE_2)) {
headLen = CommonFunctionUtils.bytesToInt(headLenBytes);
}
if (is_contain_head_len) {
headLen = headLen - head_len;
}
if (fill_len != 0) {
if (!is_contain_fill_len) {
headLen = headLen + fill_len;
}
}
headLen = headLen + body_offset;
byte[] databuffer = new byte[headLen];
IOUtils.readFully(socket.getInputStream(), databuffer);
// logger.debug("socket接收");
Context context = new Context();
context.addVariable("transaction", "__val", databuffer);
ResultMsg resultMsg = new Client().call(context, interfaceName, transName, args);
byte[] returnBytes = null;
Object obj = resultMsg.getContent();
if (obj instanceof String) {
returnBytes = ((String) obj).getBytes(encoding);
} else {
returnBytes = (byte[]) obj;
}
returnLen = returnBytes.length;
byte[] header = null;
if (is_contain_head_len) {
returnLen = returnLen + head_len;
}
if (head_len_type.equals(IServerInstance.HEAD_LEN_TYPE_10)) {
header = String.format("%0" + head_len + "d", returnLen).getBytes();
} else if (head_len_type.equals(IServerInstance.HEAD_LEN_TYPE_2)) {
header = CommonFunctionUtils.intToBytes(returnLen);
}
IOUtils.write(header, socket.getOutputStream());
IOUtils.write(returnBytes, socket.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
IOUtils.closeQuietly(socket);
} finally {
IOUtils.closeQuietly(socket);
}
}
public void handleWithNoHead() {
}
private Object[] args;
private Socket socket;
private String interfaceName;
private String transName;
private boolean has_head = true;// 是否有报文头
private String head_len_type = IServerInstance.HEAD_LEN_TYPE_10;// 报文头长度是二进制还是十进制
private int head_len = 0;// 报文头长度
private boolean is_contain_head_len = false;// 报文头长度是否包含报文头
private int fill_len = 0;// 默认为0
private boolean is_contain_fill_len = false;// 报文头长度是否包含报文头和报文体之间数据的长度 true
private int body_offset = 0;// 报文体位移量 默认为0, -1为减去1个长度,+1为增加1个长度
private String encoding = "UTF-8";
private Logger logger;
public ShortSocketHandle(Socket socket, String interfaceName, String transName, boolean has_head, String head_len_type, int head_len,
boolean is_contain_head_len, int fill_len, boolean is_contain_fill_len, int body_offset, Object[] args, String encoding, Logger logger) {
this.socket = socket;
this.interfaceName = interfaceName;
this.transName = transName;
this.has_head = has_head;
this.head_len_type = head_len_type;
this.head_len = head_len;
this.is_contain_head_len = is_contain_head_len;
this.fill_len = fill_len;
this.is_contain_fill_len = is_contain_fill_len;
this.body_offset = body_offset;
this.args = args;
this.encoding = encoding;
this.logger = logger;
}
@Override
public void run() {
if (has_head) {
handleWithHead();
} else {
handleWithNoHead();
}
}
public void handleWithHead() {
byte[] headLenBytes = new byte[head_len];
int headLen = 0;
int returnLen = 0;
try {
IOUtils.readFully(socket.getInputStream(), headLenBytes);
if (head_len_type.equals(IServerInstance.HEAD_LEN_TYPE_10)) {
headLen = Integer.parseInt(new String(headLenBytes));
} else if (head_len_type.equals(IServerInstance.HEAD_LEN_TYPE_2)) {
headLen = CommonFunctionUtils.bytesToInt(headLenBytes);
}
if (is_contain_head_len) {
headLen = headLen - head_len;
}
if (fill_len != 0) {
if (!is_contain_fill_len) {
headLen = headLen + fill_len;
}
}
headLen = headLen + body_offset;
byte[] databuffer = new byte[headLen];
IOUtils.readFully(socket.getInputStream(), databuffer);
Context context = new Context();
context.addVariable("transaction", "__val", databuffer);
ResultMsg resultMsg = new Client().call(context, interfaceName, transName, args);
byte[] returnBytes = null;
Object obj = resultMsg.getContent();
if (obj instanceof String) {
returnBytes = ((String) obj).getBytes(encoding);
} else {
returnBytes = (byte[]) obj;
}
returnLen = returnBytes.length;
byte[] header = null;
if (is_contain_head_len) {
returnLen = returnLen + head_len;
}
if (head_len_type.equals(IServerInstance.HEAD_LEN_TYPE_10)) {
header = String.format("%0" + head_len + "d", returnLen).getBytes();
} else if (head_len_type.equals(IServerInstance.HEAD_LEN_TYPE_2)) {
header = CommonFunctionUtils.intToBytes(returnLen);
}
IOUtils.write(header, socket.getOutputStream());
IOUtils.write(returnBytes, socket.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.error(e.getMessage(), e);
IOUtils.closeQuietly(socket);
} finally {
IOUtils.closeQuietly(socket);
}
}
public void handleWithNoHead() {
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment