Files
go-sam-go/common/sam3.go
2025-05-27 16:40:15 -04:00

40 lines
926 B
Go

package common
import (
"net"
"strings"
"github.com/samber/oops"
)
func connectToSAM(address string) (net.Conn, error) {
conn, err := net.Dial("tcp", address)
if err != nil {
return nil, oops.Errorf("failed to connect to SAM bridge at %s: %w", address, err)
}
return conn, nil
}
func sendHelloAndValidate(conn net.Conn, s *SAM) error {
if _, err := conn.Write(s.SAMEmit.HelloBytes()); err != nil {
return oops.Errorf("failed to send hello message: %w", err)
}
buf := make([]byte, 256)
n, err := conn.Read(buf)
if err != nil {
return oops.Errorf("failed to read SAM response: %w", err)
}
response := string(buf[:n])
switch {
case strings.Contains(response, HELLO_REPLY_OK):
log.Debug("SAM hello successful")
return nil
case response == HELLO_REPLY_NOVERSION:
return oops.Errorf("SAM bridge does not support SAMv3")
default:
return oops.Errorf("unexpected SAM response: %s", response)
}
}