github.com/ronaksoft/rony@v0.16.26-0.20230807065236-1743dbfe6959/edge/dispatcher.go (about) 1 package edge 2 3 import ( 4 "github.com/goccy/go-json" 5 "github.com/ronaksoft/rony" 6 "github.com/ronaksoft/rony/pools" 7 ) 8 9 /* 10 Creation Time: 2020 - Nov - 13 11 Created by: (ehsan) 12 Maintainers: 13 1. Ehsan N. Moosa (E2) 14 Auditor: Ehsan N. Moosa (E2) 15 Copyright Ronak Software Group 2020 16 */ 17 18 type Dispatcher interface { 19 // Encode will be called on the outgoing messages to encode them into the connection. 20 // it is responsible for write data to conn 21 Encode(conn rony.Conn, streamID int64, me *rony.MessageEnvelope) error 22 // Decode decodes the incoming wire messages and converts it to a rony.MessageEnvelope 23 Decode(data []byte, me *rony.MessageEnvelope) error 24 // Done will be called when the context has been finished, this lets cleaning up, or in case you need to flush the 25 // messages and updates in one go. 26 Done(ctx *DispatchCtx) 27 // OnOpen will be called when a new connection has been opened 28 OnOpen(conn rony.Conn, kvs ...*rony.KeyValue) 29 // OnClose will be called when a connection is closed 30 OnClose(conn rony.Conn) 31 } 32 33 // DefaultDispatcher is a default implementation of Dispatcher. You only need to set OnMessageFunc with 34 type DefaultDispatcher struct{} 35 36 func (s *DefaultDispatcher) Encode(conn rony.Conn, streamID int64, me *rony.MessageEnvelope) error { 37 buf := pools.Buffer.FromProto(me) 38 _ = conn.WriteBinary(streamID, *buf.Bytes()) 39 pools.Buffer.Put(buf) 40 41 return nil 42 } 43 44 func (s *DefaultDispatcher) Decode(data []byte, me *rony.MessageEnvelope) error { 45 return me.Unmarshal(data) 46 } 47 48 func (s *DefaultDispatcher) Done(ctx *DispatchCtx) { 49 ctx.BufferPopAll( 50 func(envelope *rony.MessageEnvelope) { 51 buf := pools.Buffer.FromProto(envelope) 52 _ = ctx.Conn().WriteBinary(ctx.StreamID(), *buf.Bytes()) 53 pools.Buffer.Put(buf) 54 }, 55 ) 56 } 57 58 func (s *DefaultDispatcher) OnOpen(conn rony.Conn, kvs ...*rony.KeyValue) { 59 for _, kv := range kvs { 60 conn.Set(kv.Key, kv.Value) 61 } 62 } 63 64 func (s *DefaultDispatcher) OnClose(conn rony.Conn) { 65 // Do nothing 66 } 67 68 type JSONDispatcher struct{} 69 70 func (j *JSONDispatcher) Encode(conn rony.Conn, streamID int64, me *rony.MessageEnvelope) error { 71 b, err := json.Marshal(me) 72 if err != nil { 73 return err 74 } 75 76 _ = conn.WriteBinary(streamID, b) 77 78 return nil 79 } 80 81 func (j *JSONDispatcher) Decode(data []byte, me *rony.MessageEnvelope) error { 82 return json.Unmarshal(data, me) 83 } 84 85 func (j *JSONDispatcher) Done(ctx *DispatchCtx) { 86 ctx.BufferPopAll( 87 func(envelope *rony.MessageEnvelope) { 88 b, err := json.Marshal(envelope) 89 if err != nil { 90 return 91 } 92 93 _ = ctx.conn.WriteBinary(ctx.StreamID(), b) 94 }, 95 ) 96 } 97 98 func (j *JSONDispatcher) OnOpen(conn rony.Conn, kvs ...*rony.KeyValue) { 99 for _, kv := range kvs { 100 conn.Set(kv.Key, kv.Value) 101 } 102 } 103 104 func (j *JSONDispatcher) OnClose(conn rony.Conn) { 105 // DO NOTHING 106 }