mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-11-14 04:43:17 +00:00
62 lines
1.2 KiB
Go
62 lines
1.2 KiB
Go
|
//go:build darwin
|
||
|
|
||
|
package libbox
|
||
|
|
||
|
import (
|
||
|
"encoding/binary"
|
||
|
"net"
|
||
|
"runtime"
|
||
|
"time"
|
||
|
|
||
|
E "github.com/sagernet/sing/common/exceptions"
|
||
|
)
|
||
|
|
||
|
type StatusMessage struct {
|
||
|
Memory int64
|
||
|
Goroutines int32
|
||
|
}
|
||
|
|
||
|
func readStatus() StatusMessage {
|
||
|
var memStats runtime.MemStats
|
||
|
runtime.ReadMemStats(&memStats)
|
||
|
var message StatusMessage
|
||
|
message.Memory = int64(memStats.HeapInuse + memStats.StackInuse + memStats.MSpanInuse)
|
||
|
message.Goroutines = int32(runtime.NumGoroutine())
|
||
|
return message
|
||
|
}
|
||
|
|
||
|
func (s *CommandServer) handleStatusConn(conn net.Conn) error {
|
||
|
var interval int64
|
||
|
err := binary.Read(conn, binary.BigEndian, &interval)
|
||
|
if err != nil {
|
||
|
return E.Cause(err, "read interval")
|
||
|
}
|
||
|
ticker := time.NewTicker(time.Duration(interval))
|
||
|
defer ticker.Stop()
|
||
|
ctx := connKeepAlive(conn)
|
||
|
for {
|
||
|
err = binary.Write(conn, binary.BigEndian, readStatus())
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
select {
|
||
|
case <-ctx.Done():
|
||
|
return nil
|
||
|
case <-ticker.C:
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (c *CommandClient) handleStatusConn(conn net.Conn) {
|
||
|
c.handler.Connected()
|
||
|
for {
|
||
|
var message StatusMessage
|
||
|
err := binary.Read(conn, binary.BigEndian, &message)
|
||
|
if err != nil {
|
||
|
c.handler.Disconnected(err.Error())
|
||
|
return
|
||
|
}
|
||
|
c.handler.WriteStatus(&message)
|
||
|
}
|
||
|
}
|