forked from mitchellh/go-vnc
-
Notifications
You must be signed in to change notification settings - Fork 16
/
common.go
90 lines (71 loc) · 1.83 KB
/
common.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
// Common things that aren't part of the RFB protocol.
package vnc
import (
"bytes"
"encoding/binary"
"fmt"
"time"
)
// VNCError implements error interface.
type VNCError struct {
desc string
}
// NewVNCError returns a custom VNCError error.
func NewVNCError(desc string) error {
return &VNCError{desc}
}
// Error returns an VNCError as a string.
func (e VNCError) Error() string {
return e.desc
}
func Errorf(format string, a ...interface{}) error {
return &VNCError{
desc: fmt.Sprintf(format, a...),
}
}
var settleDuration = 25 * time.Millisecond
// Settle returns the UI settle duration.
func Settle() time.Duration {
return settleDuration
}
// SetSettle changes the UI settle duration.
func SetSettle(s time.Duration) {
settleDuration = s
}
// settleUI allows the UI to "settle" before the next UI change is made.
func settleUI() {
time.Sleep(settleDuration)
}
type Buffer struct {
buf *bytes.Buffer // byte stream
}
func NewBuffer(b []byte) *Buffer {
return &Buffer{buf: bytes.NewBuffer(b)}
}
func (b *Buffer) Bytes() []byte {
return b.buf.Bytes()
}
func (b *Buffer) Read(data interface{}) error {
return binary.Read(b.buf, binary.BigEndian, data)
}
func (b *Buffer) Write(data interface{}) error {
return binary.Write(b.buf, binary.BigEndian, data)
}
func (b *Buffer) WriteByte(c byte) error {
return b.buf.WriteByte(c)
}
// Marshaler is the interface satisfied for marshaling messages.
type Marshaler interface {
// Marshal returns the wire encoding of a message.
Marshal() ([]byte, error)
}
// Unarshaler is the interface satisfied for unmarshaling messages.
type Unmarshaler interface {
// Unmarshal parses a wire format message into a message.
Unmarshal(data []byte) error
}
// MarshalerUnmarshaler satisfies both the Marshaler and Unmarshaler interfaces.
type MarshalerUnmarshaler interface {
Marshaler
Unmarshaler
}