| 1 |
package transport |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"log/slog" |
| 8 |
"sync" |
| 9 |
"sync/atomic" |
| 10 |
|
| 11 |
"github.com/sourcegraph/jsonrpc2" |
| 12 |
) |
| 13 |
|
| 14 |
// Connection represents a managed connection to a language server. |
| 15 |
type Connection struct { |
| 16 |
conn jsonrpc2.JSONRPC2 |
| 17 |
transport *Transport |
| 18 |
router *Router |
| 19 |
logger *slog.Logger |
| 20 |
|
| 21 |
// State management |
| 22 |
closed atomic.Bool |
| 23 |
closeMu sync.Mutex |
| 24 |
closeErr error |
| 25 |
|
| 26 |
// Request tracking |
| 27 |
requestMu sync.Mutex |
| 28 |
requests map[jsonrpc2.ID]chan *Message |
| 29 |
nextID int64 |
| 30 |
} |
| 31 |
|
| 32 |
// NewConnection creates a new managed connection. |
| 33 |
func NewConnection(ctx context.Context, stream io.ReadWriteCloser, logger *slog.Logger) (*Connection, error) { |
| 34 |
c := &Connection{ |
| 35 |
router: NewRouter(), |
| 36 |
logger: logger, |
| 37 |
requests: make(map[jsonrpc2.ID]chan *Message), |
| 38 |
} |
| 39 |
|
| 40 |
// Create JSON-RPC connection |
| 41 |
conn := jsonrpc2.NewConn( |
| 42 |
ctx, |
| 43 |
jsonrpc2.NewBufferedStream(stream, jsonrpc2.VSCodeObjectCodec{}), |
| 44 |
jsonrpc2.HandlerWithError(c.handleRequest), |
| 45 |
) |
| 46 |
|
| 47 |
c.conn = conn |
| 48 |
c.transport = NewWithConn(conn) |
| 49 |
|
| 50 |
return c, nil |
| 51 |
} |
| 52 |
|
| 53 |
// Call makes a request to the language server and waits for a response. |
| 54 |
func (c *Connection) Call(ctx context.Context, method string, params any, result any) error { |
| 55 |
if c.closed.Load() { |
| 56 |
return fmt.Errorf("connection is closed") |
| 57 |
} |
| 58 |
|
| 59 |
return c.conn.Call(ctx, method, params, result) |
| 60 |
} |
| 61 |
|
| 62 |
// Notify sends a notification to the language server. |
| 63 |
func (c *Connection) Notify(ctx context.Context, method string, params any) error { |
| 64 |
if c.closed.Load() { |
| 65 |
return fmt.Errorf("connection is closed") |
| 66 |
} |
| 67 |
|
| 68 |
return c.conn.Notify(ctx, method, params) |
| 69 |
} |
| 70 |
|
| 71 |
// handleRequest handles incoming requests from the language server. |
| 72 |
func (c *Connection) handleRequest(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) (any, error) { |
| 73 |
if c.logger != nil { |
| 74 |
c.logger.Debug("Handling request", "method", req.Method) |
| 75 |
} |
| 76 |
|
| 77 |
return c.router.Route(ctx, req) |
| 78 |
} |
| 79 |
|
| 80 |
// RegisterHandler registers a handler for a specific method. |
| 81 |
func (c *Connection) RegisterHandler(method string, handler Handler) { |
| 82 |
c.router.Handle(method, handler) |
| 83 |
} |
| 84 |
|
| 85 |
// RegisterNotificationHandler registers a notification handler. |
| 86 |
func (c *Connection) RegisterNotificationHandler(method string, handler NotificationHandler) { |
| 87 |
c.router.HandleNotification(method, handler) |
| 88 |
} |
| 89 |
|
| 90 |
// Close closes the connection. |
| 91 |
func (c *Connection) Close() error { |
| 92 |
c.closeMu.Lock() |
| 93 |
defer c.closeMu.Unlock() |
| 94 |
|
| 95 |
if c.closed.Load() { |
| 96 |
return c.closeErr |
| 97 |
} |
| 98 |
|
| 99 |
c.closed.Store(true) |
| 100 |
|
| 101 |
// Close the JSON-RPC connection |
| 102 |
if c.conn != nil { |
| 103 |
c.closeErr = c.conn.Close() |
| 104 |
} |
| 105 |
|
| 106 |
// Close any pending requests |
| 107 |
c.requestMu.Lock() |
| 108 |
for _, ch := range c.requests { |
| 109 |
close(ch) |
| 110 |
} |
| 111 |
c.requests = nil |
| 112 |
c.requestMu.Unlock() |
| 113 |
|
| 114 |
return c.closeErr |
| 115 |
} |
| 116 |
|
| 117 |
// IsConnected returns true if the connection is still active. |
| 118 |
func (c *Connection) IsConnected() bool { |
| 119 |
return !c.closed.Load() |
| 120 |
} |