log messages for sockets

This commit is contained in:
2025-10-26 18:02:43 -03:00
parent 51c51b96bf
commit 9883fd92dc
2 changed files with 7 additions and 2 deletions

View File

@@ -23,6 +23,7 @@ func CheckDownloaded(filename string) bool {
}
func main() {
log.Println("Program started")
pid := os.Getpid()
if err := os.WriteFile(filepath.Join(consts.DefaultLinux_d, "udp.pid"), []byte(fmt.Sprint(pid)), 0664); err != nil {
fmt.Println("error saving subprocess pid", err)
@@ -37,6 +38,7 @@ func main() {
if err != nil {
log.Fatal(err)
}
log.Println("Started connection listener")
defer conn.Close()
buf := make([]byte, 1500)
@@ -46,12 +48,15 @@ func main() {
log.Println("error creating udp socket", err)
}
msg := string(buf[:n])
log.Printf("Received message : %s\n", msg)
if !strings.HasPrefix(msg, "Q:") {
log.Println("error: invalid message, this message don't follow the protocol")
continue
}
filename := strings.TrimPrefix(msg, "Q:")
if CheckDownloaded(filename) {
reply := fmt.Sprintf("H:%s:%d", filename, cfg.Config.HttpPort)
log.Printf("Package founded in cache dir, answering with: '%s'\n", reply)
conn.WriteToUDP([]byte(reply), remote)
}
}