📄 socketlogic.java
字号:
/************************************************************************************
*
* 与服务器唯一的发送接口,采用独占式发送,不自动连接;有active_test判断断线并连接
*
*/
package com.ut.smgp.api.logic;
import com.ut.smgp.api.structure.*;
import com.ut.smgp.api.configure.*;
import com.ut.smgp.api.function.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class socketLogic {
private function fun=new function();
private int error_code=0;
private initStruct currentInit=new initStruct(); //保存当前建立连接使用的配置
public socketLogic() {
}
/**********************************************************************
*
*退出
* @param SMGPHeadStruct 包头
* @return int 0:成功 1:失败
*
*/
public int exit(socketStruct socket,int timeout,deliverVector deliverQueue,int type)
{
DataInputStream dataRec;
DataOutputStream dataSend;
SMGPHeadStruct head=new SMGPHeadStruct();
head.packageSize=12;
head.requestId=6;
int errorCode=0;
byte[] body=new byte[0];
synchronized(socket)
{
try {
int startTime=(int)(System.currentTimeMillis()/1000L);
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());
if(send(head,body,dataSend)!=0)
{
return configure.ERROR_SEND_FAILED;
}
for (; ; ) { //接收response
if((int)(System.currentTimeMillis()/1000L)-startTime>timeout)
{//超时
break;
}
Hashtable hash = receive(socket);
if(hash==null) {
errorCode=configure.ERROR_RECEIVE_FAILED;
break;
}
if ( ( (String) hash.get("result")).equals("ok"))
{ //接收成功
SMGPHeadStruct h = (SMGPHeadStruct) hash.get("head");
if (h.sequenceId == head.sequenceId&&h.requestId==head.requestId+0x80000000){
return 0; //发送成功
}
else //不是要接收的response,继续接收
dealDeliver(socket,hash,deliverQueue,type); //若是deliver数据,则保存入deliver接收队列并返回response
continue;
}
else
{//接收失败
errorCode=configure.ERROR_SEND_FAILED;
break;
}
}
}
catch (IOException ioexception) {
System.err.println("socketLogic.exit Failed I/O: " + ioexception);
return configure.ERROR_SEND_FAILED;
}
catch (Exception ioexception) {
//System.err.println("socketLogic.exit1 Exception: " + ioexception);
return configure.ERROR_RECEIVE_FAILED;
}
}
return errorCode;
}
/**********************************************************************
*
*发送active数据包
* @param SMGPHeadStruct 包头
* @return int 0:成功 1:失败
*
*/
public int activeTest(socketStruct socket,int retryTimes,int timeout,deliverVector deliverQueue,int type)
{
DataInputStream dataRec;
DataOutputStream dataSend;
SMGPHeadStruct head=new SMGPHeadStruct();
head.packageSize=12;
head.requestId=4;
int errorCode=0;
byte[] body=new byte[0];
synchronized(socket)
{
try {
for(int j=0;j<retryTimes;j++)
{//retry
int startTime=(int)(System.currentTimeMillis()/1000L);
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());
if(send(head,body,dataSend)!=0)
{
errorCode=configure.ERROR_SEND_FAILED;
continue;
}
for (; ; ) { //接收response
if((int)(System.currentTimeMillis()/1000L)-startTime>timeout)
{//超时
break;
}
Hashtable hash = receive(socket);
if(hash==null) {
errorCode=configure.ERROR_RECEIVE_FAILED;
break;
}
if ( ( (String) hash.get("result")).equals("ok"))
{ //接收成功
SMGPHeadStruct h = (SMGPHeadStruct) hash.get("head");
if (h.sequenceId == head.sequenceId&&h.requestId==head.requestId+0x80000000){
return 0; //发送成功
}
else //不是要接收的response,继续接收
dealDeliver(socket,hash,deliverQueue,type); //若是deliver数据,则保存入deliver接收队列并返回response
continue;
}
else
{//接收失败
errorCode=configure.ERROR_SEND_FAILED;
break;
}
}
}
}
catch (IOException ioexception) {
System.err.println("socketLogic.activeTest Failed I/O: " + ioexception);
return configure.ERROR_SEND_FAILED;
}
catch (Exception ioexception) {
System.err.println("socketLogic.activeTest Exception: " + ioexception);
return configure.ERROR_RECEIVE_FAILED;
}
}
return errorCode;
}
/**********************************************************************
*
*发送SMGP数据包
* @param SMGPHeadStruct 包头
* @return int 0:成功 1:失败
*
*/
public int submit(SMGPHeadStruct head, byte body[],socketStruct socket,int retryTimes,int timeout,deliverVector deliverQueue,byte[] msgId,int type)
{
DataInputStream dataRec;
DataOutputStream dataSend;
int errorCode=0;
synchronized(socket)
{
try {
for(int j=0;j<retryTimes;j++)
{//retry
head.sequenceId = socket.seq++;
long startTime=System.currentTimeMillis();
if(socket.seq == 0x7fffffff)
socket.seq = 1;
socket.socket.setSoTimeout(timeout); //设置超时时间
dataRec = new DataInputStream(socket.socket.getInputStream());
dataSend = new DataOutputStream(socket.socket.getOutputStream());
int sendResult=send(head,body,dataSend);
if(sendResult!=0)
{
errorCode=sendResult;
continue;
}
for (; ; ) { //接收response
if(System.currentTimeMillis()-startTime>timeout)
{//超时
break;
}
Hashtable hash = receive(socket);
if(hash==null) {
errorCode=configure.ERROR_RECEIVE_FAILED;
break;
}
if ( ( (String) hash.get("result")).equals("ok"))
{ //接收成功
SMGPHeadStruct h = (SMGPHeadStruct) hash.get("head");
if (h.sequenceId == head.sequenceId&&h.requestId==head.requestId+0x80000000){
byte[] dat=(byte[])hash.get("body");
//int result=fun.byte2Int(dat,10,1);
int result=fun.byte2Int(dat,10,4);
if(result==0)
{
// byte[] temp=new byte[10];
System.arraycopy(dat,0,msgId,0,10);
// msgId=new String(temp);
return 0; //发送成功
}
else
{//发送失败
errorCode=result;
break;
}
}
else //不是要接收的response,继续接收
{
dealDeliver(socket,hash,deliverQueue,type); //若是deliver数据,则保存入deliver接收队列并返回response
continue;
}
}
else
{//接收失败
errorCode=configure.ERROR_SEND_FAILED;
break;
}
}
}
}
catch (IOException ioexception) {
System.err.println("socketLogic.submit Failed I/O: " + ioexception);
return configure.ERROR_SEND_FAILED;
}
}
return errorCode;
}
/*****************************************************
* 群发
*
*/
public void submitMulti(SubmitMultiStruct[] multi, socketStruct socket,
int retryTimes, int timeout,
deliverVector deliverQueue, int type) {
DataInputStream dataRec;
DataOutputStream dataSend;
int errorCode = 0;
boolean isOk = true;
synchronized (socket) {
try {
//int test = 0;
long startTime = System.currentTimeMillis(); //发送开始时间
socket.socket.setSoTimeout(timeout); //设置超时时间
dataRec = new DataInputStream(socket.socket.getInputStream());
dataSend = new DataOutputStream(socket.socket.getOutputStream());
for (int j = 0; j < retryTimes + 1; j++) { //重发
for (int i = 0; i < multi.length; i++) {
if (multi[i].result != -1 && multi[i].result != 0) { //未发送或发送失败
if (multi[i].result == -2) { //未发送
multi[i].head.sequenceId = socket.seq++;
if (socket.seq == 0x7fffffff) {
socket.seq = 1;
}
}
int sendResult = send(multi[i].head, multi[i].body, dataSend);
//System.out.println(test++);
if (sendResult == 0) {
multi[i].result = -1;
}
}
}
for (; ; ) { //接收response
if (System.currentTimeMillis() - startTime > timeout) { //超时
break;
}
Hashtable hash = receive(socket);
if (hash == null) {
errorCode = configure.ERROR_RECEIVE_FAILED;
break;
}
if ( ( (String) hash.get("result")).equals("ok")) { //接收成功
SMGPHeadStruct h = (SMGPHeadStruct) hash.get("head");
boolean bln = false;
for (int k = 0; k < multi.length; k++) {
if (h.sequenceId == multi[k].head.sequenceId &&
h.requestId == multi[k].head.requestId + 0x80000000) {
bln = true;
byte[] dat = (byte[]) hash.get("body");
int result =fun.byte2Int(dat,10,4);;
if (result == 0) {
System.arraycopy(dat, 0, multi[k].msgId, 0, 10);
// System.arraycopy(dat, 10, multi[k].congestionState, 0, 5);
multi[k].result = 0; //发送成功****************
}
else { //发送失败
errorCode = result;
multi[k].result = result;
break;
}
}
}
if (!bln) { //不是要接收的response,继续接收
dealDeliver(socket, hash, deliverQueue, type); //若是deliver数据,则保存入deliver接收队列并返回response
continue;
}
}
else { //接收失败
errorCode = configure.ERROR_SEND_FAILED;
break;
}
//重发控制
boolean blnRetry = false;
boolean blnSendAll = true;
for (int k = 0; k < multi.length; k++) {
if (multi[k].result == -1) {
blnSendAll = false;
}
if (multi[k].result != 0 && multi[k].result != -1 &&
j < retryTimes) {
// System.out.println("break");
blnRetry = true;
// break; //发送失败,且重发次数未满,则重发
}
}
if (blnRetry & blnSendAll) { //已接收全部的状态报告后再判断是否重发
break;
}
//处理结果检验
isOk = true;
for (int k = 0; k < multi.length; k++) {
if (multi[k].result != 0) {
isOk = false;
}
}
if (isOk) {
break; //全部处理成功,退出
}
}
if (isOk) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -