Upgrade networking

This commit is contained in:
Bruno Rybársky 2024-08-30 21:20:41 +02:00
parent 728a1de2e5
commit 90a1177a63
2 changed files with 30 additions and 18 deletions

26
main.go

@ -143,7 +143,7 @@ var mapWindow *sdl.Window
var mapSurface *sdl.Surface var mapSurface *sdl.Surface
var mapRendererRect *sdl.Rect var mapRendererRect *sdl.Rect
var netPlayerMapper = map[*net.Conn]*Player{} var netPlayerMapper = map[*net.TCPConn]*Player{}
var clientInitialized = false var clientInitialized = false
@ -175,13 +175,19 @@ func main() {
bases = createBases(players, gameMap) bases = createBases(players, gameMap)
} else { } else {
// Create a connection to the server // 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 { if err != nil {
log.Fatalf("Failed to connect to server: %v", err) log.Fatalf("Failed to connect to server: %v", err)
} }
defer conn.Close() defer conn.Close()
go handleConnectionClient(&conn, players, bases, bullets, bulletParticles) go handleConnectionClient(conn, players, bases, bullets, bulletParticles)
for !clientInitialized { for !clientInitialized {
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
} }
@ -194,7 +200,13 @@ func main() {
} }
if config.Server { 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 { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -202,7 +214,7 @@ func main() {
defer listen.Close() defer listen.Close()
go func() { go func() {
for { for {
conn, err := listen.Accept() conn, err := listen.AcceptTCP()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -226,7 +238,6 @@ func main() {
deltaTime := currentTime - prevTime deltaTime := currentTime - prevTime
prevTime = currentTime prevTime = currentTime
worldLock.Lock() worldLock.Lock()
playerUpdateMutex.Lock()
// Catch up in case of a large delta time // Catch up in case of a large delta time
for deltaTime > maxDeltaTime { for deltaTime > maxDeltaTime {
@ -282,7 +293,6 @@ func main() {
resetMapProfilingCounters(&totalRenderTime, &totalScalingTime, &totalFrameTime, &frameCount) resetMapProfilingCounters(&totalRenderTime, &totalScalingTime, &totalFrameTime, &frameCount)
} }
worldLock.Unlock() worldLock.Unlock()
playerUpdateMutex.Unlock()
enforceFrameRate(currentTime, 60) enforceFrameRate(currentTime, 60)
} }
} }
@ -321,6 +331,7 @@ func runGameLogic(players map[uint32]*Player, gameMap *GameMap, bases map[uint32
} }
} }
playerUpdateMutex.Lock()
for _, bullet := range bullets { for _, bullet := range bullets {
(*bullet).tick(gameMap, bulletParticles, bullets, players) (*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 { for _, bulletParticle := range bulletParticles {
bulletParticle.tick(bulletParticles) bulletParticle.tick(bulletParticles)
} }
playerUpdateMutex.Unlock()
} }
func initPlayer(playerIndex uint8, player *Player) { func initPlayer(playerIndex uint8, player *Player) {

@ -20,7 +20,7 @@ var lastPlayerID = uint32(0)
type Player struct { type Player struct {
local bool local bool
connection *net.Conn connection *net.TCPConn
playerColors PlayerColors playerColors PlayerColors
keyMap KeyMap keyMap KeyMap
joyMap JoyMap 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 coordsAreValid := false
var posX, posY int32 var posX, posY int32
maxTries := 1000 maxTries := 1000
@ -795,7 +795,7 @@ func createPlayer(local bool, thisPlayerColors PlayerColors, conn *net.Conn, key
return lastPlayerID - 1 return lastPlayerID - 1
} }
func sendMessageRaw(data []byte, conn *net.Conn) (fail bool) { func sendMessageRaw(data []byte, conn *net.TCPConn) (fail bool) {
if conn != nil { if conn != nil {
// Send the length of the message first // Send the length of the message first
length := uint32(len(data)) length := uint32(len(data))
@ -1210,7 +1210,7 @@ func (player *Player) sendPositionToServer() bool {
return false return false
} }
func sendVersionToServer(connection *net.Conn) { func sendVersionToServer(connection *net.TCPConn) {
if connection != nil && config.Client { if connection != nil && config.Client {
response := tunnelerProto.ServerBound{ response := tunnelerProto.ServerBound{
ServerBoundMessage: &tunnelerProto.ServerBound_PlayerStartRequest{ 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() defer conn.Close()
r := bufio.NewReader(conn) r := bufio.NewReader(conn)
for { for {
@ -1311,7 +1311,7 @@ func handleRequest(conn net.Conn, players map[uint32]*Player, bullets map[uint32
return 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) defer delete(players, newPlayerID)
player := players[newPlayerID] player := players[newPlayerID]
bases[newPlayerID] = createBase(gameMap, 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)) uint32(float64(player.gameObject.baseRect.W)*1.5))
defer bases[newPlayerID].delete(bases) defer bases[newPlayerID].delete(bases)
netPlayerMapper[&conn] = player netPlayerMapper[conn] = player
fail := player.sendVersionBackToPlayer() fail := player.sendVersionBackToPlayer()
if fail { 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) log.Printf("Sent location to %d", newPlayerID)
case *tunnelerProto.ServerBound_PlayerPosition: case *tunnelerProto.ServerBound_PlayerPosition:
player := netPlayerMapper[&conn] player := netPlayerMapper[conn]
if player == nil { if player == nil {
return return
} }
@ -1366,7 +1366,7 @@ func handleRequest(conn net.Conn, players map[uint32]*Player, bullets map[uint32
} }
player.gameObject.orientation = uint8(newPosition.Orientation) player.gameObject.orientation = uint8(newPosition.Orientation)
case *tunnelerProto.ServerBound_PlayerAction: case *tunnelerProto.ServerBound_PlayerAction:
player := netPlayerMapper[&conn] player := netPlayerMapper[conn]
if player == nil { if player == nil {
return 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 var player *Player
sendVersionToServer(conn) sendVersionToServer(conn)
r := bufio.NewReader(*conn) r := bufio.NewReader(conn)
for { for {
// Read the length of the incoming message (4 bytes) // Read the length of the incoming message (4 bytes)
lengthBuf := make([]byte, 4) lengthBuf := make([]byte, 4)