-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
100 lines (89 loc) · 1.96 KB
/
client.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
96
97
98
99
100
package unix
import (
"bufio"
"net"
"sync"
"player/event"
"player/logger"
"github.com/rs/xid"
)
// A unix socket client
type Client struct {
// Unexported Fields
id string
conn net.Conn
server *Server
wg *sync.WaitGroup
closed bool
closeC chan bool
}
// Returns the clients ID
func (c *Client) ID() string {
return c.id
}
// Connect to a Unix socket
func (c *Client) Connect(address string) error {
conn, err := net.Dial("unix", address)
if err != nil {
return err
}
c.conn = conn
return nil
}
// Read data from the socket
func (c *Client) Read() ([]byte, error) {
buf := bufio.NewReader(c.conn)
b, err := buf.ReadBytes('\n') // EOF on connction close
if err != nil {
// On read error we close the client, it's likely that
// the client has gone away so we can't read or write from
// it anymore
if err := c.Close(); err != nil {
logger.WithError(err).Error("error closing socket client on read error")
}
}
return b, err
}
// Writes data to the client unix socket connection
func (c *Client) Write(b []byte) (int, error) {
last := b[len(b)-1]
if last != '\n' {
b = append(b, '\n')
}
return c.conn.Write(b)
}
// Close the Client, closing the connection
func (c *Client) Close() error {
event.Del(c) // Delete the client from the event hub
if !c.closed {
logger.Debug("close socket client")
defer logger.Info("closed socket client")
close(c.closeC)
if c.conn != nil {
if err := c.conn.Close(); err != nil {
logger.WithError(err).Error("failed to close socket client conn")
}
}
if c.server != nil {
c.server.clients.Del(c.id)
}
c.closed = true
c.wg.Wait()
}
return nil
}
// Constructs a new Client
func NewClient() *Client {
return &Client{
id: xid.New().String(),
wg: &sync.WaitGroup{},
closeC: make(chan bool),
}
}
// Constructs a new server client
func NewServerClient(server *Server, conn net.Conn) *Client {
client := NewClient()
client.conn = conn
client.server = server
return client
}