github.com/nyan233/littlerpc@v0.4.6-0.20230316182519-0c8d5c48abaf/core/common/msgwriter/writer_nomux.go (about) 1 package msgwriter 2 3 import ( 4 "fmt" 5 "github.com/nyan233/littlerpc/core/common/errorhandler" 6 "github.com/nyan233/littlerpc/core/container" 7 perror "github.com/nyan233/littlerpc/core/protocol/error" 8 message2 "github.com/nyan233/littlerpc/core/protocol/message" 9 ) 10 11 type lRPCNoMux struct{} 12 13 func (l *lRPCNoMux) Header() []byte { 14 return []byte{message2.MagicNumber} 15 } 16 17 func NewLRPCNoMux(writers ...Writer) Writer { 18 return &lRPCNoMux{} 19 } 20 21 func (l *lRPCNoMux) Write(arg Argument, header byte) (err perror.LErrorDesc) { 22 if err = encoder(arg); err != nil { 23 return err 24 } 25 bp := arg.Pool.Get().(*container.Slice[byte]) 26 bp.Reset() 27 defer arg.Pool.Put(bp) 28 marshalErr := message2.Marshal(arg.Message, bp) 29 if marshalErr != nil { 30 return arg.EHandle.LWarpErrorDesc(errorhandler.ErrMessageEncoding, marshalErr.Error()) 31 } 32 writeN, wErr := arg.Conn.Write(*bp) 33 defer func() { 34 if arg.OnComplete != nil { 35 arg.OnComplete(*bp, err) 36 } 37 }() 38 if wErr != nil { 39 return arg.EHandle.LWarpErrorDesc(errorhandler.ErrConnection, 40 fmt.Sprintf("Write NoMuxMessage failed, bytes len : %v, err = %v", len(*bp), wErr)) 41 } 42 if writeN != bp.Len() { 43 return arg.EHandle.LWarpErrorDesc(errorhandler.ErrConnection, 44 fmt.Sprintf("NoMux write bytes not equal : w(%d) b(%d)", writeN, bp.Len())) 45 } 46 if arg.OnDebug != nil { 47 arg.OnDebug(*bp, false) 48 } 49 return nil 50 } 51 52 func (l *lRPCNoMux) Reset() { 53 return 54 }