Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase the buffer on darwin #3

Merged
merged 1 commit into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
build-win:
CGO_ENABLE=0 GOOS=windows go build -o bin/transfer.exe
CGO_ENABLE=0 GOOS=windows go build -o bin/win/transfer.exe

build-darwin:
CGO_ENABLE=0 GOOS=darwin go build -o bin/transfer
CGO_ENABLE=0 GOOS=darwin go build -o bin/darwin/transfer

build-linux:
CGO_ENABLE=0 GOOS=linux go build -o bin/transfer
CGO_ENABLE=0 GOOS=linux go build -o bin/linux/transfer

build-all: build-darwin build-linux build-win

test:
go test ./...
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ Data transfer via UDP protocal.
## Features
* Send files to an unknown target IP address in a local network

## Install
Using [hd](https://github.com/LinuxSuRen/http-downloader/) to install it:

```shell
hd i transfer
```

## Usage
Wait for the data:
```shell
Expand Down
2 changes: 1 addition & 1 deletion header.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (h *headerBuilder) Build() (err error) {

switch runtime.GOOS {
case "darwin":
h.chunk = 500 // default value on darwin is 9216
h.chunk = 9000 // default value on darwin is 9216
default:
h.chunk = 60000
}
Expand Down
51 changes: 2 additions & 49 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package main

import (
"fmt"
"net"
"strconv"
"strings"
"time"

Expand All @@ -28,53 +26,8 @@ func retry(count int, callback func() error) (err error) {
if err = callback(); err == nil {
break
}
time.Sleep(10 * time.Millisecond)
}
return
}

// waitingMissing read data, returns the missing index.
// Consider it has finished if the index is -1.
func waitingMissing(conn net.Conn) (index int, ok bool, err error) {
// format: miss0000000012, the index is 12
message := make([]byte, 14)

var rlen int
if err = conn.SetReadDeadline(time.Now().Add(time.Second * 3)); err != nil {
return
}

if rlen, err = conn.Read(message[:]); err == nil && rlen == 14 {
index, ok = checkMissing(message)
}
return
}

func requestMissing(conn *net.UDPConn, index int, remote *net.UDPAddr) (err error) {
_, err = conn.WriteTo([]byte("miss"+fillContainerWithNumber(index, 10)), remote)
return
}

func requestDone(conn *net.UDPConn, remote *net.UDPAddr) (err error) {
for i := 0; i < 3; i++ {
_, err = conn.WriteTo([]byte("done"+fillContainerWithNumber(0, 10)), remote)

time.Sleep(time.Second)
}
return
}

func checkMissing(message []byte) (index int, ok bool) {
var err error

if strings.HasPrefix(string(message), "miss") {
msg := strings.TrimSpace(strings.TrimPrefix(string(message), "miss"))
if index, err = strconv.Atoi(msg); err == nil {
ok = true
}
} else if strings.HasPrefix(string(message), "done") {
index = -1
ok = true
// mainly do this on the darwin
time.Sleep(100 * time.Millisecond)
}
return
}
Expand Down
42 changes: 39 additions & 3 deletions send.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"net"
"os"
"regexp"
"strconv"
"strings"
"time"
)

Expand Down Expand Up @@ -101,9 +103,6 @@ func (o *sendOption) runE(cmd *cobra.Command, args []string) (err error) {
_, err := conn.Write(builder.CreateHeader(i, buf[:n]))
return err
})
if err != nil {
fmt.Println(err)
}

if i == 0 {
// give more time to init file for the first package
Expand Down Expand Up @@ -175,3 +174,40 @@ func send(f *os.File, reader *bufio.Reader, conn net.Conn, index, chunk int, bui
})
return
}

// waitingMissing read data, returns the missing index.
// Consider it has finished if the index is -1.
func waitingMissing(conn net.Conn) (index int, ok bool, err error) {
// format: miss0000000012, the index is 12
message := make([]byte, 14)

var rlen int
if err = conn.SetReadDeadline(time.Now().Add(time.Second * 3)); err != nil {
return
}

if rlen, err = conn.Read(message[:]); err == nil && rlen == 14 {
index, ok = checkMissing(message)
}
return
}

func checkMissing(message []byte) (index int, ok bool) {
var err error

if strings.HasPrefix(string(message), "miss") {
msg := strings.TrimSpace(strings.TrimPrefix(string(message), "miss"))
if index, err = strconv.Atoi(msg); err == nil {
ok = true
}
} else if strings.HasPrefix(string(message), "done") {
index = -1
ok = true
}
return
}

func requestMissing(conn *net.UDPConn, index int, remote *net.UDPAddr) (err error) {
_, err = conn.WriteTo([]byte("miss"+fillContainerWithNumber(index, 10)), remote)
return
}
9 changes: 9 additions & 0 deletions wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,15 @@ func (o *waitOption) runE(cmd *cobra.Command, args []string) error {
return nil
}

func requestDone(conn *net.UDPConn, remote *net.UDPAddr) (err error) {
for i := 0; i < 3; i++ {
_, err = conn.WriteTo([]byte("done"+fillContainerWithNumber(0, 10)), remote)

time.Sleep(time.Second)
}
return
}

func newWaitCmd() (cmd *cobra.Command) {
opt := &waitOption{}
cmd = &cobra.Command{
Expand Down