mirror of
https://github.com/netfun2000/rttys_zhaojh329.git
synced 2026-02-27 09:53:24 +08:00
perf: Write file data to local while transferring
Before, the download was started after all file data were sent from the device to the browser. Now, the download starts when the file data begins to transfer. Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"rttys/client"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
@@ -32,6 +33,15 @@ const (
|
||||
msgTypeMax = msgTypeAck
|
||||
)
|
||||
|
||||
const (
|
||||
msgTypeFileSend = iota
|
||||
msgTypeFileRecv
|
||||
msgTypeFileInfo
|
||||
msgTypeFileData
|
||||
msgTypeFileAck
|
||||
msgTypeFileAbort
|
||||
)
|
||||
|
||||
const rttyProto uint8 = 3
|
||||
const heartbeatInterval = time.Second * 5
|
||||
|
||||
@@ -56,6 +66,50 @@ type termMessage struct {
|
||||
data []byte
|
||||
}
|
||||
|
||||
type fileMessage struct {
|
||||
sid string
|
||||
data []byte
|
||||
}
|
||||
|
||||
type fileProxy struct {
|
||||
reader *io.PipeReader
|
||||
writer *io.PipeWriter
|
||||
}
|
||||
|
||||
func (fp *fileProxy) Read(b []byte) (int, error) {
|
||||
return fp.reader.Read(b)
|
||||
}
|
||||
|
||||
func (fp *fileProxy) Write(dev client.Client, sid string, b []byte) {
|
||||
go func() {
|
||||
_, err := fp.writer.Write(b)
|
||||
if err != nil {
|
||||
fp.Cancel(dev, sid)
|
||||
dev.(*device).br.fileProxy.Delete(sid)
|
||||
return
|
||||
}
|
||||
fp.Ack(dev, sid)
|
||||
}()
|
||||
}
|
||||
|
||||
func (fp *fileProxy) Close() {
|
||||
fp.writer.Close()
|
||||
}
|
||||
|
||||
func (fp *fileProxy) Cancel(dev client.Client, sid string) {
|
||||
b := make([]byte, 33)
|
||||
copy(b, sid)
|
||||
b[32] = msgTypeFileAbort
|
||||
dev.WriteMsg(msgTypeFile, b)
|
||||
}
|
||||
|
||||
func (fp *fileProxy) Ack(dev client.Client, sid string) {
|
||||
b := make([]byte, 33)
|
||||
copy(b, sid)
|
||||
b[32] = msgTypeFileAck
|
||||
dev.WriteMsg(msgTypeFile, b)
|
||||
}
|
||||
|
||||
type loginAckMsg struct {
|
||||
devid string
|
||||
sid string
|
||||
@@ -251,16 +305,12 @@ func (dev *device) readLoop() {
|
||||
|
||||
sid := string(b[:32])
|
||||
|
||||
b = b[31:]
|
||||
|
||||
if typ == msgTypeFile {
|
||||
b[0] = 1
|
||||
dev.br.fileMessage <- &fileMessage{sid, b[32:]}
|
||||
} else {
|
||||
b[0] = 0
|
||||
dev.br.termMessage <- &termMessage{sid, b[32:]}
|
||||
}
|
||||
|
||||
dev.br.termMessage <- &termMessage{sid, b}
|
||||
|
||||
case msgTypeCmd:
|
||||
if msgLen < 1 {
|
||||
log.Error().Msg("msgTypeCmd: invalid")
|
||||
|
||||
Reference in New Issue
Block a user