always connect
All checks were successful
Build and Publish / BuildAndDeploy (push) Successful in 1m51s
All checks were successful
Build and Publish / BuildAndDeploy (push) Successful in 1m51s
This commit is contained in:
@@ -12,12 +12,13 @@ type Client struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Dial(address string) (*Client, error) {
|
func Dial(address string) (*Client, error) {
|
||||||
conn, err := net.Dial("tcp", address)
|
|
||||||
|
mux, err := NewTCPClientMux(address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
client := &Client{
|
client := &Client{
|
||||||
TCPClientMux: NewTCPClientMux(conn),
|
TCPClientMux: mux,
|
||||||
}
|
}
|
||||||
return client, nil
|
return client, nil
|
||||||
}
|
}
|
||||||
@@ -28,14 +29,40 @@ func (c *Client) Close() {
|
|||||||
|
|
||||||
type TCPClientMux struct {
|
type TCPClientMux struct {
|
||||||
net.Conn
|
net.Conn
|
||||||
|
Errors chan error
|
||||||
|
ErrorCount int
|
||||||
|
address string
|
||||||
*PacketQueue
|
*PacketQueue
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTCPClientMux(connection net.Conn) *TCPClientMux {
|
func NewTCPClientMux(address string) (*TCPClientMux, error) {
|
||||||
return &TCPClientMux{
|
connection, err := net.Dial("tcp", address)
|
||||||
Conn: connection,
|
if err != nil {
|
||||||
PacketQueue: NewPacketQueue(connection),
|
return nil, err
|
||||||
}
|
}
|
||||||
|
return &TCPClientMux{
|
||||||
|
Errors: make(chan error),
|
||||||
|
ErrorCount: 0,
|
||||||
|
Conn: connection,
|
||||||
|
address: address,
|
||||||
|
PacketQueue: NewPacketQueue(connection),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *TCPClientMux) Connect() error {
|
||||||
|
if m.Conn == nil {
|
||||||
|
connection, err := net.Dial("tcp", m.address)
|
||||||
|
if err != nil {
|
||||||
|
|
||||||
|
m.Errors <- err
|
||||||
|
m.ErrorCount++
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
m.ErrorCount = 0
|
||||||
|
m.Conn = connection
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *TCPClientMux) Close() {
|
func (m *TCPClientMux) Close() {
|
||||||
@@ -43,7 +70,11 @@ func (m *TCPClientMux) Close() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *TCPClientMux) SendPacket(messageType uint16, data []byte) error {
|
func (m *TCPClientMux) SendPacket(messageType uint16, data []byte) error {
|
||||||
err := binary.Write(m.Conn, binary.LittleEndian, Packet{
|
err := m.Connect()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = binary.Write(m.Conn, binary.LittleEndian, Packet{
|
||||||
Version: 1,
|
Version: 1,
|
||||||
MessageType: messageType,
|
MessageType: messageType,
|
||||||
DataLength: uint16(len(data)),
|
DataLength: uint16(len(data)),
|
||||||
|
|||||||
Reference in New Issue
Block a user