mirror of
https://github.com/schollz/croc.git
synced 2025-10-11 13:21:00 +02:00
fix receiver
This commit is contained in:
parent
8645685173
commit
b8689c8eeb
3 changed files with 24 additions and 24 deletions
|
@ -2,6 +2,7 @@ package receiver
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/pion/webrtc/v2"
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
@ -51,15 +52,21 @@ func (s *Session) CreateDataHandler() {
|
|||
})
|
||||
}
|
||||
|
||||
func (s *Session) ReceiveData() {
|
||||
s.receiveData()
|
||||
func (s *Session) ReceiveData(pathToFile string) {
|
||||
s.receiveData(pathToFile)
|
||||
s.sess.OnCompletion()
|
||||
}
|
||||
|
||||
func (s *Session) receiveData() {
|
||||
func (s *Session) receiveData(pathToFile string) error {
|
||||
log.Infoln("Starting to receive data...")
|
||||
defer log.Infoln("Stopped receiving data...")
|
||||
|
||||
f, err := os.OpenFile(pathToFile, os.O_RDWR|os.O_CREATE, 0755)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
log.Infoln("Stopped receiving data...")
|
||||
f.Close()
|
||||
}()
|
||||
// Consume the message channel, until done
|
||||
// Does not stop on error
|
||||
for {
|
||||
|
@ -67,12 +74,12 @@ func (s *Session) receiveData() {
|
|||
case <-s.sess.Done:
|
||||
s.sess.NetworkStats.Stop()
|
||||
fmt.Printf("\nNetwork: %s\n", s.sess.NetworkStats.String())
|
||||
return
|
||||
return nil
|
||||
case msg := <-s.msgChannel:
|
||||
n, err := s.stream.Write(msg.Data)
|
||||
n, err := f.Write(msg.Data)
|
||||
|
||||
if err != nil {
|
||||
log.Errorln(err)
|
||||
return err
|
||||
} else {
|
||||
currentSpeed := s.sess.NetworkStats.Bandwidth()
|
||||
fmt.Printf("Transferring at %.2f MB/s\r", currentSpeed)
|
||||
|
@ -80,6 +87,7 @@ func (s *Session) receiveData() {
|
|||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Session) CreateConnection() (err error) {
|
||||
|
|
|
@ -3,31 +3,29 @@ package receiver
|
|||
import (
|
||||
"io"
|
||||
|
||||
"github.com/pion/webrtc/v2"
|
||||
internalSess "github.com/schollz/croc/v5/internal/session"
|
||||
"github.com/schollz/croc/v5/pkg/session/common"
|
||||
"github.com/pion/webrtc/v2"
|
||||
)
|
||||
|
||||
// Session is a receiver session
|
||||
type Session struct {
|
||||
sess internalSess.Session
|
||||
stream io.Writer
|
||||
msgChannel chan webrtc.DataChannelMessage
|
||||
initialized bool
|
||||
}
|
||||
|
||||
func new(s internalSess.Session, f io.Writer) *Session {
|
||||
func new(s internalSess.Session) *Session {
|
||||
return &Session{
|
||||
sess: s,
|
||||
stream: f,
|
||||
msgChannel: make(chan webrtc.DataChannelMessage, 4096*2),
|
||||
initialized: false,
|
||||
}
|
||||
}
|
||||
|
||||
// New creates a new receiver session
|
||||
func New(f io.Writer) *Session {
|
||||
return new(internalSess.New(nil, nil), f)
|
||||
func New() *Session {
|
||||
return new(internalSess.New(nil, nil))
|
||||
}
|
||||
|
||||
// Config contains custom configuration for a session
|
||||
|
@ -38,10 +36,5 @@ type Config struct {
|
|||
|
||||
// NewWith createa a new receiver Session with custom configuration
|
||||
func NewWith(c Config) *Session {
|
||||
return new(internalSess.New(c.SDPProvider, c.SDPOutput), c.Stream)
|
||||
}
|
||||
|
||||
// SetStream changes the stream, useful for WASM integration
|
||||
func (s *Session) SetStream(stream io.Writer) {
|
||||
s.stream = stream
|
||||
return new(internalSess.New(c.SDPProvider, c.SDPOutput))
|
||||
}
|
||||
|
|
|
@ -390,7 +390,8 @@ func (c *Client) processMessage(m Message) (err error) {
|
|||
Num: m.Num,
|
||||
}.String()).Err()
|
||||
// start receiving data
|
||||
c.recvSess.ReceiveData()
|
||||
pathToFile := path.Join(c.FilesToTransfer[c.FilesToTransferCurrentNum].FolderRemote, c.FilesToTransfer[c.FilesToTransferCurrentNum].Name)
|
||||
c.recvSess.ReceiveData(pathToFile)
|
||||
|
||||
case "datachannel-answer":
|
||||
c.log.Debug("got answer:", m.Message)
|
||||
|
@ -533,9 +534,7 @@ func (c *Client) dataChannelReceive() (err error) {
|
|||
return err
|
||||
}
|
||||
|
||||
c.recvSess = recvSess.NewWith(recvSess.Config{
|
||||
Stream: c.CurrentFile,
|
||||
})
|
||||
c.recvSess = recvSess.NewWith(recvSess.Config{})
|
||||
|
||||
err = c.recvSess.CreateConnection()
|
||||
if err != nil {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue