neko/internal/webrtc/send.go

67 lines
1.3 KiB
Go
Raw Normal View History

2021-02-12 22:13:55 +01:00
package webrtc
import (
"bytes"
2021-02-14 14:40:17 +01:00
"encoding/binary"
2021-02-12 22:13:55 +01:00
"demodesk/neko/internal/types"
2021-09-02 21:06:58 +02:00
"demodesk/neko/internal/webrtc/payload"
2021-02-12 22:13:55 +01:00
)
2021-02-14 17:11:21 +01:00
func (peer *WebRTCPeerCtx) SendCursorPosition(x, y int) error {
2021-08-29 17:09:13 +02:00
peer.mu.Lock()
defer peer.mu.Unlock()
2021-02-14 17:11:21 +01:00
if peer.dataChannel == nil {
2021-08-29 17:09:13 +02:00
return types.ErrWebRTCDataChannelNotFound
2021-02-12 22:13:55 +01:00
}
2021-09-02 21:06:58 +02:00
data := payload.CursorPosition{
Header: payload.Header{
Event: payload.OP_CURSOR_POSITION,
2021-02-12 22:13:55 +01:00
Length: 7,
},
X: uint16(x),
Y: uint16(y),
}
buffer := &bytes.Buffer{}
2021-02-13 00:01:56 +01:00
if err := binary.Write(buffer, binary.BigEndian, data); err != nil {
return err
}
2021-02-14 17:11:21 +01:00
return peer.dataChannel.Send(buffer.Bytes())
2021-02-12 22:13:55 +01:00
}
func (peer *WebRTCPeerCtx) SendCursorImage(cur *types.CursorImage, img []byte) error {
2021-08-29 17:09:13 +02:00
peer.mu.Lock()
defer peer.mu.Unlock()
2021-02-14 17:11:21 +01:00
if peer.dataChannel == nil {
2021-08-29 17:09:13 +02:00
return types.ErrWebRTCDataChannelNotFound
2021-02-12 22:13:55 +01:00
}
2021-09-02 21:06:58 +02:00
data := payload.CursorImage{
Header: payload.Header{
Event: payload.OP_CURSOR_IMAGE,
Length: uint16(11 + len(img)),
2021-02-12 22:13:55 +01:00
},
2021-02-14 14:40:17 +01:00
Width: cur.Width,
2021-02-12 22:13:55 +01:00
Height: cur.Height,
2021-02-14 14:40:17 +01:00
Xhot: cur.Xhot,
Yhot: cur.Yhot,
2021-02-12 22:13:55 +01:00
}
buffer := &bytes.Buffer{}
2021-02-13 00:01:56 +01:00
if err := binary.Write(buffer, binary.BigEndian, data); err != nil {
return err
}
2021-02-13 00:01:56 +01:00
if err := binary.Write(buffer, binary.BigEndian, img); err != nil {
return err
}
2021-02-14 17:11:21 +01:00
return peer.dataChannel.Send(buffer.Bytes())
2021-02-12 22:13:55 +01:00
}