1
1
Fork 0
mirror of https://github.com/schollz/croc.git synced 2025-10-11 13:21:00 +02:00

added bar to receiver

This commit is contained in:
Zack Scholl 2019-04-10 09:34:41 -07:00
parent c45c7b00d7
commit 0627bbd83c
2 changed files with 35 additions and 8 deletions

View file

@ -393,7 +393,7 @@ func (c *Client) processMessage(m Message) (err error) {
}.String()).Err() }.String()).Err()
// start receiving data // start receiving data
pathToFile := path.Join(c.FilesToTransfer[c.FilesToTransferCurrentNum].FolderRemote, c.FilesToTransfer[c.FilesToTransferCurrentNum].Name) pathToFile := path.Join(c.FilesToTransfer[c.FilesToTransferCurrentNum].FolderRemote, c.FilesToTransfer[c.FilesToTransferCurrentNum].Name)
c.recvSess.ReceiveData(pathToFile) c.recvSess.ReceiveData(pathToFile, c.FilesToTransfer[c.FilesToTransferCurrentNum].Size)
fmt.Println("\ndone receiving") fmt.Println("\ndone receiving")
err = c.redisdb.Publish(c.nameOutChannel, Message{ err = c.redisdb.Publish(c.nameOutChannel, Message{
Type: "close-sender", Type: "close-sender",

View file

@ -4,11 +4,13 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"time"
"github.com/mattn/go-colorable" "github.com/mattn/go-colorable"
"github.com/pion/webrtc/v2" "github.com/pion/webrtc/v2"
internalSess "github.com/schollz/croc/v5/src/webrtc/internal/session" internalSess "github.com/schollz/croc/v5/src/webrtc/internal/session"
"github.com/schollz/croc/v5/src/webrtc/pkg/session/common" "github.com/schollz/croc/v5/src/webrtc/pkg/session/common"
"github.com/schollz/progressbar/v2"
logrus "github.com/sirupsen/logrus" logrus "github.com/sirupsen/logrus"
) )
@ -118,14 +120,26 @@ func (s *Session) CreateDataHandler() {
}) })
} }
func (s *Session) ReceiveData(pathToFile string) { func (s *Session) ReceiveData(pathToFile string, fileSize int64) {
s.receiveData(pathToFile) s.receiveData(pathToFile, fileSize)
s.sess.OnCompletion() s.sess.OnCompletion()
} }
func (s *Session) receiveData(pathToFile string) error { func (s *Session) receiveData(pathToFile string, fileSize int64) error {
log.Debugln("Starting to receive data...") log.Debugln("Starting to receive data...")
log.Debugf("receiving %s", pathToFile) log.Debugf("receiving %s", pathToFile)
// truncate if nessecary
stat, errStat := os.Stat(pathToFile)
if errStat == nil {
if stat.Size() != fileSize {
err := os.Truncate(pathToFile, fileSize)
if err != nil {
return err
}
}
}
f, err := os.OpenFile(pathToFile, os.O_RDWR|os.O_CREATE, 0755) f, err := os.OpenFile(pathToFile, os.O_RDWR|os.O_CREATE, 0755)
if err != nil { if err != nil {
return err return err
@ -134,6 +148,9 @@ func (s *Session) receiveData(pathToFile string) error {
log.Debugln("Stopped receiving data...") log.Debugln("Stopped receiving data...")
f.Close() f.Close()
}() }()
firstByte := true
var bar *progressbar.ProgressBar
// Consume the message channel, until done // Consume the message channel, until done
// Does not stop on error // Does not stop on error
for { for {
@ -144,13 +161,23 @@ func (s *Session) receiveData(pathToFile string) error {
return nil return nil
case msg := <-s.msgChannel: case msg := <-s.msgChannel:
n, err := f.Write(msg.Data) n, err := f.Write(msg.Data)
if err != nil { if err != nil {
return err return err
} else { } else {
currentSpeed := s.sess.NetworkStats.Bandwidth() if firstByte {
fmt.Printf("Transferring at %.2f MB/s\r", currentSpeed) bar = progressbar.NewOptions64(
s.sess.NetworkStats.AddBytes(uint64(n)) fileSize,
progressbar.OptionSetRenderBlankState(true),
progressbar.OptionSetBytes64(fileSize),
progressbar.OptionSetWriter(os.Stderr),
progressbar.OptionThrottle(1/60*time.Second),
)
firstByte = false
}
bar.Add(n)
// currentSpeed := s.sess.NetworkStats.Bandwidth()
// fmt.Printf("Transferring at %.2f MB/s\r", currentSpeed)
// s.sess.NetworkStats.AddBytes(uint64(n))
} }
} }
} }