Skip to content

internal/jsonrpc2: abstract Stream from net.Conn #256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions internal/jsonrpc2/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"encoding/json"
"fmt"
"io"
"net"
"strconv"
"strings"
)
Expand All @@ -31,23 +30,23 @@ type Stream interface {
Close() error
}

// Framer wraps a network connection up into a Stream.
// Framer wraps an io.ReadWriteCloser up into a Stream.
// It is responsible for the framing and encoding of messages into wire form.
// NewRawStream and NewHeaderStream are implementations of a Framer.
type Framer func(conn net.Conn) Stream
type Framer func(conn io.ReadWriteCloser) Stream

// NewRawStream returns a Stream built on top of a net.Conn.
// NewRawStream returns a Stream built on top of an io.ReadWriteCloser.
// The messages are sent with no wrapping, and rely on json decode consistency
// to determine message boundaries.
func NewRawStream(conn net.Conn) Stream {
func NewRawStream(conn io.ReadWriteCloser) Stream {
return &rawStream{
conn: conn,
in: json.NewDecoder(conn),
}
}

type rawStream struct {
conn net.Conn
conn io.ReadWriteCloser
in *json.Decoder
}

Expand Down Expand Up @@ -83,18 +82,18 @@ func (s *rawStream) Close() error {
return s.conn.Close()
}

// NewHeaderStream returns a Stream built on top of a net.Conn.
// NewHeaderStream returns a Stream built on top of a io.ReadWriteCloser.
// The messages are sent with HTTP content length and MIME type headers.
// This is the format used by LSP and others.
func NewHeaderStream(conn net.Conn) Stream {
func NewHeaderStream(conn io.ReadWriteCloser) Stream {
return &headerStream{
conn: conn,
in: bufio.NewReader(conn),
}
}

type headerStream struct {
conn net.Conn
conn io.ReadWriteCloser
in *bufio.Reader
}

Expand Down