Darien Raymond 8 years ago
parent
commit
8c6f73f30b
3 changed files with 26 additions and 17 deletions
  1. 4 17
      app/proxyman/mux/frame.go
  2. 1 0
      app/proxyman/mux/session.go
  3. 21 0
      common/bitmask/byte.go

+ 4 - 17
app/proxyman/mux/frame.go

@@ -1,6 +1,7 @@
 package mux
 
 import (
+	"v2ray.com/core/common/bitmask"
 	"v2ray.com/core/common/buf"
 	"v2ray.com/core/common/net"
 	"v2ray.com/core/common/serial"
@@ -15,24 +16,10 @@ const (
 	SessionStatusKeepAlive SessionStatus = 0x04
 )
 
-type Option byte
-
 const (
-	OptionData Option = 0x01
+	OptionData bitmask.Byte = 0x01
 )
 
-func (o Option) Has(x Option) bool {
-	return (o & x) == x
-}
-
-func (o *Option) Add(x Option) {
-	*o = (*o | x)
-}
-
-func (o *Option) Clear(x Option) {
-	*o = (*o & (^x))
-}
-
 type TargetNetwork byte
 
 const (
@@ -64,7 +51,7 @@ n bytes - address
 type FrameMetadata struct {
 	Target        net.Destination
 	SessionID     uint16
-	Option        Option
+	Option        bitmask.Byte
 	SessionStatus SessionStatus
 }
 
@@ -120,7 +107,7 @@ func ReadFrameFrom(b []byte) (*FrameMetadata, error) {
 	f := &FrameMetadata{
 		SessionID:     serial.BytesToUint16(b[:2]),
 		SessionStatus: SessionStatus(b[2]),
-		Option:        Option(b[3]),
+		Option:        bitmask.Byte(b[3]),
 	}
 
 	b = b[4:]

+ 1 - 0
app/proxyman/mux/session.go

@@ -122,6 +122,7 @@ type Session struct {
 	transferType protocol.TransferType
 }
 
+// Close closes all resources associated with this session.
 func (s *Session) Close() {
 	s.output.Close()
 	s.input.Close()

+ 21 - 0
common/bitmask/byte.go

@@ -0,0 +1,21 @@
+package bitmask
+
+// Byte is a bitmask in byte.
+type Byte byte
+
+// Has returns true if this bitmask contains another bitmask.
+func (b Byte) Has(bb Byte) bool {
+	return (b & bb) != 0
+}
+
+func (b *Byte) Add(bb Byte) {
+	*b |= bb
+}
+
+func (b *Byte) Clear(bb Byte) {
+	*b &= ^bb
+}
+
+func (b *Byte) Toggle(bb Byte) {
+	*b ^= bb
+}