-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadThread.java
More file actions
139 lines (118 loc) · 2.27 KB
/
ReadThread.java
File metadata and controls
139 lines (118 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package com.fontlose.relayctrl;
import java.io.IOException;
import java.net.Socket;
import android.os.Message;
import android.util.Log;
public class ReadThread extends Thread {
boolean state;
HandleMsg hOptMsg;
byte[] sData;
Socket socket;
public ReadThread(HandleMsg hmsg,byte[] sData,Socket socket)
{
hOptMsg=hmsg;
this.sData=sData;
this.socket=socket;
}
/*
* @see java.lang.Thread#run()
* 线程接收主循环
*/
public void run()
{
int rlRead;
state=true;
try
{
while(state)
{
rlRead=socket.getInputStream().read(sData);//对方断开返回-1
if(rlRead>0)
{
//RelayCtrlActivity.showMessage(sData.toString());
unpackageCmd(sData,rlRead);
}
else
{
state=false;
hOptMsg.sendEmptyMessage(DataProcess.CLOSETCP);
break;
}
}
}
catch (Exception e) {
Log.v("tcpserver",e.getMessage());
state=false;
hOptMsg.sendEmptyMessage(DataProcess.CLOSETCP);
}
}
public void abortRead()
{
if(socket==null) return;
try
{
socket.shutdownInput();
socket.shutdownOutput();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
state = false;
}
public void unpackageCmd(byte[] cmd,int len)
{
int size=len;
if(size<=0) return;
int i;
boolean cmdStart=false;
int cmdInx = 0;
byte sum=0;
for(i=0;i<size;i++)
{
if(cmdStart==false)
{//22 01 00 01 01 01 02 23
if(cmd[i]==0x22)
{
cmdStart=true;
cmdInx =0;
sum=0x22;
}
}
else
{
cmdInx++;
switch(cmdInx)
{
case 1:
if(cmd[i]!=0x01) cmdStart=false;
sum+=cmd[i];
break;
case 2:
if((cmd[i]&0xFE)!=0x00) cmdStart=false;
sum+=cmd[i];
break;
case 3:
case 4:
case 5:
case 6:
sum+=cmd[i];
break;
case 7:
if(cmd[i]==sum)
{
if(hOptMsg==null) return;
Message msg=new Message();
msg.what=DataProcess.RELAYSTATE;
msg.arg1=cmd[i-1];
msg.arg1=(msg.arg1<<8)|cmd[i-2];
msg.arg1=(msg.arg1<<8)|cmd[i-3];
msg.arg1=(msg.arg1<<8)|cmd[i-4];
hOptMsg.sendMessage(msg);
}
cmdStart=false;
break;
}
}
}
}
}