WIP jstreamrpc: still trying to figure out how to mesh jstream with jstreamrpc
This commit is contained in:
parent
6b6355d77f
commit
816a2575a0
@ -6,7 +6,6 @@ package jstreamrpc
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
|
||||
"github.com/mediocregopher/mediocre-go-lib/jstream"
|
||||
"github.com/mediocregopher/mediocre-go-lib/mrpc"
|
||||
@ -39,18 +38,8 @@ const (
|
||||
|
||||
func unmarshalBody(i interface{}, el jstream.Element) error {
|
||||
switch iT := i.(type) {
|
||||
case func(*jstream.StreamReader) error:
|
||||
stream, err := el.DecodeStream()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return iT(stream)
|
||||
case *io.Reader:
|
||||
ioR, err := el.DecodeBytes()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*iT = ioR
|
||||
case *jstream.Element:
|
||||
*iT = el
|
||||
return nil
|
||||
default:
|
||||
return el.DecodeValue(i)
|
||||
@ -60,9 +49,7 @@ func unmarshalBody(i interface{}, el jstream.Element) error {
|
||||
func marshalBody(w *jstream.StreamWriter, i interface{}) error {
|
||||
switch iT := i.(type) {
|
||||
case func(*jstream.StreamWriter) error:
|
||||
return w.EncodeStream(0, iT)
|
||||
case io.Reader:
|
||||
return w.EncodeBytes(0, iT)
|
||||
return iT(w)
|
||||
default:
|
||||
return w.EncodeValue(iT)
|
||||
}
|
||||
@ -88,17 +75,20 @@ func HandleCall(
|
||||
return errors.New("request head missing 'method' field")
|
||||
}
|
||||
|
||||
var didReadBody bool
|
||||
ctx = context.WithValue(ctx, ctxValR, r)
|
||||
ctx = context.WithValue(ctx, ctxValW, w)
|
||||
|
||||
body := r.Next()
|
||||
if body.Err != nil {
|
||||
return body.Err
|
||||
}
|
||||
|
||||
rw := new(mrpc.ResponseWriter)
|
||||
h.ServeRPC(mrpc.Request{
|
||||
Context: ctx,
|
||||
Method: head.Method,
|
||||
Unmarshal: func(i interface{}) error {
|
||||
didReadBody = true
|
||||
return unmarshalBody(i, r.Next())
|
||||
return unmarshalBody(i, body)
|
||||
},
|
||||
Debug: head.debug.Debug,
|
||||
}, rw)
|
||||
@ -118,13 +108,10 @@ func HandleCall(
|
||||
}
|
||||
}
|
||||
|
||||
// Reading the tail (and maybe discarding the body) should only be done once
|
||||
// marshalBody has finished
|
||||
if !didReadBody {
|
||||
if err := r.Next().Discard(); err != nil {
|
||||
// make sure the body has been consumed
|
||||
if err := body.Discard(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if err := w.EncodeValue(resTail{
|
||||
debug: debug{Debug: rw.Debug},
|
||||
@ -138,32 +125,40 @@ func HandleCall(
|
||||
|
||||
/*
|
||||
func sqr(r mrpc.Request, rw *mrpc.ResponseWriter) {
|
||||
ch := make(chan int)
|
||||
rw.Response = func(w *jstream.StreamWriter) error {
|
||||
for i := range ch {
|
||||
if err := w.EncodeValue(i * i); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
var el jstream.Element
|
||||
if err := r.Unmarshal(&el); err != nil {
|
||||
rw.Response = err
|
||||
return
|
||||
}
|
||||
|
||||
sr, err := el.DecodeStream()
|
||||
if err != nil {
|
||||
rw.Response = err
|
||||
return
|
||||
}
|
||||
|
||||
ch := make(chan int)
|
||||
go func() {
|
||||
defer close(ch)
|
||||
err := r.Unmarshal(func(r *jstream.StreamReader) error {
|
||||
for {
|
||||
var i int
|
||||
if err := r.Next().Value(&i); err == jstream.ErrStreamEnded {
|
||||
return nil
|
||||
if err := sr.Next().Value(&i); err == jstream.ErrStreamEnded {
|
||||
return
|
||||
} else if err != nil {
|
||||
return err
|
||||
panic("TODO")
|
||||
}
|
||||
ch <- i
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
panic("TODO")
|
||||
}
|
||||
}()
|
||||
|
||||
rw.Response = func(sw *jstream.StreamWriter) error {
|
||||
sw = sw.EncodeStream()
|
||||
for i := range ch {
|
||||
if err := sw.EncodeValue(i * i); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user