-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient.go
160 lines (137 loc) · 3.24 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Package drpc implements the Discord RPC protocol.
package drpc
import (
"bytes"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"net"
"os"
"syscall"
)
var (
ErrConnFailed = errors.New("drpc: connection failed")
)
// Client is a Discord RPC client.
type Client struct {
id string
conn net.Conn
}
// New creates a new Discord RPC client with the given application ID.
//
// The application ID given must not be empty.
func New(id string) (*Client, error) {
if id == "" {
return nil, errors.New("drpc: application id is empty")
}
return &Client{id: id}, nil
}
// Connect connects the client to the Discord RPC server and sends a handshake.
// It returns nil if the client is already connected.
func (c *Client) Connect() (err error) {
if c.conn != nil {
return nil
}
if c.conn, err = connect(); err != nil {
return err
}
if err = c.Write(OpHandshake, Handshake{
V: "1",
ClientID: c.id,
}); err != nil {
return err
}
_, err = c.Read()
return err
}
// Close closes the client connection.
func (c *Client) Close() error {
if c.conn != nil {
if err := c.conn.Close(); err != nil {
return err
}
c.conn = nil
}
return nil
}
// Write writes a message to the connection.
// It automatically reconnects if the connection is closed.
func (c *Client) Write(opcode Opcode, payload interface{}) error {
if err := c.Connect(); err != nil {
return err
}
v, err := json.Marshal(payload)
if err != nil {
return err
}
buf := bytes.NewBuffer(nil)
if err = binary.Write(buf, binary.LittleEndian, opcode); err != nil {
return err
}
if err = binary.Write(buf, binary.LittleEndian, uint32(len(v))); err != nil {
return err
}
if _, err = buf.Write(v); err != nil {
return err
}
_, err = c.conn.Write(buf.Bytes())
// Attempt re-connection only if the socket
// has been closed or broken
if errors.Is(err, syscall.EPIPE) {
if err := c.Close(); err != nil {
return fmt.Errorf("drpc: reconnect close: %w", err)
}
if err := c.Connect(); err != nil {
return fmt.Errorf("drpc: reconnect: %w", err)
}
return c.Write(opcode, payload)
}
return err
}
// Read reads a message from the connection.
// It automatically reconnects if the connection is closed.
func (c *Client) Read() (*Message, error) {
if err := c.Connect(); err != nil {
return nil, err
}
buf := make([]byte, 512)
n, err := c.conn.Read(buf)
if err != nil {
return nil, err
}
if n <= 8 {
return nil, errors.New("drpc: invalid message length")
}
msg := &Message{binary.LittleEndian.Uint32(buf[:4]), buf[8:n]}
switch msg.Opcode {
// case OpPing:
// i think it is sent by the client, not the server
case OpClose:
if err = c.Close(); err != nil {
return nil, err
}
}
return msg, nil
}
// SetActivity sets the player's activity.
func (c *Client) SetActivity(activity Activity) (err error) {
if err = c.Write(OpFrame, FrameSetActivity{
FrameHeader: NewFrameHeader("SET_ACTIVITY"),
Args: FrameSetActivityArgs{
PID: os.Getpid(),
Activity: activity,
},
}); err != nil {
return err
}
_, err = c.Read()
return err
}
// func (c *Client) Subscribe(event string) (err error) {
// if err = c.Write(OpFrame, NewFrameHeaderWithEvent("SUBSCRIBE", event)); err != nil {
// return err
// }
// _, err = c.Read()
// return err
// }