sing-box/experimental/libbox/command_shared.go

40 lines
745 B
Go
Raw Normal View History

2023-07-02 16:45:30 +08:00
package libbox
import (
"encoding/binary"
"io"
E "github.com/sagernet/sing/common/exceptions"
2024-06-24 09:49:15 +08:00
"github.com/sagernet/sing/common/varbin"
2023-07-02 16:45:30 +08:00
)
func readError(reader io.Reader) error {
var hasError bool
err := binary.Read(reader, binary.BigEndian, &hasError)
if err != nil {
return err
}
if hasError {
2024-06-24 09:49:15 +08:00
errorMessage, err := varbin.ReadValue[string](reader, binary.BigEndian)
2023-07-02 16:45:30 +08:00
if err != nil {
return err
}
return E.New(errorMessage)
}
return nil
}
func writeError(writer io.Writer, wErr error) error {
err := binary.Write(writer, binary.BigEndian, wErr != nil)
if err != nil {
return err
}
if wErr != nil {
2024-06-24 09:49:15 +08:00
err = varbin.Write(writer, binary.BigEndian, wErr.Error())
2023-07-02 16:45:30 +08:00
if err != nil {
return err
}
}
return nil
}