sshpong/internal/netwrk/netwrk.go

75 lines
1.2 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{},
}
2024-08-14 23:33:05 -06:00
listener, err := net.Listen("tcp", "127.0.0.1:12345")
2024-08-02 13:04:41 -06:00
if err != nil {
log.Fatal(err)
}
defer listener.Close()
go func() {
for {
conn, err := listener.Accept()
2024-08-14 23:33:05 -06:00
log.Println("got a connection!")
2024-08-02 13:04:41 -06:00
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 23:33:05 -06:00
gameListener, err := net.Listen("tcp", "127.0.0.1:42069")
2024-08-14 17:26:51 -06:00
if err != nil {
log.Fatal(err)
}
defer gameListener.Close()
2024-08-14 23:33:05 -06:00
for {
conn, err := gameListener.Accept()
if err != nil {
log.Println(err)
continue
2024-08-08 18:08:55 -06:00
}
2024-08-14 23:33:05 -06:00
handleGameConnection(conn)
}
2024-08-06 09:58:43 -06:00
}