sshpong/internal/netwrk/netwrk.go
2024-08-14 23:33:05 -06:00

75 lines
1.2 KiB
Go

package netwrk
import (
"log"
"net"
)
type Client struct {
name string
conn net.Conn
ready bool
}
type ClientPool struct {
clients map[string]Client
}
type GameClients struct {
client1 chan GameMessage
client2 chan GameMessage
}
type GameChans struct {
games map[string]GameClients
}
var clientPool *ClientPool
var gameChans *GameChans
// Starts listening on port 12345 for TCP connections
// Also creates client pool and game connection singletons
func Listen() {
clientPool = &ClientPool{
clients: map[string]Client{},
}
listener, err := net.Listen("tcp", "127.0.0.1:12345")
if err != nil {
log.Fatal(err)
}
defer listener.Close()
go func() {
for {
conn, err := listener.Accept()
log.Println("got a connection!")
if err != nil {
log.Println(err)
continue
}
go handleLobbyConnection(conn)
}
}()
gameChans = &GameChans{
games: map[string]GameClients{},
}
gameListener, err := net.Listen("tcp", "127.0.0.1:42069")
if err != nil {
log.Fatal(err)
}
defer gameListener.Close()
for {
conn, err := gameListener.Accept()
if err != nil {
log.Println(err)
continue
}
handleGameConnection(conn)
}
}