Newer
Older
)
type HandlerFunc func(w ResponseWriter, r *Request)
type Handler interface {
ServeRPC(w ResponseWriter, r *Request)
}
}
func (fn HandlerFunc) ServeRPC(w ResponseWriter, r *Request) {
(fn)(w, r)
}
Edward Lee
committed
peer PeerInfo
func NewRequest(ctx context.Context, id string, method string, params any) *Request {
r := &Request{ctx: ctx}
Edward Lee
committed
Edward Lee
committed
func (r *Request) ParamSlice() []any {
var params []any
var jpool = jsoniter.NewIterator(jsoniter.ConfigCompatibleWithStandardLibrary).Pool()
func (r *Request) Iter(fn func(j *jsoniter.Iterator) error) error {
it := jpool.BorrowIterator(r.Params())
defer jpool.ReturnIterator(it)
return fn(it)
}
func (r *Request) ParamArray(a ...any) error {
var params []json.RawMessage
if err != nil {
return err
}
} else {
break
}
}
return nil
}
Edward Lee
committed
func (r *Request) Remote() string {
return r.peer.RemoteAddr
}
func (r *Request) Peer() PeerInfo {
return r.peer
}
func (r *Request) WithContext(ctx context.Context) *Request {
if ctx == nil {
panic("nil context")
}
r.ctx = ctx
r2 := new(Request)
*r2 = *r
r2.ctx = ctx
r2.msg = r.msg
return r2
}
Edward Lee
committed
func (r *Request) Msg() jsonrpcMessage {
return r.msg
}
type ResponseWriterMsg struct {
r *Request
n *Notifier
s *Subscription
msg *jsonrpcMessage
options options
}
type options struct {
sorted bool
func UpgradeToSubscription(w ResponseWriter, r *Request) (*Subscription, error) {
not, ok := NotifierFromContext(r.ctx)
if !ok || not == nil {
return nil, errors.New("subscription not supported")
}
return not.CreateSubscription(), nil
}
func (w *ResponseWriterMsg) Option(k string, v any) {
switch k {
case "sorted":
func (w *ResponseWriterMsg) Send(args any, e error) (err error) {
cm := w.r.Msg()
if e != nil {
w.msg = cm.errorResponse(e)
return nil
}
switch c := args.(type) {
case *Subscription:
w.s = c
default:
return nil
}
func (w *ResponseWriterMsg) Notify(args any) (err error) {
if w.s == nil || w.n == nil {
return ErrSubscriptionNotFound
bts, _ := jzon.Marshal(args)
err = w.n.send(w.s, bts)
if err != nil {
return err