-
Notifications
You must be signed in to change notification settings - Fork 40
/
connect_other.go
executable file
·95 lines (64 loc) · 1.4 KB
/
connect_other.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//go:build linux || darwin
// +build linux darwin
package ipc
import (
"errors"
"net"
"os"
"strings"
"syscall"
"time"
)
// Server create a unix socket and start listening connections - for unix and linux
func (s *Server) run() error {
base := "/tmp/"
sock := ".sock"
if err := os.RemoveAll(base + s.name + sock); err != nil {
return err
}
var oldUmask int
if s.unMask {
oldUmask = syscall.Umask(0)
}
listen, err := net.Listen("unix", base+s.name+sock)
if s.unMask {
syscall.Umask(oldUmask)
}
if err != nil {
return err
}
s.listen = listen
go s.acceptLoop()
s.status = Listening
return nil
}
// Client connect to the unix socket created by the server - for unix and linux
func (c *Client) dial() error {
base := "/tmp/"
sock := ".sock"
startTime := time.Now()
for {
if c.timeout != 0 {
if time.Since(startTime).Seconds() > c.timeout {
c.status = Closed
return errors.New("timed out trying to connect")
}
}
conn, err := net.Dial("unix", base+c.Name+sock)
if err != nil {
if strings.Contains(err.Error(), "connect: no such file or directory") {
} else if strings.Contains(err.Error(), "connect: connection refused") {
} else {
c.received <- &Message{Err: err, MsgType: -1}
}
} else {
c.conn = conn
err = c.handshake()
if err != nil {
return err
}
return nil
}
time.Sleep(c.retryTimer * time.Second)
}
}