| 1 |
package wl |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"syscall" |
| 7 |
) |
| 8 |
|
| 9 |
type Event struct { |
| 10 |
pid ProxyId |
| 11 |
Opcode uint32 |
| 12 |
data []byte |
| 13 |
scms []syscall.SocketControlMessage |
| 14 |
off int |
| 15 |
} |
| 16 |
|
| 17 |
func (c *Context) readEvent() (*Event, error) { |
| 18 |
buf := bytePool.Take(8) |
| 19 |
control := bytePool.Take(24) |
| 20 |
|
| 21 |
n, oobn, _, _, err := c.conn.ReadMsgUnix(buf[:], control) |
| 22 |
if err != nil { |
| 23 |
return nil, err |
| 24 |
} |
| 25 |
if n != 8 { |
| 26 |
return nil, fmt.Errorf("Unable to read message header.") |
| 27 |
} |
| 28 |
ev := new(Event) |
| 29 |
if oobn > 0 { |
| 30 |
if oobn > len(control) { |
| 31 |
return nil, fmt.Errorf("Unsufficient control msg buffer") |
| 32 |
} |
| 33 |
scms, err := syscall.ParseSocketControlMessage(control) |
| 34 |
if err != nil { |
| 35 |
return nil, fmt.Errorf("Control message parse error: %s", err) |
| 36 |
} |
| 37 |
ev.scms = scms |
| 38 |
} |
| 39 |
|
| 40 |
ev.pid = ProxyId(order.Uint32(buf[0:4])) |
| 41 |
ev.Opcode = uint32(order.Uint16(buf[4:6])) |
| 42 |
size := uint32(order.Uint16(buf[6:8])) |
| 43 |
|
| 44 |
|
| 45 |
data := bytePool.Take(int(size) - 8) |
| 46 |
n, err = c.conn.Read(data) |
| 47 |
if err != nil { |
| 48 |
return nil, err |
| 49 |
} |
| 50 |
if n != int(size)-8 { |
| 51 |
return nil, fmt.Errorf("Invalid message size.") |
| 52 |
} |
| 53 |
ev.data = data |
| 54 |
|
| 55 |
bytePool.Give(buf) |
| 56 |
bytePool.Give(control) |
| 57 |
|
| 58 |
return ev, nil |
| 59 |
} |
| 60 |
|
| 61 |
func (ev *Event) FD() uintptr { |
| 62 |
if ev.scms == nil { |
| 63 |
return 0 |
| 64 |
} |
| 65 |
fds, err := syscall.ParseUnixRights(&ev.scms[0]) |
| 66 |
if err != nil { |
| 67 |
panic("Unable to parse unix rights") |
| 68 |
} |
| 69 |
|
| 70 |
ev.scms = append(ev.scms, ev.scms[1:]...) |
| 71 |
return uintptr(fds[0]) |
| 72 |
} |
| 73 |
|
| 74 |
func (ev *Event) Uint32() uint32 { |
| 75 |
buf := ev.next(4) |
| 76 |
if len(buf) != 4 { |
| 77 |
panic("Unable to read unsigned int") |
| 78 |
} |
| 79 |
return order.Uint32(buf) |
| 80 |
} |
| 81 |
|
| 82 |
func (ev *Event) Proxy(c *Context) Proxy { |
| 83 |
return c.lookupProxy(ProxyId(ev.Uint32())) |
| 84 |
} |
| 85 |
|
| 86 |
func (ev *Event) String() string { |
| 87 |
l := int(ev.Uint32()) |
| 88 |
buf := ev.next(l) |
| 89 |
if len(buf) != l { |
| 90 |
panic("Unable to read string") |
| 91 |
} |
| 92 |
ret := string(bytes.TrimRight(buf, "\x00")) |
| 93 |
|
| 94 |
if (l & 0x3) != 0 { |
| 95 |
ev.next(4 - (l & 0x3)) |
| 96 |
} |
| 97 |
return ret |
| 98 |
} |
| 99 |
|
| 100 |
func (ev *Event) Int32() int32 { |
| 101 |
return int32(ev.Uint32()) |
| 102 |
} |
| 103 |
|
| 104 |
func (ev *Event) Float32() float32 { |
| 105 |
return float32(fixedToFloat64(ev.Int32())) |
| 106 |
} |
| 107 |
|
| 108 |
func (ev *Event) Array() []int32 { |
| 109 |
l := int(ev.Uint32()) |
| 110 |
arr := make([]int32, l/4) |
| 111 |
for i := range arr { |
| 112 |
arr[i] = ev.Int32() |
| 113 |
} |
| 114 |
return arr |
| 115 |
} |
| 116 |
|
| 117 |
func (ev *Event) next(n int) []byte { |
| 118 |
ret := ev.data[ev.off : ev.off+n] |
| 119 |
ev.off += n |
| 120 |
return ret |
| 121 |
} |