sshpong/cmd/server/main.go

140 lines
2.8 KiB
Go
Raw Normal View History

2024-08-03 11:05:14 -06:00
package main
import (
"fmt"
2024-09-26 19:49:59 -06:00
"log"
"log/slog"
"net"
2024-10-03 22:41:33 -06:00
"os"
"sshpong/internal/config"
2024-09-26 19:49:59 -06:00
"sshpong/internal/lobby"
2024-10-03 22:41:33 -06:00
"sshpong/internal/pong"
"strings"
2024-09-26 19:49:59 -06:00
"sync"
2024-08-03 11:05:14 -06:00
)
2024-08-15 20:08:23 -06:00
var exit chan bool
2024-09-26 19:49:59 -06:00
var games sync.Map
2024-08-15 20:08:23 -06:00
2024-08-03 11:05:14 -06:00
func main() {
2024-10-03 22:41:33 -06:00
if len(os.Args) == 1 {
config.LoadConfig("")
} else {
config.LoadConfig(os.Args[1])
}
slog.SetLogLoggerLevel(slog.Level(config.Config.LogLevel))
2024-09-26 19:49:59 -06:00
fmt.Println("Starting sshpong lobby...")
go LobbyListen()
fmt.Println("Lobby started")
2024-08-03 11:05:14 -06:00
2024-10-03 22:41:33 -06:00
fmt.Println("Starting game listener...")
go GamesListen()
fmt.Println("Game listener started")
2024-08-14 23:33:05 -06:00
2024-08-15 20:08:23 -06:00
_ = <-exit
2024-08-03 11:05:14 -06:00
}
2024-09-26 19:49:59 -06:00
// Starts listening on port 12345 for TCP connections
// Also creates client pool and game connection singletons
func LobbyListen() {
listener, err := net.Listen("tcp", "127.0.0.1:12345")
if err != nil {
2024-10-03 22:41:33 -06:00
slog.Error("Error setting up listener for lobby. Exiting...", err)
2024-09-26 19:49:59 -06:00
}
defer listener.Close()
l := lobby.CreateLobby()
for {
conn, err := listener.Accept()
if err != nil {
log.Println(err)
continue
}
2024-09-28 23:26:06 -06:00
go func() {
2024-10-24 18:15:39 -06:00
client, msgOut, err := l.InitialConnectionHandler(conn)
if err != nil {
conn.Write(msgOut)
return
}
2024-09-28 23:26:06 -06:00
_, err = conn.Write(msgOut)
if err != nil {
slog.Debug("error writing to new player... disconnecting")
msg, err := lobby.Marshal(lobby.DisconnectData{
From: client.Username,
}, lobby.Disconnect)
if err != nil {
slog.Error("error marshalling disconnect message on player connect")
}
l.BroadcastToLobby(msg)
}
go l.HandleLobbyConnection(client)
}()
2024-09-26 19:49:59 -06:00
}
}
2024-10-03 22:41:33 -06:00
func GamesListen() {
type GameClients struct {
Client1 lobby.Client
Client2 lobby.Client
}
gameListener, err := net.Listen("tcp", "127.0.0.1:42069")
if err != nil {
log.Fatal(err)
}
2024-10-24 17:52:14 -06:00
defer gameListener.Close()
2024-10-03 22:41:33 -06:00
for {
conn, err := gameListener.Accept()
if err != nil {
log.Println(err)
continue
}
slog.Debug("Received game connection")
go func(conn net.Conn) {
messageBytes := make([]byte, 126)
n, err := conn.Read(messageBytes)
if err != nil {
log.Printf("Error reading game ID on connection %s", err)
}
gInfo := strings.SplitAfter(string(messageBytes[:n]), ":")
if err != nil {
log.Printf("Game id was not a string? %s", err)
}
slog.Debug("Game request data", slog.Any("game info", gInfo))
game, ok := games.Load(gInfo[0])
if !ok {
games.Store(gInfo[0], GameClients{Client1: lobby.Client{
Username: gInfo[1],
Conn: conn,
}, Client2: lobby.Client{}})
} else {
gameclients, _ := game.(GameClients)
client2 := lobby.Client{
Username: gInfo[1],
Conn: conn,
}
games.Store(gInfo[0], GameClients{
Client1: gameclients.Client1,
Client2: client2})
go pong.StartGame(gameclients.Client1.Conn, client2.Conn, gameclients.Client1.Username, client2.Username)
}
}(conn)
}
}