mirror of
https://github.com/onyx-and-iris/q3rcon.git
synced 2026-04-18 07:43:31 +00:00
move listen() into Rcon
rename channel variables
This commit is contained in:
@@ -1,19 +1,15 @@
|
||||
package conn
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/onyx-and-iris/q3rcon/internal/packet"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type UDPConn struct {
|
||||
conn *net.UDPConn
|
||||
response packet.Response
|
||||
conn *net.UDPConn
|
||||
}
|
||||
|
||||
func New(host string, port int) (UDPConn, error) {
|
||||
@@ -28,8 +24,7 @@ func New(host string, port int) (UDPConn, error) {
|
||||
log.Infof("Outgoing address %s", conn.RemoteAddr())
|
||||
|
||||
return UDPConn{
|
||||
conn: conn,
|
||||
response: packet.NewResponse(),
|
||||
conn: conn,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -42,39 +37,13 @@ func (c UDPConn) Write(buf []byte) (int, error) {
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (c UDPConn) Listen(timeout time.Duration, resp chan<- string, errChan chan<- error) {
|
||||
c.conn.SetReadDeadline(time.Now().Add(timeout))
|
||||
done := make(chan struct{})
|
||||
var sb strings.Builder
|
||||
buf := make([]byte, 2048)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-done:
|
||||
resp <- sb.String()
|
||||
return
|
||||
default:
|
||||
rlen, _, err := c.conn.ReadFromUDP(buf)
|
||||
if err != nil {
|
||||
e, ok := err.(net.Error)
|
||||
if ok {
|
||||
if e.Timeout() {
|
||||
close(done)
|
||||
} else {
|
||||
errChan <- e
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
if rlen < len(c.response.Header()) {
|
||||
continue
|
||||
}
|
||||
|
||||
if bytes.HasPrefix(buf, c.response.Header()) {
|
||||
sb.Write(buf[len(c.response.Header()):rlen])
|
||||
}
|
||||
}
|
||||
func (c UDPConn) ReadUntil(timeout time.Time, buf []byte) (int, error) {
|
||||
c.conn.SetReadDeadline(timeout)
|
||||
rlen, _, err := c.conn.ReadFromUDP(buf)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return rlen, nil
|
||||
}
|
||||
|
||||
func (c UDPConn) Close() error {
|
||||
|
||||
Reference in New Issue
Block a user