func (e *Endpoint) Close() error {
	return e.close(nil)
}
Close releases the resources used by endpoint.
package rpc
import (
	"bufio"
	"errors"
	"fmt"
	"io"
	"reflect"
	"sync"
	"github.com/neovim/go-client/msgpack"
)
type kind int
const (
	requestMessage      kind = 0
	replyMessage        kind = 1
	notificationMessage kind = 2
)
type state int
const (
	stateInit state = iota
	stateClosed
)
var (
	ErrClosed = errors.New("msgpack/rpc: session closed")
	ErrInternal = errors.New("msgpack/rpc: internal error")
	ErrHandlerNotFunction = errors.New("msgpack/rpc: handler not a function")
	ErrInvalidHandlerReturn = errors.New("msgpack/rpc: handler return must be (), (error) or (valueType, error)")
	ErrInvalidArgument = errors.New("msgpack/rpc: invalid argument")
)
type Error struct {
	Value interface{}
}
func (e Error) Error() string {
	return fmt.Sprintf("%v", e.Value)
}
type Call struct {
	Args   interface{}
	Reply  interface{}
	Err    error
	Done   chan *Call
	Method string
}
func (c *Call) done(e *Endpoint, err error) {
	c.Err = err
	select {
	case c.Done <- c:
	default:
		e.logf("msgpack/rpc: done channel over capacity for method %s", c.Method)
	}
}
type handler struct {
	fn   reflect.Value
	args []reflect.Value
}
type notification struct {
	call   func([]reflect.Value) []reflect.Value
	next   *notification
	method string
	args   []reflect.Value
}
type Endpoint struct {
	err  error
	logf func(fmt string, args ...interface{})
	done   chan struct{}
	closer io.Closer
	bw     *bufio.Writer
	enc    *msgpack.Encoder
	dec    *msgpack.Decoder
	handlers          map[string]*handler
	pending           map[uint64]*Call
	notificationsCond *sync.Cond
	arg           reflect.Value
	notifications []*notification
	state         state
	id            uint64
	mu              sync.Mutex
	handlersMu      sync.RWMutex
	encMu           sync.Mutex
	notificationsMu sync.Mutex
}
type Option struct{ f func(*Endpoint) }
func WithExtensions(extensions msgpack.ExtensionMap) Option {
	return Option{func(e *Endpoint) {
		e.dec.SetExtensions(extensions)
	}}
}
func WithLogf(f func(fmt string, args ...interface{})) Option {
	return Option{func(e *Endpoint) {
		e.logf = f
	}}
}
func NewEndpoint(r io.Reader, w io.Writer, c io.Closer, options ...Option) (*Endpoint, error) {
	bw := bufio.NewWriter(w)
	e := &Endpoint{
		done:     make(chan struct{}),
		handlers: make(map[string]*handler),
		pending:  make(map[uint64]*Call),
		closer:   c,
		bw:       bw,
		enc:      msgpack.NewEncoder(bw),
		dec:      msgpack.NewDecoder(r),
	}
	for _, option := range options {
		option.f(e)
	}
	return e, nil
}
func (e *Endpoint) decodeUint(what string) (uint64, error) {
	if err := e.dec.Unpack(); err != nil {
		return 0, err
	}
	t := e.dec.Type()
	if t != msgpack.Uint && t != msgpack.Int {
		return 0, fmt.Errorf("msgpack/rpc: error decoding %s, found %s", what, e.dec.Type())
	}
	return e.dec.Uint(), nil
}
func (e *Endpoint) decodeString(what string) (string, error) {
	if err := e.dec.Unpack(); err != nil {
		return "", err
	}
	if e.dec.Type() != msgpack.String {
		return "", fmt.Errorf("msgpack/rpc: error decoding %s, found %s", what, e.dec.Type())
	}
	return e.dec.String(), nil
}
func (e *Endpoint) skip(n int) error {
	for i := 0; i < n; i++ {
		if err := e.dec.Unpack(); err != nil {
			return err
		}
		if err := e.dec.Skip(); err != nil {
			return err
		}
	}
	return nil
}
func (e *Endpoint) Serve() error {
	e.notificationsCond = sync.NewCond(&e.notificationsMu)
	defer e.enqueNotification(nil)
	go e.runNotifications()
	for {
		if err := e.dec.Unpack(); err != nil {
			if err == io.EOF {
				err = nil
			}
			return e.close(err)
		}
		messageLen := e.dec.Len()
		if messageLen < 1 {
			return e.close(fmt.Errorf("msgpack/rpc: invalid message length %d", messageLen))
		}
		messageType, err := e.decodeUint("message type")
		if err != nil {
			return e.close(err)
		}
		switch kind(messageType) {
		case requestMessage:
			err = e.handleRequest(messageLen)
		case replyMessage:
			err = e.handleReply(messageLen)
		case notificationMessage:
			err = e.handleNotification(messageLen)
		default:
			err = fmt.Errorf("msgpack/rpc: unknown message type %d", messageType)
		}
		if err != nil {
			return e.close(err)
		}
	}
}
func (e *Endpoint) close(err error) error {
	e.mu.Lock()
	defer e.mu.Unlock()
	if e.state == stateClosed {
		return e.err
	}
	e.state = stateClosed
	e.err = err
	for _, call := range e.pending {
		call.done(e, ErrClosed)
	}
	e.pending = nil
	err = e.closer.Close()
	if e.err == nil {
		e.err = err
	}
	return e.err
}