extract webrtc payload to own module.

This commit is contained in:
Miroslav Šedivý 2021-09-02 21:06:58 +02:00
parent e812cb9fbf
commit 00fc3afcd7
6 changed files with 83 additions and 75 deletions

View File

@ -4,48 +4,16 @@ import (
"bytes"
"encoding/binary"
"github.com/pion/webrtc/v3"
"demodesk/neko/internal/types"
"demodesk/neko/internal/webrtc/payload"
)
const (
OP_MOVE = 0x01
OP_SCROLL = 0x02
OP_KEY_DOWN = 0x03
OP_KEY_UP = 0x04
OP_BTN_DOWN = 0x05
OP_BTN_UP = 0x06
)
type PayloadHeader struct {
Event uint8
Length uint16
}
type PayloadMove struct {
PayloadHeader
X uint16
Y uint16
}
type PayloadScroll struct {
PayloadHeader
X int16
Y int16
}
type PayloadKey struct {
PayloadHeader
Key uint32
}
func (manager *WebRTCManagerCtx) handle(msg webrtc.DataChannelMessage, session types.Session) error {
func (manager *WebRTCManagerCtx) handle(data []byte, session types.Session) error {
// add session id to logger context
logger := manager.logger.With().Str("session_id", session.ID()).Logger()
buffer := bytes.NewBuffer(msg.Data)
header := &PayloadHeader{}
buffer := bytes.NewBuffer(data)
header := &payload.Header{}
hbytes := make([]byte, 3)
if _, err := buffer.Read(hbytes); err != nil {
@ -56,19 +24,19 @@ func (manager *WebRTCManagerCtx) handle(msg webrtc.DataChannelMessage, session t
return err
}
buffer = bytes.NewBuffer(msg.Data)
buffer = bytes.NewBuffer(data)
switch header.Event {
case OP_MOVE:
payload := &PayloadMove{}
case payload.OP_MOVE:
payload := &payload.Move{}
if err := binary.Read(buffer, binary.BigEndian, payload); err != nil {
return err
}
manager.desktop.Move(int(payload.X), int(payload.Y))
manager.curPosition.Set(int(payload.X), int(payload.Y))
case OP_SCROLL:
payload := &PayloadScroll{}
case payload.OP_SCROLL:
payload := &payload.Scroll{}
if err := binary.Read(buffer, binary.BigEndian, payload); err != nil {
return err
}
@ -78,8 +46,8 @@ func (manager *WebRTCManagerCtx) handle(msg webrtc.DataChannelMessage, session t
Int16("x", payload.X).
Int16("y", payload.Y).
Msg("scroll")
case OP_KEY_DOWN:
payload := &PayloadKey{}
case payload.OP_KEY_DOWN:
payload := &payload.Key{}
if err := binary.Read(buffer, binary.BigEndian, payload); err != nil {
return err
}
@ -89,8 +57,8 @@ func (manager *WebRTCManagerCtx) handle(msg webrtc.DataChannelMessage, session t
} else {
logger.Trace().Uint32("key", payload.Key).Msg("key down")
}
case OP_KEY_UP:
payload := &PayloadKey{}
case payload.OP_KEY_UP:
payload := &payload.Key{}
if err := binary.Read(buffer, binary.BigEndian, payload); err != nil {
return err
}
@ -100,8 +68,8 @@ func (manager *WebRTCManagerCtx) handle(msg webrtc.DataChannelMessage, session t
} else {
logger.Trace().Uint32("key", payload.Key).Msg("key up")
}
case OP_BTN_DOWN:
payload := &PayloadKey{}
case payload.OP_BTN_DOWN:
payload := &payload.Key{}
if err := binary.Read(buffer, binary.BigEndian, payload); err != nil {
return err
}
@ -111,8 +79,8 @@ func (manager *WebRTCManagerCtx) handle(msg webrtc.DataChannelMessage, session t
} else {
logger.Trace().Uint32("key", payload.Key).Msg("button down")
}
case OP_BTN_UP:
payload := &PayloadKey{}
case payload.OP_BTN_UP:
payload := &payload.Key{}
if err := binary.Read(buffer, binary.BigEndian, payload); err != nil {
return err
}

View File

@ -322,7 +322,7 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session, videoID strin
return
}
if err = manager.handle(message, session); err != nil {
if err = manager.handle(message.Data, session); err != nil {
logger.Err(err).Msg("data handle failed")
}
})

View File

@ -0,0 +1,30 @@
package payload
const (
OP_MOVE = 0x01
OP_SCROLL = 0x02
OP_KEY_DOWN = 0x03
OP_KEY_UP = 0x04
OP_BTN_DOWN = 0x05
OP_BTN_UP = 0x06
)
type Move struct {
Header
X uint16
Y uint16
}
type Scroll struct {
Header
X int16
Y int16
}
type Key struct {
Header
Key uint32
}

View File

@ -0,0 +1,22 @@
package payload
const (
OP_CURSOR_POSITION = 0x01
OP_CURSOR_IMAGE = 0x02
)
type CursorPosition struct {
Header
X uint16
Y uint16
}
type CursorImage struct {
Header
Width uint16
Height uint16
Xhot uint16
Yhot uint16
}

View File

@ -0,0 +1,6 @@
package payload
type Header struct {
Event uint8
Length uint16
}

View File

@ -5,27 +5,9 @@ import (
"encoding/binary"
"demodesk/neko/internal/types"
"demodesk/neko/internal/webrtc/payload"
)
const (
OP_CURSOR_POSITION = 0x01
OP_CURSOR_IMAGE = 0x02
)
type PayloadCursorPosition struct {
PayloadHeader
X uint16
Y uint16
}
type PayloadCursorImage struct {
PayloadHeader
Width uint16
Height uint16
Xhot uint16
Yhot uint16
}
func (peer *WebRTCPeerCtx) SendCursorPosition(x, y int) error {
peer.mu.Lock()
defer peer.mu.Unlock()
@ -34,9 +16,9 @@ func (peer *WebRTCPeerCtx) SendCursorPosition(x, y int) error {
return types.ErrWebRTCDataChannelNotFound
}
data := PayloadCursorPosition{
PayloadHeader: PayloadHeader{
Event: OP_CURSOR_POSITION,
data := payload.CursorPosition{
Header: payload.Header{
Event: payload.OP_CURSOR_POSITION,
Length: 7,
},
X: uint16(x),
@ -59,9 +41,9 @@ func (peer *WebRTCPeerCtx) SendCursorImage(cur *types.CursorImage, img []byte) e
return types.ErrWebRTCDataChannelNotFound
}
data := PayloadCursorImage{
PayloadHeader: PayloadHeader{
Event: OP_CURSOR_IMAGE,
data := payload.CursorImage{
Header: payload.Header{
Event: payload.OP_CURSOR_IMAGE,
Length: uint16(11 + len(img)),
},
Width: cur.Width,