TCP|UDP 客户端数据转发支持更新
parent
dffb269247
commit
5bf90d1b63
|
@ -20,7 +20,7 @@ func cmdhelp() {
|
||||||
var page = 0
|
var page = 0
|
||||||
fmt.Printf(">-------Help(%v)-------<\n", page)
|
fmt.Printf(">-------Help(%v)-------<\n", page)
|
||||||
for i := 0; i < len(commands); i++ {
|
for i := 0; i < len(commands); i++ {
|
||||||
strout(config.outputCode, fmt.Sprintf(" %-10v --%v\n", commands[i].name, commands[i].description))
|
strout(out, config.outputCode, fmt.Sprintf(" %-10v --%v\n", commands[i].name, commands[i].description))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func cmdexit() {
|
func cmdexit() {
|
||||||
|
|
40
config.go
40
config.go
|
@ -1,16 +1,54 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"net"
|
||||||
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
portName string
|
portName string
|
||||||
baudRate int
|
baudRate int
|
||||||
dataBits int
|
dataBits int
|
||||||
stopBits int
|
stopBits int
|
||||||
|
parityBit int
|
||||||
outputCode string
|
outputCode string
|
||||||
inputCode string
|
inputCode string
|
||||||
endStr string
|
endStr string
|
||||||
enableLog bool
|
enableLog bool
|
||||||
logFilePath string
|
logFilePath string
|
||||||
parityBit int
|
forWard int
|
||||||
|
address string
|
||||||
}
|
}
|
||||||
|
type FoeWardMode int
|
||||||
|
|
||||||
|
const (
|
||||||
|
NOT FoeWardMode = iota
|
||||||
|
TCPS
|
||||||
|
TCPC
|
||||||
|
UDPS
|
||||||
|
UDPC
|
||||||
|
)
|
||||||
|
|
||||||
var config Config
|
var config Config
|
||||||
|
|
||||||
|
func setForWard() (conn net.Conn) {
|
||||||
|
switch FoeWardMode(config.forWard) {
|
||||||
|
case TCPS:
|
||||||
|
|
||||||
|
case TCPC:
|
||||||
|
conn, err = net.Dial("tcp", config.address)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
case UDPS:
|
||||||
|
|
||||||
|
case UDPC:
|
||||||
|
conn, err = net.Dial("udp", config.address)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
panic("未知模式设置")
|
||||||
|
}
|
||||||
|
return conn
|
||||||
|
}
|
||||||
|
|
4
flag.go
4
flag.go
|
@ -38,8 +38,10 @@ var (
|
||||||
endStr = Flag{ptrVal{string: &config.endStr}, "e", "end", Val{string: "\n"}, "终端换行符"}
|
endStr = Flag{ptrVal{string: &config.endStr}, "e", "end", Val{string: "\n"}, "终端换行符"}
|
||||||
enableLog = Flag{ptrVal{bool: &config.enableLog}, "l", "log", Val{bool: false}, "是否启用日志保存"}
|
enableLog = Flag{ptrVal{bool: &config.enableLog}, "l", "log", Val{bool: false}, "是否启用日志保存"}
|
||||||
logFilePath = Flag{ptrVal{string: &config.logFilePath}, "P", "Path", Val{string: "./Log.txt"}, "日志保存路径"}
|
logFilePath = Flag{ptrVal{string: &config.logFilePath}, "P", "Path", Val{string: "./Log.txt"}, "日志保存路径"}
|
||||||
|
forWard = Flag{ptrVal{int: &config.forWard}, "f", "forward", Val{int: 0}, "转发模式(0: 无 1:TCP-S 2:TCP-C 3:UDP-S 4:UDP-C)"}
|
||||||
|
address = Flag{ptrVal{string: &config.address}, "a", "address", Val{string: "127.0.0.1:12345"}, "转发服务地址"}
|
||||||
parityBit = Flag{ptrVal{int: &config.parityBit}, "v", "verify", Val{int: 0}, "奇偶校验(0:无校验、1:奇校验、2:偶校验、3:1校验、4:0校验)"}
|
parityBit = Flag{ptrVal{int: &config.parityBit}, "v", "verify", Val{int: 0}, "奇偶校验(0:无校验、1:奇校验、2:偶校验、3:1校验、4:0校验)"}
|
||||||
flags = []Flag{portName, baudRate, dataBits, stopBits, outputCode, inputCode, endStr, enableLog, logFilePath, parityBit}
|
flags = []Flag{portName, baudRate, dataBits, stopBits, outputCode, inputCode, endStr, enableLog, logFilePath, forWard, address, parityBit}
|
||||||
)
|
)
|
||||||
|
|
||||||
type ValType int
|
type ValType int
|
||||||
|
|
51
main.go
51
main.go
|
@ -18,6 +18,13 @@ var (
|
||||||
args []string
|
args []string
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
in io.Reader = os.Stdin
|
||||||
|
out io.Writer = os.Stdout
|
||||||
|
ins = []io.Reader{os.Stdin}
|
||||||
|
outs = []io.Writer{os.Stdout}
|
||||||
|
)
|
||||||
|
|
||||||
func checkPortAvailability(name string) ([]string, error) {
|
func checkPortAvailability(name string) ([]string, error) {
|
||||||
ports, err := serial.GetPortsList()
|
ports, err := serial.GetPortsList()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -45,8 +52,8 @@ func init() {
|
||||||
cmdinit()
|
cmdinit()
|
||||||
}
|
}
|
||||||
|
|
||||||
func input() {
|
func input(in io.Reader) {
|
||||||
input := bufio.NewScanner(os.Stdin)
|
input := bufio.NewScanner(in)
|
||||||
var ok = false
|
var ok = false
|
||||||
for input.Scan() {
|
for input.Scan() {
|
||||||
ok = false
|
ok = false
|
||||||
|
@ -74,8 +81,8 @@ func input() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func strout(cs, str string) {
|
func strout(out io.Writer, cs, str string) {
|
||||||
err = charsetconv.EncodeWith(strings.NewReader(str), os.Stdout, charsetconv.Charset(cs), false)
|
err = charsetconv.EncodeWith(strings.NewReader(str), out, charsetconv.Charset(cs), false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -83,10 +90,13 @@ func strout(cs, str string) {
|
||||||
|
|
||||||
func output(out io.Writer) {
|
func output(out io.Writer) {
|
||||||
if strings.Compare(config.inputCode, "hex") == 0 {
|
if strings.Compare(config.inputCode, "hex") == 0 {
|
||||||
b, _ := bufio.NewReader(io.LimitReader(serialPort, 16)).Peek(16)
|
b := make([]byte, 16)
|
||||||
_, err = fmt.Fprintf(out, "% X %q \n", b, b)
|
r, _ := io.LimitReader(serialPort, 16).Read(b)
|
||||||
|
if r != 0 {
|
||||||
|
strout(out, config.outputCode, fmt.Sprintf("% X %q \n", b, b))
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
err = charsetconv.ConvertWith(io.LimitReader(serialPort, 1024), charsetconv.Charset(config.inputCode), out, charsetconv.Charset(config.outputCode), false)
|
err = charsetconv.ConvertWith(serialPort, charsetconv.Charset(config.inputCode), out, charsetconv.Charset(config.outputCode), false)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
@ -116,20 +126,29 @@ func main() {
|
||||||
}
|
}
|
||||||
}(serialPort)
|
}(serialPort)
|
||||||
|
|
||||||
go input()
|
if FoeWardMode(config.forWard) != NOT {
|
||||||
|
conn := setForWard()
|
||||||
|
ins = append(ins, conn)
|
||||||
|
outs = append(outs, conn)
|
||||||
|
defer conn.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(ins) != 1 {
|
||||||
|
in = io.MultiReader(ins...)
|
||||||
|
}
|
||||||
|
go input(in)
|
||||||
|
|
||||||
if config.enableLog {
|
if config.enableLog {
|
||||||
f, err := os.OpenFile(config.logFilePath, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
|
f, err := os.OpenFile(config.logFilePath, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
out := io.MultiWriter(os.Stdout, f)
|
outs = append(outs, f)
|
||||||
for {
|
}
|
||||||
output(out)
|
if len(outs) != 1 {
|
||||||
}
|
out = io.MultiWriter(outs...)
|
||||||
} else {
|
}
|
||||||
for {
|
for {
|
||||||
output(os.Stdout)
|
output(out)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue