-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
66 lines (54 loc) · 1.79 KB
/
client.go
File metadata and controls
66 lines (54 loc) · 1.79 KB
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
// SPDX-License-Identifier: AGPL-3.0-or-later
package dataexchange
import (
"time"
"github.com/TeoSlayer/pilotprotocol/pkg/driver"
"github.com/TeoSlayer/pilotprotocol/pkg/protocol"
)
// Client connects to a remote data exchange service on port 1001.
type Client struct {
conn *driver.Conn
}
// Dial connects to a remote agent's data exchange port.
func Dial(d *driver.Driver, addr protocol.Addr) (*Client, error) {
conn, err := d.DialAddr(addr, protocol.PortDataExchange)
if err != nil {
return nil, err
}
return &Client{conn: conn}, nil
}
// SendText sends a text frame.
func (c *Client) SendText(text string) error {
return WriteFrame(c.conn, &Frame{Type: TypeText, Payload: []byte(text)})
}
// SendJSON sends a JSON frame.
func (c *Client) SendJSON(data []byte) error {
return WriteFrame(c.conn, &Frame{Type: TypeJSON, Payload: data})
}
// SendBinary sends a binary frame.
func (c *Client) SendBinary(data []byte) error {
return WriteFrame(c.conn, &Frame{Type: TypeBinary, Payload: data})
}
// SendFile sends a file frame with a filename and data.
func (c *Client) SendFile(name string, data []byte) error {
return WriteFrame(c.conn, &Frame{Type: TypeFile, Filename: name, Payload: data})
}
// SendTrace wraps data in a TypeTrace frame with the current nanosecond clock.
// Returns sentAtNs so the caller can correlate it against the timing ACK.
func (c *Client) SendTrace(innerType uint32, data []byte) (sentAtNs int64, err error) {
sentAtNs = time.Now().UnixNano()
err = WriteTraceFrame(c.conn, &TraceFrame{
SentAtNs: sentAtNs,
InnerType: innerType,
Payload: data,
})
return
}
// Recv reads the next frame from the connection.
func (c *Client) Recv() (*Frame, error) {
return ReadFrame(c.conn)
}
// Close closes the connection.
func (c *Client) Close() error {
return c.conn.Close()
}