2018-09-24 15:24:58 -06:00
|
|
|
// Copyright 2018 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-10-11 14:08:39 -06:00
|
|
|
"encoding/json"
|
2019-11-17 12:29:15 -07:00
|
|
|
"fmt"
|
2018-09-24 15:24:58 -06:00
|
|
|
|
|
|
|
"golang.org/x/tools/internal/jsonrpc2"
|
2019-08-13 13:07:39 -06:00
|
|
|
"golang.org/x/tools/internal/telemetry/log"
|
|
|
|
"golang.org/x/tools/internal/telemetry/trace"
|
2019-07-10 19:11:23 -06:00
|
|
|
"golang.org/x/tools/internal/xcontext"
|
2018-09-24 15:24:58 -06:00
|
|
|
)
|
|
|
|
|
2019-10-11 14:08:39 -06:00
|
|
|
const (
|
|
|
|
// RequestCancelledError should be used when a request is cancelled early.
|
|
|
|
RequestCancelledError = -32800
|
|
|
|
)
|
|
|
|
|
2019-07-11 22:43:12 -06:00
|
|
|
type canceller struct{ jsonrpc2.EmptyHandler }
|
2019-03-29 12:16:59 -06:00
|
|
|
|
2019-07-11 10:57:35 -06:00
|
|
|
type clientHandler struct {
|
2020-01-24 14:40:38 -07:00
|
|
|
jsonrpc2.EmptyHandler
|
2019-07-11 10:57:35 -06:00
|
|
|
client Client
|
|
|
|
}
|
|
|
|
|
|
|
|
type serverHandler struct {
|
2020-01-24 14:40:38 -07:00
|
|
|
jsonrpc2.EmptyHandler
|
2019-07-11 10:57:35 -06:00
|
|
|
server Server
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:08:39 -06:00
|
|
|
func (canceller) Request(ctx context.Context, conn *jsonrpc2.Conn, direction jsonrpc2.Direction, r *jsonrpc2.WireRequest) context.Context {
|
|
|
|
if direction == jsonrpc2.Receive && r.Method == "$/cancelRequest" {
|
|
|
|
var params CancelParams
|
|
|
|
if err := json.Unmarshal(*r.Params, ¶ms); err != nil {
|
|
|
|
log.Error(ctx, "", err)
|
|
|
|
} else {
|
2019-11-17 12:29:15 -07:00
|
|
|
v := jsonrpc2.ID{}
|
|
|
|
if n, ok := params.ID.(float64); ok {
|
|
|
|
v.Number = int64(n)
|
|
|
|
} else if s, ok := params.ID.(string); ok {
|
|
|
|
v.Name = s
|
|
|
|
} else {
|
|
|
|
log.Error(ctx, fmt.Sprintf("Request ID %v malformed", params.ID), nil)
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
conn.Cancel(v)
|
2019-10-11 14:08:39 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
2019-07-11 10:57:35 -06:00
|
|
|
func (canceller) Cancel(ctx context.Context, conn *jsonrpc2.Conn, id jsonrpc2.ID, cancelled bool) bool {
|
|
|
|
if cancelled {
|
|
|
|
return false
|
|
|
|
}
|
2019-07-10 19:11:23 -06:00
|
|
|
ctx = xcontext.Detach(ctx)
|
2019-06-26 20:46:12 -06:00
|
|
|
ctx, done := trace.StartSpan(ctx, "protocol.canceller")
|
|
|
|
defer done()
|
2019-06-24 22:50:01 -06:00
|
|
|
conn.Notify(ctx, "$/cancelRequest", &CancelParams{ID: id})
|
2019-07-11 10:57:35 -06:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-01-21 13:48:02 -07:00
|
|
|
func (canceller) Deliver(ctx context.Context, r *jsonrpc2.Request, delivered bool) bool {
|
|
|
|
// Hide cancellations from downstream handlers.
|
|
|
|
return r.Method == "$/cancelRequest"
|
|
|
|
}
|
|
|
|
|
2019-07-10 19:01:12 -06:00
|
|
|
func NewClient(ctx context.Context, stream jsonrpc2.Stream, client Client) (context.Context, *jsonrpc2.Conn, Server) {
|
2019-07-10 13:19:29 -06:00
|
|
|
ctx = WithClient(ctx, client)
|
2019-03-28 19:06:01 -06:00
|
|
|
conn := jsonrpc2.NewConn(stream)
|
2019-07-12 17:26:13 -06:00
|
|
|
conn.AddHandler(&clientHandler{client: client})
|
2020-01-24 14:40:38 -07:00
|
|
|
conn.AddHandler(&canceller{})
|
2019-07-10 19:01:12 -06:00
|
|
|
return ctx, conn, &serverDispatcher{Conn: conn}
|
2018-09-24 15:24:58 -06:00
|
|
|
}
|
|
|
|
|
2019-07-10 19:01:12 -06:00
|
|
|
func NewServer(ctx context.Context, stream jsonrpc2.Stream, server Server) (context.Context, *jsonrpc2.Conn, Client) {
|
2019-03-28 19:06:01 -06:00
|
|
|
conn := jsonrpc2.NewConn(stream)
|
2019-03-29 17:04:29 -06:00
|
|
|
client := &clientDispatcher{Conn: conn}
|
2019-07-10 13:19:29 -06:00
|
|
|
ctx = WithClient(ctx, client)
|
2019-07-12 17:26:13 -06:00
|
|
|
conn.AddHandler(&serverHandler{server: server})
|
2020-01-24 14:40:38 -07:00
|
|
|
conn.AddHandler(&canceller{})
|
2019-07-10 19:01:12 -06:00
|
|
|
return ctx, conn, client
|
2018-09-24 15:24:58 -06:00
|
|
|
}
|
|
|
|
|
2019-07-12 17:26:13 -06:00
|
|
|
func sendParseError(ctx context.Context, req *jsonrpc2.Request, err error) {
|
2018-11-13 20:49:07 -07:00
|
|
|
if _, ok := err.(*jsonrpc2.Error); !ok {
|
|
|
|
err = jsonrpc2.NewErrorf(jsonrpc2.CodeParseError, "%v", err)
|
2018-09-24 15:24:58 -06:00
|
|
|
}
|
2019-06-24 07:18:31 -06:00
|
|
|
if err := req.Reply(ctx, nil, err); err != nil {
|
2019-07-14 21:08:10 -06:00
|
|
|
log.Error(ctx, "", err)
|
2018-11-13 20:49:07 -07:00
|
|
|
}
|
2018-09-24 15:24:58 -06:00
|
|
|
}
|