lnwire: use write buffer in Encode methods

This commit changes the WriteElement and WriteElements methods to take a
write buffer instead of io.Writer. The corresponding Encode methods are
changed to use the write buffer.
This commit is contained in:
yyforyongyu
2021-06-17 14:17:30 +08:00
parent 879d3cc86c
commit aa1561c60d
36 changed files with 139 additions and 76 deletions

View File

@@ -75,13 +75,8 @@ func (a addressType) AddrLen() uint16 {
}
// WriteElement is a one-stop shop to write the big endian representation of
// any element which is to be serialized for the wire protocol. The passed
// io.Writer should be backed by an appropriately sized byte slice, or be able
// to dynamically expand to accommodate additional data.
//
// TODO(roasbeef): this should eventually draw from a buffer pool for
// serialization.
func WriteElement(w io.Writer, element interface{}) error {
// any element which is to be serialized for the wire protocol.
func WriteElement(w *bytes.Buffer, element interface{}) error {
switch e := element.(type) {
case NodeAlias:
if _, err := w.Write(e[:]); err != nil {
@@ -437,10 +432,10 @@ func WriteElement(w io.Writer, element interface{}) error {
}
// WriteElements is writes each element in the elements slice to the passed
// io.Writer using WriteElement.
func WriteElements(w io.Writer, elements ...interface{}) error {
// buffer using WriteElement.
func WriteElements(buf *bytes.Buffer, elements ...interface{}) error {
for _, element := range elements {
err := WriteElement(w, element)
err := WriteElement(buf, element)
if err != nil {
return err
}