neko/internal/webrtc/handler.go

158 lines
3.6 KiB
Go
Raw Normal View History

package webrtc
import (
"bytes"
"encoding/binary"
"math"
"time"
"github.com/demodesk/neko/internal/webrtc/payload"
"github.com/demodesk/neko/pkg/types"
"github.com/pion/webrtc/v3"
2023-04-17 10:47:34 +12:00
"github.com/rs/zerolog"
)
2023-04-17 10:47:34 +12:00
func (manager *WebRTCManagerCtx) handle(
logger zerolog.Logger, data []byte,
dataChannel *webrtc.DataChannel,
session types.Session,
) error {
isHost := session.IsHost()
2021-08-30 04:59:46 +12:00
2021-10-22 09:55:01 +13:00
//
// parse header
//
2021-09-03 07:06:58 +12:00
buffer := bytes.NewBuffer(data)
2021-10-22 09:55:01 +13:00
header := &payload.Header{}
2023-04-11 09:12:11 +12:00
if err := binary.Read(buffer, binary.BigEndian, header); err != nil {
return err
}
2021-10-22 09:55:01 +13:00
//
// parse body
//
2021-10-24 07:25:18 +13:00
// handle cursor move event
if header.Event == payload.OP_MOVE {
2021-09-03 07:06:58 +12:00
payload := &payload.Move{}
2021-02-13 12:01:56 +13:00
if err := binary.Read(buffer, binary.BigEndian, payload); err != nil {
return err
}
2021-10-24 07:25:18 +13:00
x, y := int(payload.X), int(payload.Y)
2023-04-17 10:47:34 +12:00
if isHost {
// handle active cursor movement
2021-10-24 07:25:18 +13:00
manager.desktop.Move(x, y)
manager.curPosition.Set(x, y)
} else {
// handle inactive cursor movement
2021-11-02 05:31:00 +13:00
session.SetCursor(types.Cursor{
X: x,
Y: y,
})
2021-10-24 07:25:18 +13:00
}
return nil
} else if header.Event == payload.OP_PING {
ping := &payload.Ping{}
if err := binary.Read(buffer, binary.BigEndian, ping); err != nil {
return err
}
2023-04-11 09:12:11 +12:00
// create pong header
header := payload.Header{
Event: payload.OP_PONG,
Length: 19,
}
// generate server timestamp
serverTs := uint64(time.Now().UnixMilli())
// generate pong payload
pong := payload.Pong{
Ping: *ping,
ServerTs1: uint32(serverTs / math.MaxUint32),
ServerTs2: uint32(serverTs % math.MaxUint32),
}
buffer := &bytes.Buffer{}
2023-04-11 09:12:11 +12:00
if err := binary.Write(buffer, binary.BigEndian, header); err != nil {
return err
}
if err := binary.Write(buffer, binary.BigEndian, pong); err != nil {
return err
}
return dataChannel.Send(buffer.Bytes())
2021-10-24 07:25:18 +13:00
}
// continue only if session is host
2023-04-17 10:47:34 +12:00
if !isHost {
2021-10-24 07:25:18 +13:00
return nil
}
switch header.Event {
2021-09-03 07:06:58 +12:00
case payload.OP_SCROLL:
payload := &payload.Scroll{}
2021-02-13 12:01:56 +13:00
if err := binary.Read(buffer, binary.BigEndian, payload); err != nil {
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).
Msg("scroll")
2021-09-03 07:06:58 +12:00
case payload.OP_KEY_DOWN:
payload := &payload.Key{}
2021-02-13 12:01:56 +13:00
if err := binary.Read(buffer, binary.BigEndian, payload); err != nil {
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")
}
2021-09-03 07:06:58 +12:00
case payload.OP_KEY_UP:
payload := &payload.Key{}
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
}
2021-09-03 07:06:58 +12:00
case payload.OP_BTN_DOWN:
payload := &payload.Key{}
2021-02-24 09:25:55 +13:00
if err := binary.Read(buffer, binary.BigEndian, payload); err != nil {
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")
}
2021-09-03 07:06:58 +12:00
case payload.OP_BTN_UP:
payload := &payload.Key{}
2021-02-24 09:25:55 +13:00
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
}
}
return nil
}