From 90a1177a63adaaebf9380b819f69a8137035bc35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Ryb=C3=A1rsky?= Date: Fri, 30 Aug 2024 21:20:41 +0200 Subject: [PATCH] Upgrade networking --- main.go | 26 +++++++++++++++++++------- player.go | 22 +++++++++++----------- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/main.go b/main.go index 410cfe9..b1b2002 100644 --- a/main.go +++ b/main.go @@ -143,7 +143,7 @@ var mapWindow *sdl.Window var mapSurface *sdl.Surface var mapRendererRect *sdl.Rect -var netPlayerMapper = map[*net.Conn]*Player{} +var netPlayerMapper = map[*net.TCPConn]*Player{} var clientInitialized = false @@ -175,13 +175,19 @@ func main() { bases = createBases(players, gameMap) } else { // Create a connection to the server - conn, err := net.Dial("tcp", config.Address) + addr, err := net.ResolveTCPAddr("tcp", config.Address) + + if err != nil { + log.Fatal("Error resolving address " + config.Address + ": " + err.Error()) + } + + conn, err := net.DialTCP("tcp", nil, addr) if err != nil { log.Fatalf("Failed to connect to server: %v", err) } defer conn.Close() - go handleConnectionClient(&conn, players, bases, bullets, bulletParticles) + go handleConnectionClient(conn, players, bases, bullets, bulletParticles) for !clientInitialized { time.Sleep(100 * time.Millisecond) } @@ -194,7 +200,13 @@ func main() { } if config.Server { - listen, err := net.Listen("tcp", "0.0.0.0:5074") + // Create a connection to the server + addr, err := net.ResolveTCPAddr("tcp", config.Address) + + if err != nil { + log.Fatal("Error resolving address " + config.Address + ": " + err.Error()) + } + listen, err := net.ListenTCP("tcp", addr) if err != nil { log.Fatal(err) } @@ -202,7 +214,7 @@ func main() { defer listen.Close() go func() { for { - conn, err := listen.Accept() + conn, err := listen.AcceptTCP() if err != nil { log.Fatal(err) } @@ -226,7 +238,6 @@ func main() { deltaTime := currentTime - prevTime prevTime = currentTime worldLock.Lock() - playerUpdateMutex.Lock() // Catch up in case of a large delta time for deltaTime > maxDeltaTime { @@ -282,7 +293,6 @@ func main() { resetMapProfilingCounters(&totalRenderTime, &totalScalingTime, &totalFrameTime, &frameCount) } worldLock.Unlock() - playerUpdateMutex.Unlock() enforceFrameRate(currentTime, 60) } } @@ -321,6 +331,7 @@ func runGameLogic(players map[uint32]*Player, gameMap *GameMap, bases map[uint32 } } + playerUpdateMutex.Lock() for _, bullet := range bullets { (*bullet).tick(gameMap, bulletParticles, bullets, players) } @@ -336,6 +347,7 @@ func runGameLogic(players map[uint32]*Player, gameMap *GameMap, bases map[uint32 for _, bulletParticle := range bulletParticles { bulletParticle.tick(bulletParticles) } + playerUpdateMutex.Unlock() } func initPlayer(playerIndex uint8, player *Player) { diff --git a/player.go b/player.go index 3006fa5..1d24beb 100644 --- a/player.go +++ b/player.go @@ -20,7 +20,7 @@ var lastPlayerID = uint32(0) type Player struct { local bool - connection *net.Conn + connection *net.TCPConn playerColors PlayerColors keyMap KeyMap joyMap JoyMap @@ -608,7 +608,7 @@ func closeThings(players map[uint32]*Player) { } } -func createPlayer(local bool, thisPlayerColors PlayerColors, conn *net.Conn, keyMap KeyMap, joyMap JoyMap, joyStick *sdl.Joystick, gameMap *GameMap, players map[uint32]*Player) uint32 { +func createPlayer(local bool, thisPlayerColors PlayerColors, conn *net.TCPConn, keyMap KeyMap, joyMap JoyMap, joyStick *sdl.Joystick, gameMap *GameMap, players map[uint32]*Player) uint32 { coordsAreValid := false var posX, posY int32 maxTries := 1000 @@ -795,7 +795,7 @@ func createPlayer(local bool, thisPlayerColors PlayerColors, conn *net.Conn, key return lastPlayerID - 1 } -func sendMessageRaw(data []byte, conn *net.Conn) (fail bool) { +func sendMessageRaw(data []byte, conn *net.TCPConn) (fail bool) { if conn != nil { // Send the length of the message first length := uint32(len(data)) @@ -1210,7 +1210,7 @@ func (player *Player) sendPositionToServer() bool { return false } -func sendVersionToServer(connection *net.Conn) { +func sendVersionToServer(connection *net.TCPConn) { if connection != nil && config.Client { response := tunnelerProto.ServerBound{ ServerBoundMessage: &tunnelerProto.ServerBound_PlayerStartRequest{ @@ -1273,7 +1273,7 @@ func (player *Player) sendShootToServer(isSuper bool) { } } -func handleRequest(conn net.Conn, players map[uint32]*Player, bullets map[uint32]*Bullet, bases map[uint32]*Base) { +func handleRequest(conn *net.TCPConn, players map[uint32]*Player, bullets map[uint32]*Bullet, bases map[uint32]*Base) { defer conn.Close() r := bufio.NewReader(conn) for { @@ -1311,7 +1311,7 @@ func handleRequest(conn net.Conn, players map[uint32]*Player, bullets map[uint32 return } - newPlayerID := createPlayer(false, playerColors[len(players)%len(playerColors)], &conn, KeyMap{}, JoyMap{}, nil, gameMap, players) + newPlayerID := createPlayer(false, playerColors[len(players)%len(playerColors)], conn, KeyMap{}, JoyMap{}, nil, gameMap, players) defer delete(players, newPlayerID) player := players[newPlayerID] bases[newPlayerID] = createBase(gameMap, @@ -1322,7 +1322,7 @@ func handleRequest(conn net.Conn, players map[uint32]*Player, bullets map[uint32 uint32(float64(player.gameObject.baseRect.W)*1.5)) defer bases[newPlayerID].delete(bases) - netPlayerMapper[&conn] = player + netPlayerMapper[conn] = player fail := player.sendVersionBackToPlayer() if fail { @@ -1346,7 +1346,7 @@ func handleRequest(conn net.Conn, players map[uint32]*Player, bullets map[uint32 log.Printf("Sent location to %d", newPlayerID) case *tunnelerProto.ServerBound_PlayerPosition: - player := netPlayerMapper[&conn] + player := netPlayerMapper[conn] if player == nil { return } @@ -1366,7 +1366,7 @@ func handleRequest(conn net.Conn, players map[uint32]*Player, bullets map[uint32 } player.gameObject.orientation = uint8(newPosition.Orientation) case *tunnelerProto.ServerBound_PlayerAction: - player := netPlayerMapper[&conn] + player := netPlayerMapper[conn] if player == nil { return } @@ -1389,10 +1389,10 @@ func handleRequest(conn net.Conn, players map[uint32]*Player, bullets map[uint32 } } -func handleConnectionClient(conn *net.Conn, players map[uint32]*Player, bases map[uint32]*Base, bullets map[uint32]*Bullet, bulletParticles map[uint32]*BulletParticle) { +func handleConnectionClient(conn *net.TCPConn, players map[uint32]*Player, bases map[uint32]*Base, bullets map[uint32]*Bullet, bulletParticles map[uint32]*BulletParticle) { var player *Player sendVersionToServer(conn) - r := bufio.NewReader(*conn) + r := bufio.NewReader(conn) for { // Read the length of the incoming message (4 bytes) lengthBuf := make([]byte, 4)