?? 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 + -