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

View file

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

View file

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