Skip to content

Commit

Permalink
Removed the use of 'curl'
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonito committed Mar 10, 2019
1 parent 0a02c8c commit 82655a0
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 19 deletions.
8 changes: 6 additions & 2 deletions README.md
Expand Up @@ -23,15 +23,19 @@ As of today, it works well with small files. It doesn't work with huge files, du
### Sender

```bash
# Terminal 1
gfile send --file filename

# Terminal 2
echo "$SDP" | gfile sdp
```

- Run the command
- A base64 encoded SDP will appear, send it to the remote client
- Follow the instructions to curl the client's SDP to your process
- Follow the instructions to send the client's SDP to your process
- The file transfer should start

> Due to terms restrictions (ability to treat lines with +1024 characters), the SDP must be sent through `curl`
> Due to terms restrictions (ability to treat lines with +1024 characters), the SDP must be sent through the `gfile sdp` command
### Receiver

Expand Down
2 changes: 2 additions & 0 deletions cmd/install.go
Expand Up @@ -4,6 +4,7 @@ import (
"sort"

"github.com/Antonito/gfile/cmd/receive"
"github.com/Antonito/gfile/cmd/sdp"
"github.com/Antonito/gfile/cmd/send"
"gopkg.in/urfave/cli.v1"
)
Expand All @@ -13,6 +14,7 @@ func Install(app *cli.App) {
app.Commands = []cli.Command{
send.New(),
receive.New(),
sdp.New(),
}
sort.Sort(cli.CommandsByName(app.Commands))
}
51 changes: 51 additions & 0 deletions cmd/sdp/cmd.go
@@ -0,0 +1,51 @@
package sdp

import (
"fmt"
"net/http"
"strings"

"github.com/Antonito/gfile/pkg/utils"
"github.com/pions/webrtc"
"gopkg.in/urfave/cli.v1"
)

func handler(c *cli.Context) error {
var encoded string
for {
data, err := utils.MustReadStdin()
if err == nil {
// We decode it, but never use it,
// just to make sure the data is correct
offer := webrtc.SessionDescription{}
if err := utils.Decode(data, &offer); err == nil {
encoded = data
break
}
}
fmt.Println("Invalid SDP, try again...")
}

body := strings.NewReader(encoded)
req, err := http.NewRequest("POST", "http://localhost:8080/sdp", body)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}

// New creates the command
func New() cli.Command {
return cli.Command{
Name: "sdp",
Usage: "Sends a SDP to the already running instance of the `send` command",
Action: handler,
}
}
11 changes: 4 additions & 7 deletions cmd/send/session/init.go
Expand Up @@ -24,7 +24,7 @@ func (s *Session) Connect() error {

// Wait for the answer to be pasted
fmt.Println(`Please, provide the SDP via:
curl localhost:8080/sdp --data "$SDP"`)
echo "$SDP" | gfile sdp`)
answer := webrtc.SessionDescription{}
for {
if err := utils.Decode(<-sdpChan, &answer); err == nil {
Expand Down Expand Up @@ -84,12 +84,9 @@ func (s *Session) createOffer() error {
}

func (s *Session) createDataChannel() error {
ordered := true
maxPacketLifeTime := uint16(5000)
dataChannel, err := s.peerConnection.CreateDataChannel("data", &webrtc.DataChannelInit{
Ordered: &ordered,
MaxPacketLifeTime: &maxPacketLifeTime,
})
//ordered := true
//maxPacketLifeTime := uint16(0xFFFF)
dataChannel, err := s.peerConnection.CreateDataChannel("data", nil)
if err != nil {
return err
}
Expand Down
11 changes: 1 addition & 10 deletions cmd/send/session/state.go
Expand Up @@ -13,7 +13,6 @@ func (s *Session) setStateManager() {
// This will notify you when the peer has connected/disconnected
s.peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String())
fmt.Printf("Connection state is %v\n", s.peerConnection.ConnectionState)
if connectionState == webrtc.ICEConnectionStateDisconnected {
s.stopSending <- struct{}{}
}
Expand All @@ -25,10 +24,9 @@ func (s *Session) writeToNetwork() {
defer fmt.Println("Stopped sending data...")

for {
SELECT:
select {
case <-s.stopSending:
fmt.Printf("Pausing network I/O... (remaining at least %v packets)", len(s.output))
fmt.Printf("Pausing network I/O... (remaining at least %v packets)\n", len(s.output))
return
case data := <-s.output:
if data.n == 0 {
Expand All @@ -41,13 +39,6 @@ func (s *Session) writeToNetwork() {

for len(s.msgToBeSent) != 0 {
cur := s.msgToBeSent[0]

// TODO: Correct check
if s.dataChannel.ReadyState != webrtc.DataChannelStateOpen {
fmt.Printf("Status: %v, dropping %v bytes\n", s.dataChannel.ReadyState, data.n)
break SELECT
}

// Writing packet
if err := s.dataChannel.Send(cur.buff); err != nil {
fmt.Printf("Error, cannot send to client: %v\n", err)
Expand Down

0 comments on commit 82655a0

Please sign in to comment.