2017-07-03 23:54:55 +03:00
|
|
|
package vnc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
var (
|
|
|
|
// DefaultClientMessages slice of default client messages sent to server
|
|
|
|
DefaultClientMessages = []ClientMessage{
|
|
|
|
&SetPixelFormat{},
|
|
|
|
&SetEncodings{},
|
|
|
|
&FramebufferUpdateRequest{},
|
|
|
|
&KeyEvent{},
|
|
|
|
&PointerEvent{},
|
|
|
|
&ClientCutText{},
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultServerMessages slice of default server messages sent to client
|
|
|
|
DefaultServerMessages = []ServerMessage{
|
|
|
|
&FramebufferUpdate{},
|
|
|
|
&SetColorMapEntries{},
|
|
|
|
&Bell{},
|
|
|
|
&ServerCutText{},
|
|
|
|
}
|
|
|
|
)
|
2017-07-03 23:54:55 +03:00
|
|
|
|
2017-07-04 14:24:41 +03:00
|
|
|
// ClientMessageType represents RFB message type
|
2017-07-04 01:36:10 +03:00
|
|
|
type ClientMessageType uint8
|
2017-07-03 23:54:55 +03:00
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
//go:generate stringer -type=ClientMessageType
|
2017-07-03 23:54:55 +03:00
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Client-to-Server message types.
|
|
|
|
const (
|
|
|
|
SetPixelFormatMsgType ClientMessageType = iota
|
|
|
|
_
|
|
|
|
SetEncodingsMsgType
|
|
|
|
FramebufferUpdateRequestMsgType
|
|
|
|
KeyEventMsgType
|
|
|
|
PointerEventMsgType
|
|
|
|
ClientCutTextMsgType
|
|
|
|
)
|
|
|
|
|
2017-07-04 14:24:41 +03:00
|
|
|
// ServerMessageType represents RFB message type
|
2017-07-03 23:54:55 +03:00
|
|
|
type ServerMessageType uint8
|
|
|
|
|
2017-07-04 14:24:41 +03:00
|
|
|
// Server-to-Client message types
|
2017-07-03 23:54:55 +03:00
|
|
|
const (
|
|
|
|
FramebufferUpdateMsgType ServerMessageType = iota
|
|
|
|
SetColorMapEntriesMsgType
|
|
|
|
BellMsgType
|
|
|
|
ServerCutTextMsgType
|
|
|
|
)
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// ServerInit struct used in server init handshake
|
|
|
|
type ServerInit struct {
|
|
|
|
FBWidth, FBHeight uint16
|
|
|
|
PixelFormat PixelFormat
|
|
|
|
NameLength uint32
|
|
|
|
NameText []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// String provide stringer
|
|
|
|
func (srvInit ServerInit) String() string {
|
|
|
|
return fmt.Sprintf("Width: %d, Height: %d, PixelFormat: %s, NameLength: %d, MameText: %s", srvInit.FBWidth, srvInit.FBHeight, srvInit.PixelFormat, srvInit.NameLength, srvInit.NameText)
|
|
|
|
}
|
|
|
|
|
2017-07-04 14:24:41 +03:00
|
|
|
type ClientMessage interface {
|
|
|
|
String() string
|
|
|
|
Type() ClientMessageType
|
|
|
|
Read(Conn) (ClientMessage, error)
|
|
|
|
Write(Conn) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type ServerMessage interface {
|
|
|
|
String() string
|
|
|
|
Type() ServerMessageType
|
|
|
|
Read(Conn) (ServerMessage, error)
|
|
|
|
Write(Conn) error
|
|
|
|
}
|
|
|
|
|
2017-07-03 23:54:55 +03:00
|
|
|
// FramebufferUpdate holds a FramebufferUpdate wire format message.
|
|
|
|
type FramebufferUpdate struct {
|
|
|
|
_ [1]byte // pad
|
|
|
|
NumRect uint16 // number-of-rectangles
|
|
|
|
Rects []*Rectangle // rectangles
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// String provide stringer
|
2017-07-03 23:54:55 +03:00
|
|
|
func (msg *FramebufferUpdate) String() string {
|
|
|
|
return fmt.Sprintf("rects %d rectangle[]: { %v }", msg.NumRect, msg.Rects)
|
|
|
|
}
|
|
|
|
|
2017-07-04 14:24:41 +03:00
|
|
|
// Type return MessageType
|
2017-07-03 23:54:55 +03:00
|
|
|
func (*FramebufferUpdate) Type() ServerMessageType {
|
|
|
|
return FramebufferUpdateMsgType
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Read unmarshal message from conn
|
2017-07-03 23:54:55 +03:00
|
|
|
func (*FramebufferUpdate) Read(c Conn) (ServerMessage, error) {
|
|
|
|
msg := FramebufferUpdate{}
|
|
|
|
var pad [1]byte
|
|
|
|
if err := binary.Read(c, binary.BigEndian, &pad); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := binary.Read(c, binary.BigEndian, &msg.NumRect); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for i := uint16(0); i < msg.NumRect; i++ {
|
|
|
|
rect := NewRectangle()
|
|
|
|
if err := rect.Read(c); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
msg.Rects = append(msg.Rects, rect)
|
|
|
|
}
|
|
|
|
return &msg, nil
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Write marshals message to conn
|
2017-07-03 23:54:55 +03:00
|
|
|
func (msg *FramebufferUpdate) Write(c Conn) error {
|
|
|
|
if err := binary.Write(c, binary.BigEndian, msg.Type()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var pad [1]byte
|
|
|
|
if err := binary.Write(c, binary.BigEndian, pad); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := binary.Write(c, binary.BigEndian, msg.NumRect); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, rect := range msg.Rects {
|
|
|
|
if err := rect.Write(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return c.Flush()
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// ServerCutText represents server message
|
2017-07-03 23:54:55 +03:00
|
|
|
type ServerCutText struct {
|
|
|
|
_ [1]byte
|
|
|
|
Length uint32
|
|
|
|
Text []byte
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// String returns string
|
2017-07-03 23:54:55 +03:00
|
|
|
func (msg *ServerCutText) String() string {
|
|
|
|
return fmt.Sprintf("lenght: %d text: %s", msg.Length, msg.Text)
|
|
|
|
}
|
|
|
|
|
2017-07-04 14:24:41 +03:00
|
|
|
// Type returns MessageType
|
2017-07-03 23:54:55 +03:00
|
|
|
func (*ServerCutText) Type() ServerMessageType {
|
|
|
|
return ServerCutTextMsgType
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Read unmarshal message from conn
|
2017-07-03 23:54:55 +03:00
|
|
|
func (*ServerCutText) Read(c Conn) (ServerMessage, error) {
|
|
|
|
msg := ServerCutText{}
|
|
|
|
|
|
|
|
var pad [1]byte
|
|
|
|
if err := binary.Read(c, binary.BigEndian, &pad); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := binary.Read(c, binary.BigEndian, &msg.Length); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
msg.Text = make([]byte, msg.Length)
|
|
|
|
if err := binary.Read(c, binary.BigEndian, &msg.Text); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &msg, nil
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Write marshal message to conn
|
2017-07-03 23:54:55 +03:00
|
|
|
func (msg *ServerCutText) Write(c Conn) error {
|
|
|
|
if err := binary.Write(c, binary.BigEndian, msg.Type()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var pad [1]byte
|
|
|
|
if err := binary.Write(c, binary.BigEndian, pad); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg.Length < uint32(len(msg.Text)) {
|
|
|
|
msg.Length = uint32(len(msg.Text))
|
|
|
|
}
|
|
|
|
if err := binary.Write(c, binary.BigEndian, msg.Length); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := binary.Write(c, binary.BigEndian, msg.Text); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.Flush()
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Bell server message
|
2017-07-03 23:54:55 +03:00
|
|
|
type Bell struct{}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// String return string
|
2017-07-03 23:54:55 +03:00
|
|
|
func (*Bell) String() string {
|
|
|
|
return fmt.Sprintf("bell")
|
|
|
|
}
|
|
|
|
|
2017-07-04 14:24:41 +03:00
|
|
|
// Type returns MessageType
|
2017-07-03 23:54:55 +03:00
|
|
|
func (*Bell) Type() ServerMessageType {
|
|
|
|
return BellMsgType
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Read unmarshal message from conn
|
2017-07-03 23:54:55 +03:00
|
|
|
func (*Bell) Read(c Conn) (ServerMessage, error) {
|
|
|
|
return &Bell{}, nil
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Write marshal message to conn
|
2017-07-03 23:54:55 +03:00
|
|
|
func (msg *Bell) Write(c Conn) error {
|
|
|
|
if err := binary.Write(c, binary.BigEndian, msg.Type()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.Flush()
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// SetColorMapEntries server message
|
2017-07-03 23:54:55 +03:00
|
|
|
type SetColorMapEntries struct {
|
|
|
|
_ [1]byte
|
|
|
|
FirstColor uint16
|
|
|
|
ColorsNum uint16
|
|
|
|
Colors []Color
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// String returns string
|
2017-07-03 23:54:55 +03:00
|
|
|
func (msg *SetColorMapEntries) String() string {
|
|
|
|
return fmt.Sprintf("first color: %d, numcolors: %d, colors[]: { %v }", msg.FirstColor, msg.ColorsNum, msg.Colors)
|
|
|
|
}
|
|
|
|
|
2017-07-04 14:24:41 +03:00
|
|
|
// Type returns MessageType
|
2017-07-03 23:54:55 +03:00
|
|
|
func (*SetColorMapEntries) Type() ServerMessageType {
|
|
|
|
return SetColorMapEntriesMsgType
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Read unmrashal message from conn
|
2017-07-03 23:54:55 +03:00
|
|
|
func (*SetColorMapEntries) Read(c Conn) (ServerMessage, error) {
|
|
|
|
msg := SetColorMapEntries{}
|
|
|
|
var pad [1]byte
|
|
|
|
if err := binary.Read(c, binary.BigEndian, &pad); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := binary.Read(c, binary.BigEndian, &msg.FirstColor); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := binary.Read(c, binary.BigEndian, &msg.ColorsNum); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
msg.Colors = make([]Color, msg.ColorsNum)
|
|
|
|
colorMap := c.ColorMap()
|
|
|
|
|
|
|
|
for i := uint16(0); i < msg.ColorsNum; i++ {
|
|
|
|
color := &msg.Colors[i]
|
|
|
|
if err := binary.Read(c, binary.BigEndian, &color); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
colorMap[msg.FirstColor+i] = *color
|
|
|
|
}
|
|
|
|
c.SetColorMap(colorMap)
|
|
|
|
return &msg, nil
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Write marshal message to conn
|
2017-07-03 23:54:55 +03:00
|
|
|
func (msg *SetColorMapEntries) Write(c Conn) error {
|
|
|
|
if err := binary.Write(c, binary.BigEndian, msg.Type()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var pad [1]byte
|
|
|
|
if err := binary.Write(c, binary.BigEndian, &pad); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := binary.Write(c, binary.BigEndian, msg.FirstColor); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg.ColorsNum < uint16(len(msg.Colors)) {
|
|
|
|
msg.ColorsNum = uint16(len(msg.Colors))
|
|
|
|
}
|
|
|
|
if err := binary.Write(c, binary.BigEndian, msg.ColorsNum); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(msg.Colors); i++ {
|
|
|
|
color := msg.Colors[i]
|
|
|
|
if err := binary.Write(c, binary.BigEndian, color); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Flush()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPixelFormat holds the wire format message.
|
|
|
|
type SetPixelFormat struct {
|
|
|
|
_ [3]byte // padding
|
|
|
|
PF PixelFormat // pixel-format
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// String returns string
|
2017-07-03 23:54:55 +03:00
|
|
|
func (msg *SetPixelFormat) String() string {
|
|
|
|
return fmt.Sprintf("%s", msg.PF)
|
|
|
|
}
|
|
|
|
|
2017-07-04 14:24:41 +03:00
|
|
|
// Type returns MessageType
|
2017-07-03 23:54:55 +03:00
|
|
|
func (*SetPixelFormat) Type() ClientMessageType {
|
|
|
|
return SetPixelFormatMsgType
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Write marshal message to conn
|
2017-07-03 23:54:55 +03:00
|
|
|
func (msg *SetPixelFormat) Write(c Conn) error {
|
|
|
|
if err := binary.Write(c, binary.BigEndian, msg.Type()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := binary.Write(c, binary.BigEndian, msg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
pf := c.PixelFormat()
|
|
|
|
// Invalidate the color map.
|
|
|
|
if pf.TrueColor != 1 {
|
|
|
|
c.SetColorMap(ColorMap{})
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Flush()
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Read unmarshal message from conn
|
2017-07-03 23:54:55 +03:00
|
|
|
func (*SetPixelFormat) Read(c Conn) (ClientMessage, error) {
|
|
|
|
msg := SetPixelFormat{}
|
|
|
|
if err := binary.Read(c, binary.BigEndian, &msg); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &msg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetEncodings holds the wire format message, sans encoding-type field.
|
|
|
|
type SetEncodings struct {
|
|
|
|
_ [1]byte // padding
|
|
|
|
EncNum uint16 // number-of-encodings
|
|
|
|
Encodings []EncodingType
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// String return string
|
2017-07-03 23:54:55 +03:00
|
|
|
func (msg *SetEncodings) String() string {
|
|
|
|
return fmt.Sprintf("encnum: %d, encodings[]: { %v }", msg.EncNum, msg.Encodings)
|
|
|
|
}
|
|
|
|
|
2017-07-04 14:24:41 +03:00
|
|
|
// Type returns MessageType
|
2017-07-03 23:54:55 +03:00
|
|
|
func (*SetEncodings) Type() ClientMessageType {
|
|
|
|
return SetEncodingsMsgType
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Read unmarshal message from conn
|
2017-07-03 23:54:55 +03:00
|
|
|
func (*SetEncodings) Read(c Conn) (ClientMessage, error) {
|
|
|
|
msg := SetEncodings{}
|
|
|
|
var pad [1]byte
|
|
|
|
if err := binary.Read(c, binary.BigEndian, &pad); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := binary.Read(c, binary.BigEndian, &msg.EncNum); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var enc EncodingType
|
|
|
|
for i := uint16(0); i < msg.EncNum; i++ {
|
|
|
|
if err := binary.Read(c, binary.BigEndian, &enc); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
msg.Encodings = append(msg.Encodings, enc)
|
|
|
|
}
|
|
|
|
c.SetEncodings(msg.Encodings)
|
|
|
|
return &msg, nil
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Write marshal message to conn
|
2017-07-03 23:54:55 +03:00
|
|
|
func (msg *SetEncodings) Write(c Conn) error {
|
|
|
|
if err := binary.Write(c, binary.BigEndian, msg.Type()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var pad [1]byte
|
|
|
|
if err := binary.Write(c, binary.BigEndian, pad); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if uint16(len(msg.Encodings)) > msg.EncNum {
|
|
|
|
msg.EncNum = uint16(len(msg.Encodings))
|
|
|
|
}
|
|
|
|
if err := binary.Write(c, binary.BigEndian, msg.EncNum); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, enc := range msg.Encodings {
|
|
|
|
if err := binary.Write(c, binary.BigEndian, enc); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return c.Flush()
|
|
|
|
}
|
|
|
|
|
|
|
|
// FramebufferUpdateRequest holds the wire format message.
|
|
|
|
type FramebufferUpdateRequest struct {
|
|
|
|
Inc uint8 // incremental
|
|
|
|
X, Y uint16 // x-, y-position
|
|
|
|
Width, Height uint16 // width, height
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// String returns string
|
2017-07-03 23:54:55 +03:00
|
|
|
func (msg *FramebufferUpdateRequest) String() string {
|
|
|
|
return fmt.Sprintf("incremental: %d, x: %d, y: %d, width: %d, height: %d", msg.Inc, msg.X, msg.Y, msg.Width, msg.Height)
|
|
|
|
}
|
|
|
|
|
2017-07-04 14:24:41 +03:00
|
|
|
// Type returns MessageType
|
2017-07-03 23:54:55 +03:00
|
|
|
func (*FramebufferUpdateRequest) Type() ClientMessageType {
|
|
|
|
return FramebufferUpdateRequestMsgType
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Read unmarshal message from conn
|
2017-07-03 23:54:55 +03:00
|
|
|
func (*FramebufferUpdateRequest) Read(c Conn) (ClientMessage, error) {
|
|
|
|
msg := FramebufferUpdateRequest{}
|
|
|
|
if err := binary.Read(c, binary.BigEndian, &msg); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &msg, nil
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Write marshal message to conn
|
2017-07-03 23:54:55 +03:00
|
|
|
func (msg *FramebufferUpdateRequest) Write(c Conn) error {
|
|
|
|
if err := binary.Write(c, binary.BigEndian, msg.Type()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := binary.Write(c, binary.BigEndian, msg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.Flush()
|
|
|
|
}
|
|
|
|
|
|
|
|
// KeyEvent holds the wire format message.
|
|
|
|
type KeyEvent struct {
|
|
|
|
Down uint8 // down-flag
|
|
|
|
_ [2]byte // padding
|
|
|
|
Key Key // key
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// String returns string
|
2017-07-03 23:54:55 +03:00
|
|
|
func (msg *KeyEvent) String() string {
|
|
|
|
return fmt.Sprintf("down: %d, key: %v", msg.Down, msg.Key)
|
|
|
|
}
|
|
|
|
|
2017-07-04 14:24:41 +03:00
|
|
|
// Type returns MessageType
|
2017-07-03 23:54:55 +03:00
|
|
|
func (*KeyEvent) Type() ClientMessageType {
|
|
|
|
return KeyEventMsgType
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Read unmarshal message from conn
|
2017-07-03 23:54:55 +03:00
|
|
|
func (*KeyEvent) Read(c Conn) (ClientMessage, error) {
|
|
|
|
msg := KeyEvent{}
|
|
|
|
if err := binary.Read(c, binary.BigEndian, &msg); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &msg, nil
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Write marshal message to conn
|
2017-07-03 23:54:55 +03:00
|
|
|
func (msg *KeyEvent) Write(c Conn) error {
|
|
|
|
if err := binary.Write(c, binary.BigEndian, msg.Type()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := binary.Write(c, binary.BigEndian, msg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.Flush()
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// PointerEvent message holds the wire format message
|
2017-07-03 23:54:55 +03:00
|
|
|
type PointerEvent struct {
|
|
|
|
Mask uint8 // button-mask
|
|
|
|
X, Y uint16 // x-, y-position
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// String returns string
|
2017-07-03 23:54:55 +03:00
|
|
|
func (msg *PointerEvent) String() string {
|
|
|
|
return fmt.Sprintf("mask %d, x: %d, y: %d", msg.Mask, msg.X, msg.Y)
|
|
|
|
}
|
|
|
|
|
2017-07-04 14:24:41 +03:00
|
|
|
// Type returns MessageType
|
2017-07-03 23:54:55 +03:00
|
|
|
func (*PointerEvent) Type() ClientMessageType {
|
|
|
|
return PointerEventMsgType
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Read unmarshal message from conn
|
2017-07-03 23:54:55 +03:00
|
|
|
func (*PointerEvent) Read(c Conn) (ClientMessage, error) {
|
|
|
|
msg := PointerEvent{}
|
|
|
|
if err := binary.Read(c, binary.BigEndian, &msg); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &msg, nil
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Write marshal message to conn
|
2017-07-03 23:54:55 +03:00
|
|
|
func (msg *PointerEvent) Write(c Conn) error {
|
|
|
|
if err := binary.Write(c, binary.BigEndian, msg.Type()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := binary.Write(c, binary.BigEndian, msg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.Flush()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ClientCutText holds the wire format message, sans the text field.
|
|
|
|
type ClientCutText struct {
|
|
|
|
_ [3]byte // padding
|
|
|
|
Length uint32 // length
|
|
|
|
Text []byte
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// String returns string
|
2017-07-03 23:54:55 +03:00
|
|
|
func (msg *ClientCutText) String() string {
|
|
|
|
return fmt.Sprintf("length: %d, text: %s", msg.Length, msg.Text)
|
|
|
|
}
|
|
|
|
|
2017-07-04 14:24:41 +03:00
|
|
|
// Type returns MessageType
|
2017-07-03 23:54:55 +03:00
|
|
|
func (*ClientCutText) Type() ClientMessageType {
|
|
|
|
return ClientCutTextMsgType
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Read unmarshal message from conn
|
2017-07-03 23:54:55 +03:00
|
|
|
func (*ClientCutText) Read(c Conn) (ClientMessage, error) {
|
|
|
|
msg := ClientCutText{}
|
|
|
|
var pad [3]byte
|
|
|
|
if err := binary.Read(c, binary.BigEndian, &pad); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := binary.Read(c, binary.BigEndian, &msg.Length); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
msg.Text = make([]byte, msg.Length)
|
|
|
|
if err := binary.Read(c, binary.BigEndian, &msg.Text); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &msg, nil
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:36:10 +03:00
|
|
|
// Write marshal message to conn
|
2017-07-03 23:54:55 +03:00
|
|
|
func (msg *ClientCutText) Write(c Conn) error {
|
|
|
|
if err := binary.Write(c, binary.BigEndian, msg.Type()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var pad [3]byte
|
|
|
|
if err := binary.Write(c, binary.BigEndian, &pad); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if uint32(len(msg.Text)) > msg.Length {
|
|
|
|
msg.Length = uint32(len(msg.Text))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := binary.Write(c, binary.BigEndian, msg.Length); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := binary.Write(c, binary.BigEndian, msg.Text); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Flush()
|
|
|
|
}
|