-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrenderer.go
62 lines (57 loc) · 1.34 KB
/
renderer.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
package main
import (
"encoding/binary"
"errors"
"net"
"time"
)
type Response struct {
Version uint32
Success uint32
// There are actually more fields being sent, but do we care?
}
type Request struct {
Version uint32
CmdPriority uint32
X uint32
Y uint32
Z uint32
Map [44]byte
}
func getSocketConnection(renderd_socket string) (net.Conn, error) {
renderd_socket_type, renderd_tcp_addr := getSocketType(renderd_socket)
if renderd_socket_type == "tcp" {
return net.DialTCP("tcp", nil, renderd_tcp_addr)
}
return net.Dial("unix", renderd_socket)
}
func requestRender(x, y, z uint32, map_name, renderd_socket string, renderd_timeout time.Duration, priority int) error {
c, err := getSocketConnection(renderd_socket)
if err != nil {
return err
}
defer c.Close()
if renderd_timeout > 0 {
c.SetDeadline(time.Now().Add(renderd_timeout))
}
request := Request{
Version: 3,
CmdPriority: uint32(priority),
X: x,
Y: y,
Z: z,
}
copy(request.Map[:], []byte(map_name))
if err := binary.Write(c, binary.LittleEndian, request); err != nil {
return err
}
response := make([]byte, 56)
n, err := c.Read(response)
if err != nil {
return err
}
if n != len(response) {
return errors.New("could not read response. Unexpected number of bytes")
}
return nil
}