2024-12-03 21:47:12 +08:00
|
|
|
//go:build !cgo
|
|
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bufio"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"net"
|
|
|
|
|
"strconv"
|
|
|
|
|
)
|
|
|
|
|
|
2025-01-13 19:08:17 +08:00
|
|
|
var conn net.Conn
|
|
|
|
|
|
|
|
|
|
func sendMessage(message Message) {
|
|
|
|
|
res, err := message.Json()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
send(Action{
|
|
|
|
|
Method: messageMethod,
|
2025-02-09 18:39:38 +08:00
|
|
|
}.getResult(res))
|
2025-01-13 19:08:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func send(data []byte) {
|
|
|
|
|
if conn == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
_, _ = conn.Write(append(data, []byte("\n")...))
|
|
|
|
|
}
|
2024-12-03 21:47:12 +08:00
|
|
|
|
|
|
|
|
func startServer(arg string) {
|
2025-01-13 19:08:17 +08:00
|
|
|
|
2024-12-03 21:47:12 +08:00
|
|
|
_, err := strconv.Atoi(arg)
|
2025-01-13 19:08:17 +08:00
|
|
|
|
2024-12-03 21:47:12 +08:00
|
|
|
if err != nil {
|
|
|
|
|
conn, err = net.Dial("unix", arg)
|
|
|
|
|
} else {
|
|
|
|
|
conn, err = net.Dial("tcp", fmt.Sprintf("127.0.0.1:%s", arg))
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defer func(conn net.Conn) {
|
|
|
|
|
_ = conn.Close()
|
|
|
|
|
}(conn)
|
|
|
|
|
|
|
|
|
|
reader := bufio.NewReader(conn)
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
data, err := reader.ReadString('\n')
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
var action = &Action{}
|
|
|
|
|
|
|
|
|
|
err = json.Unmarshal([]byte(data), action)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-09 18:39:38 +08:00
|
|
|
go handleAction(action, func(data interface{}) {
|
|
|
|
|
send(action.getResult(data))
|
2024-12-03 21:47:12 +08:00
|
|
|
})
|
|
|
|
|
}
|
2025-01-13 19:08:17 +08:00
|
|
|
}
|
2024-12-03 21:47:12 +08:00
|
|
|
|
2025-02-09 18:39:38 +08:00
|
|
|
func nextHandle(action *Action, result func(data interface{})) bool {
|
2025-01-13 19:08:17 +08:00
|
|
|
return false
|
2024-12-03 21:47:12 +08:00
|
|
|
}
|