(command string, data any, opts *wshrpc.RpcOpts)
| 761 | } |
| 762 | |
| 763 | func (w *WshRpc) SendComplexRequest(command string, data any, opts *wshrpc.RpcOpts) (rtnHandler *RpcRequestHandler, rtnErr error) { |
| 764 | if w.IsServerDone() { |
| 765 | return nil, errors.New("server is no longer running, cannot send new requests") |
| 766 | } |
| 767 | if opts == nil { |
| 768 | opts = &wshrpc.RpcOpts{} |
| 769 | } |
| 770 | timeoutMs := opts.Timeout |
| 771 | if timeoutMs <= 0 { |
| 772 | timeoutMs = DefaultTimeoutMs |
| 773 | } |
| 774 | defer func() { |
| 775 | panichandler.PanicHandler("SendComplexRequest", recover()) |
| 776 | }() |
| 777 | if command == "" { |
| 778 | return nil, fmt.Errorf("command cannot be empty") |
| 779 | } |
| 780 | handler := &RpcRequestHandler{ |
| 781 | w: w, |
| 782 | ctxCancelFn: &atomic.Pointer[context.CancelFunc]{}, |
| 783 | } |
| 784 | var cancelFn context.CancelFunc |
| 785 | handler.ctx, cancelFn = context.WithTimeout(context.Background(), time.Duration(timeoutMs)*time.Millisecond) |
| 786 | handler.ctxCancelFn.Store(&cancelFn) |
| 787 | if !opts.NoResponse { |
| 788 | handler.reqId = uuid.New().String() |
| 789 | } |
| 790 | req := &RpcMessage{ |
| 791 | Command: command, |
| 792 | ReqId: handler.reqId, |
| 793 | Data: data, |
| 794 | Timeout: timeoutMs, |
| 795 | Route: opts.Route, |
| 796 | } |
| 797 | barr, err := json.Marshal(req) |
| 798 | if err != nil { |
| 799 | return nil, err |
| 800 | } |
| 801 | handler.respCh = w.registerRpc(handler, command, opts.Route, handler.reqId) |
| 802 | select { |
| 803 | case w.OutputCh <- barr: |
| 804 | return handler, nil |
| 805 | case <-handler.ctx.Done(): |
| 806 | handler.finalize() |
| 807 | return nil, fmt.Errorf("timeout sending request") |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | func (w *WshRpc) IsServerDone() bool { |
| 812 | w.Lock.Lock() |
no test coverage detected