introduced Ping() function

This commit is contained in:
2025-06-28 13:17:11 +02:00
parent 0e44626c13
commit 4fef37ceca

View File

@ -2,7 +2,6 @@ package mpvipc
import ( import (
"context" "context"
"net"
"time" "time"
) )
@ -30,15 +29,18 @@ func Quit(socket string) error {
return err return err
} }
func Ping(socket string) bool {
_, err := GetPID(socket)
return err == nil
}
func IsReady(ctx context.Context, socket string) <-chan bool { func IsReady(ctx context.Context, socket string) <-chan bool {
out := make(chan bool, 1) out := make(chan bool, 1)
go func() { go func() {
defer close(out) defer close(out)
conn, err := net.Dial("unix", socket) if Ping(socket) {
if err == nil {
defer conn.Close()
out <- true out <- true
return return
} }
@ -49,9 +51,7 @@ func IsReady(ctx context.Context, socket string) <-chan bool {
for { for {
select { select {
case <-ticker.C: case <-ticker.C:
conn, err := net.Dial("unix", socket) if Ping(socket) {
if err == nil {
defer conn.Close()
out <- true out <- true
return return
} }