neko/internal/webrtc/handler.go

97 lines
2.6 KiB
Go
Raw Normal View History

package webrtc
import (
"bytes"
"encoding/binary"
2021-08-30 04:59:46 +12:00
"demodesk/neko/internal/types"
2021-09-03 07:06:58 +12:00
"demodesk/neko/internal/webrtc/payload"
)
2021-09-03 07:06:58 +12:00
func (manager *WebRTCManagerCtx) handle(data []byte, session types.Session) error {
2021-08-30 04:59:46 +12:00
// add session id to logger context
logger := manager.logger.With().Str("session_id", session.ID()).Logger()
2021-09-03 07:06:58 +12:00
buffer := bytes.NewBuffer(data)
header := &payload.Header{}
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 {
return err
}
2021-09-03 07:06:58 +12:00
buffer = bytes.NewBuffer(data)
switch header.Event {
2021-09-03 07:06:58 +12:00
case payload.OP_MOVE:
payload := &payload.Move{}
2021-02-13 12:01:56 +13:00
if err := binary.Read(buffer, binary.BigEndian, payload); err != nil {
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))
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
}