This commit is contained in:
Verox007 2023-12-19 20:49:11 +01:00
parent 156b005a0b
commit 8a7ccc8fd9
21 changed files with 28 additions and 27 deletions

View File

@ -1,8 +1,8 @@
package net package network
import ( import (
"bytes" "bytes"
"cimeyclust.com/steel/pkg/net/packets" "cimeyclust.com/steel/pkg/network/packets"
"cimeyclust.com/steel/pkg/utils" "cimeyclust.com/steel/pkg/utils"
"context" "context"
"fmt" "fmt"
@ -379,7 +379,7 @@ func (conn *Conn) SetReadDeadline(t time.Time) error {
} }
// SetWriteDeadline has no behaviour. It is merely there to satisfy the // SetWriteDeadline has no behaviour. It is merely there to satisfy the
// net.Conn interface. // network.Conn interface.
func (conn *Conn) SetWriteDeadline(time.Time) error { func (conn *Conn) SetWriteDeadline(time.Time) error {
return nil return nil
} }

View File

@ -1,4 +1,4 @@
package net package network
import ( import (
"cimeyclust.com/steel/pkg/utils" "cimeyclust.com/steel/pkg/utils"

View File

@ -1,4 +1,4 @@
package net package network
import ( import (
"errors" "errors"

View File

@ -1,8 +1,9 @@
package net package network
import ( import (
"bytes" "bytes"
"cimeyclust.com/steel/pkg/net/packets" "cimeyclust.com/steel/pkg/cmd"
"cimeyclust.com/steel/pkg/network/packets"
"fmt" "fmt"
"log/slog" "log/slog"
"math" "math"
@ -31,15 +32,11 @@ type ListenConfig struct {
// Listener implements a RakNet connection listener. It follows the same // Listener implements a RakNet connection listener. It follows the same
// methods as those implemented by the TCPListener in the net package. Listener // methods as those implemented by the TCPListener in the net package. Listener
// implements the net.Listener interface. // implements the network.Listener interface.
type Listener struct { type Listener struct {
once sync.Once once sync.Once
closed chan struct{} closed chan struct{}
// log is a logger that errors from packet decoding are logged to. It may be
// set to a logger that simply discards the messages.
log *slog.Logger
conn net.PacketConn conn net.PacketConn
// incoming is a channel of incoming connections. Connections that end up in // incoming is a channel of incoming connections. Connections that end up in
// here will also end up in the connections map. // here will also end up in the connections map.
@ -86,12 +83,8 @@ func (l ListenConfig) Listen(address string) (*Listener, error) {
conn: conn, conn: conn,
incoming: make(chan *Conn), incoming: make(chan *Conn),
closed: make(chan struct{}), closed: make(chan struct{}),
log: slog.Default(),
id: listenerID.Add(1), id: listenerID.Add(1),
} }
if l.ErrorLog != nil {
listener.log = l.ErrorLog
}
go listener.listen() go listener.listen()
return listener, nil return listener, nil
@ -172,7 +165,7 @@ func (listener *Listener) listen() {
// ownership has been taken by the buffer, but we can do this anyway // ownership has been taken by the buffer, but we can do this anyway
// because we copy the data later. // because we copy the data later.
if err := listener.handle(buf, addr); err != nil { if err := listener.handle(buf, addr); err != nil {
listener.log.Error("listener: handle packet: "+err.Error(), "address", addr.String()) cmd.Logger.Error(fmt.Sprintf("listener: handle packet: %v from %s", err, addr.String()))
} }
buf.Reset() buf.Reset()
} }
@ -298,8 +291,17 @@ func (listener *Listener) handleUnconnectedPing(b *bytes.Buffer, addr net.Addr)
if err := pk.Read(b); err != nil { if err := pk.Read(b); err != nil {
return fmt.Errorf("error reading unconnected ping: %v", err) return fmt.Errorf("error reading unconnected ping: %v", err)
} }
cmd.Logger.Info(fmt.Sprintf("Buffer length: %v", b.Len()))
b.Reset() b.Reset()
cmd.Logger.Info(fmt.Sprintf("Received unconnected ping from %s", addr.String()))
// Log pongData if it is set.
if data := listener.pongData.Load(); data != nil {
cmd.Logger.Info(fmt.Sprintf("Pong data: %v", data))
} else {
cmd.Logger.Info("No pong data set")
}
cmd.Logger.Info(fmt.Sprintf("Ping data: "))
(&packets.UnconnectedPong{ServerGUID: listener.id, SendTimestamp: pk.SendTimestamp, Data: *listener.pongData.Load()}).Write(b) (&packets.UnconnectedPong{ServerGUID: listener.id, SendTimestamp: pk.SendTimestamp, Data: *listener.pongData.Load()}).Write(b)
_, err := listener.conn.WriteTo(b.Bytes(), addr) _, err := listener.conn.WriteTo(b.Bytes(), addr)
return err return err

View File

@ -1,4 +1,4 @@
package net package network
import ( import (
"cimeyclust.com/steel/pkg/cmd" "cimeyclust.com/steel/pkg/cmd"
@ -34,9 +34,7 @@ func Run(baseCtx context.Context, addr string) {
listener.Close() listener.Close()
}() }()
for { for {
cmd.Logger.Info("Waiting for connection...")
conn, err := listener.Accept() conn, err := listener.Accept()
cmd.Logger.Info("Connection accepted")
if err != nil { if err != nil {
select { select {
case <-ctx.Done(): case <-ctx.Done():
@ -61,8 +59,9 @@ func handleRequest(conn net.Conn) {
// Close the connection when you're done with it. // Close the connection when you're done with it.
defer conn.Close() defer conn.Close()
cmd.Logger.Info("Tet")
b := make([]byte, 1024*1024*4) b := make([]byte, 1024*1024*4)
_, _ = conn.Read(b) n, _ := conn.Read(b)
cmd.Logger.Info(fmt.Sprintf("Current n: %v", n))
cmd.Logger.Info(fmt.Sprintf("Received: %v", b)) cmd.Logger.Info(fmt.Sprintf("Received: %v", b))
_, _ = conn.Write([]byte{1, 2, 3})
} }

View File

@ -1,4 +1,4 @@
package net package network
import "cimeyclust.com/steel/pkg/utils" import "cimeyclust.com/steel/pkg/utils"

View File

@ -1,7 +1,7 @@
package net package network
import ( import (
"cimeyclust.com/steel/pkg/net/packets" "cimeyclust.com/steel/pkg/network/packets"
"cimeyclust.com/steel/pkg/utils" "cimeyclust.com/steel/pkg/utils"
"time" "time"
) )

View File

@ -2,7 +2,7 @@ package main
import ( import (
"cimeyclust.com/steel/pkg/cmd" "cimeyclust.com/steel/pkg/cmd"
"cimeyclust.com/steel/pkg/net" "cimeyclust.com/steel/pkg/network"
"context" "context"
"fmt" "fmt"
"os" "os"
@ -36,7 +36,7 @@ func main() {
go func() { go func() {
defer wg.Done() defer wg.Done()
net.Run(ctx, "localhost:19132") network.Run(ctx, "0.0.0.0:19132")
}() }()
// Handles Ctrl+C // Handles Ctrl+C