mirror of
https://github.com/onyx-and-iris/q3rcon.git
synced 2026-04-19 08:13:31 +00:00
remove internal packages, move everything into q3rcon
{request}.buf initialised with 0 length.
encoder, decoder interfaces added.
This commit is contained in:
53
conn.go
Normal file
53
conn.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package q3rcon
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"github.com/charmbracelet/log"
|
||||
)
|
||||
|
||||
type UDPConn struct {
|
||||
conn *net.UDPConn
|
||||
}
|
||||
|
||||
func newUDPConn(host string, port int) (*UDPConn, error) {
|
||||
udpAddr, err := net.ResolveUDPAddr("udp4", net.JoinHostPort(host, fmt.Sprintf("%d", port)))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
conn, err := net.DialUDP("udp4", nil, udpAddr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
log.Infof("Outgoing address %s", conn.RemoteAddr())
|
||||
|
||||
return &UDPConn{
|
||||
conn: conn,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *UDPConn) Write(buf []byte) (int, error) {
|
||||
n, err := c.conn.Write(buf)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (c *UDPConn) Read(buf []byte) (int, error) {
|
||||
rlen, _, err := c.conn.ReadFromUDP(buf)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return rlen, nil
|
||||
}
|
||||
|
||||
func (c *UDPConn) Close() error {
|
||||
err := c.conn.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user