2020-10-23 03:54:50 +13:00
|
|
|
package webrtc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
|
2021-02-02 10:45:44 +13:00
|
|
|
"github.com/pion/webrtc/v3"
|
2021-08-30 04:59:46 +12:00
|
|
|
|
|
|
|
"demodesk/neko/internal/types"
|
2020-10-23 03:54:50 +13:00
|
|
|
)
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
const (
|
2021-02-15 02:40:17 +13:00
|
|
|
OP_MOVE = 0x01
|
|
|
|
OP_SCROLL = 0x02
|
2020-11-02 04:09:48 +13:00
|
|
|
OP_KEY_DOWN = 0x03
|
2021-02-15 02:40:17 +13:00
|
|
|
OP_KEY_UP = 0x04
|
2021-02-24 09:25:55 +13:00
|
|
|
OP_BTN_DOWN = 0x05
|
|
|
|
OP_BTN_UP = 0x06
|
2020-11-02 04:09:48 +13:00
|
|
|
)
|
2020-10-23 03:54:50 +13:00
|
|
|
|
|
|
|
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
|
2021-02-24 09:25:55 +13:00
|
|
|
Key uint32
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2021-08-30 04:59:46 +12:00
|
|
|
func (manager *WebRTCManagerCtx) handle(msg webrtc.DataChannelMessage, session types.Session) error {
|
|
|
|
// add session id to logger context
|
|
|
|
logger := manager.logger.With().Str("session_id", session.ID()).Logger()
|
|
|
|
|
2020-10-23 03:54:50 +13:00
|
|
|
buffer := bytes.NewBuffer(msg.Data)
|
|
|
|
header := &PayloadHeader{}
|
|
|
|
hbytes := make([]byte, 3)
|
|
|
|
|
|
|
|
if _, err := buffer.Read(hbytes); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-13 12:01:56 +13:00
|
|
|
if err := binary.Read(bytes.NewBuffer(hbytes), binary.BigEndian, header); err != nil {
|
2020-10-23 03:54:50 +13:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer = bytes.NewBuffer(msg.Data)
|
|
|
|
|
|
|
|
switch header.Event {
|
|
|
|
case OP_MOVE:
|
|
|
|
payload := &PayloadMove{}
|
2021-02-13 12:01:56 +13:00
|
|
|
if err := binary.Read(buffer, binary.BigEndian, payload); err != nil {
|
2020-10-23 03:54:50 +13:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
manager.desktop.Move(int(payload.X), int(payload.Y))
|
2021-03-01 06:50:32 +13:00
|
|
|
manager.curPosition.Set(int(payload.X), int(payload.Y))
|
2020-10-23 03:54:50 +13:00
|
|
|
case OP_SCROLL:
|
|
|
|
payload := &PayloadScroll{}
|
2021-02-13 12:01:56 +13:00
|
|
|
if err := binary.Read(buffer, binary.BigEndian, payload); err != nil {
|
2020-10-23 03:54:50 +13:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-08-30 04:59:46 +12:00
|
|
|
manager.desktop.Scroll(int(payload.X), int(payload.Y))
|
|
|
|
logger.Trace().
|
2021-08-03 04:37:54 +12:00
|
|
|
Int16("x", payload.X).
|
|
|
|
Int16("y", payload.Y).
|
2020-10-23 03:54:50 +13:00
|
|
|
Msg("scroll")
|
|
|
|
case OP_KEY_DOWN:
|
|
|
|
payload := &PayloadKey{}
|
2021-02-13 12:01:56 +13:00
|
|
|
if err := binary.Read(buffer, binary.BigEndian, payload); err != nil {
|
2020-10-23 03:54:50 +13:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-24 09:25:55 +13:00
|
|
|
if err := manager.desktop.KeyDown(payload.Key); err != nil {
|
2021-08-30 04:59:46 +12:00
|
|
|
logger.Warn().Err(err).Uint32("key", payload.Key).Msg("key down failed")
|
|
|
|
} else {
|
|
|
|
logger.Trace().Uint32("key", payload.Key).Msg("key down")
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
case OP_KEY_UP:
|
|
|
|
payload := &PayloadKey{}
|
2021-02-24 09:25:55 +13:00
|
|
|
if err := binary.Read(buffer, binary.BigEndian, payload); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := manager.desktop.KeyUp(payload.Key); err != nil {
|
2021-08-30 04:59:46 +12:00
|
|
|
logger.Warn().Err(err).Uint32("key", payload.Key).Msg("key up failed")
|
|
|
|
} else {
|
|
|
|
logger.Trace().Uint32("key", payload.Key).Msg("key up")
|
2021-02-24 09:25:55 +13:00
|
|
|
}
|
|
|
|
case OP_BTN_DOWN:
|
|
|
|
payload := &PayloadKey{}
|
|
|
|
if err := binary.Read(buffer, binary.BigEndian, payload); err != nil {
|
2020-10-23 03:54:50 +13:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-24 09:25:55 +13:00
|
|
|
if err := manager.desktop.ButtonDown(payload.Key); err != nil {
|
2021-08-30 04:59:46 +12:00
|
|
|
logger.Warn().Err(err).Uint32("key", payload.Key).Msg("button down failed")
|
|
|
|
} else {
|
|
|
|
logger.Trace().Uint32("key", payload.Key).Msg("button down")
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
2021-02-24 09:25:55 +13:00
|
|
|
case OP_BTN_UP:
|
|
|
|
payload := &PayloadKey{}
|
|
|
|
if err := binary.Read(buffer, binary.BigEndian, payload); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := manager.desktop.ButtonUp(payload.Key); err != nil {
|
2021-08-30 04:59:46 +12:00
|
|
|
logger.Warn().Err(err).Uint32("key", payload.Key).Msg("button up failed")
|
|
|
|
} else {
|
|
|
|
logger.Trace().Uint32("key", payload.Key).Msg("button up")
|
2021-02-24 09:25:55 +13:00
|
|
|
}
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|