Simplify code and improve error handling

This commit is contained in:
David Buezas
2023-06-04 22:37:29 +02:00
parent 75818d6967
commit 64804cbc87
+18 -30
View File
@@ -7,7 +7,6 @@ import (
"net" "net"
"net/http" "net/http"
"net/url" "net/url"
"sync"
"time" "time"
"github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/api"
@@ -85,12 +84,8 @@ func discover() ([]api.Stream, error) {
responseChan := make(chan []byte) responseChan := make(chan []byte)
// Wait group to synchronize goroutines go receiveResponses(connection, responseChan)
var wg sync.WaitGroup go sendBroadcasts(connection)
wg.Add(1)
go receiveResponses(connection, responseChan, &wg)
wg.Add(1)
go sendBroadcasts(connection, &wg)
var items []api.Stream var items []api.Stream
// Process received responses // Process received responses
@@ -114,7 +109,11 @@ func discover() ([]api.Stream, error) {
} }
if msg.NetCommon.HostIP != "" && msg.NetCommon.HostName != "" { if msg.NetCommon.HostIP != "" && msg.NetCommon.HostName != "" {
hostIP := hexToDecimalBytes(msg.NetCommon.HostIP) hostIP, err := hexToDecimalBytes(msg.NetCommon.HostIP)
if err != nil {
log.Err(err).Msgf("[dvrip] Error parsing IP: %s", err)
continue
}
u := &url.URL{ u := &url.URL{
Scheme: "dvrip", Scheme: "dvrip",
@@ -127,27 +126,26 @@ func discover() ([]api.Stream, error) {
queryParams.Add("subtype", "0") queryParams.Add("subtype", "0")
u.RawQuery = queryParams.Encode() u.RawQuery = queryParams.Encode()
// Check if the URL already exists in the array uri := u.String()
exists := false exists := false
for _, otherUrl := range items { for _, otherUrl := range items {
if otherUrl.URL == u.String() { if otherUrl.URL == uri {
exists = true exists = true
break break
} }
} }
if !exists { if !exists {
items = append(items, api.Stream{Name: msg.NetCommon.HostName, URL: u.String()}) items = append(items, api.Stream{Name: msg.NetCommon.HostName, URL: uri})
} }
} }
} }
wg.Wait()
return items, nil return items, nil
} }
func receiveResponses(conn *net.UDPConn, responseChan chan<- []byte, wg *sync.WaitGroup) { func receiveResponses(conn *net.UDPConn, responseChan chan<- []byte) {
defer wg.Done()
buffer := make([]byte, 1024) buffer := make([]byte, 1024)
for { for {
@@ -171,9 +169,7 @@ func receiveResponses(conn *net.UDPConn, responseChan chan<- []byte, wg *sync.Wa
} }
} }
func sendBroadcasts(conn *net.UDPConn, wg *sync.WaitGroup) { func sendBroadcasts(conn *net.UDPConn) {
defer wg.Done()
// broadcasting the same multiple times because the devies some times don't answer // broadcasting the same multiple times because the devies some times don't answer
hexStreams := []string{ hexStreams := []string{
"ff00000000000000000000000000fa0500000000", "ff00000000000000000000000000fa0500000000",
@@ -200,23 +196,15 @@ func sendBroadcasts(conn *net.UDPConn, wg *sync.WaitGroup) {
} }
} }
func reverseArray(arr []byte) { func hexToDecimalBytes(hexIP string) (string, error) {
for i, j := 0, len(arr)-1; i < j; i, j = i+1, j-1 { // Remove the '0x' prefix
arr[i], arr[j] = arr[j], arr[i]
}
}
func hexToDecimalBytes(hexIP string) string {
// Remove the '0x' prefix if present
hexIP = hexIP[2:] hexIP = hexIP[2:]
// Decode the hexadecimal string to a byte slice
decimalBytes, err := hex.DecodeString(hexIP) decimalBytes, err := hex.DecodeString(hexIP)
if err != nil { if err != nil {
return "0.0.0.0" return "", err
} }
reverseArray(decimalBytes) return fmt.Sprintf("%d.%d.%d.%d", decimalBytes[3], decimalBytes[2], decimalBytes[1], decimalBytes[0]), nil
return fmt.Sprintf("%d.%d.%d.%d", decimalBytes[0], decimalBytes[1], decimalBytes[2], decimalBytes[3])
} }
func apiDvrip(w http.ResponseWriter, r *http.Request) { func apiDvrip(w http.ResponseWriter, r *http.Request) {