From c54004ae38e9dc7fc18e69f58ff939bd46181966 Mon Sep 17 00:00:00 2001 From: xc Date: Mon, 1 Sep 2025 18:47:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0jsonrpc=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scagent/core/JsonRpc.go | 19 +++++++++++++++++++ scagent/main.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 scagent/core/JsonRpc.go diff --git a/scagent/core/JsonRpc.go b/scagent/core/JsonRpc.go new file mode 100644 index 0000000..8e8ffb2 --- /dev/null +++ b/scagent/core/JsonRpc.go @@ -0,0 +1,19 @@ +package core + +// 传入的参数 +type Args struct { + FilePath string `json:"file_path"` +} + +// 返回的参数 +type Reply struct { + FilePath string `json:"file_path"` +} + +// +type FileRpc string + +// 获取文件路径 +func (f *FileRpc) GetFilePath(args *Args, replay *Reply) error { + return nil +} diff --git a/scagent/main.go b/scagent/main.go index 4350ee1..3eefa90 100644 --- a/scagent/main.go +++ b/scagent/main.go @@ -2,10 +2,17 @@ package main import ( "fmt" + "net" + "net/rpc" + "net/rpc/jsonrpc" "os" "os/signal" + "scagnet/config" + "scagnet/core" "scagnet/util" "syscall" + + "go.uber.org/zap" ) // 输入-d 参数,以守护进程方式运行 @@ -36,11 +43,38 @@ func main() { // fmt.Println("JSON RPC服务端") // 初始化日志 logger := util.NewProductionLogger() + defer logger.Sync() logger.Info("JSON RPC服务端启动") // 检查是否以守护进程方式运行 if isDaemon() { // 以守护进程方式运行 + jrpc := new(core.FileRpc) + rpc.Register(jrpc) + // + sport := fmt.Sprintf(":%v", config.G.Port) + tcpAddr, err := net.ResolveTCPAddr("tcp", sport) + if err != nil { + logger.Error("ResolveTCPAddr failed", zap.Error(err)) + return + } + + // 监听 + listenr, err := net.ListenTCP("tcp", tcpAddr) + if err != nil { + logger.Error("ListenTCP failed", zap.Error(err)) + return + } + + for { + conn, err := listenr.AcceptTCP() + if err != nil { + logger.Error("AcceptTCP failed", zap.Error(err)) + continue + } + jsonrpc.ServeConn(conn) + } + } // 优雅退出 // quit := make(chan os.Signal)