fix golangci linter

This commit is contained in:
chenkaiC4
2018-08-15 13:26:29 +08:00
committed by Sean DuBois
parent 1e3981d44c
commit 2b7ab19a94
20 changed files with 70 additions and 54 deletions

View File

@@ -8,9 +8,5 @@ linters:
enable-all: true enable-all: true
disable: disable:
- maligned - maligned
- prealloc
- unconvert
- lll - lll
- dupl - dupl
- goconst
- gocyclo

View File

@@ -90,3 +90,6 @@ type SyntaxError struct {
func (e *SyntaxError) Error() string { func (e *SyntaxError) Error() string {
return fmt.Sprintf("syntax error: %v", e.Err) return fmt.Sprintf("syntax error: %v", e.Err)
} }
// ErrUnknownType indicates a Unknown info
var ErrUnknownType = errors.New("Unknown")

View File

@@ -116,19 +116,19 @@ func (s *State) HandleDTLSPacket(packet []byte, local, remote string) ([]byte, e
rawLocal := C.CString(local) rawLocal := C.CString(local)
rawRemote := C.CString(remote) rawRemote := C.CString(remote)
packetRaw := C.CBytes(packet) packetRaw := C.CBytes(packet) // unsafe.Pointer
defer func() { defer func() {
C.free(unsafe.Pointer(rawLocal)) C.free(unsafe.Pointer(rawLocal))
C.free(unsafe.Pointer(rawRemote)) C.free(unsafe.Pointer(rawRemote))
C.free(unsafe.Pointer(packetRaw)) C.free(packetRaw)
}() }()
if ret := C.dtls_handle_incoming(s.dtlsSession, packetRaw, C.int(len(packet)), rawLocal, rawRemote); ret != nil { if ret := C.dtls_handle_incoming(s.dtlsSession, packetRaw, C.int(len(packet)), rawLocal, rawRemote); ret != nil {
defer func() { defer func() {
C.free(unsafe.Pointer(ret.buf)) C.free(ret.buf)
C.free(unsafe.Pointer(ret)) C.free(unsafe.Pointer(ret))
}() }()
return []byte(C.GoBytes(ret.buf, ret.len)), nil return C.GoBytes(ret.buf, ret.len), nil
} }
return nil, nil return nil, nil
} }
@@ -144,11 +144,11 @@ func (s *State) Send(packet []byte, local, remote string) (bool, error) {
rawLocal := C.CString(local) rawLocal := C.CString(local)
rawRemote := C.CString(remote) rawRemote := C.CString(remote)
packetRaw := C.CBytes(packet) packetRaw := C.CBytes(packet) // unsafe.Pointer
defer func() { defer func() {
C.free(unsafe.Pointer(rawLocal)) C.free(unsafe.Pointer(rawLocal))
C.free(unsafe.Pointer(rawRemote)) C.free(unsafe.Pointer(rawRemote))
C.free(unsafe.Pointer(packetRaw)) C.free(packetRaw)
}() }()
return bool(C.dtls_handle_outgoing(s.dtlsSession, packetRaw, C.int(len(packet)), rawLocal, rawRemote)), nil return bool(C.dtls_handle_outgoing(s.dtlsSession, packetRaw, C.int(len(packet)), rawLocal, rawRemote)), nil

View File

@@ -48,13 +48,13 @@ type Manager struct {
} }
// NewManager creates a new network.Manager // NewManager creates a new network.Manager
func NewManager(bufferTransportGenerator BufferTransportGenerator, dataChannelEventHandler DataChannelEventHandler, iceNotifier ICENotifier) (m *Manager, err error) { func NewManager(btg BufferTransportGenerator, dcet DataChannelEventHandler, ntf ICENotifier) (m *Manager, err error) {
m = &Manager{ m = &Manager{
iceNotifier: iceNotifier, iceNotifier: ntf,
bufferTransports: make(map[uint32]chan<- *rtp.Packet), bufferTransports: make(map[uint32]chan<- *rtp.Packet),
srtpContexts: make(map[string]*srtp.Context), srtpContexts: make(map[string]*srtp.Context),
bufferTransportGenerator: bufferTransportGenerator, bufferTransportGenerator: btg,
dataChannelEventHandler: dataChannelEventHandler, dataChannelEventHandler: dcet,
} }
m.dtlsState, err = dtls.NewState() m.dtlsState, err = dtls.NewState()
if err != nil { if err != nil {
@@ -232,11 +232,13 @@ func (m *Manager) dataChannelInboundHandler(data []byte, streamIdentifier uint16
case sctp.PayloadTypeWebRTCString: case sctp.PayloadTypeWebRTCString:
fallthrough fallthrough
case sctp.PayloadTypeWebRTCStringEmpty: case sctp.PayloadTypeWebRTCStringEmpty:
m.dataChannelEventHandler(&DataChannelMessage{streamIdentifier: streamIdentifier, Payload: &datachannel.PayloadString{Data: data}}) payload := &datachannel.PayloadString{Data: data}
m.dataChannelEventHandler(&DataChannelMessage{streamIdentifier: streamIdentifier, Payload: payload})
case sctp.PayloadTypeWebRTCBinary: case sctp.PayloadTypeWebRTCBinary:
fallthrough fallthrough
case sctp.PayloadTypeWebRTCBinaryEmpty: case sctp.PayloadTypeWebRTCBinaryEmpty:
m.dataChannelEventHandler(&DataChannelMessage{streamIdentifier: streamIdentifier, Payload: &datachannel.PayloadBinary{Data: data}}) payload := &datachannel.PayloadBinary{Data: data}
m.dataChannelEventHandler(&DataChannelMessage{streamIdentifier: streamIdentifier, Payload: payload})
default: default:
fmt.Printf("Unhandled Payload Protocol Identifier %v \n", payloadType) fmt.Printf("Unhandled Payload Protocol Identifier %v \n", payloadType)
} }

View File

@@ -52,7 +52,7 @@ func (p *port) handleSRTP(buffer []byte) {
srtpContext, ok := p.m.srtpContexts[contextMapKey] srtpContext, ok := p.m.srtpContexts[contextMapKey]
if !ok { if !ok {
var err error var err error
srtpContext, err = srtp.CreateContext([]byte(p.m.certPair.ServerWriteKey[0:16]), []byte(p.m.certPair.ServerWriteKey[16:]), p.m.certPair.Profile, packet.SSRC) srtpContext, err = srtp.CreateContext(p.m.certPair.ServerWriteKey[0:16], p.m.certPair.ServerWriteKey[16:], p.m.certPair.Profile, packet.SSRC)
if err != nil { if err != nil {
fmt.Println("Failed to build SRTP context") fmt.Println("Failed to build SRTP context")
return return

View File

@@ -21,7 +21,7 @@ func (p *port) sendRTP(packet *rtp.Packet, dst net.Addr) {
srtpContext, ok := p.m.srtpContexts[contextMapKey] srtpContext, ok := p.m.srtpContexts[contextMapKey]
if !ok { if !ok {
var err error var err error
srtpContext, err = srtp.CreateContext([]byte(p.m.certPair.ClientWriteKey[0:16]), []byte(p.m.certPair.ClientWriteKey[16:]), p.m.certPair.Profile, packet.SSRC) srtpContext, err = srtp.CreateContext(p.m.certPair.ClientWriteKey[0:16], p.m.certPair.ClientWriteKey[16:], p.m.certPair.Profile, packet.SSRC)
if err != nil { if err != nil {
fmt.Println("Failed to build SRTP context") fmt.Println("Failed to build SRTP context")
return return

View File

@@ -148,12 +148,12 @@ func (a *Association) packetizeOutbound(raw []byte, streamIdentifier uint16, pay
var chunks []*chunkPayloadData var chunks []*chunkPayloadData
for remaining != 0 { for remaining != 0 {
l := min(a.myMaxMTU, uint16(remaining)) l := min(a.myMaxMTU, remaining)
chunks = append(chunks, &chunkPayloadData{ chunks = append(chunks, &chunkPayloadData{
streamIdentifier: streamIdentifier, streamIdentifier: streamIdentifier,
userData: raw[i : uint16(i)+l], userData: raw[i : i+l],
beginingFragment: i == 0, beginingFragment: i == 0,
endingFragment: uint16(remaining)-l == 0, endingFragment: remaining-l == 0,
immediateSack: false, immediateSack: false,
payloadType: payloadType, payloadType: payloadType,
streamSequenceNumber: seqNum, streamSequenceNumber: seqNum,

View File

@@ -98,12 +98,12 @@ func (c *chunkHeader) unmarshal(raw []byte) error {
} }
c.typ = chunkType(raw[0]) c.typ = chunkType(raw[0])
c.flags = byte(raw[1]) c.flags = raw[1]
length := binary.BigEndian.Uint16(raw[2:]) length := binary.BigEndian.Uint16(raw[2:])
// Length includes Chunk header // Length includes Chunk header
valueLength := int(length - chunkHeaderSize) valueLength := int(length - chunkHeaderSize)
lengthAfterValue := len(raw) - (chunkHeaderSize + int(valueLength)) lengthAfterValue := len(raw) - (chunkHeaderSize + valueLength)
if lengthAfterValue < 0 { if lengthAfterValue < 0 {
return errors.Errorf("Not enough data left in SCTP packet to satisfy requested length remain %d req %d ", valueLength, len(raw)-chunkHeaderSize) return errors.Errorf("Not enough data left in SCTP packet to satisfy requested length remain %d req %d ", valueLength, len(raw)-chunkHeaderSize)

View File

@@ -89,8 +89,8 @@ func (i *chunkInitCommon) unmarshal(raw []byte) error {
} }
i.params = append(i.params, p) i.params = append(i.params, p)
padding := getPadding(p.length()) padding := getPadding(p.length())
offset += int(p.length() + padding) offset += p.length() + padding
remaining -= int(p.length() + padding) remaining -= p.length() + padding
} else { } else {
break break
} }

View File

@@ -72,7 +72,7 @@ func (s *SessionDescription) Marshal() (raw string) {
} }
} }
var rawTimeZones []string rawTimeZones := make([]string, 0)
for _, z := range s.TimeZones { for _, z := range s.TimeZones {
rawTimeZones = append(rawTimeZones, z.String()) rawTimeZones = append(rawTimeZones, z.String())
} }

View File

@@ -61,7 +61,7 @@ type MediaName struct {
} }
func (m *MediaName) String() *string { func (m *MediaName) String() *string {
var formats []string formats := make([]string, 0)
for _, format := range m.Formats { for _, format := range m.Formats {
formats = append(formats, strconv.Itoa(format)) formats = append(formats, strconv.Itoa(format))
} }

View File

@@ -40,7 +40,7 @@ type RepeatTime struct {
} }
func (r *RepeatTime) String() *string { func (r *RepeatTime) String() *string {
var fields []string fields := make([]string, 0)
fields = append(fields, strconv.FormatInt(r.Interval, 10)) fields = append(fields, strconv.FormatInt(r.Interval, 10))
fields = append(fields, strconv.FormatInt(r.Duration, 10)) fields = append(fields, strconv.FormatInt(r.Duration, 10))
for _, value := range r.Offsets { for _, value := range r.Offsets {

View File

@@ -117,7 +117,7 @@ func (c *Context) generateSessionSalt() ([]byte, error) {
labelAndIndexOverKdr := []byte{labelSalt, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} labelAndIndexOverKdr := []byte{labelSalt, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
for i, j := len(labelAndIndexOverKdr)-1, len(sessionSalt)-1; i >= 0; i, j = i-1, j-1 { for i, j := len(labelAndIndexOverKdr)-1, len(sessionSalt)-1; i >= 0; i, j = i-1, j-1 {
sessionSalt[j] = byte(sessionSalt[j]) ^ byte(labelAndIndexOverKdr[i]) sessionSalt[j] = sessionSalt[j] ^ labelAndIndexOverKdr[i]
} }
// That value is padded and encrypted as above. // That value is padded and encrypted as above.

View File

@@ -40,7 +40,8 @@ type RTCRtpTransceiverDirection int
const ( const (
// RTCRtpTransceiverDirectionSendrecv indicates the RTCRtpSender will offer to send RTP and RTCRtpReceiver the will offer to receive RTP // RTCRtpTransceiverDirectionSendrecv indicates the RTCRtpSender will offer to send RTP and RTCRtpReceiver the will
// offer to receive RTP
RTCRtpTransceiverDirectionSendrecv RTCRtpTransceiverDirection = iota + 1 RTCRtpTransceiverDirectionSendrecv RTCRtpTransceiverDirection = iota + 1
// RTCRtpTransceiverDirectionSendonly indicates the RTCRtpSender will offer to send RTP // RTCRtpTransceiverDirectionSendonly indicates the RTCRtpSender will offer to send RTP
@@ -49,7 +50,8 @@ const (
// RTCRtpTransceiverDirectionRecvonly indicates the RTCRtpReceiver the will offer to receive RTP // RTCRtpTransceiverDirectionRecvonly indicates the RTCRtpReceiver the will offer to receive RTP
RTCRtpTransceiverDirectionRecvonly RTCRtpTransceiverDirectionRecvonly
// RTCRtpTransceiverDirectionInactive indicates the RTCRtpSender won't offer to send RTP and RTCRtpReceiver the won't offer to receive RTP // RTCRtpTransceiverDirectionInactive indicates the RTCRtpSender won't offer to send RTP and RTCRtpReceiver the
// won't offer to receive RTP
RTCRtpTransceiverDirectionInactive RTCRtpTransceiverDirectionInactive
) )
@@ -64,7 +66,7 @@ func (t RTCRtpTransceiverDirection) String() string {
case RTCRtpTransceiverDirectionInactive: case RTCRtpTransceiverDirectionInactive:
return "inactive" return "inactive"
default: default:
return "Unknown" return ErrUnknownType.Error()
} }
} }
@@ -122,9 +124,9 @@ type RTCSample struct {
// RTCTrack represents a track that is communicated // RTCTrack represents a track that is communicated
type RTCTrack struct { type RTCTrack struct {
ID string
PayloadType uint8 PayloadType uint8
Kind RTCRtpCodecType Kind RTCRtpCodecType
ID string
Label string Label string
Ssrc uint32 Ssrc uint32
Codec *RTCRtpCodec Codec *RTCRtpCodec

View File

@@ -159,7 +159,7 @@ func (t RTCRtpCodecType) String() string {
case RTCRtpCodecTypeVideo: case RTCRtpCodecTypeVideo:
return "video" return "video"
default: default:
return "Unknown" return ErrUnknownType.Error()
} }
} }

View File

@@ -47,7 +47,7 @@ func (t ServerType) String() string {
case ServerTypeTURN: case ServerTypeTURN:
return "turn" return "turn"
default: default:
return "Unknown" return ErrUnknownType.Error()
} }
} }
@@ -69,7 +69,7 @@ func (t TransportType) String() string {
case TransportTCP: case TransportTCP:
return "tcp" return "tcp"
default: default:
return "Unknown" return ErrUnknownType.Error()
} }
} }

View File

@@ -1,5 +1,10 @@
package ice package ice
import "github.com/pkg/errors"
// ErrUnknownType indicates a Unknown info
var ErrUnknownType = errors.New("Unknown")
// ConnectionState is an enum showing the state of a ICE Connection // ConnectionState is an enum showing the state of a ICE Connection
type ConnectionState int type ConnectionState int
@@ -71,6 +76,6 @@ func (t GatheringState) String() string {
case GatheringStateComplete: case GatheringStateComplete:
return "complete" return "complete"
default: default:
return "Unknown" return ErrUnknownType.Error()
} }
} }

View File

@@ -24,7 +24,7 @@ func (t RTCICECredentialType) String() string {
case RTCICECredentialTypeOauth: case RTCICECredentialTypeOauth:
return "oauth" return "oauth"
default: default:
return "Unknown" return ErrUnknownType.Error()
} }
} }
@@ -59,11 +59,13 @@ type RTCOAuthCredential struct {
AccessToken string AccessToken string
} }
// RTCICETransportPolicy defines the ICE candidate policy [JSEP] (section 3.5.3.) used to surface the permitted candidates // RTCICETransportPolicy defines the ICE candidate policy [JSEP] (section 3.5.3.) used to
// surface the permitted candidates
type RTCICETransportPolicy int type RTCICETransportPolicy int
const ( const (
// RTCICETransportPolicyRelay indicates only media relay candidates such as candidates passing through a TURN server are used // RTCICETransportPolicyRelay indicates only media relay candidates such as candidates passing
// through a TURN server are used
RTCICETransportPolicyRelay RTCICETransportPolicy = iota + 1 RTCICETransportPolicyRelay RTCICETransportPolicy = iota + 1
// RTCICETransportPolicyAll indicates any type of candidate is used // RTCICETransportPolicyAll indicates any type of candidate is used
@@ -77,11 +79,12 @@ func (t RTCICETransportPolicy) String() string {
case RTCICETransportPolicyAll: case RTCICETransportPolicyAll:
return "all" return "all"
default: default:
return "Unknown" return ErrUnknownType.Error()
} }
} }
// RTCBundlePolicy affects which media tracks are negotiated if the remote endpoint is not bundle-aware, and what ICE candidates are gathered. // RTCBundlePolicy affects which media tracks are negotiated if the remote endpoint is not bundle-aware,
// and what ICE candidates are gathered.
type RTCBundlePolicy int type RTCBundlePolicy int
const ( const (
@@ -105,7 +108,7 @@ func (t RTCBundlePolicy) String() string {
case RTCRtcpMuxPolicyMaxBundle: case RTCRtcpMuxPolicyMaxBundle:
return "max-bundle" return "max-bundle"
default: default:
return "Unknown" return ErrUnknownType.Error()
} }
} }
@@ -127,15 +130,16 @@ func (t RTCRtcpMuxPolicy) String() string {
case RTCRtcpMuxPolicyRequire: case RTCRtcpMuxPolicyRequire:
return "require" return "require"
default: default:
return "Unknown" return ErrUnknownType.Error()
} }
} }
// RTCConfiguration contains RTCPeerConfiguration options // RTCConfiguration contains RTCPeerConfiguration options
type RTCConfiguration struct { type RTCConfiguration struct {
// ICEServers holds multiple RTCICEServer instances, each describing one server which may be used by the ICE agent; // ICEServers holds multiple RTCICEServer instances, each describing one server which may be used by the ICE agent;
// these are typically STUN and/or TURN servers. If this isn't specified, the ICE agent may choose to use its own ICE servers; // these are typically STUN and/or TURN servers. If this isn't specified, the ICE agent may choose to use its own
// otherwise, the connection attempt will be made with no STUN or TURN server available, which limits the connection to local peers. // ICE servers; otherwise, the connection attempt will be made with no STUN or TURN server available, which limits
// the connection to local peers.
ICEServers []RTCICEServer ICEServers []RTCICEServer
ICETransportPolicy RTCICETransportPolicy ICETransportPolicy RTCICETransportPolicy
BundlePolicy RTCBundlePolicy BundlePolicy RTCBundlePolicy

View File

@@ -55,7 +55,7 @@ func (t RTCPeerConnectionState) String() string {
case RTCPeerConnectionStateClosed: case RTCPeerConnectionStateClosed:
return "closed" return "closed"
default: default:
return "Unknown" return ErrUnknownType.Error()
} }
} }

View File

@@ -34,10 +34,12 @@ const (
// RTCSignalingStateHaveRemoteOffer indicates A remote description, of type "offer", has been successfully applied. // RTCSignalingStateHaveRemoteOffer indicates A remote description, of type "offer", has been successfully applied.
RTCSignalingStateHaveRemoteOffer RTCSignalingStateHaveRemoteOffer
// RTCSignalingStateHaveLocalPranswer indicates A remote description of type "offer" has been successfully applied and a local description of type "pranswer" has been successfully applied. // RTCSignalingStateHaveLocalPranswer indicates A remote description of type "offer" has been successfully applied
// and a local description of type "pranswer" has been successfully applied.
RTCSignalingStateHaveLocalPranswer RTCSignalingStateHaveLocalPranswer
// RTCSignalingStateHaveRemotePranswer indicates A local description of type "offer" has been successfully applied and a remote description of type "pranswer" has been successfully applied. // RTCSignalingStateHaveRemotePranswer indicates A local description of type "offer" has been successfully applied
// and a remote description of type "pranswer" has been successfully applied.
RTCSignalingStateHaveRemotePranswer RTCSignalingStateHaveRemotePranswer
// RTCSignalingStateClosed indicates The RTCPeerConnection has been closed. // RTCSignalingStateClosed indicates The RTCPeerConnection has been closed.
@@ -59,7 +61,7 @@ func (t RTCSignalingState) String() string {
case RTCSignalingStateClosed: case RTCSignalingStateClosed:
return "closed" return "closed"
default: default:
return "Unknown" return ErrUnknownType.Error()
} }
} }
@@ -73,10 +75,12 @@ const (
// RTCSdpTypePranswer indicates that a description MUST be treated as an SDP answer, but not a final answer. // RTCSdpTypePranswer indicates that a description MUST be treated as an SDP answer, but not a final answer.
RTCSdpTypePranswer RTCSdpTypePranswer
// RTCSdpTypeAnswer indicates that a description MUST be treated as an SDP final answer, and the offer-answer exchange MUST be considered complete. // RTCSdpTypeAnswer indicates that a description MUST be treated as an SDP final answer, and the offer-answer
// exchange MUST be considered complete.
RTCSdpTypeAnswer RTCSdpTypeAnswer
// RTCSdpTypeRollback indicates that a description MUST be treated as canceling the current SDP negotiation and moving the SDP offer and answer back to what it was in the previous stable state. // RTCSdpTypeRollback indicates that a description MUST be treated as canceling the current SDP negotiation
// and moving the SDP offer and answer back to what it was in the previous stable state.
RTCSdpTypeRollback RTCSdpTypeRollback
) )
@@ -91,7 +95,7 @@ func (t RTCSdpType) String() string {
case RTCSdpTypeRollback: case RTCSdpTypeRollback:
return "rollback" return "rollback"
default: default:
return "Unknown" return ErrUnknownType.Error()
} }
} }