diff options
author | Keuin <[email protected]> | 2023-07-01 22:10:22 +0800 |
---|---|---|
committer | Keuin <[email protected]> | 2023-07-01 22:10:44 +0800 |
commit | b8d0d0c3b6b2ffe40921aa4c247c101dd0ce958d (patch) | |
tree | c14f512312a6ee2fab6bdaef7cb55dfedbf19262 /danmaku | |
parent | 8f07f6f4d7e91a9e4b7164a39759907fac5fb8a1 (diff) |
Refactor: rename consts to follow Go idiom. Fix const comment style.
Diffstat (limited to 'danmaku')
-rw-r--r-- | danmaku/client.go | 8 | ||||
-rw-r--r-- | danmaku/dmmsg/danmu.go | 8 | ||||
-rw-r--r-- | danmaku/dmmsg/util.go | 2 | ||||
-rw-r--r-- | danmaku/dmpkg/auth.go | 6 | ||||
-rw-r--r-- | danmaku/dmpkg/decode.go | 10 | ||||
-rw-r--r-- | danmaku/dmpkg/package.go | 15 | ||||
-rw-r--r-- | danmaku/dmpkg/raw.go | 12 |
7 files changed, 33 insertions, 28 deletions
diff --git a/danmaku/client.go b/danmaku/client.go index 3a1298d..b785e23 100644 --- a/danmaku/client.go +++ b/danmaku/client.go @@ -15,8 +15,8 @@ import ( "nhooyr.io/websocket" ) -// Bilibili uses only binary WebSocket messages -const kBilibiliWebSocketMessageType = websocket.MessageBinary +// BilibiliWebSocketMessageType Bilibili uses only binary WebSocket messages +const BilibiliWebSocketMessageType = websocket.MessageBinary type DanmakuClient struct { ws *websocket.Conn @@ -34,7 +34,7 @@ type wsDatagramIO struct { } func (w *wsDatagramIO) Consume(data []byte) error { - return w.ws.Write(w.ctx, kBilibiliWebSocketMessageType, data) + return w.ws.Write(w.ctx, BilibiliWebSocketMessageType, data) } func (w *wsDatagramIO) Get() (data []byte, err error) { @@ -42,7 +42,7 @@ func (w *wsDatagramIO) Get() (data []byte, err error) { if err != nil { return } - if typ != kBilibiliWebSocketMessageType { + if typ != BilibiliWebSocketMessageType { err = fmt.Errorf("invalid message type: expected a binary WebSocket message, however got %v", typ.String()) } return diff --git a/danmaku/dmmsg/danmu.go b/danmaku/dmmsg/danmu.go index 2cc5dfa..f0229d2 100644 --- a/danmaku/dmmsg/danmu.go +++ b/danmaku/dmmsg/danmu.go @@ -23,11 +23,11 @@ func (dm DanMuMessage) String() string { dm.SourceUser.Nickname, dm.SourceUser.UID, dm.Content) } -const kInvalidDanmakuJson = "invalid danmaku JSON document" +const InvalidDanmakuJson = "invalid danmaku JSON document" func ParseDanmakuMessage(body RawDanMuMessage) (dmm DanMuMessage, err error) { if len(body.Info) != 16 { - err = fmt.Errorf("%s: \"info\" length != 16", kInvalidDanmakuJson) + err = fmt.Errorf("%s: \"info\" length != 16", InvalidDanmakuJson) return } @@ -41,14 +41,14 @@ func ParseDanmakuMessage(body RawDanMuMessage) (dmm DanMuMessage, err error) { var ok bool uid, ok := userInfo[0].(float64) if !ok { - err = fmt.Errorf("%s: uid is not a float64: %v", kInvalidDanmakuJson, userInfo[0]) + err = fmt.Errorf("%s: uid is not a float64: %v", InvalidDanmakuJson, userInfo[0]) return } dmm.SourceUser.UID = int64(uid) dmm.SourceUser.Nickname, ok = userInfo[1].(string) if !ok { - err = fmt.Errorf("%s: nickname is not a string: %v", kInvalidDanmakuJson, userInfo[1]) + err = fmt.Errorf("%s: nickname is not a string: %v", InvalidDanmakuJson, userInfo[1]) return } return diff --git a/danmaku/dmmsg/util.go b/danmaku/dmmsg/util.go index 9d41ab9..d38814b 100644 --- a/danmaku/dmmsg/util.go +++ b/danmaku/dmmsg/util.go @@ -9,7 +9,7 @@ func castValue[T any](obj interface{}) (thing T, err error) { casted, ok := (obj).(T) if !ok { err = fmt.Errorf("%s: required value is not of type \"%v\": %v", - kInvalidDanmakuJson, reflect.TypeOf(thing).String(), obj) + InvalidDanmakuJson, reflect.TypeOf(thing).String(), obj) return } thing = casted diff --git a/danmaku/dmpkg/auth.go b/danmaku/dmpkg/auth.go index c39fbd9..5caf868 100644 --- a/danmaku/dmpkg/auth.go +++ b/danmaku/dmpkg/auth.go @@ -23,11 +23,11 @@ type authInfo struct { // NewAuth creates a new authentication exchange. func NewAuth(protocol ProtocolVer, roomId common.RoomId, authKey string) (exc DanmakuExchange) { exc, _ = NewPlainExchange(OpConnect, authInfo{ - UID: kUidGuest, + UID: UidGuest, RoomId: uint64(roomId), ProtoVer: int(protocol), - Platform: kPlatformWeb, - Type: kAuthTypeDefault, + Platform: PlatformWeb, + Type: AuthTypeDefault, Key: authKey, }) return diff --git a/danmaku/dmpkg/decode.go b/danmaku/dmpkg/decode.go index 7d9f796..e352fcc 100644 --- a/danmaku/dmpkg/decode.go +++ b/danmaku/dmpkg/decode.go @@ -7,14 +7,14 @@ import ( ) func DecodeExchange(data []byte) (exc DanmakuExchange, err error) { - if ln := len(data); ln < kHeaderLength { - err = fmt.Errorf("incomplete datagram: length = %v < %v", ln, kHeaderLength) + if ln := len(data); ln < HeaderLength { + err = fmt.Errorf("incomplete datagram: length = %v < %v", ln, HeaderLength) return } // unpack header var exchangeHeader DanmakuExchangeHeader - err = struc.Unpack(bytes.NewReader(data[:kHeaderLength]), &exchangeHeader) + err = struc.Unpack(bytes.NewReader(data[:HeaderLength]), &exchangeHeader) if err != nil { err = fmt.Errorf("cannot unpack exchange header: %w", err) return @@ -22,9 +22,9 @@ func DecodeExchange(data []byte) (exc DanmakuExchange, err error) { headerLength := exchangeHeader.HeaderLength // validate header length, fail fast if not match - if headerLength != kHeaderLength { + if headerLength != HeaderLength { err = fmt.Errorf("invalid header length, "+ - "the protocol implementation might be obsolete: %v != %v", headerLength, kHeaderLength) + "the protocol implementation might be obsolete: %v != %v", headerLength, HeaderLength) return } diff --git a/danmaku/dmpkg/package.go b/danmaku/dmpkg/package.go index 3175a18..51c769a 100644 --- a/danmaku/dmpkg/package.go +++ b/danmaku/dmpkg/package.go @@ -31,8 +31,10 @@ func (e *DanmakuExchange) String() string { e.Length, e.ProtocolVer, e.Operation, e.Body) } -const kHeaderLength = 16 -const kSequenceId = 1 +const ( + HeaderLength = 16 + SequenceId = 1 +) type ProtocolVer uint16 @@ -47,9 +49,12 @@ const ( ProtoBrotli ProtocolVer = 3 ) -const kUidGuest = 0 -const kPlatformWeb = "web" -const kAuthTypeDefault = 2 // magic number, not sure what does it mean +const ( + UidGuest = 0 + PlatformWeb = "web" + // AuthTypeDefault magic number, not sure what does it mean + AuthTypeDefault = 2 +) func (e *DanmakuExchange) Marshal() (data []byte, err error) { var buffer bytes.Buffer diff --git a/danmaku/dmpkg/raw.go b/danmaku/dmpkg/raw.go index 17b538a..34cd2e6 100644 --- a/danmaku/dmpkg/raw.go +++ b/danmaku/dmpkg/raw.go @@ -6,7 +6,7 @@ import ( "math" ) -const kMaxBodyLength = math.MaxUint32 - uint64(kHeaderLength) +const MaxBodyLength = math.MaxUint32 - uint64(HeaderLength) // NewPlainExchange creates a new exchange with raw body specified. // body: a struct or a raw string @@ -28,18 +28,18 @@ func NewPlainExchange(operation Operation, body interface{}) (exc DanmakuExchang } } - length := uint64(kHeaderLength + len(bodyData)) - if length > kMaxBodyLength { - err = fmt.Errorf("body is too large (> %d)", kMaxBodyLength) + length := uint64(HeaderLength + len(bodyData)) + if length > MaxBodyLength { + err = fmt.Errorf("body is too large (> %d)", MaxBodyLength) return } exc = DanmakuExchange{ DanmakuExchangeHeader: DanmakuExchangeHeader{ Length: uint32(length), - HeaderLength: kHeaderLength, + HeaderLength: HeaderLength, ProtocolVer: ProtoPlainJson, Operation: operation, - SequenceId: kSequenceId, + SequenceId: SequenceId, }, Body: bodyData, } |