This commit is contained in:
matst80
2024-11-11 19:47:44 +01:00
parent e63a92213b
commit 3eaa42b615
4 changed files with 33 additions and 28 deletions

View File

@@ -22,10 +22,12 @@ type PacketQueue struct {
//connection net.Conn
}
const cap = 150
func NewPacketQueue(connection net.Conn) *PacketQueue {
queue := &PacketQueue{
Packets: make([]PacketWithData, 0),
Packets: make([]PacketWithData, 0, cap),
//connection: connection,
}
go func() {
@@ -53,7 +55,6 @@ func NewPacketQueue(connection net.Conn) *PacketQueue {
func (p *PacketQueue) HandleData(t uint32, data []byte) {
ts := time.Now()
cap := 150
l := make([]PacketWithData, 0, cap)
p.mu.RLock()
breakAt := ts.Add(-time.Millisecond * 250)
@@ -85,14 +86,13 @@ func (p *PacketQueue) Expect(messageType uint32, timeToWait time.Duration) (*Pac
return nil, fmt.Errorf("timeout waiting for message type %d", messageType)
}
p.mu.RLock()
defer p.mu.RUnlock()
for _, packet := range p.Packets {
if packet.MessageType == messageType && packet.Added.After(start) {
if !packet.Consumed && packet.MessageType == messageType && packet.Added.After(start) {
packet.Consumed = true
p.mu.RUnlock()
return &packet, nil
}
}
p.mu.RUnlock()
time.Sleep(time.Millisecond * 2)
time.Sleep(time.Millisecond * 4)
}
}