local_socket/socket_unix.go
2017-10-24 11:57:54 +09:00

30 lines
687 B
Go

package local_socket
import (
"net"
"os"
"path/filepath"
)
// LocalSocket is a socket to communicate with other processes in the same box.
// LocalSocket satisfies io.Reader, io.Writer, net.Conn interfaces.
type LocalSocket struct {
conn *net.UnixConn
}
// NewLocalSocket creates LocalSocket instance
func NewLocalSocket(pathName string) (*LocalSocket, error) {
socketType := "unix" // SOCK_STREAM
conn, err := net.DialUnix(socketType, nil, &net.UnixAddr{filepath.Join(os.TempDir(), pathName), socketType})
if err != nil {
return nil, err
}
return newLocalSocket(conn), nil
}
func newLocalSocket(conn *net.UnixConn) *LocalSocket {
return &LocalSocket{
conn: conn,
}
}