From 8c6f73f30b155375bc412d477d1a8f4d518e9cfb Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Sat, 21 Oct 2017 20:40:31 +0200 Subject: [PATCH] bitmask --- app/proxyman/mux/frame.go | 21 ++++----------------- app/proxyman/mux/session.go | 1 + common/bitmask/byte.go | 21 +++++++++++++++++++++ 3 files changed, 26 insertions(+), 17 deletions(-) create mode 100644 common/bitmask/byte.go diff --git a/app/proxyman/mux/frame.go b/app/proxyman/mux/frame.go index 393fa00bf..b31ffed4f 100644 --- a/app/proxyman/mux/frame.go +++ b/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:] diff --git a/app/proxyman/mux/session.go b/app/proxyman/mux/session.go index 60816dd11..0cb5592e3 100644 --- a/app/proxyman/mux/session.go +++ b/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() diff --git a/common/bitmask/byte.go b/common/bitmask/byte.go new file mode 100644 index 000000000..724fd9d93 --- /dev/null +++ b/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 +}