2023-07-02 08:45:30 +00:00
|
|
|
package libbox
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
2024-06-24 01:49:15 +00:00
|
|
|
"github.com/sagernet/sing/common/varbin"
|
2023-07-02 08:45:30 +00: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 01:49:15 +00:00
|
|
|
errorMessage, err := varbin.ReadValue[string](reader, binary.BigEndian)
|
2023-07-02 08:45:30 +00: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 01:49:15 +00:00
|
|
|
err = varbin.Write(writer, binary.BigEndian, wErr.Error())
|
2023-07-02 08:45:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|