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
disable:
- maligned
- prealloc
- unconvert
- lll
- dupl
- goconst
- gocyclo

View File

@@ -90,3 +90,6 @@ type SyntaxError struct {
func (e *SyntaxError) Error() string {
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)
rawRemote := C.CString(remote)
packetRaw := C.CBytes(packet)
packetRaw := C.CBytes(packet) // unsafe.Pointer
defer func() {
C.free(unsafe.Pointer(rawLocal))
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 {
defer func() {
C.free(unsafe.Pointer(ret.buf))
C.free(ret.buf)
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
}
@@ -144,11 +144,11 @@ func (s *State) Send(packet []byte, local, remote string) (bool, error) {
rawLocal := C.CString(local)
rawRemote := C.CString(remote)
packetRaw := C.CBytes(packet)
packetRaw := C.CBytes(packet) // unsafe.Pointer
defer func() {
C.free(unsafe.Pointer(rawLocal))
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

View File

@@ -48,13 +48,13 @@ type Manager struct {
}
// 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{
iceNotifier: iceNotifier,
iceNotifier: ntf,
bufferTransports: make(map[uint32]chan<- *rtp.Packet),
srtpContexts: make(map[string]*srtp.Context),
bufferTransportGenerator: bufferTransportGenerator,
dataChannelEventHandler: dataChannelEventHandler,
bufferTransportGenerator: btg,
dataChannelEventHandler: dcet,
}
m.dtlsState, err = dtls.NewState()
if err != nil {
@@ -232,11 +232,13 @@ func (m *Manager) dataChannelInboundHandler(data []byte, streamIdentifier uint16
case sctp.PayloadTypeWebRTCString:
fallthrough
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:
fallthrough
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:
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]
if !ok {
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 {
fmt.Println("Failed to build SRTP context")
return

View File

@@ -21,7 +21,7 @@ func (p *port) sendRTP(packet *rtp.Packet, dst net.Addr) {
srtpContext, ok := p.m.srtpContexts[contextMapKey]
if !ok {
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 {
fmt.Println("Failed to build SRTP context")
return

View File

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

View File

@@ -98,12 +98,12 @@ func (c *chunkHeader) unmarshal(raw []byte) error {
}
c.typ = chunkType(raw[0])
c.flags = byte(raw[1])
c.flags = raw[1]
length := binary.BigEndian.Uint16(raw[2:])
// Length includes Chunk header
valueLength := int(length - chunkHeaderSize)
lengthAfterValue := len(raw) - (chunkHeaderSize + int(valueLength))
lengthAfterValue := len(raw) - (chunkHeaderSize + valueLength)
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)

View File

@@ -89,8 +89,8 @@ func (i *chunkInitCommon) unmarshal(raw []byte) error {
}
i.params = append(i.params, p)
padding := getPadding(p.length())
offset += int(p.length() + padding)
remaining -= int(p.length() + padding)
offset += p.length() + padding
remaining -= p.length() + padding
} else {
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 {
rawTimeZones = append(rawTimeZones, z.String())
}

View File

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

View File

@@ -40,7 +40,7 @@ type RepeatTime struct {
}
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.Duration, 10))
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}
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.

View File

@@ -40,7 +40,8 @@ type RTCRtpTransceiverDirection int
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
// 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
// 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
)
@@ -64,7 +66,7 @@ func (t RTCRtpTransceiverDirection) String() string {
case RTCRtpTransceiverDirectionInactive:
return "inactive"
default:
return "Unknown"
return ErrUnknownType.Error()
}
}
@@ -122,9 +124,9 @@ type RTCSample struct {
// RTCTrack represents a track that is communicated
type RTCTrack struct {
ID string
PayloadType uint8
Kind RTCRtpCodecType
ID string
Label string
Ssrc uint32
Codec *RTCRtpCodec

View File

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

View File

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

View File

@@ -1,5 +1,10 @@
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
type ConnectionState int
@@ -71,6 +76,6 @@ func (t GatheringState) String() string {
case GatheringStateComplete:
return "complete"
default:
return "Unknown"
return ErrUnknownType.Error()
}
}

View File

@@ -24,7 +24,7 @@ func (t RTCICECredentialType) String() string {
case RTCICECredentialTypeOauth:
return "oauth"
default:
return "Unknown"
return ErrUnknownType.Error()
}
}
@@ -59,11 +59,13 @@ type RTCOAuthCredential struct {
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
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
// RTCICETransportPolicyAll indicates any type of candidate is used
@@ -77,11 +79,12 @@ func (t RTCICETransportPolicy) String() string {
case RTCICETransportPolicyAll:
return "all"
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
const (
@@ -105,7 +108,7 @@ func (t RTCBundlePolicy) String() string {
case RTCRtcpMuxPolicyMaxBundle:
return "max-bundle"
default:
return "Unknown"
return ErrUnknownType.Error()
}
}
@@ -127,15 +130,16 @@ func (t RTCRtcpMuxPolicy) String() string {
case RTCRtcpMuxPolicyRequire:
return "require"
default:
return "Unknown"
return ErrUnknownType.Error()
}
}
// RTCConfiguration contains RTCPeerConfiguration options
type RTCConfiguration struct {
// 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;
// otherwise, the connection attempt will be made with no STUN or TURN server available, which limits the connection to local peers.
// these are typically STUN and/or TURN servers. If this isn't specified, the ICE agent may choose to use its own
// 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
ICETransportPolicy RTCICETransportPolicy
BundlePolicy RTCBundlePolicy

View File

@@ -55,7 +55,7 @@ func (t RTCPeerConnectionState) String() string {
case RTCPeerConnectionStateClosed:
return "closed"
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
// 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
// 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
// RTCSignalingStateClosed indicates The RTCPeerConnection has been closed.
@@ -59,7 +61,7 @@ func (t RTCSignalingState) String() string {
case RTCSignalingStateClosed:
return "closed"
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
// 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
// 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
)
@@ -91,7 +95,7 @@ func (t RTCSdpType) String() string {
case RTCSdpTypeRollback:
return "rollback"
default:
return "Unknown"
return ErrUnknownType.Error()
}
}