mirror of
https://github.com/schollz/croc.git
synced 2025-10-11 13:21:00 +02:00
increase buffer
This commit is contained in:
parent
56f9d576ff
commit
a1b5de3abe
3 changed files with 112 additions and 83 deletions
2
go.mod
2
go.mod
|
@ -5,12 +5,12 @@ go 1.12
|
|||
require (
|
||||
github.com/go-redis/redis v6.15.2+incompatible
|
||||
github.com/mattn/go-colorable v0.1.1
|
||||
github.com/pions/pkg v0.0.0-20181115215726-b60cd756f712 // indirect
|
||||
github.com/pions/webrtc v1.2.1-0.20190404195536-1202dbaa06ad
|
||||
github.com/schollz/mnemonicode v1.0.1
|
||||
github.com/schollz/pake v1.1.0
|
||||
github.com/schollz/progressbar/v2 v2.11.0
|
||||
github.com/sirupsen/logrus v1.4.1
|
||||
github.com/stretchr/testify v1.3.0
|
||||
golang.org/x/crypto v0.0.0-20190404164418-38d8ce5564a5
|
||||
golang.org/x/net v0.0.0-20181220203305-927f97764cc3 // indirect
|
||||
)
|
||||
|
|
14
go.sum
14
go.sum
|
@ -1,12 +1,17 @@
|
|||
github.com/cheekybits/genny v1.0.0 h1:uGGa4nei+j20rOSeDeP5Of12XVm7TGUd4dJA9RDitfE=
|
||||
github.com/cheekybits/genny v1.0.0/go.mod h1:+tQajlRqAUrPI7DOSpB0XAqZYtQakVtB7wXkRAgjxjQ=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
|
||||
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
|
||||
github.com/go-redis/redis v6.15.2+incompatible h1:9SpNVG76gr6InJGxoZ6IuuxaCOQwDAhzyXg+Bs+0Sb4=
|
||||
github.com/go-redis/redis v6.15.2+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
|
||||
github.com/golang/mock v1.2.0 h1:28o5sBqPkBsMGnC6b4MvE2TzSr5/AT4c/1fLqVGIwlk=
|
||||
github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
|
||||
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
|
||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
|
||||
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
|
||||
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
|
||||
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
||||
|
@ -21,7 +26,9 @@ github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd
|
|||
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
|
||||
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw=
|
||||
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||
github.com/onsi/ginkgo v1.7.0 h1:WSHQ+IS43OoUrWtD1/bbclrwK8TTH5hzp+umCiuxHgs=
|
||||
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||
github.com/onsi/gomega v1.4.3 h1:RE1xgDvH7imwFD45h+u2SgIfERHlS2yNG4DObb5BSKU=
|
||||
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
|
||||
github.com/pions/datachannel v1.2.1 h1:zbSwtPqVcjqUM81A3VTR7lGfeAnQgxJhheXSfBk16SM=
|
||||
github.com/pions/datachannel v1.2.1/go.mod h1:Vd79tKnghclBJLCYNqN0xQzw3u3P2N/7QHYoLCLh2+Q=
|
||||
|
@ -62,6 +69,7 @@ github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
|
|||
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
|
||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/schollz/mnemonicode v1.0.1 h1:LiH5hwADZwjwnfXsaD4xgnMyTAtaKHN+e5AyjRU6WSU=
|
||||
github.com/schollz/mnemonicode v1.0.1/go.mod h1:cl4UAOhUV0mkdjMj/QYaUZbZZdF8BnOqoz8rHMzwboY=
|
||||
|
@ -74,6 +82,7 @@ github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMB
|
|||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/tscholl2/siec v0.0.0-20180721101609-21667da05937 h1:lhssCpSe3TjKcbvUoPzFMuv9oUyZDgI3Cmgolfw2C90=
|
||||
github.com/tscholl2/siec v0.0.0-20180721101609-21667da05937/go.mod h1:KL9+ubr1JZdaKjgAaHr+tCytEncXBa1pR6FjbTsOJnw=
|
||||
|
@ -86,6 +95,7 @@ golang.org/x/crypto v0.0.0-20190404164418-38d8ce5564a5/go.mod h1:WFFai1msRO1wXaE
|
|||
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20181220203305-927f97764cc3 h1:eH6Eip3UpmR+yM/qI9Ijluzb1bNv/cAU/n+6l8tRSis=
|
||||
golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA=
|
||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
|
@ -96,7 +106,11 @@ golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e h1:nFYrTHrdrAOpShe27kaFHjsqY
|
|||
golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
|
||||
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
|
||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
|
||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
|
||||
gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
|
||||
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
|
|
179
src/croc/croc.go
179
src/croc/croc.go
|
@ -26,7 +26,8 @@ import (
|
|||
|
||||
var log = logrus.New()
|
||||
|
||||
const BufferSize = 4096 * 2
|
||||
const BufferSize = 4096
|
||||
const Channels = 2
|
||||
|
||||
func init() {
|
||||
log.SetFormatter(&logrus.TextFormatter{ForceColors: true})
|
||||
|
@ -63,16 +64,20 @@ type Client struct {
|
|||
nameInChannel string
|
||||
|
||||
// webrtc connections
|
||||
peerConnection *webrtc.PeerConnection
|
||||
dataChannel *webrtc.DataChannel
|
||||
peerConnection [8]*webrtc.PeerConnection
|
||||
dataChannel [8]*webrtc.DataChannel
|
||||
|
||||
quit chan bool
|
||||
bar *progressbar.ProgressBar
|
||||
|
||||
mutex *sync.Mutex
|
||||
quit chan bool
|
||||
}
|
||||
|
||||
type Message struct {
|
||||
Type string `json:"t,omitempty"`
|
||||
Message string `json:"m,omitempty"`
|
||||
Bytes []byte `json:"b,omitempty"`
|
||||
Num int `json:"n,omitempty"`
|
||||
}
|
||||
|
||||
type Chunk struct {
|
||||
|
@ -158,6 +163,7 @@ func New(sender bool, sharedSecret string) (c *Client, err error) {
|
|||
})
|
||||
}
|
||||
|
||||
c.mutex = &sync.Mutex{}
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -275,7 +281,7 @@ func (c *Client) transfer(options TransferOptions) (err error) {
|
|||
|
||||
func (c *Client) sendOverRedis() (err error) {
|
||||
go func() {
|
||||
bar := progressbar.NewOptions(
|
||||
c.bar = progressbar.NewOptions(
|
||||
int(c.FilesToTransfer[c.FilesToTransferCurrentNum].Size),
|
||||
progressbar.OptionSetRenderBlankState(true),
|
||||
progressbar.OptionSetBytes(int(c.FilesToTransfer[c.FilesToTransferCurrentNum].Size)),
|
||||
|
@ -290,7 +296,7 @@ func (c *Client) sendOverRedis() (err error) {
|
|||
for {
|
||||
buf := make([]byte, 4096*128)
|
||||
n, errRead := c.CurrentFile.Read(buf)
|
||||
bar.Add(n)
|
||||
c.bar.Add(n)
|
||||
chunk := Chunk{
|
||||
Bytes: buf[:n],
|
||||
Location: location,
|
||||
|
@ -363,16 +369,16 @@ func (c *Client) processMessage(m Message) (err error) {
|
|||
if err != nil {
|
||||
return
|
||||
}
|
||||
c.log.Debug("got offer:", m.Message)
|
||||
c.log.Debugf("got offer for %d:", m.Num)
|
||||
// Set the remote SessionDescription
|
||||
err = c.peerConnection.SetRemoteDescription(offer)
|
||||
err = c.peerConnection[m.Num].SetRemoteDescription(offer)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Sets the LocalDescription, and starts our UDP listeners
|
||||
var answer webrtc.SessionDescription
|
||||
answer, err = c.peerConnection.CreateAnswer(nil)
|
||||
answer, err = c.peerConnection[m.Num].CreateAnswer(nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -381,6 +387,7 @@ func (c *Client) processMessage(m Message) (err error) {
|
|||
err = c.redisdb.Publish(c.nameOutChannel, Message{
|
||||
Type: "datachannel-answer",
|
||||
Message: Encode(answer),
|
||||
Num: m.Num,
|
||||
}.String()).Err()
|
||||
case "datachannel-answer":
|
||||
c.log.Debug("got answer:", m.Message)
|
||||
|
@ -391,23 +398,24 @@ func (c *Client) processMessage(m Message) (err error) {
|
|||
return
|
||||
}
|
||||
// Apply the answer as the remote description
|
||||
err = c.peerConnection.SetRemoteDescription(answer)
|
||||
err = c.peerConnection[m.Num].SetRemoteDescription(answer)
|
||||
case "close-sender":
|
||||
c.peerConnection.Close()
|
||||
c.peerConnection = nil
|
||||
c.dataChannel.Close()
|
||||
c.dataChannel = nil
|
||||
c.Step4FileTransfer = false
|
||||
c.Step3RecipientRequestFile = false
|
||||
c.peerConnection[m.Num].Close()
|
||||
c.peerConnection[m.Num] = nil
|
||||
c.dataChannel[m.Num].Close()
|
||||
c.dataChannel[m.Num] = nil
|
||||
// c.Step4FileTransfer = false
|
||||
// c.Step3RecipientRequestFile = false
|
||||
err = c.redisdb.Publish(c.nameOutChannel, Message{
|
||||
Type: "close-recipient",
|
||||
Num: m.Num,
|
||||
}.String()).Err()
|
||||
case "close-recipient":
|
||||
c.peerConnection.Close()
|
||||
c.peerConnection = nil
|
||||
c.dataChannel = nil
|
||||
c.Step4FileTransfer = false
|
||||
c.Step3RecipientRequestFile = false
|
||||
c.peerConnection[m.Num].Close()
|
||||
c.peerConnection[m.Num] = nil
|
||||
c.dataChannel[m.Num] = nil
|
||||
// c.Step4FileTransfer = false
|
||||
// c.Step3RecipientRequestFile = false
|
||||
}
|
||||
if err != nil {
|
||||
return
|
||||
|
@ -510,27 +518,47 @@ func (c *Client) updateState() (err error) {
|
|||
}
|
||||
c.Step3RecipientRequestFile = true
|
||||
// start receiving data
|
||||
go func() {
|
||||
err = c.dataChannelReceive()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}()
|
||||
// Register message handling
|
||||
c.bar = progressbar.NewOptions64(
|
||||
c.FilesToTransfer[c.FilesToTransferCurrentNum].Size,
|
||||
progressbar.OptionSetRenderBlankState(true),
|
||||
progressbar.OptionSetBytes64(c.FilesToTransfer[c.FilesToTransferCurrentNum].Size),
|
||||
progressbar.OptionSetWriter(os.Stderr),
|
||||
progressbar.OptionThrottle(1/60*time.Second),
|
||||
)
|
||||
|
||||
for inum := 0; inum < Channels; inum++ {
|
||||
go func(i int) {
|
||||
err = c.dataChannelReceive(i)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}(inum)
|
||||
}
|
||||
}
|
||||
if c.IsSender && c.Step3RecipientRequestFile && !c.Step4FileTransfer {
|
||||
c.log.Debug("start sending data!")
|
||||
c.Step4FileTransfer = true
|
||||
go func() {
|
||||
err = c.dataChannelSend()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}()
|
||||
c.bar = progressbar.NewOptions64(
|
||||
c.FilesToTransfer[c.FilesToTransferCurrentNum].Size,
|
||||
progressbar.OptionSetRenderBlankState(true),
|
||||
progressbar.OptionSetBytes64(c.FilesToTransfer[c.FilesToTransferCurrentNum].Size),
|
||||
progressbar.OptionSetWriter(os.Stderr),
|
||||
progressbar.OptionThrottle(1/60*time.Second),
|
||||
)
|
||||
for inum := 0; inum < Channels; inum++ {
|
||||
go func(i int) {
|
||||
err = c.dataChannelSend(i)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}(inum)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Client) dataChannelReceive() (err error) {
|
||||
func (c *Client) dataChannelReceive(num int) (err error) {
|
||||
// Prepare the configuration
|
||||
config := webrtc.Configuration{
|
||||
ICEServers: []webrtc.ICEServer{
|
||||
|
@ -541,19 +569,19 @@ func (c *Client) dataChannelReceive() (err error) {
|
|||
}
|
||||
|
||||
// Create a new RTCPeerConnection
|
||||
c.peerConnection, err = webrtc.NewPeerConnection(config)
|
||||
c.peerConnection[num], err = webrtc.NewPeerConnection(config)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Set the handler for ICE connection state
|
||||
// This will notify you when the peer has connected/disconnected
|
||||
c.peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
|
||||
c.peerConnection[num].OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
|
||||
fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String())
|
||||
})
|
||||
|
||||
// Register data channel creation handling
|
||||
c.peerConnection.OnDataChannel(func(d *webrtc.DataChannel) {
|
||||
c.peerConnection[num].OnDataChannel(func(d *webrtc.DataChannel) {
|
||||
|
||||
// Register channel opening handling
|
||||
d.OnOpen(func() {
|
||||
|
@ -562,26 +590,17 @@ func (c *Client) dataChannelReceive() (err error) {
|
|||
|
||||
startTime := false
|
||||
timer := time.Now()
|
||||
var mutex = &sync.Mutex{}
|
||||
piecesToDo := make(map[int64]bool)
|
||||
for i := int64(0); i < c.FilesToTransfer[c.FilesToTransferCurrentNum].Size; i += BufferSize {
|
||||
piecesToDo[i] = true
|
||||
}
|
||||
// Register message handling
|
||||
bar := progressbar.NewOptions64(
|
||||
c.FilesToTransfer[c.FilesToTransferCurrentNum].Size,
|
||||
progressbar.OptionSetRenderBlankState(true),
|
||||
progressbar.OptionSetBytes64(c.FilesToTransfer[c.FilesToTransferCurrentNum].Size),
|
||||
progressbar.OptionSetWriter(os.Stderr),
|
||||
progressbar.OptionThrottle(1/60*time.Second),
|
||||
)
|
||||
d.OnMessage(func(msg webrtc.DataChannelMessage) {
|
||||
if bytes.Equal([]byte("done"), msg.Data) {
|
||||
c.CurrentFile.Close()
|
||||
c.log.Debug(time.Since(timer))
|
||||
c.log.Debug("telling transfer is over")
|
||||
err = c.redisdb.Publish(c.nameOutChannel, Message{
|
||||
Type: "close-sender",
|
||||
Num: num,
|
||||
}.String()).Err()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
@ -599,14 +618,14 @@ func (c *Client) dataChannelReceive() (err error) {
|
|||
panic(errM)
|
||||
}
|
||||
var n int
|
||||
mutex.Lock()
|
||||
c.mutex.Lock()
|
||||
n, err = c.CurrentFile.WriteAt(chunk.Bytes, chunk.Location)
|
||||
mutex.Unlock()
|
||||
c.log.Debugf("wrote %d bytes to %d (%d)", n, chunk.Location,num)
|
||||
c.mutex.Unlock()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
// c.log.Debugf("wrote %d bytes to %d", n, chunk.Location)
|
||||
bar.Add(n)
|
||||
c.bar.Add(n)
|
||||
})
|
||||
|
||||
})
|
||||
|
@ -615,7 +634,7 @@ func (c *Client) dataChannelReceive() (err error) {
|
|||
return
|
||||
}
|
||||
|
||||
func (c *Client) dataChannelSend() (err error) {
|
||||
func (c *Client) dataChannelSend(num int) (err error) {
|
||||
// Everything below is the pion-WebRTC API! Thanks for using it ❤️.
|
||||
|
||||
// Prepare the configuration
|
||||
|
@ -628,25 +647,25 @@ func (c *Client) dataChannelSend() (err error) {
|
|||
}
|
||||
|
||||
// Create a new RTCPeerConnection
|
||||
c.peerConnection, err = webrtc.NewPeerConnection(config)
|
||||
c.peerConnection[num], err = webrtc.NewPeerConnection(config)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Create a datachannel with label 'data'
|
||||
c.dataChannel, err = c.peerConnection.CreateDataChannel("data", nil)
|
||||
c.dataChannel[num], err = c.peerConnection[num].CreateDataChannel("data", nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Set the handler for ICE connection state
|
||||
// This will notify you when the peer has connected/disconnected
|
||||
c.peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
|
||||
c.peerConnection[num].OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
|
||||
fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String())
|
||||
})
|
||||
|
||||
c.dataChannel.OnOpen(func() {
|
||||
fmt.Printf("Data channel '%s'-'%d' open. Random messages will now be sent to any connected DataChannels every 5 seconds\n", c.dataChannel.Label(), c.dataChannel.ID())
|
||||
c.dataChannel[num].OnOpen(func() {
|
||||
fmt.Printf("Data channel '%s'-'%d' open. Random messages will now be sent to any connected DataChannels every 5 seconds\n", c.dataChannel[num].Label(), c.dataChannel[num].ID())
|
||||
time.Sleep(100 * time.Microsecond)
|
||||
|
||||
pathToFile := path.Join(c.FilesToTransfer[c.FilesToTransferCurrentNum].FolderSource, c.FilesToTransfer[c.FilesToTransferCurrentNum].Name)
|
||||
|
@ -659,17 +678,9 @@ func (c *Client) dataChannelSend() (err error) {
|
|||
}
|
||||
defer file.Close()
|
||||
|
||||
fstats, _ := file.Stat()
|
||||
bar := progressbar.NewOptions64(
|
||||
fstats.Size(),
|
||||
progressbar.OptionSetRenderBlankState(true),
|
||||
progressbar.OptionSetBytes64(fstats.Size()),
|
||||
progressbar.OptionSetWriter(os.Stderr),
|
||||
progressbar.OptionThrottle(1/60*time.Second),
|
||||
)
|
||||
|
||||
buffer := make([]byte, BufferSize)
|
||||
var location int64
|
||||
chunkNum := 0.0
|
||||
for {
|
||||
bytesread, err := file.Read(buffer)
|
||||
if err != nil {
|
||||
|
@ -679,23 +690,26 @@ func (c *Client) dataChannelSend() (err error) {
|
|||
break
|
||||
}
|
||||
|
||||
mSend := Chunk{
|
||||
Bytes: buffer[:bytesread],
|
||||
Location: location,
|
||||
if math.Mod(chunkNum, float64(Channels)) == float64(num) {
|
||||
mSend := Chunk{
|
||||
Bytes: buffer[:bytesread],
|
||||
Location: location,
|
||||
}
|
||||
dataToSend, _ := json.Marshal(mSend)
|
||||
c.bar.Add(bytesread)
|
||||
err = c.dataChannel[num].Send(dataToSend)
|
||||
if err != nil {
|
||||
c.log.Debug("Could not send on data channel", err.Error())
|
||||
continue
|
||||
}
|
||||
time.Sleep(100 * time.Microsecond)
|
||||
}
|
||||
dataToSend, _ := json.Marshal(mSend)
|
||||
|
||||
bar.Add(bytesread)
|
||||
err = c.dataChannel.Send(dataToSend)
|
||||
if err != nil {
|
||||
c.log.Debug("Could not send on data channel", err.Error())
|
||||
continue
|
||||
}
|
||||
location += int64(bytesread)
|
||||
time.Sleep(100 * time.Microsecond)
|
||||
chunkNum += 1.0
|
||||
}
|
||||
c.log.Debug("sending done signal")
|
||||
err = c.dataChannel.Send([]byte("done"))
|
||||
err = c.dataChannel[num].Send([]byte("done"))
|
||||
if err != nil {
|
||||
c.log.Debug(err)
|
||||
}
|
||||
|
@ -703,17 +717,17 @@ func (c *Client) dataChannelSend() (err error) {
|
|||
})
|
||||
|
||||
// Register text message handling
|
||||
c.dataChannel.OnMessage(func(msg webrtc.DataChannelMessage) {
|
||||
fmt.Printf("Message from DataChannel '%s': '%s'\n", c.dataChannel.Label(), string(msg.Data))
|
||||
c.dataChannel[num].OnMessage(func(msg webrtc.DataChannelMessage) {
|
||||
fmt.Printf("Message from DataChannel '%s': '%s'\n", c.dataChannel[num].Label(), string(msg.Data))
|
||||
})
|
||||
// Create an offer to send to the browser
|
||||
offer, err := c.peerConnection.CreateOffer(nil)
|
||||
offer, err := c.peerConnection[num].CreateOffer(nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Sets the LocalDescription, and starts our UDP listeners
|
||||
err = c.peerConnection.SetLocalDescription(offer)
|
||||
err = c.peerConnection[num].SetLocalDescription(offer)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -723,6 +737,7 @@ func (c *Client) dataChannelSend() (err error) {
|
|||
err = c.redisdb.Publish(c.nameOutChannel, Message{
|
||||
Type: "datachannel-offer",
|
||||
Message: Encode(offer),
|
||||
Num: num,
|
||||
}.String()).Err()
|
||||
if err != nil {
|
||||
return
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue