84 lines
1.3 KiB
Go
84 lines
1.3 KiB
Go
package src
|
|
|
|
import (
|
|
"log"
|
|
"net"
|
|
"os"
|
|
)
|
|
|
|
// InitSocket 初始化 socket 文件
|
|
func InitSocket() {
|
|
log.Println("Start listen command")
|
|
// 删除旧的 socket 文件
|
|
if _, err := os.Stat(SocketFile); err == nil {
|
|
os.Remove(SocketFile)
|
|
}
|
|
|
|
listener, err := net.Listen("unix", SocketFile)
|
|
if err != nil {
|
|
log.Fatalf("Failed to listen on socket: %v", err)
|
|
}
|
|
defer listener.Close()
|
|
|
|
for {
|
|
log.Println("Waiting for connections...")
|
|
conn, err := listener.Accept()
|
|
if err != nil {
|
|
log.Printf("Failed to accept connection: %v", err)
|
|
continue
|
|
}
|
|
|
|
go handleConnection(conn)
|
|
}
|
|
}
|
|
|
|
/*
|
|
*
|
|
处理连接
|
|
*/
|
|
func handleConnection(conn net.Conn) {
|
|
defer conn.Close()
|
|
|
|
buf := make([]byte, 1024)
|
|
n, err := conn.Read(buf)
|
|
if err != nil {
|
|
log.Printf("Failed to read command: %v", err)
|
|
return
|
|
}
|
|
|
|
command := string(buf[:n])
|
|
log.Printf("Received command: %s", command)
|
|
|
|
// 在这里处理接收到的命令
|
|
switch command {
|
|
case "stop":
|
|
onStop()
|
|
default:
|
|
onCommand(command)
|
|
}
|
|
}
|
|
|
|
/*
|
|
*
|
|
收到停止命令
|
|
*/
|
|
func onStop() {
|
|
log.Println("Stopping daemon...")
|
|
os.Remove(PidFile)
|
|
log.Println("Remove PID File...")
|
|
os.Remove(SocketFile)
|
|
log.Println("Remove Socket File...")
|
|
os.Exit(0)
|
|
}
|
|
|
|
/*
|
|
收到命令
|
|
*/
|
|
func onCommand(command string) {
|
|
|
|
}
|
|
|
|
func onConfig() {
|
|
|
|
}
|