Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Better socket disconnect handling #13

Merged
merged 1 commit into from
May 8, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions box/box.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
var (
SocketActive bool
Socket net.Conn
SocketPath string
SocketPattern string
targetMessage int32
)
Expand Down Expand Up @@ -62,11 +63,22 @@ func connectTCP(addr string) net.Conn {
func connectSocket(addr string) net.Conn {
conn, err := net.Dial("unix", addr)
if err != nil {
log.Fatal(err)
log.Error(err)
}
return conn
}

func reconnectSocket() {
log.Info("Reconnecting socket")
if Socket != nil {
Socket.Close()
}
Socket = connectSocket(SocketPath)
if Socket == nil {
log.Fatal("Failed to reconnect Socket, bailing out")
}
}

func writeUDP(conn *net.UDPConn, value string) {
log.Debugf("Writing to UDP connection '%s'", value)
_, err := fmt.Fprintf(conn, "%s\r\n", value)
Expand All @@ -84,10 +96,15 @@ func writeTCP(conn net.Conn, value string) {
}

func writeSock(conn net.Conn, value string) {
if conn == nil {
reconnectSocket()
}
log.Debugf("Writing to TCP connection: '%s'", value)
_, err := conn.Write([]byte(value))
if err != nil {
log.Error(err)
reconnectSocket()
writeSock(Socket, fmt.Sprintf(SocketPattern, value))
}
}

Expand Down Expand Up @@ -143,8 +160,9 @@ func Execute(sendUDP bool, targetAddr string, pathfinderAddr string, pathfinderA

SocketActive = socket
if socket {
Socket = connectSocket(socketPath)
SocketPath = socketPath
SocketPattern = socketPattern
Socket = connectSocket(SocketPath)
}
var target *net.UDPConn
if sendUDP {
Expand Down