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

View File

@ -322,7 +322,7 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session, videoID strin
return 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") 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" "encoding/binary"
"demodesk/neko/internal/types" "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 { func (peer *WebRTCPeerCtx) SendCursorPosition(x, y int) error {
peer.mu.Lock() peer.mu.Lock()
defer peer.mu.Unlock() defer peer.mu.Unlock()
@ -34,9 +16,9 @@ func (peer *WebRTCPeerCtx) SendCursorPosition(x, y int) error {
return types.ErrWebRTCDataChannelNotFound return types.ErrWebRTCDataChannelNotFound
} }
data := PayloadCursorPosition{ data := payload.CursorPosition{
PayloadHeader: PayloadHeader{ Header: payload.Header{
Event: OP_CURSOR_POSITION, Event: payload.OP_CURSOR_POSITION,
Length: 7, Length: 7,
}, },
X: uint16(x), X: uint16(x),
@ -59,9 +41,9 @@ func (peer *WebRTCPeerCtx) SendCursorImage(cur *types.CursorImage, img []byte) e
return types.ErrWebRTCDataChannelNotFound return types.ErrWebRTCDataChannelNotFound
} }
data := PayloadCursorImage{ data := payload.CursorImage{
PayloadHeader: PayloadHeader{ Header: payload.Header{
Event: OP_CURSOR_IMAGE, Event: payload.OP_CURSOR_IMAGE,
Length: uint16(11 + len(img)), Length: uint16(11 + len(img)),
}, },
Width: cur.Width, Width: cur.Width,