mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2026-02-26 16:57:08 +00:00
chore: add Int32Enum for common/atomic
This commit is contained in:
@@ -1,12 +1,11 @@
|
||||
package tunnel
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type TunnelMode int
|
||||
type TunnelMode int32
|
||||
|
||||
// ModeMapping is a mapping for Mode enum
|
||||
var ModeMapping = map[string]TunnelMode{
|
||||
@@ -21,30 +20,6 @@ const (
|
||||
Direct
|
||||
)
|
||||
|
||||
// UnmarshalYAML unserialize Mode with yaml
|
||||
func (m *TunnelMode) UnmarshalYAML(unmarshal func(any) error) error {
|
||||
var tp string
|
||||
unmarshal(&tp)
|
||||
mode, exist := ModeMapping[strings.ToLower(tp)]
|
||||
if !exist {
|
||||
return errors.New("invalid mode")
|
||||
}
|
||||
*m = mode
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalJSON unserialize Mode
|
||||
func (m *TunnelMode) UnmarshalJSON(data []byte) error {
|
||||
var tp string
|
||||
json.Unmarshal(data, &tp)
|
||||
mode, exist := ModeMapping[strings.ToLower(tp)]
|
||||
if !exist {
|
||||
return errors.New("invalid mode")
|
||||
}
|
||||
*m = mode
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalText unserialize Mode
|
||||
func (m *TunnelMode) UnmarshalText(data []byte) error {
|
||||
mode, exist := ModeMapping[strings.ToLower(string(data))]
|
||||
@@ -55,16 +30,6 @@ func (m *TunnelMode) UnmarshalText(data []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalYAML serialize TunnelMode with yaml
|
||||
func (m TunnelMode) MarshalYAML() (any, error) {
|
||||
return m.String(), nil
|
||||
}
|
||||
|
||||
// MarshalJSON serialize Mode
|
||||
func (m TunnelMode) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(m.String())
|
||||
}
|
||||
|
||||
// MarshalText serialize Mode
|
||||
func (m TunnelMode) MarshalText() ([]byte, error) {
|
||||
return []byte(m.String()), nil
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
package tunnel
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
type TunnelStatus int
|
||||
type TunnelStatus int32
|
||||
|
||||
// StatusMapping is a mapping for Status enum
|
||||
var StatusMapping = map[string]TunnelStatus{
|
||||
@@ -22,30 +20,6 @@ const (
|
||||
Running
|
||||
)
|
||||
|
||||
// UnmarshalYAML unserialize Status with yaml
|
||||
func (s *TunnelStatus) UnmarshalYAML(unmarshal func(any) error) error {
|
||||
var tp string
|
||||
unmarshal(&tp)
|
||||
status, exist := StatusMapping[strings.ToLower(tp)]
|
||||
if !exist {
|
||||
return errors.New("invalid status")
|
||||
}
|
||||
*s = status
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalJSON unserialize Status
|
||||
func (s *TunnelStatus) UnmarshalJSON(data []byte) error {
|
||||
var tp string
|
||||
json.Unmarshal(data, &tp)
|
||||
status, exist := StatusMapping[strings.ToLower(tp)]
|
||||
if !exist {
|
||||
return errors.New("invalid status")
|
||||
}
|
||||
*s = status
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalText unserialize Status
|
||||
func (s *TunnelStatus) UnmarshalText(data []byte) error {
|
||||
status, exist := StatusMapping[strings.ToLower(string(data))]
|
||||
@@ -56,16 +30,6 @@ func (s *TunnelStatus) UnmarshalText(data []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalYAML serialize TunnelMode with yaml
|
||||
func (s TunnelStatus) MarshalYAML() (any, error) {
|
||||
return s.String(), nil
|
||||
}
|
||||
|
||||
// MarshalJSON serialize Status
|
||||
func (s TunnelStatus) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(s.String())
|
||||
}
|
||||
|
||||
// MarshalText serialize Status
|
||||
func (s TunnelStatus) MarshalText() ([]byte, error) {
|
||||
return []byte(s.String()), nil
|
||||
@@ -83,25 +47,3 @@ func (s TunnelStatus) String() string {
|
||||
return "Unknown"
|
||||
}
|
||||
}
|
||||
|
||||
type AtomicStatus struct {
|
||||
value atomic.Int32
|
||||
}
|
||||
|
||||
func (a *AtomicStatus) Store(s TunnelStatus) {
|
||||
a.value.Store(int32(s))
|
||||
}
|
||||
|
||||
func (a *AtomicStatus) Load() TunnelStatus {
|
||||
return TunnelStatus(a.value.Load())
|
||||
}
|
||||
|
||||
func (a *AtomicStatus) String() string {
|
||||
return a.Load().String()
|
||||
}
|
||||
|
||||
func newAtomicStatus(s TunnelStatus) *AtomicStatus {
|
||||
a := &AtomicStatus{}
|
||||
a.Store(s)
|
||||
return a
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
status = newAtomicStatus(Suspend)
|
||||
status = atomic.NewInt32Enum(Suspend)
|
||||
udpInit sync.Once
|
||||
udpQueues []chan C.PacketAdapter
|
||||
natTable = nat.New()
|
||||
@@ -59,7 +59,7 @@ var (
|
||||
// default timeout for UDP session
|
||||
udpTimeout = 60 * time.Second
|
||||
|
||||
findProcessMode = atomic.NewTypedValue(P.FindProcessStrict)
|
||||
findProcessMode = atomic.NewInt32Enum(P.FindProcessStrict)
|
||||
|
||||
fakeIPRange netip.Prefix
|
||||
|
||||
|
||||
Reference in New Issue
Block a user