github.com/sagernet/sing-box@v1.9.0-rc.20/experimental/libbox/command_power.go (about)

     1  package libbox
     2  
     3  import (
     4  	"encoding/binary"
     5  	"net"
     6  
     7  	E "github.com/sagernet/sing/common/exceptions"
     8  	"github.com/sagernet/sing/common/rw"
     9  )
    10  
    11  func (c *CommandClient) ServiceReload() error {
    12  	conn, err := c.directConnect()
    13  	if err != nil {
    14  		return err
    15  	}
    16  	defer conn.Close()
    17  	err = binary.Write(conn, binary.BigEndian, uint8(CommandServiceReload))
    18  	if err != nil {
    19  		return err
    20  	}
    21  	var hasError bool
    22  	err = binary.Read(conn, binary.BigEndian, &hasError)
    23  	if err != nil {
    24  		return err
    25  	}
    26  	if hasError {
    27  		errorMessage, err := rw.ReadVString(conn)
    28  		if err != nil {
    29  			return err
    30  		}
    31  		return E.New(errorMessage)
    32  	}
    33  	return nil
    34  }
    35  
    36  func (s *CommandServer) handleServiceReload(conn net.Conn) error {
    37  	rErr := s.handler.ServiceReload()
    38  	err := binary.Write(conn, binary.BigEndian, rErr != nil)
    39  	if err != nil {
    40  		return err
    41  	}
    42  	if rErr != nil {
    43  		return rw.WriteVString(conn, rErr.Error())
    44  	}
    45  	return nil
    46  }
    47  
    48  func (c *CommandClient) ServiceClose() error {
    49  	conn, err := c.directConnect()
    50  	if err != nil {
    51  		return err
    52  	}
    53  	defer conn.Close()
    54  	err = binary.Write(conn, binary.BigEndian, uint8(CommandServiceClose))
    55  	if err != nil {
    56  		return err
    57  	}
    58  	var hasError bool
    59  	err = binary.Read(conn, binary.BigEndian, &hasError)
    60  	if err != nil {
    61  		return nil
    62  	}
    63  	if hasError {
    64  		errorMessage, err := rw.ReadVString(conn)
    65  		if err != nil {
    66  			return nil
    67  		}
    68  		return E.New(errorMessage)
    69  	}
    70  	return nil
    71  }
    72  
    73  func (s *CommandServer) handleServiceClose(conn net.Conn) error {
    74  	rErr := s.service.Close()
    75  	s.handler.PostServiceClose()
    76  	err := binary.Write(conn, binary.BigEndian, rErr != nil)
    77  	if err != nil {
    78  		return err
    79  	}
    80  	if rErr != nil {
    81  		return rw.WriteVString(conn, rErr.Error())
    82  	}
    83  	return nil
    84  }