use persistent connections
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user