Fix Poke timeout, input CommandLow handling, and add Poke Popup
This commit is contained in:
@@ -35,6 +35,21 @@ func sanitizeForLog(s string) string {
|
||||
}
|
||||
|
||||
func (c *Client) handleCommand(pkt *protocol.Packet) error {
|
||||
// Select the correct queue and counters based on PacketType
|
||||
var queue map[uint16]*protocol.Packet
|
||||
var expectedPID *uint16
|
||||
var fragmentState *bool
|
||||
|
||||
if pkt.Header.PacketType() == protocol.PacketTypeCommandLow {
|
||||
queue = c.CommandLowQueue
|
||||
expectedPID = &c.ExpectedCommandLowPID
|
||||
fragmentState = &c.FragmentStateLow
|
||||
} else {
|
||||
queue = c.CommandQueue
|
||||
expectedPID = &c.ExpectedCommandPID
|
||||
fragmentState = &c.FragmentState
|
||||
}
|
||||
|
||||
// Check if Encrypted
|
||||
// PacketTypeCommand is usually encrypted.
|
||||
// Flag check? The flag is in the Header (e.g. Unencrypted flag).
|
||||
@@ -100,14 +115,14 @@ func (c *Client) handleCommand(pkt *protocol.Packet) error {
|
||||
|
||||
// Queue-based fragment reassembly (like ts3j)
|
||||
// Store packet in queue
|
||||
c.CommandQueue[pkt.Header.PacketID] = &protocol.Packet{
|
||||
queue[pkt.Header.PacketID] = &protocol.Packet{
|
||||
Header: pkt.Header,
|
||||
Data: append([]byte{}, data...), // Clone data (already decrypted)
|
||||
}
|
||||
|
||||
// Try to process packets in order
|
||||
for {
|
||||
nextPkt, ok := c.CommandQueue[c.ExpectedCommandPID]
|
||||
nextPkt, ok := queue[*expectedPID]
|
||||
if !ok {
|
||||
// Missing packet, wait for it
|
||||
break
|
||||
@@ -117,16 +132,16 @@ func (c *Client) handleCommand(pkt *protocol.Packet) error {
|
||||
|
||||
if isFragmented {
|
||||
// Toggle fragment state
|
||||
c.FragmentState = !c.FragmentState
|
||||
*fragmentState = !*fragmentState
|
||||
|
||||
if c.FragmentState {
|
||||
if *fragmentState {
|
||||
// Starting a new fragment sequence
|
||||
// Don't process yet, wait for more
|
||||
c.ExpectedCommandPID++
|
||||
*expectedPID++
|
||||
continue
|
||||
} else {
|
||||
// Ending fragment sequence - reassemble all
|
||||
reassembled, compressed := c.reassembleFragments()
|
||||
reassembled, compressed := c.reassembleFragmentsCustom(queue, *expectedPID)
|
||||
if reassembled == nil {
|
||||
log.Printf("Fragment reassembly failed")
|
||||
break
|
||||
@@ -144,9 +159,9 @@ func (c *Client) handleCommand(pkt *protocol.Packet) error {
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if c.FragmentState {
|
||||
} else if *fragmentState {
|
||||
// Middle fragment - keep collecting
|
||||
c.ExpectedCommandPID++
|
||||
*expectedPID++
|
||||
continue
|
||||
} else {
|
||||
// Non-fragmented packet - process normally
|
||||
@@ -165,10 +180,11 @@ func (c *Client) handleCommand(pkt *protocol.Packet) error {
|
||||
}
|
||||
|
||||
// Remove processed packet from queue
|
||||
delete(c.CommandQueue, c.ExpectedCommandPID)
|
||||
c.ExpectedCommandPID++
|
||||
delete(queue, *expectedPID)
|
||||
*expectedPID++
|
||||
|
||||
// Process the command
|
||||
// Fix: processCommand should probably handle execution
|
||||
if err := c.processCommand(data, nextPkt); err != nil {
|
||||
log.Printf("Error processing command: %v", err)
|
||||
}
|
||||
@@ -177,16 +193,17 @@ func (c *Client) handleCommand(pkt *protocol.Packet) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// reassembleFragments collects all buffered fragments in order and returns reassembled data
|
||||
func (c *Client) reassembleFragments() ([]byte, bool) {
|
||||
// reassembleFragmentsCustom collects all buffered fragments in order from the given queue
|
||||
// ending at currentPID.
|
||||
func (c *Client) reassembleFragmentsCustom(queue map[uint16]*protocol.Packet, currentPID uint16) ([]byte, bool) {
|
||||
var result []byte
|
||||
compressed := false
|
||||
|
||||
// Find the start of the fragment sequence (scan backwards from current)
|
||||
startPID := c.ExpectedCommandPID
|
||||
startPID := currentPID
|
||||
for {
|
||||
prevPID := startPID - 1
|
||||
pkt, ok := c.CommandQueue[prevPID]
|
||||
pkt, ok := queue[prevPID]
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
@@ -198,9 +215,9 @@ func (c *Client) reassembleFragments() ([]byte, bool) {
|
||||
startPID = prevPID
|
||||
}
|
||||
|
||||
// Now collect from startPID to ExpectedCommandPID (inclusive)
|
||||
for pid := startPID; pid <= c.ExpectedCommandPID; pid++ {
|
||||
pkt, ok := c.CommandQueue[pid]
|
||||
// Now collect from startPID to currentPID (inclusive)
|
||||
for pid := startPID; pid <= currentPID; pid++ {
|
||||
pkt, ok := queue[pid]
|
||||
if !ok {
|
||||
log.Printf("Missing fragment PID=%d during reassembly", pid)
|
||||
return nil, false
|
||||
@@ -212,11 +229,11 @@ func (c *Client) reassembleFragments() ([]byte, bool) {
|
||||
}
|
||||
|
||||
result = append(result, pkt.Data...)
|
||||
delete(c.CommandQueue, pid)
|
||||
delete(queue, pid)
|
||||
}
|
||||
|
||||
log.Printf("Reassembled fragments PID %d-%d, total %d bytes, compressed=%v",
|
||||
startPID, c.ExpectedCommandPID, len(result), compressed)
|
||||
startPID, currentPID, len(result), compressed)
|
||||
|
||||
return result, compressed
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user