initial commit

This commit is contained in:
Beric Bearnson 2024-08-02 13:04:41 -06:00
commit 28e5a1ba78
5 changed files with 158 additions and 0 deletions

3
client/go.mod Normal file
View File

@ -0,0 +1,3 @@
module pongclient
go 1.22.1

5
server/go.mod Normal file
View File

@ -0,0 +1,5 @@
module sshpong
go 1.22.1
require github.com/google/uuid v1.6.0

2
server/go.sum Normal file
View File

@ -0,0 +1,2 @@
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=

5
server/main.go Normal file
View File

@ -0,0 +1,5 @@
package main
func main() {
}

143
server/netwrk/netwrk.go Normal file
View File

@ -0,0 +1,143 @@
package netwrk
import (
"fmt"
"log"
"net"
"time"
"github.com/google/uuid"
)
type Client struct {
name string
conn net.Conn
}
type ClientPool struct {
clients map[string]Client
}
type GameClients struct {
client1 Client
client2 Client
}
type GameConnections struct {
games map[string]GameClients
}
var clientPool *ClientPool
var gameConnections *GameConnections
func Listen() {
clientPool = &ClientPool{
clients: map[string]Client{},
}
gameConnections = &GameConnections{
games: map[string]GameClients{},
}
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
}
name := make([]byte, 1024)
n, err := conn.Read(name)
if err != nil {
log.Println(fmt.Sprintf("Failed to read from connection: %s", conn.LocalAddr()))
} else {
clientPool.clients[uuid.New().String()] = Client{
name: string(name[:n]),
conn: conn,
}
}
}
}()
}
func GetPool() (map[string]Client, bool) {
if clientPool.clients != nil {
return clientPool.clients, true
}
return clientPool.clients, false
}
func CreateGame(clientID1, clientID2 string) (string, error) {
client1, ok := clientPool.clients[clientID1]
if !ok {
return "", fmt.Errorf("Client 1 was not found in client pool :(")
}
if err := client1.conn.SetWriteDeadline(time.Time{}); err != nil {
return "", fmt.Errorf("Client 1 was not responsive")
}
client2, ok := clientPool.clients[clientID2]
if !ok {
return "", fmt.Errorf("Client 2 was not found in client pool :(")
}
if err := client2.conn.SetWriteDeadline(time.Time{}); err != nil {
return "", fmt.Errorf("Client 2 was not responsive")
}
gameID := uuid.New().String()
gameConnections.games[gameID] = GameClients{
client1: client1,
client2: client2,
}
delete(clientPool.clients, clientID1)
delete(clientPool.clients, clientID2)
return gameID, nil
}
func SendGameUpdateToClients(gameID string, bytes []byte) error {
clients, ok := gameConnections.games[gameID]
if !ok {
return fmt.Errorf("Could not find game clients record")
}
_, err := clients.client1.conn.Write(bytes)
if err != nil {
return fmt.Errorf("Could not write to client1 connection")
}
_, err = clients.client1.conn.Write(bytes)
if err != nil {
return fmt.Errorf("Could not write to client2 connection")
}
return nil
}
func PingAndCleanLobbyClients() {
ping := []byte("ping")
deadClients := []string{}
for id, client := range clientPool.clients {
client.conn.SetWriteDeadline(time.Now().Add(time.Second))
_, err := client.conn.Write(ping)
if err != nil {
log.Println("Could not write to client, deleting connection:", id)
deadClients = append(deadClients, id)
}
}
for _, id := range deadClients {
delete(clientPool.clients, id)
}
}