sshpong/internal/netwrk/netwrk.go

76 lines
1.1 KiB
Go
Raw Normal View History

2024-08-02 13:04:41 -06:00
package netwrk
import (
"log"
"net"
)
type Client struct {
2024-08-06 09:58:43 -06:00
name string
conn net.Conn
ready bool
2024-08-02 13:04:41 -06:00
}
type ClientPool struct {
clients map[string]Client
}
type GameClients struct {
2024-08-09 08:48:12 -06:00
client1 chan GameMessage
client2 chan GameMessage
2024-08-02 13:04:41 -06:00
}
2024-08-09 08:48:12 -06:00
type GameChans struct {
2024-08-02 13:04:41 -06:00
games map[string]GameClients
}
var clientPool *ClientPool
2024-08-09 08:48:12 -06:00
var gameChans *GameChans
2024-08-02 13:04:41 -06:00
2024-08-05 18:00:41 -06:00
// Starts listening on port 12345 for TCP connections
// Also creates client pool and game connection singletons
2024-08-02 13:04:41 -06:00
func Listen() {
clientPool = &ClientPool{
clients: map[string]Client{},
}
listener, err := net.Listen("tcp", ":12345")
if err != nil {
log.Fatal(err)
}
defer listener.Close()
go func() {
for {
conn, err := listener.Accept()
if err != nil {
log.Println(err)
continue
}
2024-08-06 09:58:43 -06:00
go handleLobbyConnection(conn)
}
}()
2024-08-08 18:08:55 -06:00
2024-08-09 08:48:12 -06:00
gameChans = &GameChans{
games: map[string]GameClients{},
}
2024-08-14 17:26:51 -06:00
gameListener, err := net.Listen("tcp", ":42069")
if err != nil {
log.Fatal(err)
}
defer gameListener.Close()
2024-08-08 18:08:55 -06:00
go func() {
for {
2024-08-14 17:26:51 -06:00
conn, err := gameListener.Accept()
2024-08-08 18:08:55 -06:00
if err != nil {
log.Println(err)
continue
}
2024-08-09 08:48:12 -06:00
handleGameConnection(conn)
2024-08-08 18:08:55 -06:00
}
2024-08-09 08:48:12 -06:00
}()
2024-08-06 09:58:43 -06:00
}