This commit is contained in:
Gangphon 2024-06-06 22:11:01 +08:00
parent db4c46e7e9
commit d6917767b4
2 changed files with 39 additions and 19 deletions

View File

@ -11,7 +11,7 @@ android {
minSdk 21
targetSdk 34
versionCode 1
versionName "2.1.1"
versionName "2.1.2"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

View File

@ -20,8 +20,10 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
@ -37,6 +39,8 @@ public class SocketThread extends Thread {
Socket socket;
InputStream in;
OutputStream out;
SimpleDateFormat fmt = new SimpleDateFormat("MM-dd HH:mm:ss");
public SocketThread(MainActivity main, Socket socket) {
this.main = main;
this.socket = socket;
@ -48,22 +52,26 @@ public class SocketThread extends Thread {
Util.socketThreads.add(this);
in = socket.getInputStream();
out = socket.getOutputStream();
HashSet<String> hases = null;
HashSet<String> existed = null;
ByteArrayOutputStream progJson = null;
while(true) {
var obj = JSMap.from(in);
var _type = obj.stnn("_type");
Util.println("_type: "+_type);
if("consult".equals(_type)) {
JSList<String> ids = obj.jslist("idList");
hases = new HashSet<>();
if(ids!=null) for(int i=0; i<ids.size(); i++) {
if(new File(Util.programDir + "/" + ids.get(i)).exists()) ids.remove(i--);
else hases.add(ids.get(i));
JSList<JSMap> files = obj.jslist("files");
if(files==null) new JSMap("_type", _type, "idList", obj.jslist("idList")).write(out);
else {
existed = new HashSet<>();
for(var fil : files) {
var name = fil.stnn("name");
var file = new File(Util.programDir + "/" + name);
if(file.isFile() && file.length()==fil.intg("size")) existed.add(name);
}
new JSMap("_type", _type, "existed", existed).write(out);
}
new JSMap("_type", _type, "idList", ids).write(out);
} else if("proStart".equals(_type)) {
Util.deleteFiles(obj.intg("proSize"), hases);
Util.deleteFiles(obj.intg("proSize"), existed);
} else if("fileStart".equals(_type)) {
var size = obj.intg("size");
var name = obj.stnn("id");
@ -73,10 +81,15 @@ public class SocketThread extends Thread {
IOs.writeCloseOut(progJson, in, size);
} else {
var fOut = new FileOutputStream(Util.programDir + "/" + name);
IOs.write(fOut, in, size);
fOut.flush();
fOut.getFD().sync();
fOut.close();
try {
IOs.write(fOut, in, size);
fOut.flush();
fOut.getFD().sync();
fOut.close();
} catch(Throwable e) {
fOut.close();
new File(Util.programDir + "/" + name).delete();
}
}
} else if("imgFileStart".equals(_type)) {
var size = obj.intg("size");
@ -127,8 +140,8 @@ public class SocketThread extends Thread {
var Fmt = new SimpleDateFormat("yy-MM-dd HH:mm:ss.SSS");
var dur = 0;
if(main.progView!=null) for(var page : main.progView.pages) dur += page.tDur;
writer.append("ProgSend: ").append(Fmt.format(new File(Util.programDir + "/program").lastModified())).append(" ProgDur: ").append(String.valueOf(dur));
if(main.progView!=null) writer.append(" Pages: ").append(String.valueOf(main.progView.avas.size())).append(" / ").append(String.valueOf(main.progView.pages.size()));
writer.append("ProgSend: ").append(Fmt.format(new File(Util.programDir + "/program").lastModified())).append(" ProgDur: ").append(String.valueOf(dur)).append("ms");
if(main.progView!=null) writer.append(" Size Avas: ").append(String.valueOf(main.progView.avas.size())).append(" Pages: ").append(String.valueOf(main.progView.pages.size()));
writer.append("\n");
writer.append(" Launch: ").append(Fmt.format(main.launchMilli)).append("\n");
writer.append(" Sync: ").append(Fmt.format(main.syncMs)).append("\n");
@ -207,20 +220,27 @@ public class SocketThread extends Thread {
if(files == null) return;
Arrays.sort(files, (f1, f2) -> (int) (f2.lastModified() - f1.lastModified()));
var writer = new OutputStreamWriter(out);
for(var file : files) writer.append(file.getName()).append(' ').append(String.valueOf(file.length())).append('\n');
for(var file : files) writer.append(fmt.format(new Date(file.lastModified()))).append(' ').append(file.getName()).append(' ').append(String.valueOf(file.length())).append('\n');
writer.append('\n');
writer.flush();
} else if("GetFile".equals(_type)) {
var name = obj.str("name");
if(name!=null) IOs.writeCloseIn(out, new FileInputStream(Util.programDir+"/"+name));
else new JSMap("code", 1, "msg", "name is null").write(out);
if(name==null) new JSMap("msg", "name is null").write(out);
else {
var file = new File(Util.programDir+"/"+name);
if(! file.isFile()) new JSMap("msg", "file not exist").write(out);
else {
new JSMap("len", file.length()).write(out);
IOs.writeCloseIn(out, new FileInputStream(file));
}
}
}
out.flush();
Util.println("cmd end");
}
} catch (Throwable e) {
var emsg = e.getMessage();
if(emsg!=null && ("Socket closed".equals(emsg) || emsg.endsWith("end-of-input"))) {
if(e instanceof SocketTimeoutException || "Socket closed".equals(emsg) || (emsg!=null && emsg.endsWith("end-of-input"))) {
Util.println(emsg);
return;
}