fix WebRTC send: remove structures with dynamic size.

This commit is contained in:
Miroslav Šedivý 2021-02-12 23:46:57 +01:00
parent 8a3b3f14cb
commit ed9f0276c7

View File

@ -1,6 +1,7 @@
package webrtc package webrtc
import ( import (
"fmt"
"bytes" "bytes"
"encoding/binary" "encoding/binary"
@ -25,12 +26,11 @@ type PayloadCursorImage struct {
Height uint16 Height uint16
Xhot uint16 Xhot uint16
Yhot uint16 Yhot uint16
Uri string
} }
func (webrtc_peer *WebRTCPeerCtx) SendCursorPosition(x, y int) error { func (webrtc_peer *WebRTCPeerCtx) SendCursorPosition(x, y int) error {
if webrtc_peer.dataChannel == nil { if webrtc_peer.dataChannel == nil {
return nil return fmt.Errorf("no data channel")
} }
data := PayloadCursorPosition{ data := PayloadCursorPosition{
@ -42,17 +42,20 @@ func (webrtc_peer *WebRTCPeerCtx) SendCursorPosition(x, y int) error {
Y: uint16(y), Y: uint16(y),
} }
var buffer bytes.Buffer buffer := &bytes.Buffer{}
binary.Write(&buffer, binary.LittleEndian, data) if err := binary.Write(buffer, binary.LittleEndian, data); err != nil {
return err
}
return webrtc_peer.dataChannel.Send(buffer.Bytes()) return webrtc_peer.dataChannel.Send(buffer.Bytes())
} }
func (webrtc_peer *WebRTCPeerCtx) SendCursorImage(cur *types.CursorImage) error { func (webrtc_peer *WebRTCPeerCtx) SendCursorImage(cur *types.CursorImage) error {
if webrtc_peer.dataChannel == nil { if webrtc_peer.dataChannel == nil {
return nil return fmt.Errorf("no data channel")
} }
uri, err := utils.GetCursorImageURI(cur) img, err := utils.GetCursorImage(cur)
if err != nil { if err != nil {
return err return err
} }
@ -60,16 +63,23 @@ func (webrtc_peer *WebRTCPeerCtx) SendCursorImage(cur *types.CursorImage) error
data := PayloadCursorImage{ data := PayloadCursorImage{
PayloadHeader: PayloadHeader{ PayloadHeader: PayloadHeader{
Event: OP_CURSOR_IMAGE, Event: OP_CURSOR_IMAGE,
Length: uint16(11 + len(uri)), Length: uint16(11 + len(img)),
}, },
Width: cur.Width, Width: cur.Width,
Height: cur.Height, Height: cur.Height,
Xhot: cur.Xhot, Xhot: cur.Xhot,
Yhot: cur.Yhot, Yhot: cur.Yhot,
Uri: uri,
} }
var buffer bytes.Buffer buffer := &bytes.Buffer{}
binary.Write(&buffer, binary.LittleEndian, data)
if err := binary.Write(buffer, binary.LittleEndian, data); err != nil {
return err
}
if err := binary.Write(buffer, binary.LittleEndian, img); err != nil {
return err
}
return webrtc_peer.dataChannel.Send(buffer.Bytes()) return webrtc_peer.dataChannel.Send(buffer.Bytes())
} }