Skip to content

Commit

Permalink
v2 init ...
Browse files Browse the repository at this point in the history
  • Loading branch information
ywanbing committed Sep 10, 2022
1 parent 2a04974 commit e7e53ed
Show file tree
Hide file tree
Showing 14 changed files with 365 additions and 11 deletions.
32 changes: 32 additions & 0 deletions cmd/app.go
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
}
20 changes: 20 additions & 0 deletions cmd/clinet.go
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)
}
5 changes: 3 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package main
package cmd

import (
"flag"
"fmt"
"github.com/ywanbing/ft/internal"
"os"
"path"

"github.com/ywanbing/ft/internal"
)

var help = `
Expand Down
32 changes: 32 additions & 0 deletions cmd/server.go
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
},
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/ywanbing/ft

go 1.16

require github.com/google/uuid v1.2.0
require (
github.com/google/uuid v1.2.0
github.com/urfave/cli/v2 v2.15.0
)
15 changes: 15 additions & 0 deletions go.sum
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=
16 changes: 8 additions & 8 deletions internal/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ const (
)

func StartServer(addr string, dir string) {
//通过ResolveTCPAddr实例一个具体的tcp断点
// 通过ResolveTCPAddr实例一个具体的tcp断点
tcpAddr, _ := net.ResolveTCPAddr("tcp", addr)
//打开一个tcp断点监听
// 打开一个tcp断点监听
tcpListener, _ := net.ListenTCP("tcp", tcpAddr)
defer tcpListener.Close()
fmt.Println("Server ready to read ...")
//循环接收客户端的连接,创建一个协程具体去处理连接
// 循环接收客户端的连接,创建一个协程具体去处理连接
for {
tcpConn, err := tcpListener.AcceptTCP()
if err != nil {
Expand All @@ -39,9 +39,9 @@ func StartServer(addr string, dir string) {
}
}

//具体处理连接过程方法
// 具体处理连接过程方法
func tcpPipe(c *Client, dir string) {
//tcp连接的地址
// tcp连接的地址
ipStr := c.c.RemoteAddr().String()

defer func() {
Expand Down Expand Up @@ -153,7 +153,7 @@ func tcpPipe(c *Client, dir string) {
}
}

//PathExists 判断文件夹是否存在
// PathExists 判断文件夹是否存在
func PathExists(path string) bool {
_, err := os.Stat(path)
if err != nil && os.IsNotExist(err) {
Expand All @@ -163,9 +163,9 @@ func PathExists(path string) bool {
}

func StartClient(addr string, fileName string) (err error) {
//通过ResolveTCPAddr实例一个具体的tcp断点
// 通过ResolveTCPAddr实例一个具体的tcp断点
tcpAddr, _ := net.ResolveTCPAddr("tcp", addr)
//打开一个tcp断点监听
// 打开一个tcp断点监听
tcpListener, _ := net.DialTCP("tcp", nil, tcpAddr)
defer tcpListener.Close()

Expand Down
14 changes: 14 additions & 0 deletions main.go
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)
}
}
66 changes: 66 additions & 0 deletions pkg/msg/msg.go
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
}
20 changes: 20 additions & 0 deletions pkg/msg/pool.go
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{}
},
}
)
1 change: 1 addition & 0 deletions pkg/server/network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package server
10 changes: 10 additions & 0 deletions pkg/server/server.go
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")
)
Loading

0 comments on commit e7e53ed

Please sign in to comment.