DownloadFileReceiverThread.java
5.11 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
130
131
132
133
134
135
136
package com.sunvote.xpadcomm;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import com.sunvote.util.LogUtil;
/**
* Created by Elvis on 2017/12/25 15:55
* Email:Eluis@psunsky.com
* 版权所有:长沙中天电子设计开发有限公司
* Description: 人大通用版XPadAppRD
* 下载文件,接收数据线程
*/
public class DownloadFileReceiverThread extends Thread {
public static final String TAG = "ReceiverThread";
public static final int DEFAULT_PROT = 15155;
private boolean run = true;
private DatagramSocket udpSocketServ;
private int port = DEFAULT_PROT;
private final int BLOCK_SIZE = 1024 * 50;
private byte[] udpRecvBuf;
public DownloadFileReceiverThread(int port) {
super(TAG);
this.port = port;
}
public interface OnDownloadCmdListener {
/**
* 文件信息
* @param filename 文件名
* @param filelength 文件长度
* @param fileFlag 文件标志
*/
void onReceiverFileInfo(String filename, long filelength, byte fileFlag);
/**
* 文件数据包
* @param blockId 第几包
* @param datas 数据段落
* @param start 起始位置
* @param dataLen 数据长度
*/
void onReceiverFileData(int blockId, byte[] datas, int start, int dataLen);
/**
* 文件下载完成
*/
void onFileDownloadCompeleted();
}
@Override
public void run() {
udpRecvBuf = new byte[BLOCK_SIZE + 8];
DatagramPacket packet = new DatagramPacket(udpRecvBuf, udpRecvBuf.length);
int cmdType;
long fileLen;
while (isRun()) {
if (udpSocketServ == null) {
try {
udpSocketServ = new DatagramSocket(port);
udpSocketServ.setSoTimeout(15000);
} catch (Exception ex) {
LogUtil.e(TAG, ex);
udpSocketServ = null;
}
}
if (udpSocketServ != null) {
try {
udpSocketServ.receive(packet);
if (packet.getLength() > 0) {
LogUtil.i(TAG, "receiver data: ", packet.getData(), packet.getLength());
if ((udpRecvBuf[0] & 0xff) == 0xFE && (udpRecvBuf[1] & 0xff) == 0xAA) {
byte fileFlag = (byte) (udpRecvBuf[2] & 0xff);
cmdType = udpRecvBuf[3];
if (cmdType == 1) { //文件信息
fileLen = ((udpRecvBuf[8] & 0xff) << 3 * 8)
| ((udpRecvBuf[9] & 0xff) << 2 * 8)
| ((udpRecvBuf[10] & 0xff) << 1 * 8)
| (udpRecvBuf[11] & 0xff);
byte fileNameLen = udpRecvBuf[12];
byte[] namebuf = new byte[fileNameLen];
System.arraycopy(udpRecvBuf, 13, namebuf, 0, fileNameLen);
String fileName = new String(namebuf, "unicode");
LogUtil.d(TAG, "File Len:" + fileLen + ",fileName:" + fileName + ", fileFlag:" + fileFlag);
if(onDownloadCmdListener != null){
onDownloadCmdListener.onReceiverFileInfo(fileName,fileLen,fileFlag);
}
} else if (cmdType == 2) {
int blockId = ((udpRecvBuf[4] & 0xff) << 8) | (udpRecvBuf[5] & 0xff);
int dataLen = ((udpRecvBuf[6] & 0xff) << 8) | (udpRecvBuf[7] & 0xff);
LogUtil.i(TAG, "Rece blockid:" + blockId + " ,datalen:" + dataLen);
if(onDownloadCmdListener != null){
onDownloadCmdListener.onReceiverFileData(blockId,udpRecvBuf,8,dataLen);
}
}else if(cmdType == 3){
if(onDownloadCmdListener != null){
onDownloadCmdListener.onFileDownloadCompeleted();
}
}
}
}
} catch (IOException e) {
LogUtil.e(TAG, e);
if(udpSocketServ != null) {
udpSocketServ.close();
udpSocketServ = null;
}
}
}
}
LogUtil.i(TAG,"receiver thread finish.");
}
public void close() {
run = false;
if(udpSocketServ != null){
udpSocketServ.close();
udpSocketServ = null;
}
}
public boolean isRun() {
return run;
}
private OnDownloadCmdListener onDownloadCmdListener;
public void setOnDownloadCmdListener(OnDownloadCmdListener onDownloadCmdListener) {
this.onDownloadCmdListener = onDownloadCmdListener;
}
}