@@ -4,7 +4,10 @@ import (
44 "context"
55
66 "github.com/reactivex/rxgo/v2"
7+ "github.com/yomorun/yomo/core/quic"
8+ "github.com/yomorun/yomo/internal/core"
79 "github.com/yomorun/yomo/internal/decoder"
10+ "github.com/yomorun/yomo/internal/frame"
811 "github.com/yomorun/yomo/logger"
912)
1013
@@ -13,11 +16,8 @@ type Factory interface {
1316 // FromChannel creates a new Stream from a channel.
1417 FromChannel (ctx context.Context , channel chan interface {}) Stream
1518
16- // FromReader creates a new Stream from decoder.Reader.
17- FromReader (ctx context.Context , reader decoder.Reader ) Stream
18-
19- // FromReaderWithDecoder creates a new Stream with decoder.
20- FromReaderWithDecoder (readers chan decoder.Reader , opts ... decoder.Option ) Stream
19+ // FromQuicStream creates a new Stream from QuicStream.
20+ FromQuicStream (ctx context.Context , stream quic.Stream ) Stream
2121
2222 // FromItems creates a new Stream from items.
2323 FromItems (ctx context.Context , items []interface {}) Stream
@@ -60,56 +60,32 @@ func (fac *factoryImpl) FromChannel(ctx context.Context, channel chan interface{
6060 return CreateObservable (ctx , f )
6161}
6262
63- // FromReader creates a new Stream from decoder.Reader .
64- func (fac * factoryImpl ) FromReader (ctx context.Context , reader decoder. Reader ) Stream {
63+ // FromQuicStream creates a new RxStream from QUIC Stream .
64+ func (fac * factoryImpl ) FromQuicStream (ctx context.Context , stream quic. Stream ) Stream {
6565 f := func (ctx context.Context , next chan rxgo.Item ) {
6666 defer close (next )
6767
68- frameChan := reader .Read ()
6968 for {
70- select {
71- case <- ctx .Done ():
72- return
73- case frame , ok := <- frameChan :
74- if ! ok {
75- return
76- }
69+ f , err := core .ParseFrame (stream )
70+ if err != nil {
71+ logger .Error ("Parse the frame failed" , "err" , err )
72+ break
73+ }
7774
78- logger .Debug ("Receive frame from source." )
79- next <- Of (frame .Data ())
75+ switch f .Type () {
76+ case frame .TagOfDataFrame :
77+ dataFrame := f .(* frame.DataFrame )
78+ logger .Debug ("Receive data frame from source." , "TransactionID" , dataFrame .TransactionID ())
79+ next <- Of (dataFrame .GetCarriage ())
80+ default :
81+ logger .Debug ("Only support data frame in RxStream." , "type" , f .Type ())
8082 }
8183 }
8284 }
8385
8486 return CreateObservable (ctx , f )
8587}
8688
87- // FromReaderWithDecoder creates a Stream with decoder.
88- func (fac * factoryImpl ) FromReaderWithDecoder (readers chan decoder.Reader , opts ... decoder.Option ) Stream {
89- f := func (ctx context.Context , next chan rxgo.Item ) {
90- defer close (next )
91-
92- for {
93- select {
94- case <- ctx .Done ():
95- return
96- case reader , ok := <- readers :
97- if ! ok {
98- return
99- }
100-
101- go func () {
102- frameChan := reader .Read ()
103- for frame := range frameChan {
104- Of (decoder .FromItems ([]interface {}{frame .Data ()}, opts ... )).SendContext (ctx , next )
105- }
106- }()
107- }
108- }
109- }
110- return CreateObservable (decoder .GetContext (opts ... ), f )
111- }
112-
11389// FromItems creates a new Stream from items.
11490func (fac * factoryImpl ) FromItems (ctx context.Context , items []interface {}) Stream {
11591 next := make (chan rxgo.Item )
0 commit comments