refactor handler.

This commit is contained in:
Miroslav Šedivý 2023-04-10 23:12:11 +02:00
parent c676d7a3a2
commit dfea7fca42
4 changed files with 27 additions and 30 deletions

View File

@ -21,13 +21,8 @@ func (manager *WebRTCManagerCtx) handle(data []byte, dataChannel *webrtc.DataCha
buffer := bytes.NewBuffer(data) buffer := bytes.NewBuffer(data)
hbytes := make([]byte, 3)
if _, err := buffer.Read(hbytes); err != nil {
return err
}
header := &payload.Header{} header := &payload.Header{}
if err := binary.Read(bytes.NewBuffer(hbytes), binary.BigEndian, header); err != nil { if err := binary.Read(buffer, binary.BigEndian, header); err != nil {
return err return err
} }
@ -35,8 +30,6 @@ func (manager *WebRTCManagerCtx) handle(data []byte, dataChannel *webrtc.DataCha
// parse body // parse body
// //
buffer = bytes.NewBuffer(data)
// handle cursor move event // handle cursor move event
if header.Event == payload.OP_MOVE { if header.Event == payload.OP_MOVE {
payload := &payload.Move{} payload := &payload.Move{}
@ -64,8 +57,8 @@ func (manager *WebRTCManagerCtx) handle(data []byte, dataChannel *webrtc.DataCha
return err return err
} }
// change header event to pong // create pong header
ping.Header = payload.Header{ header := payload.Header{
Event: payload.OP_PONG, Event: payload.OP_PONG,
Length: 19, Length: 19,
} }
@ -81,6 +74,11 @@ func (manager *WebRTCManagerCtx) handle(data []byte, dataChannel *webrtc.DataCha
} }
buffer := &bytes.Buffer{} buffer := &bytes.Buffer{}
if err := binary.Write(buffer, binary.BigEndian, header); err != nil {
return err
}
if err := binary.Write(buffer, binary.BigEndian, pong); err != nil { if err := binary.Write(buffer, binary.BigEndian, pong); err != nil {
return err return err
} }

View File

@ -13,28 +13,20 @@ const (
) )
type Move struct { type Move struct {
Header
X uint16 X uint16
Y uint16 Y uint16
} }
type Scroll struct { type Scroll struct {
Header
X int16 X int16
Y int16 Y int16
} }
type Key struct { type Key struct {
Header
Key uint32 Key uint32
} }
type Ping struct { type Ping struct {
Header
// client's timestamp split into two uint32 // client's timestamp split into two uint32
ClientTs1 uint32 ClientTs1 uint32
ClientTs2 uint32 ClientTs2 uint32

View File

@ -9,15 +9,11 @@ const (
) )
type CursorPosition struct { type CursorPosition struct {
Header
X uint16 X uint16
Y uint16 Y uint16
} }
type CursorImage struct { type CursorImage struct {
Header
Width uint16 Width uint16
Height uint16 Height uint16
Xhot uint16 Xhot uint16

View File

@ -202,16 +202,22 @@ func (peer *WebRTCPeerCtx) SendCursorPosition(x, y int) error {
peer.mu.Lock() peer.mu.Lock()
defer peer.mu.Unlock() defer peer.mu.Unlock()
data := payload.CursorPosition{ header := payload.Header{
Header: payload.Header{
Event: payload.OP_CURSOR_POSITION, Event: payload.OP_CURSOR_POSITION,
Length: 7, Length: 7,
}, }
data := payload.CursorPosition{
X: uint16(x), X: uint16(x),
Y: uint16(y), Y: uint16(y),
} }
buffer := &bytes.Buffer{} buffer := &bytes.Buffer{}
if err := binary.Write(buffer, binary.BigEndian, header); err != nil {
return err
}
if err := binary.Write(buffer, binary.BigEndian, data); err != nil { if err := binary.Write(buffer, binary.BigEndian, data); err != nil {
return err return err
} }
@ -223,11 +229,12 @@ func (peer *WebRTCPeerCtx) SendCursorImage(cur *types.CursorImage, img []byte) e
peer.mu.Lock() peer.mu.Lock()
defer peer.mu.Unlock() defer peer.mu.Unlock()
data := payload.CursorImage{ header := payload.Header{
Header: payload.Header{
Event: payload.OP_CURSOR_IMAGE, Event: payload.OP_CURSOR_IMAGE,
Length: uint16(11 + len(img)), Length: uint16(11 + len(img)),
}, }
data := payload.CursorImage{
Width: cur.Width, Width: cur.Width,
Height: cur.Height, Height: cur.Height,
Xhot: cur.Xhot, Xhot: cur.Xhot,
@ -236,6 +243,10 @@ func (peer *WebRTCPeerCtx) SendCursorImage(cur *types.CursorImage, img []byte) e
buffer := &bytes.Buffer{} buffer := &bytes.Buffer{}
if err := binary.Write(buffer, binary.BigEndian, header); err != nil {
return err
}
if err := binary.Write(buffer, binary.BigEndian, data); err != nil { if err := binary.Write(buffer, binary.BigEndian, data); err != nil {
return err return err
} }