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"
|
|
|
|
|
|
|
|
"golang.org/x/tools/internal/jsonrpc2"
|
2019-06-24 22:50:01 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/telemetry/trace"
|
2019-03-29 17:04:29 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/xlog"
|
2019-07-10 19:11:23 -06:00
|
|
|
"golang.org/x/tools/internal/xcontext"
|
2018-09-24 15:24:58 -06:00
|
|
|
)
|
|
|
|
|
2019-07-11 12:53:13 -06:00
|
|
|
type DocumentUri = string
|
|
|
|
|
2019-03-29 12:16:59 -06:00
|
|
|
const defaultMessageBufferSize = 20
|
2019-06-14 11:17:16 -06:00
|
|
|
const defaultRejectIfOverloaded = false
|
2019-03-29 12:16:59 -06:00
|
|
|
|
2019-06-24 07:18:31 -06:00
|
|
|
func canceller(ctx context.Context, conn *jsonrpc2.Conn, id jsonrpc2.ID) {
|
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})
|
2018-09-24 15:24:58 -06:00
|
|
|
}
|
|
|
|
|
2019-03-29 17:04:29 -06:00
|
|
|
func NewClient(stream jsonrpc2.Stream, client Client) (*jsonrpc2.Conn, Server, xlog.Logger) {
|
|
|
|
log := xlog.New(NewLogger(client))
|
2019-03-28 19:06:01 -06:00
|
|
|
conn := jsonrpc2.NewConn(stream)
|
2019-03-29 12:16:59 -06:00
|
|
|
conn.Capacity = defaultMessageBufferSize
|
2019-06-14 11:17:16 -06:00
|
|
|
conn.RejectIfOverloaded = defaultRejectIfOverloaded
|
2019-03-29 17:04:29 -06:00
|
|
|
conn.Handler = clientHandler(log, client)
|
2019-03-28 19:06:01 -06:00
|
|
|
conn.Canceler = jsonrpc2.Canceler(canceller)
|
2019-03-29 17:04:29 -06:00
|
|
|
return conn, &serverDispatcher{Conn: conn}, log
|
2018-09-24 15:24:58 -06:00
|
|
|
}
|
|
|
|
|
2019-03-29 17:04:29 -06:00
|
|
|
func NewServer(stream jsonrpc2.Stream, server Server) (*jsonrpc2.Conn, Client, xlog.Logger) {
|
2019-03-28 19:06:01 -06:00
|
|
|
conn := jsonrpc2.NewConn(stream)
|
2019-03-29 17:04:29 -06:00
|
|
|
client := &clientDispatcher{Conn: conn}
|
|
|
|
log := xlog.New(NewLogger(client))
|
2019-03-29 12:16:59 -06:00
|
|
|
conn.Capacity = defaultMessageBufferSize
|
2019-06-14 11:17:16 -06:00
|
|
|
conn.RejectIfOverloaded = defaultRejectIfOverloaded
|
2019-03-29 17:04:29 -06:00
|
|
|
conn.Handler = serverHandler(log, server)
|
2019-03-28 19:06:01 -06:00
|
|
|
conn.Canceler = jsonrpc2.Canceler(canceller)
|
2019-03-29 17:04:29 -06:00
|
|
|
return conn, client, log
|
2018-09-24 15:24:58 -06:00
|
|
|
}
|
|
|
|
|
2019-06-24 07:18:31 -06:00
|
|
|
func sendParseError(ctx context.Context, log xlog.Logger, 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-03-29 17:04:29 -06:00
|
|
|
log.Errorf(ctx, "%v", err)
|
2018-11-13 20:49:07 -07:00
|
|
|
}
|
2018-09-24 15:24:58 -06:00
|
|
|
}
|