Skip to content

Commit

Permalink
去除TCP服务端连接
Browse files Browse the repository at this point in the history
  • Loading branch information
jixishi committed Apr 26, 2024
1 parent d19c09e commit e3415ae
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 54 deletions.
34 changes: 0 additions & 34 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ type FoeWardMode int

const (
NOT FoeWardMode = iota
TCPS
TCPC
UDPC
)
Expand All @@ -43,41 +42,8 @@ func setForWardClient() (conn net.Conn) {
if err != nil {
log.Fatal(err)
}

default:
panic("未知模式设置")
}
return conn
}
func setForWardServer() {
switch FoeWardMode(config.forWard) {
case TCPS:
listen, err := net.Listen("tcp", config.address)
if err != nil {
log.Fatal(err)
}
for {
conn, err := listen.Accept() // 监听客户端的连接请求
if err != nil {
log.Println("Accept() failed, err: ", err)
continue
}
go process(conn) // 启动一个goroutine来处理客户端的连接请求
}
default:
panic("未知模式设置")
}
}
func process(conn net.Conn) {
defer conn.Close() // 关闭连接
//reader := bufio.NewReader(serialPort)
outs = append(outs, conn)
defer func() {
for i, w := range outs {
if w == conn {
outs = append(outs[:i], outs[i+1:]...)
}
}
}()
input(conn)
}
2 changes: 1 addition & 1 deletion flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var (
endStr = Flag{ptrVal{string: &config.endStr}, "e", "end", Val{string: "\n"}, "终端换行符"}
enableLog = Flag{ptrVal{bool: &config.enableLog}, "l", "log", Val{bool: false}, "是否启用日志保存"}
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-C)"}
forWard = Flag{ptrVal{int: &config.forWard}, "f", "forward", Val{int: 0}, "转发模式(0: 无 1:TCP-C 2:UDP-C)"}
address = Flag{ptrVal{string: &config.address}, "a", "address", Val{string: "127.0.0.1:12345"}, "转发服务地址"}
frameSize = Flag{ptrVal{int: &config.forWard}, "F", "Frame", Val{int: 16}, "帧大小"}
parityBit = Flag{ptrVal{int: &config.parityBit}, "v", "verify", Val{int: 0}, "奇偶校验(0:无校验、1:奇校验、2:偶校验、3:1校验、4:0校验)"}
Expand Down
32 changes: 13 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ var (
out io.Writer = os.Stdout
ins = []io.Reader{os.Stdin}
outs = []io.Writer{os.Stdout}
outn = 1
)

func checkPortAvailability(name string) ([]string, error) {
Expand Down Expand Up @@ -99,7 +98,7 @@ func output() {
strout(out, config.outputCode, fmt.Sprintf("% X %q \n", b, b))
}
} else {
err = charsetconv.ConvertWith(io.LimitReader(serialPort, int64(config.frameSize*4)), 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 {
log.Fatal(err)
Expand Down Expand Up @@ -131,19 +130,15 @@ func main() {
}(serialPort)

if FoeWardMode(config.forWard) != NOT {
if FoeWardMode(config.forWard) == TCPC || FoeWardMode(config.forWard) == UDPC {
conn := setForWardClient()
ins = append(ins, conn)
outs = append(outs, conn)
defer func(conn net.Conn) {
err := conn.Close()
if err != nil {
log.Fatal(err)
}
}(conn)
} else {
go setForWardServer()
}
conn := setForWardClient()
ins = append(ins, conn)
outs = append(outs, conn)
defer func(conn net.Conn) {
err := conn.Close()
if err != nil {
log.Fatal(err)
}
}(conn)
}
if len(ins) != 0 {
for _, reader := range ins {
Expand All @@ -157,11 +152,10 @@ func main() {
}
outs = append(outs, f)
}
if len(outs) != 1 {
out = io.MultiWriter(outs...)
}
for {
if len(outs) != outn {
outn = len(outs)
out = io.MultiWriter(outs...)
}
output()
}
}

0 comments on commit e3415ae

Please sign in to comment.