package jsonrpc2
import (
"context"
)
type Handler interface {
Deliver(ctx context.Context, r *Request, delivered bool) bool
Cancel(ctx context.Context, conn *Conn, id ID, cancelled bool) bool
Request(ctx context.Context, conn *Conn, direction Direction, r *WireRequest) context.Context
Response(ctx context.Context, conn *Conn, direction Direction, r *WireResponse) context.Context
Done(ctx context.Context, err error)
Read(ctx context.Context, bytes int64) context.Context
Wrote(ctx context.Context, bytes int64) context.Context
Error(ctx context.Context, err error)
}
type Direction bool
const (
Send = Direction(true)
Receive = Direction(false)
)
func (d Direction) String() string {
switch d {
case Send:
return "send"
case Receive:
return "receive"
default:
panic("unreachable")
}
}
type EmptyHandler struct{}
func (EmptyHandler) Deliver(ctx context.Context, r *Request, delivered bool) bool {
return false
}
func (EmptyHandler) Cancel(ctx context.Context, conn *Conn, id ID, cancelled bool) bool {
return false
}
func (EmptyHandler) Request(ctx context.Context, conn *Conn, direction Direction, r *WireRequest) context.Context {
return ctx
}
func (EmptyHandler) Response(ctx context.Context, conn *Conn, direction Direction, r *WireResponse) context.Context {
return ctx
}
func (EmptyHandler) Done(ctx context.Context, err error) {
}
func (EmptyHandler) Read(ctx context.Context, bytes int64) context.Context {
return ctx
}
func (EmptyHandler) Wrote(ctx context.Context, bytes int64) context.Context {
return ctx
}
func (EmptyHandler) Error(ctx context.Context, err error) {}
type defaultHandler struct{ EmptyHandler }
func (defaultHandler) Deliver(ctx context.Context, r *Request, delivered bool) bool {
if delivered {
return false
}
if !r.IsNotify() {
r.Reply(ctx, nil, NewErrorf(CodeMethodNotFound, "method %q not found", r.Method))
}
return true
}