| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- package mcu_ctrl_board
- /*
- #include "mcu_ctrl_board.h"
- */
- import "C"
- import (
- "encoding/json"
- "fmt"
- "sync"
- "time"
- "hnyfkj.com.cn/rtu/linux/baseapp"
- "hnyfkj.com.cn/rtu/linux/utils/jsonrpc2"
- )
- var (
- // 存储所有待处理的JSON RPC 2.0请求ID和对应的应答通道
- pendingRequests sync.Map
- )
- // 判断报文的角色是什么
- func detectRPC2Role(jsonStr string) string {
- var obj map[string]json.RawMessage
- if json.Unmarshal([]byte(jsonStr), &obj) != nil {
- return "invalid"
- }
- if string(obj["jsonrpc"]) != `"2.0"` {
- return "invalid"
- }
- switch {
- case obj["method"] != nil:
- return "request"
- case obj["result"] != nil || obj["error"] != nil:
- return "response"
- default:
- return "unknown"
- }
- }
- // 发送通知, 不用等应答
- func SendRPC2Notify(req *jsonrpc2.Request) error {
- if req == nil || req.ID != nil {
- return fmt.Errorf("invalid notify")
- }
- jsonStr, err := req.String()
- if err != nil {
- return err
- }
- return Board.WriteDataToUart([]byte(jsonStr))
- }
- // 发送请求, 并等待应答
- func SendRPC2Request(req *jsonrpc2.Request, timeout int /*单位: ms*/) (*jsonrpc2.Response, error) {
- if req == nil || req.ID == nil {
- return nil, fmt.Errorf("invalid request")
- }
- id := *req.ID
- ch := make(chan *jsonrpc2.Response, 1)
- pendingRequests.Store(id, ch)
- defer pendingRequests.Delete(id)
- jsonStr, err := req.String()
- if err != nil {
- return nil, err
- }
- err = Board.WriteDataToUart([]byte(jsonStr))
- if err != nil {
- return nil, err
- }
- timer := time.NewTimer(time.Duration(timeout) * time.Millisecond)
- defer timer.Stop()
- select {
- case resp := <-ch:
- return resp, nil
- case <-timer.C:
- return nil, fmt.Errorf("request %v timed out after %d ms", id, timeout)
- }
- }
- // 获取主动请求的应答
- func handleRPC2Response(jsonStr string) error {
- w, err := jsonrpc2.ParseResponse(jsonStr)
- if err != nil {
- return fmt.Errorf("an error occurred while parsing JSON-RPC response: %v", err)
- }
- if w.ID == nil {
- return nil
- }
- id := *w.ID
- v, ok := pendingRequests.Load(id)
- if !ok {
- return fmt.Errorf("orphan response id=%d, no pending request", id)
- }
- ch, ok := v.(chan *jsonrpc2.Response)
- if !ok {
- return fmt.Errorf("invalid response channel for id=%d", id)
- }
- select {
- case ch <- w:
- return nil
- default:
- return fmt.Errorf("response dropped for id=%d, channel not receiving", id)
- }
- }
- // 处理JSONRPC 2.0报文
- func processRPC2(jsonStr string) *C.char {
- s := jsonStr
- role := detectRPC2Role(s)
- var w *jsonrpc2.Response
- wret := func() *C.char {
- if w == nil { // 无应答
- return nil
- }
- // 下面由C.CString()分配的内存由C侧释放
- b, err := w.String()
- if err != nil {
- return C.CString(fmt.Sprintf("an error occurred while calling the String() method of jsonrpc2.Response: %v", err))
- }
- return C.CString(b)
- }
- if role == "response" {
- err := handleRPC2Response(s)
- if err != nil {
- baseapp.Logger.Errorf("[%s] 解析请求返回的应答时发生错误: %v", MODULE_NAME, err)
- }
- return nil
- }
- if role != "request" {
- return nil
- }
- r, err := jsonrpc2.ParseRequest(s)
- if err != nil {
- w = jsonrpc2.BuildError(nil, jsonrpc2.ErrParse, "")
- return wret()
- }
- call := func(f func(*jsonrpc2.Request) (*jsonrpc2.Response, error)) *jsonrpc2.Response {
- w, e := f(r)
- if e != nil { // 调用本地RPC函数执行时发生错误
- return jsonrpc2.BuildError(r, jsonrpc2.ErrInternal, "")
- }
- return w
- }
- switch r.Method {
- case "echo":
- w = call(test_echo)
- default:
- w = jsonrpc2.BuildError(r, jsonrpc2.ErrMethodNotFound, "")
- }
- return wret()
- }
- // 数据回显本地测试方法
- func test_echo(r *jsonrpc2.Request) (*jsonrpc2.Response, error) {
- if r.Params == nil {
- return jsonrpc2.BuildError(r, jsonrpc2.ErrInvalidParams, ""), nil
- }
- return jsonrpc2.BuildResult(r, r.Params)
- }
|