Commit e8a25b29 by s_guodong

代码整理

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