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

don't send stream

This commit is contained in:
Zack Scholl 2019-04-08 14:55:40 -07:00
parent dbe9f0df02
commit 8645685173
3 changed files with 19 additions and 21 deletions

View file

@ -3,14 +3,20 @@ package sender
import (
"fmt"
"io"
"os"
log "github.com/sirupsen/logrus"
)
func (s *Session) readFile() {
func (s *Session) readFile(pathToFile string) error {
f, err := os.Open(pathToFile)
if err != nil {
return err
}
log.Infof("Starting to read data...")
s.readingStats.Start()
defer func() {
f.Close()
s.readingStats.Pause()
log.Infof("Stopped reading data...")
close(s.output)
@ -19,15 +25,15 @@ func (s *Session) readFile() {
for {
// Read file
s.dataBuff = s.dataBuff[:cap(s.dataBuff)]
n, err := s.stream.Read(s.dataBuff)
n, err := f.Read(s.dataBuff)
if err != nil {
if err == io.EOF {
s.readingStats.Stop()
log.Debugf("Got EOF after %v bytes!\n", s.readingStats.Bytes())
return
return nil
}
log.Errorf("Read Error: %v\n", err)
return
return err
}
s.dataBuff = s.dataBuff[:n]
s.readingStats.AddBytes(uint64(n))
@ -38,6 +44,7 @@ func (s *Session) readFile() {
buff: append([]byte(nil), s.dataBuff...),
}
}
return nil
}
func (s *Session) onBufferedAmountLow() func() {

View file

@ -1,7 +1,6 @@
package sender
import (
"io"
"sync"
"github.com/pion/webrtc/v2"
@ -23,7 +22,6 @@ type outputMsg struct {
// Session is a sender session
type Session struct {
sess internalSess.Session
stream io.Reader
initialized bool
dataChannel *webrtc.DataChannel
@ -40,10 +38,9 @@ type Session struct {
}
// New creates a new sender session
func new(s internalSess.Session, f io.Reader) *Session {
func new(s internalSess.Session) *Session {
return &Session{
sess: s,
stream: f,
initialized: false,
dataBuff: make([]byte, senderBuffSize),
stopSending: make(chan struct{}, 1),
@ -54,24 +51,18 @@ func new(s internalSess.Session, f io.Reader) *Session {
}
// New creates a new receiver session
func New(f io.Reader) *Session {
return new(internalSess.New(nil, nil), f)
func New() *Session {
return new(internalSess.New(nil, nil))
}
// Config contains custom configuration for a session
type Config struct {
common.Configuration
Stream io.Reader // The Stream to read from
}
// NewWith createa a new sender 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.Reader) {
s.stream = stream
return new(internalSess.New(c.SDPProvider, c.SDPOutput))
}
func (s *Session) CreateConnection() (err error) {
@ -86,7 +77,7 @@ func (s *Session) SetSDP(sdp string) error {
return s.sess.SetSDP(sdp)
}
func (s *Session) TransferFile() {
s.readFile()
func (s *Session) TransferFile(pathToFile string) {
s.readFile(pathToFile)
s.sess.OnCompletion()
}

View file

@ -396,7 +396,8 @@ func (c *Client) processMessage(m Message) (err error) {
c.log.Debug("got answer:", m.Message)
// Apply the answer as the remote description
err = c.sendSess.SetSDP(m.Message)
c.sendSess.TransferFile()
pathToFile := path.Join(c.FilesToTransfer[c.FilesToTransferCurrentNum].FolderSource, c.FilesToTransfer[c.FilesToTransferCurrentNum].Name)
c.sendSess.TransferFile(pathToFile)
case "close-sender":
c.peerConnection[m.Num].Close()
c.peerConnection[m.Num] = nil
@ -551,7 +552,6 @@ func (c *Client) dataChannelSend() (err error) {
return
}
c.sendSess = sendSess.NewWith(sendSess.Config{
Stream: c.CurrentFile,
Configuration: common.Configuration{
OnCompletion: func() {
},