-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·101 lines (89 loc) · 2.89 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"bufio"
"flag"
"fmt"
"github.com/carsonsx/tcptester/cmd"
"github.com/carsonsx/tcptester/conf"
"github.com/carsonsx/tcptester/net"
"os"
"strings"
)
var tcpClient = new(net.TCPClient)
func main() {
flag.BoolVar(&conf.Config.Silence, "silence", conf.Config.Silence, "enable or disable receive data")
flag.IntVar(&conf.Config.MessageLengthSize, "message_length_size", conf.Config.MessageLengthSize, "set message length size, if 0 then add nothing for message head")
flag.IntVar(&conf.Config.ProtoBufferIdSize, "proto_buffer_id_size", conf.Config.ProtoBufferIdSize, "set protobuf id size")
flag.BoolVar(&conf.Config.LittleEndian, "little_endian", conf.Config.LittleEndian, "use little endian for integer encode")
flag.IntVar(&conf.Config.SyncTime, "sync_time", 1, "time for receiving data one by one")
flag.StringVar(&conf.Config.Reader, "reader", conf.Config.Reader, "set reader with line|protobuf")
flag.StringVar(&conf.Config.Parser, "parser", conf.Config.Parser, "set parser with string|protobuf")
flag.StringVar(&conf.Config.InputMode, "input_mode", conf.Config.Parser, "set input mode string|hex")
flag.Parse()
conf.CheckAndSetConfig()
fmt.Printf("Welcome to use %s.\n", conf.TCP_TESTER_NAME)
fmt.Println("You may get source code from 'https://github.com/carsonsx/tcptester'")
new(cmd.ConfigCommand).Run(tcpClient)
fmt.Println("Please type '?' for command list or '? $cmd' for specific command usage")
tcpClient.CloseListen = func() {
fmt.Println("\ndisconnected from " + tcpClient.Addr)
setDisconnectData()
printPrompt()
}
tcpClient.GetParser().SetHandler(func(v interface{}) {
fmt.Println(v)
})
if flag.NArg() > 0 {
err := new(cmd.ConnectCommand).Run(tcpClient, flag.Arg(flag.NArg()-1))
if err != nil {
os.Exit(1)
return
}
} else {
setDisconnectData()
}
for {
input()
}
tcpClient.Close()
}
func setDisconnectData() {
conf.RunData.NetworkStatus = conf.TCP_TESTER_NOT_CONNECTED
conf.RunData.ConsolePrompt = conf.TCP_TESTER_NAME
}
func printPrompt() {
fmt.Print(conf.RunData.ConsolePrompt + "> ")
}
func input() {
printPrompt()
reader := bufio.NewReader(os.Stdin)
line, _, _ := reader.ReadLine()
text := string(line)
if text == "" {
new(cmd.HelpCommand).Run(tcpClient)
return
}
fields := strings.Fields(text)
name := fields[0]
command := cmd.GetInstance(name)
if command == nil {
fmt.Println(`(error) ERR unknown command '` + name + `'`)
return
}
if command.NeedConnected() && !tcpClient.IsConnected() {
conf.RunData.NetworkStatus = conf.TCP_TESTER_NOT_CONNECTED
fmt.Println(conf.RunData.NetworkStatus)
return
}
args := fields[1:]
argLen := len(fields) - 1
if argLen > command.MaxArgCount() || argLen < command.MinArgCount() || !command.Validate(tcpClient, args...) {
fmt.Println("(error) ERR wrong arguments")
fmt.Println(command.Usage())
return
}
err := command.Run(tcpClient, args...)
if err != nil {
fmt.Println(err)
}
}