use persistent connections
Some checks failed
Build and Publish / BuildAndDeployAmd64 (push) Failing after 27s
Build and Publish / BuildAndDeploy (push) Successful in 2m19s

This commit is contained in:
matst80
2024-11-12 17:09:46 +01:00
parent 1c42cf0976
commit 98d9b2451e
4 changed files with 69 additions and 201 deletions

View File

@@ -3,11 +3,8 @@ package main
import (
"bufio"
"fmt"
"io"
"log"
"net"
"sync"
"time"
)
type PacketQueue struct {
@@ -25,7 +22,7 @@ type Listener struct {
Chan chan CallResult
}
func NewPacketQueue(connection net.Conn) *PacketQueue {
func NewPacketQueue(connection *PersistentConnection) *PacketQueue {
queue := &PacketQueue{
expectedPackages: make(map[uint32]*Listener),
}
@@ -42,24 +39,20 @@ func (p *PacketQueue) RemoveListeners() {
p.expectedPackages = make(map[uint32]*Listener)
}
func (p *PacketQueue) HandleConnection(connection net.Conn) error {
func (p *PacketQueue) HandleConnection(connection *PersistentConnection) error {
defer connection.Close()
defer p.RemoveListeners()
var packet Packet
reader := bufio.NewReader(connection)
connection.SetReadDeadline(time.Now().Add(time.Millisecond * 200))
for {
err := ReadPacket(reader, &packet)
if err != nil {
if err == io.EOF {
return nil
}
log.Printf("Error receiving packet: %v\n", err)
return err
return connection.HandleConnectionError(err)
}
if packet.Version != CurrentPacketVersion {
log.Printf("Incorrect packet version: %v\n", packet.Version)
return fmt.Errorf("incorrect packet version: %d", packet.Version)
return connection.HandleConnectionError(fmt.Errorf("incorrect packet version: %d", packet.Version))
}
if packet.DataLength == 0 {
go p.HandleData(packet.MessageType, CallResult{
@@ -71,7 +64,7 @@ func (p *PacketQueue) HandleConnection(connection net.Conn) error {
data, err := GetPacketData(reader, packet.DataLength)
if err != nil {
log.Printf("Error receiving packet data: %v\n", err)
return err
return connection.HandleConnectionError(err)
} else {
go p.HandleData(packet.MessageType, CallResult{
StatusCode: packet.StatusCode,