📄 socketlogic.java
字号:
break; //全部处理成功,退出
}
}
}
catch (IOException ioexception) {
System.err.println("socketLogic.submit Failed I/O: " + ioexception);
for (int i = 0; i < multi.length; i++) {
if (multi[i].result != -1) {
multi[i].result = configure.ERROR_SEND_FAILED;
//return;
}
}
}
}
// return errorCode;
}
/********************************************************************************
*
* 登录系统
*
*/
public int login(SMGPHeadStruct head, byte body[],socketStruct socket,int retryTimes,int timeout)
{
DataInputStream dataRec;
DataOutputStream dataSend;
int errorCode=0;
try {
head.sequenceId = socket.seq++;
if(socket.seq == 0x7fffffff)
socket.seq = 1;
socket.socket.setSoTimeout(timeout); //设置超时时间
dataRec = new DataInputStream(socket.socket.getInputStream());
dataSend = new DataOutputStream(socket.socket.getOutputStream());
errorCode=send(head,body,dataSend);
}
catch (IOException ioexception) {
System.err.println("socketLogic.login Failed I/O: " + ioexception);
return configure.ERROR_SEND_FAILED;
}
return errorCode;
}
/*********************************************************************************
*
* 发送数据
* @return 0:成功 1:失败
*/
public int send(SMGPHeadStruct head, byte body[],DataOutputStream dataSend)
{
try{
int i = head.packageSize - 12;
byte abyte1[] = fun.head2Byte(head);
dataSend.write(abyte1, 0, 12);
if (i > 0 && body != null)
dataSend.write(body, 0, i);
dataSend.flush();
}catch(InterruptedIOException e)
{//超时
System.err.println("socketLogic.timeout receive timeout: " + e);
return configure.SOCKET_SO_TIMEOUT;
}
catch(IOException ioexception)
{//其它错误
System.err.println("socketLogic.send Failed I/O: " + ioexception);
return configure.ERROR_CONNECT_FAILED;
}
catch (Exception ioexception) {
System.err.println("socketLogic.send Exception: " + ioexception);
return configure.ERROR_SEND_FAILED;
}
return 0;
}
/********************************************************************************************
*
* 保存接收deliver数据并返回response(主要用于发送数据后接收response时,意外接收到的deliver)
*由activeTest或submit等调用,端口锁定由调用的method处理
* type: 0--发送端口,1--接收端口 2--收发端口
*
**/
public int dealDeliver(socketStruct socket,Hashtable hash,deliverVector deliverQueue,int type )
{
DataInputStream dataRec;
DataOutputStream dataSend;
int errorCode=0;
try {
SMGPHeadStruct head=(SMGPHeadStruct)hash.get("head");
if ( head.requestId==3 &&( (String) hash.get("result")).equals("ok"))
{ //接收deliver成功
//if(deliverQueue(head,(byte[])hash.get("body"),deliverQueue))
if(type==0||(type!=0&&deliverQueue(head,(byte[])hash.get("body"),deliverQueue)))
{//接收的数据正确,返回deliver_resp
head.requestId=0x80000003;
head.packageSize=26;
byte[] body=new byte[14];
System.arraycopy((byte[])hash.get("body"),0,body,0,10);
/* int status=0; //若为收发、收端口,则返回正确信息
if(type==0)
status=128; //若是发送端口,返回“接口处理错”的信息
byte[] s=fun.int2Byte(status);
body[10]=s[0];*/
body[10]=0; //若为收发、收端口,则返回正确信息
if(type==0)
body[10]=(byte)0x80; //若是发送端口,返回“接口处理错”的信息
// byte[] s=fun.int2Byte(status);
// body[10]=s[0];
body[11]=0;
body[12]=0;
body[13]=0;
dataSend = new DataOutputStream(socket.socket.getOutputStream());
send(head,body,dataSend);
}
}
else
{//接收失败
errorCode=configure.ERROR_RECEIVE_FAILED;
}
}
catch(InterruptedIOException e)
{//超时退出
}
catch (IOException ioexception) {
System.err.println("socketLogic.dealDeliver Failed I/O: " + ioexception);
return configure.ERROR_RECEIVE_FAILED;
}
catch (Exception ioexception) {
System.err.println("socketLogic.dealDeliver Exception: " + ioexception);
return configure.ERROR_RECEIVE_FAILED;
}
return errorCode;
}
/**********************************************************************************************
*
* 循环接收deliver数据
*返回接收的数量
*/
public int cycleReceive(socketStruct socket,deliverVector deliverQueue)
{
DataInputStream dataRec;
DataOutputStream dataSend;
int receiveTotal=0;
int errorCode=0;
long startTime=System.currentTimeMillis();
do{//循环接收数据,直到没有数据接收或socket超时
synchronized (socket) {
try {
socket.socket.setSoTimeout(configure.SOCKET_RECEIVE_TIMEOUT); //设置超时时间
dataRec = new DataInputStream(socket.socket.getInputStream());
Hashtable hash = receive(socket);
if (hash == null) {
errorCode = configure.ERROR_RECEIVE_FAILED;
return errorCode;
}
SMGPHeadStruct head = (SMGPHeadStruct) hash.get("head");
if (head.requestId == 3 && ( (String) hash.get("result")).equals("ok")) { //接收deliver成功
if (deliverQueue(head, (byte[]) hash.get("body"), deliverQueue)) { //接收的数据正确,返回deliver_resp
head.requestId = 0x80000003;
head.packageSize = 26;
byte[] body = new byte[14];
System.arraycopy( (byte[]) hash.get("body"), 0, body, 0, 10);
body[10] = 0;
body[11] = 0;
body[12] = 0;
body[13] = 0;
dataSend = new DataOutputStream(socket.socket.getOutputStream());
send(head, body, dataSend);
receiveTotal++;
if(System.currentTimeMillis()-startTime>900) //900毫秒后退出接收
break;
}
}
else { //接收失败
errorCode = configure.ERROR_RECEIVE_FAILED;
break;
}
// }
}
catch (InterruptedIOException e) { //超时退出
break;
}
catch (IOException ioexception) {
System.err.println("socketLogic.cycleDeliver Failed I/O: " +
ioexception);
return configure.ERROR_RECEIVE_FAILED;
}
catch(Exception ioexception)
{//其它错误
System.err.println("socketLogic.cycleReceive(socketStruct socket,deliverVector deliverQueue) eXCEPTION: " + ioexception);
return configure.ERROR_RECEIVE_FAILED;
}
}}
while(true);
return receiveTotal;
// return errorCode;
}
/****************************************************************************************
*
* 将deliver数据送入相应的接收队列
*@param head、body 接受到的数据
*@param deliverQueue 数据接收队列
*/
public boolean deliverQueue(SMGPHeadStruct head,byte[] body,deliverVector deliverQueue)
{
boolean result=true;
try{
deliverStruct deliver=new deliverStruct();
deliver.setPackageLength(head.packageSize);
deliver.setRequestId(head.requestId);
deliver.setSequenceId(head.sequenceId);
int k=0;
byte[] msgId=new byte[10];
System.arraycopy(body,k,msgId,0,10);
deliver.setMsgId(msgId);
k+=10;
deliver.setIsReport(fun.byte2Int(body[k]));
k+=1;
deliver.setMsgFormat(fun.byte2Int(body[k]));
k+=1;
byte[] recvTime=new byte[14];
System.arraycopy(body,k,recvTime,0,10);
deliver.setRecvTime(recvTime);
k+=14;
byte[] srcTermId=new byte[21];
System.arraycopy(body,k,srcTermId,0,21);
deliver.setSrcTermId(srcTermId);
k+=21;
byte[] destTermId=new byte[21];
System.arraycopy(body,k,destTermId,0,21);
deliver.setDestTermId(destTermId);
k+=21;
deliver.setMsgLength(fun.byte2Int(body[k]));
k+=1;
byte[] msgContent=new byte[deliver.getMsgLength()];
System.arraycopy(body,k,msgContent,0,msgContent.length);
deliver.setMsgContent(msgContent);
k+=deliver.getMsgLength();
byte[] reserve=new byte[8];
System.arraycopy(body,k,reserve,0,8);
deliver.setReserve(reserve);
k+=8;
synchronized(deliverQueue)
{ //将接收到的数据放入接收队列
/* if(deliver.getIsReport()==1)
System.out.println("report");
else
System.out.println("deliver");*/
if (configure.QUEUE_DELIVER_MAX == 0 ||
deliverQueue.size() < configure.QUEUE_DELIVER_MAX)
deliverQueue.add(deliver);
else
result=false; //接收队列已满
}
// System.out.println("deliverQueue length="+deliverQueue.size());
}
catch(Exception e)
{
System.out.println("deliverQueue(SMGPHeadStruct head,byte[] body,deliverVector deliverQueue):"+e);
result=false;
}
return result;
}
/********************************************************************************************
*
* 接收数据,不进行任何处理
*
* dataRec接收数据
*/
public Hashtable receive(socketStruct socket)
{
DataInputStream dataRec=null;
Hashtable hash=new Hashtable();
String result="error";
SMGPHeadStruct head=new SMGPHeadStruct();
byte[] body=new byte[0];
try{
dataRec = new DataInputStream(socket.socket.getInputStream());
}
catch(IOException ioexception)
{//IO错误
System.err.println("1 socketLogic.receivet(DataInputStream dataRec) Failed I/O: " + ioexception);
result="error";
hash.put("head",head);
hash.put("body",body);
hash.put("result",result);
return hash;
}
try
{
head.packageSize = dataRec.readInt();
head.requestId = dataRec.readInt();
head.sequenceId = dataRec.readInt();
//System.out.println("head.packageSize:"+head.packageSize);
if(head.packageSize>100000||head.requestId ==0||head.packageSize <12)
{//接收倒的数据出错,需复位socket连接
socket.socket.close();
socket.socket=null;
return null;
}
body = new byte[head.packageSize-12];
result="wait"; //等待接收包体
for(int readBytes=0;readBytes<head.packageSize-12;)
{
readBytes+=dataRec.read(body, readBytes, head.packageSize-12-readBytes);
}
result="ok";
}
catch(InterruptedIOException e)
{//超时
//System.err.println("socketLogic.receivet(DataInputStream dataRec) receive timeout: " + e);
try{
if ("wait".equals(result)) { //socket超时时,数据未接收完毕,需复位socket连接
socket.socket.close();
socket.socket=null;
return null;
}
}catch(IOException ioexception){}
result="timeout";
}
catch(IOException ioexception)
{//IO错误
System.err.println(" socketLogic.receivet(DataInputStream dataRec) Failed I/O: " + ioexception);
try{
socket.socket.close(); //端口IO错误,关闭端口
socket.socket=null;
}catch(IOException ioexception1){}
result="error";
}
catch(Exception ioexception)
{//其它错误
System.err.println(" socketLogic.receivet(DataInputStream dataRec) eXCEPTION: " + ioexception);
result="error";
}
hash.put("head",head);
hash.put("body",body);
hash.put("result",result);
return hash;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -