sshpong/cmd/client/main.go

111 lines
2.0 KiB
Go
Raw Normal View History

2024-08-14 23:33:05 -06:00
package main
import (
"fmt"
2024-08-15 20:08:23 -06:00
"io"
"log"
2024-08-14 23:33:05 -06:00
"os"
2024-08-15 20:08:23 -06:00
"sshpong/internal/client"
2024-08-14 23:33:05 -06:00
"sshpong/internal/netwrk"
2024-08-15 20:08:23 -06:00
"google.golang.org/protobuf/proto"
)
2024-08-14 23:33:05 -06:00
2024-08-15 20:08:23 -06:00
var exit chan bool
2024-08-14 23:33:05 -06:00
2024-08-15 20:08:23 -06:00
func main() {
2024-08-14 23:33:05 -06:00
fmt.Println("Welcome to sshpong!")
fmt.Println("Please enter your username")
2024-08-15 20:08:23 -06:00
egress := make(chan *netwrk.LobbyMessage)
ingress := make(chan *netwrk.LobbyMessage)
2024-08-14 23:33:05 -06:00
2024-08-15 20:08:23 -06:00
buf := make([]byte, 1024)
n, err := os.Stdin.Read(buf)
2024-08-14 23:33:05 -06:00
if err != nil {
2024-08-15 20:08:23 -06:00
log.Panic("Bro your input is no good...")
2024-08-14 23:33:05 -06:00
}
2024-08-23 11:59:19 -06:00
username := string(buf[:n-1])
2024-08-14 23:33:05 -06:00
2024-08-15 20:08:23 -06:00
conn, err := netwrk.ConnectToLobby(username)
if err != nil {
log.Panic(err)
}
2024-08-14 23:33:05 -06:00
2024-08-15 20:08:23 -06:00
// User input handler
go func(egress chan *netwrk.LobbyMessage) {
buf := make([]byte, 1024)
for {
2024-08-14 23:33:05 -06:00
n, err := os.Stdin.Read(buf)
if err != nil {
2024-08-15 20:08:23 -06:00
log.Panic("Bro your input wack as fuck")
2024-08-14 23:33:05 -06:00
}
2024-08-15 20:08:23 -06:00
userMessage, err := client.HandleUserInput(buf[:n])
2024-08-23 11:59:19 -06:00
if err == io.EOF {
exit <- true
}
2024-08-15 20:08:23 -06:00
if err != nil {
fmt.Println(err)
continue
2024-08-14 23:33:05 -06:00
}
2024-08-15 20:08:23 -06:00
userMessage.PlayerId = username
egress <- userMessage
2024-08-14 23:33:05 -06:00
}
2024-08-15 20:08:23 -06:00
}(egress)
2024-08-14 23:33:05 -06:00
2024-08-15 20:08:23 -06:00
// Ingress Handler
go func(oc chan *netwrk.LobbyMessage) {
for {
msg := <-ingress
client.HandleServerMessage(msg)
}
}(ingress)
// Network writer
go func(userMessages chan *netwrk.LobbyMessage) {
for {
msg := <-userMessages
bytes, err := proto.Marshal(msg)
if err != nil {
log.Panic("Malformed proto message", err)
}
_, err = conn.Write(bytes)
if err == io.EOF {
log.Panic("Server disconnected sorry...")
} else if err != nil {
log.Panic("Error reading from server connection...")
}
}
}(egress)
// Network reader
go func(serverMessages chan *netwrk.LobbyMessage) {
buf := make([]byte, 1024)
for {
n, err := conn.Read(buf)
if err == io.EOF {
log.Panic("Server disconnected sorry...")
} else if err != nil {
2024-08-23 11:59:19 -06:00
log.Panic("Error reading from server connection...", err)
2024-08-15 20:08:23 -06:00
}
message := &netwrk.LobbyMessage{}
err = proto.Unmarshal(buf[:n], message)
if err != nil {
log.Panic("Error reading message from server")
}
serverMessages <- message
}
}(ingress)
2024-08-14 23:33:05 -06:00
2024-08-15 20:08:23 -06:00
_ = <-exit
2024-08-14 23:33:05 -06:00
}