use persistent connections
This commit is contained in:
@@ -3,7 +3,7 @@ package main
|
||||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"net"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -28,76 +28,51 @@ func (c *Client) Close() {
|
||||
}
|
||||
|
||||
type CartTCPClient struct {
|
||||
net.Conn
|
||||
*PersistentConnection
|
||||
ErrorCount int
|
||||
address string
|
||||
*CartPacketQueue
|
||||
}
|
||||
|
||||
func NewCartTCPClient(address string) (*CartTCPClient, error) {
|
||||
connection, err := net.Dial("tcp", address)
|
||||
connection, err := NewPersistentConnection(address)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &CartTCPClient{
|
||||
ErrorCount: 0,
|
||||
Conn: connection,
|
||||
address: address,
|
||||
CartPacketQueue: NewCartPacketQueue(connection),
|
||||
ErrorCount: 0,
|
||||
PersistentConnection: connection,
|
||||
address: address,
|
||||
CartPacketQueue: NewCartPacketQueue(connection),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (m *CartTCPClient) Connect() error {
|
||||
if m.Conn == nil {
|
||||
connection, err := net.Dial("tcp", m.address)
|
||||
if err != nil {
|
||||
m.ErrorCount++
|
||||
return err
|
||||
}
|
||||
m.ErrorCount = 0
|
||||
m.Conn = connection
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *CartTCPClient) SendPacket(messageType uint32, id CartId, data []byte) error {
|
||||
err := m.Connect()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = binary.Write(m.Conn, binary.LittleEndian, CartPacket{
|
||||
err := binary.Write(m.Conn, binary.LittleEndian, CartPacket{
|
||||
Version: CurrentPacketVersion,
|
||||
MessageType: messageType,
|
||||
DataLength: uint32(len(data)),
|
||||
Id: id,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
return m.HandleConnectionError(err)
|
||||
}
|
||||
_, err = m.Conn.Write(data)
|
||||
m.Conn.SetDeadline(time.Now().Add(time.Second * 10))
|
||||
return err
|
||||
return m.HandleConnectionError(err)
|
||||
}
|
||||
|
||||
// func (m *CartTCPClient) SendPacketFn(messageType uint16, id CartId, datafn func(w io.Writer) error) error {
|
||||
// data, err := GetData(datafn)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// return m.SendPacket(messageType, id, data)
|
||||
// }
|
||||
|
||||
func (m *CartTCPClient) Call(messageType uint32, id CartId, responseType uint32, data []byte) (*CallResult, error) {
|
||||
packetChan := m.Expect(responseType, id)
|
||||
err := m.SendPacket(messageType, id, data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, m.HandleConnectionError(err)
|
||||
}
|
||||
select {
|
||||
case ret := <-packetChan:
|
||||
return &ret, nil
|
||||
case <-time.After(time.Second * 10):
|
||||
return nil, fmt.Errorf("timeout")
|
||||
case <-time.After(time.Second):
|
||||
log.Printf("Timeout waiting for cart response to message type %d\n", responseType)
|
||||
return nil, m.HandleConnectionError(fmt.Errorf("timeout"))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user