diff options
author | Valery Piashchynski <[email protected]> | 2020-12-27 00:59:10 +0300 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2020-12-27 00:59:10 +0300 |
commit | 8df4896deabdab9a50a5ad3c6da6e1c7f05922af (patch) | |
tree | c2fa71733902b999c02e5abd608bff4bc7449c5c /utils/network_windows.go | |
parent | 1aaf6e6ffb015cd5a21d9d938ad84c18723973c5 (diff) |
Util -> Utils
Diffstat (limited to 'utils/network_windows.go')
-rwxr-xr-x | utils/network_windows.go | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/utils/network_windows.go b/utils/network_windows.go new file mode 100755 index 00000000..ebe343a3 --- /dev/null +++ b/utils/network_windows.go @@ -0,0 +1,43 @@ +// +build windows + +package utils + +import ( + "errors" + "fmt" + "net" + "os" + "strings" + "syscall" +) + +// CreateListener crates socket listener based on DSN definition. +func CreateListener(address string) (net.Listener, error) { + dsn := strings.Split(address, "://") + if len(dsn) != 2 { + return nil, errors.New("invalid DSN (tcp://:6001, unix://file.sock)") + } + + if dsn[0] != "unix" && dsn[0] != "tcp" { + return nil, errors.New("invalid Protocol (tcp://:6001, unix://file.sock)") + } + + if dsn[0] == "unix" && fileExists(dsn[1]) { + err := syscall.Unlink(dsn[1]) + if err != nil { + return nil, fmt.Errorf("error during the unlink syscall: error %v", err) + } + } + + return net.Listen(dsn[0], dsn[1]) +} + +// fileExists checks if a file exists and is not a directory before we +// try using it to prevent further errors. +func fileExists(filename string) bool { + info, err := os.Stat(filename) + if os.IsNotExist(err) { + return false + } + return !info.IsDir() +} |