diff --git a/pkg/net/conn.go b/pkg/network/conn.go similarity index 99% rename from pkg/net/conn.go rename to pkg/network/conn.go index 6bf18db..5f8a6d2 100644 --- a/pkg/net/conn.go +++ b/pkg/network/conn.go @@ -1,8 +1,8 @@ -package net +package network import ( "bytes" - "cimeyclust.com/steel/pkg/net/packets" + "cimeyclust.com/steel/pkg/network/packets" "cimeyclust.com/steel/pkg/utils" "context" "fmt" @@ -379,7 +379,7 @@ func (conn *Conn) SetReadDeadline(t time.Time) error { } // SetWriteDeadline has no behaviour. It is merely there to satisfy the -// net.Conn interface. +// network.Conn interface. func (conn *Conn) SetWriteDeadline(time.Time) error { return nil } diff --git a/pkg/net/datagram_window.go b/pkg/network/datagram_window.go similarity index 99% rename from pkg/net/datagram_window.go rename to pkg/network/datagram_window.go index 6fe54ca..349c908 100644 --- a/pkg/net/datagram_window.go +++ b/pkg/network/datagram_window.go @@ -1,4 +1,4 @@ -package net +package network import ( "cimeyclust.com/steel/pkg/utils" diff --git a/pkg/net/err.go b/pkg/network/err.go similarity index 98% rename from pkg/net/err.go rename to pkg/network/err.go index 76efd31..093f808 100644 --- a/pkg/net/err.go +++ b/pkg/network/err.go @@ -1,4 +1,4 @@ -package net +package network import ( "errors" diff --git a/pkg/net/listener.go b/pkg/network/listener.go similarity index 94% rename from pkg/net/listener.go rename to pkg/network/listener.go index 95de00c..f6ab219 100644 --- a/pkg/net/listener.go +++ b/pkg/network/listener.go @@ -1,8 +1,9 @@ -package net +package network import ( "bytes" - "cimeyclust.com/steel/pkg/net/packets" + "cimeyclust.com/steel/pkg/cmd" + "cimeyclust.com/steel/pkg/network/packets" "fmt" "log/slog" "math" @@ -31,15 +32,11 @@ type ListenConfig struct { // Listener implements a RakNet connection listener. It follows the same // 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 { once sync.Once 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 // incoming is a channel of incoming connections. Connections that end up in // here will also end up in the connections map. @@ -86,12 +83,8 @@ func (l ListenConfig) Listen(address string) (*Listener, error) { conn: conn, incoming: make(chan *Conn), closed: make(chan struct{}), - log: slog.Default(), id: listenerID.Add(1), } - if l.ErrorLog != nil { - listener.log = l.ErrorLog - } go listener.listen() return listener, nil @@ -172,7 +165,7 @@ func (listener *Listener) listen() { // ownership has been taken by the buffer, but we can do this anyway // because we copy the data later. 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() } @@ -298,8 +291,17 @@ func (listener *Listener) handleUnconnectedPing(b *bytes.Buffer, addr net.Addr) if err := pk.Read(b); err != nil { return fmt.Errorf("error reading unconnected ping: %v", err) } + cmd.Logger.Info(fmt.Sprintf("Buffer length: %v", b.Len())) 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) _, err := listener.conn.WriteTo(b.Bytes(), addr) return err diff --git a/pkg/net/network.go b/pkg/network/network.go similarity index 89% rename from pkg/net/network.go rename to pkg/network/network.go index 67294eb..37b5ef2 100644 --- a/pkg/net/network.go +++ b/pkg/network/network.go @@ -1,4 +1,4 @@ -package net +package network import ( "cimeyclust.com/steel/pkg/cmd" @@ -34,9 +34,7 @@ func Run(baseCtx context.Context, addr string) { listener.Close() }() for { - cmd.Logger.Info("Waiting for connection...") conn, err := listener.Accept() - cmd.Logger.Info("Connection accepted") if err != nil { select { case <-ctx.Done(): @@ -61,8 +59,9 @@ func handleRequest(conn net.Conn) { // Close the connection when you're done with it. defer conn.Close() + cmd.Logger.Info("Tet") 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)) - _, _ = conn.Write([]byte{1, 2, 3}) } diff --git a/pkg/net/packet_queue.go b/pkg/network/packet_queue.go similarity index 98% rename from pkg/net/packet_queue.go rename to pkg/network/packet_queue.go index 42749bb..472aa4a 100644 --- a/pkg/net/packet_queue.go +++ b/pkg/network/packet_queue.go @@ -1,4 +1,4 @@ -package net +package network import "cimeyclust.com/steel/pkg/utils" diff --git a/pkg/net/packets/connected_ping.go b/pkg/network/packets/connected_ping.go similarity index 100% rename from pkg/net/packets/connected_ping.go rename to pkg/network/packets/connected_ping.go diff --git a/pkg/net/packets/connected_pong.go b/pkg/network/packets/connected_pong.go similarity index 100% rename from pkg/net/packets/connected_pong.go rename to pkg/network/packets/connected_pong.go diff --git a/pkg/net/packets/connection_request.go b/pkg/network/packets/connection_request.go similarity index 100% rename from pkg/net/packets/connection_request.go rename to pkg/network/packets/connection_request.go diff --git a/pkg/net/packets/connection_request_accepted.go b/pkg/network/packets/connection_request_accepted.go similarity index 100% rename from pkg/net/packets/connection_request_accepted.go rename to pkg/network/packets/connection_request_accepted.go diff --git a/pkg/net/packets/incompatible_protocol_version.go b/pkg/network/packets/incompatible_protocol_version.go similarity index 100% rename from pkg/net/packets/incompatible_protocol_version.go rename to pkg/network/packets/incompatible_protocol_version.go diff --git a/pkg/net/packets/new_incoming_connection.go b/pkg/network/packets/new_incoming_connection.go similarity index 100% rename from pkg/net/packets/new_incoming_connection.go rename to pkg/network/packets/new_incoming_connection.go diff --git a/pkg/net/packets/open_connection_reply_1.go b/pkg/network/packets/open_connection_reply_1.go similarity index 100% rename from pkg/net/packets/open_connection_reply_1.go rename to pkg/network/packets/open_connection_reply_1.go diff --git a/pkg/net/packets/open_connection_reply_2.go b/pkg/network/packets/open_connection_reply_2.go similarity index 100% rename from pkg/net/packets/open_connection_reply_2.go rename to pkg/network/packets/open_connection_reply_2.go diff --git a/pkg/net/packets/open_connection_request_1.go b/pkg/network/packets/open_connection_request_1.go similarity index 100% rename from pkg/net/packets/open_connection_request_1.go rename to pkg/network/packets/open_connection_request_1.go diff --git a/pkg/net/packets/open_connection_request_2.go b/pkg/network/packets/open_connection_request_2.go similarity index 100% rename from pkg/net/packets/open_connection_request_2.go rename to pkg/network/packets/open_connection_request_2.go diff --git a/pkg/net/packets/packet.go b/pkg/network/packets/packet.go similarity index 100% rename from pkg/net/packets/packet.go rename to pkg/network/packets/packet.go diff --git a/pkg/net/packets/unconnected_ping.go b/pkg/network/packets/unconnected_ping.go similarity index 100% rename from pkg/net/packets/unconnected_ping.go rename to pkg/network/packets/unconnected_ping.go diff --git a/pkg/net/packets/unconnected_pong.go b/pkg/network/packets/unconnected_pong.go similarity index 100% rename from pkg/net/packets/unconnected_pong.go rename to pkg/network/packets/unconnected_pong.go diff --git a/pkg/net/resend_map.go b/pkg/network/resend_map.go similarity index 97% rename from pkg/net/resend_map.go rename to pkg/network/resend_map.go index 60bb6fb..3c4d3a8 100644 --- a/pkg/net/resend_map.go +++ b/pkg/network/resend_map.go @@ -1,7 +1,7 @@ -package net +package network import ( - "cimeyclust.com/steel/pkg/net/packets" + "cimeyclust.com/steel/pkg/network/packets" "cimeyclust.com/steel/pkg/utils" "time" ) diff --git a/steel.go b/steel.go index 38b9d44..55e5c49 100644 --- a/steel.go +++ b/steel.go @@ -2,7 +2,7 @@ package main import ( "cimeyclust.com/steel/pkg/cmd" - "cimeyclust.com/steel/pkg/net" + "cimeyclust.com/steel/pkg/network" "context" "fmt" "os" @@ -36,7 +36,7 @@ func main() { go func() { defer wg.Done() - net.Run(ctx, "localhost:19132") + network.Run(ctx, "0.0.0.0:19132") }() // Handles Ctrl+C