-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
365 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
var commands []*cli.Command | ||
|
||
// 提供其他命令注册 | ||
func registerCommand(cmd *cli.Command) { | ||
commands = append(commands, cmd) | ||
} | ||
|
||
// NewApp 创建一个 cli APP,并组装所有的命令。 | ||
func NewApp() *cli.App { | ||
app := &cli.App{ | ||
Name: "ft", | ||
Usage: "big file transfer, support various network protocols", | ||
Flags: []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "dir", | ||
Value: "./data", | ||
Aliases: []string{"d"}, | ||
Usage: "upload dir or save dir", | ||
}, | ||
}, | ||
Commands: commands, | ||
EnableBashCompletion: true, | ||
} | ||
|
||
return app | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
var clientCmd = &cli.Command{ | ||
Name: "client", | ||
Aliases: []string{"cli"}, | ||
Usage: "start an upload client.", | ||
Flags: []cli.Flag{}, | ||
Action: func(ctx *cli.Context) error { | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
registerCommand(clientCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
func init() { | ||
registerCommand(serverCmd) | ||
} | ||
|
||
var serverCmd = &cli.Command{ | ||
Name: "server", | ||
Aliases: []string{"srv"}, | ||
Usage: "start a server that receives files and listens on a specified port.", | ||
Flags: []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "network", | ||
Aliases: []string{"nw"}, | ||
Usage: "choose a network protocol(tcp|udp)", | ||
Value: "tcp", | ||
}, | ||
&cli.StringFlag{ | ||
Name: "addr", | ||
Usage: "specify a listening port", | ||
Value: "9988", | ||
}, | ||
}, | ||
Action: func(ctx *cli.Context) error { | ||
|
||
return nil | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
github.com/BurntSushi/toml v1.1.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= | ||
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w= | ||
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= | ||
github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs= | ||
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= | ||
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= | ||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= | ||
github.com/urfave/cli/v2 v2.15.0 h1:/U7qTMlBYcmo/Z34PaaVY0Gw04xoGJqEdRAiWNHNyy8= | ||
github.com/urfave/cli/v2 v2.15.0/go.mod h1:1CNUng3PtjQMtRzJO4FMXBQvkGtuYRxxiR9xMa7jMwI= | ||
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU= | ||
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8= | ||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= | ||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"os" | ||
|
||
"github.com/ywanbing/ft/cmd" | ||
) | ||
|
||
func main() { | ||
if err := cmd.NewApp().Run(os.Args); err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package msg | ||
|
||
import ( | ||
"encoding/json" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
type MsgType byte | ||
|
||
const ( | ||
MsgInvalid MsgType = iota | ||
MsgHead | ||
MsgFile | ||
MsgEnd | ||
MsgClose | ||
) | ||
|
||
type Message struct { | ||
MsgType MsgType `json:"t"` | ||
FileName string `json:"f"` | ||
Bytes []byte `json:"b"` | ||
Size uint64 `json:"s"` | ||
} | ||
|
||
func (m *Message) GC() { | ||
m.reset() | ||
msgPool.Put(m) | ||
} | ||
|
||
func (m *Message) reset() { | ||
m.MsgType = MsgInvalid | ||
m.FileName = "" | ||
m.Bytes = nil | ||
m.Size = 0 | ||
} | ||
|
||
func (m *Message) String() string { | ||
builder := builderPool.Get().(*strings.Builder) | ||
builder.Reset() | ||
defer builderPool.Put(builder) | ||
|
||
builder.WriteString("{") | ||
|
||
builder.WriteString(`"t":`) | ||
builder.WriteString(strconv.Itoa(int(m.MsgType)) + ",") | ||
|
||
builder.WriteString(`"f":`) | ||
builder.WriteString(`"` + m.FileName + `",`) | ||
|
||
builder.WriteString(`"b":`) | ||
builder.WriteString(`"` + string(m.Bytes) + `",`) | ||
|
||
builder.WriteString(`"s":`) | ||
builder.WriteString(strconv.Itoa(int(m.Size))) | ||
|
||
builder.WriteString("}") | ||
return builder.String() | ||
} | ||
|
||
// Decode will convert from bytes | ||
func Decode(b []byte) (m *Message, err error) { | ||
m = msgPool.Get().(*Message) | ||
err = json.Unmarshal(b, &m) | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package msg | ||
|
||
import ( | ||
"strings" | ||
"sync" | ||
) | ||
|
||
var ( | ||
msgPool = sync.Pool{ | ||
New: func() any { | ||
return &Message{} | ||
}, | ||
} | ||
|
||
builderPool = sync.Pool{ | ||
New: func() any { | ||
return &strings.Builder{} | ||
}, | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package server |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package server | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
var ( | ||
MAGIC_BYTES = []byte("f00t") | ||
EmErr = fmt.Errorf("dont have msg") | ||
) |
Oops, something went wrong.