github.com/inazumav/sing-box@v0.0.0-20230926072359-ab51429a14f1/experimental/libbox/command_shared.go (about)

     1  package libbox
     2  
     3  import (
     4  	"encoding/binary"
     5  	"io"
     6  
     7  	E "github.com/sagernet/sing/common/exceptions"
     8  	"github.com/sagernet/sing/common/rw"
     9  )
    10  
    11  func readError(reader io.Reader) error {
    12  	var hasError bool
    13  	err := binary.Read(reader, binary.BigEndian, &hasError)
    14  	if err != nil {
    15  		return err
    16  	}
    17  	if hasError {
    18  		errorMessage, err := rw.ReadVString(reader)
    19  		if err != nil {
    20  			return err
    21  		}
    22  		return E.New(errorMessage)
    23  	}
    24  	return nil
    25  }
    26  
    27  func writeError(writer io.Writer, wErr error) error {
    28  	err := binary.Write(writer, binary.BigEndian, wErr != nil)
    29  	if err != nil {
    30  		return err
    31  	}
    32  	if wErr != nil {
    33  		err = rw.WriteVString(writer, wErr.Error())
    34  		if err != nil {
    35  			return err
    36  		}
    37  	}
    38  	return nil
    39  }