Commit 5f168bc8 authored by Alan's avatar Alan
Browse files

Ajout du serveur externe

parent 51530624
module project
go 1.15
package main
import (
"log"
"project/server"
)
func main() {
if err := server.ListenAndServe(":26322"); err != nil {
log.Fatal(err)
}
}
package server
import (
"bufio"
"fmt"
"log"
"net"
"os"
"strconv"
"strings"
)
type server struct {
logger *log.Logger
listener net.Listener
clients map[net.Conn]struct{}
register chan net.Conn
unregister chan net.Conn
broadcast chan string
updateData chan dataReceive
}
type dataReceive struct {
action string
idPlayer int
colorSelect int
}
func ListenAndServe(addr string) error {
s := &server{
logger: log.New(os.Stdout, "", log.LstdFlags|log.Lshortfile),
clients: make(map[net.Conn]struct{}),
register: make(chan net.Conn),
unregister: make(chan net.Conn),
broadcast: make(chan string),
updateData: make(chan dataReceive),
}
// FIXME utiliser le package net pour créer un nouveau Listener
// le Listener doit écouter en TCP sur l'adresse addr
listener, err := net.Listen("tcp", addr)
s.listener = listener
_, port, err := net.SplitHostPort(s.listener.Addr().String())
if err != nil {
s.logger.Printf("Lecture du port échouée: %s", err)
}
s.logger.Printf("En attente de connexions sur le port %s", port)
go s.acceptConnections()
s.serve()
return nil
}
func (s *server) serve() {
for {
select {
case c := <-s.register:
s.clients[c] = struct{}{}
s.logger.Println("Nouvelle connexion")
case message := <-s.broadcast:
for c := range s.clients {
writer := bufio.NewWriter(c)
if message == "updatePlayers" {
msg := fmt.Sprintf("players-%d\n", len(s.clients))
writer.WriteString(msg)
writer.Flush()
s.logger.Printf("Msg send %s", msg)
} else {
msg := fmt.Sprintf("%s\n", message)
writer.WriteString(msg)
writer.Flush()
s.logger.Printf("Msg send %s", msg)
}
}
case data := <-s.updateData:
for c := range s.clients {
writer := bufio.NewWriter(c)
if data.action == "colorSelect" {
msg := fmt.Sprintf("%s-%d-%d\n", data.action, data.idPlayer, data.colorSelect)
writer.WriteString(msg)
writer.Flush()
s.logger.Printf("Msg send %s", msg)
}
}
case conn := <-s.unregister:
delete(s.clients, conn)
s.logger.Println("Connexion fermée")
}
}
}
func (s *server) acceptConnections() {
for {
if len(s.clients) < 4 {
conn, _ := s.listener.Accept()
go s.manageConnection(conn)
}
}
}
func (s *server) manageConnection(conn net.Conn) {
s.register <- conn
clientReader := bufio.NewReader(conn)
s.broadcast <- "updatePlayers"
for {
message, _ := clientReader.ReadString('\n')
if len(message) > 0 {
msgRemoveN := strings.TrimSuffix(message, "\n")
msg := strings.Split(msgRemoveN, "-")
fmt.Printf("Msg received %s", msg)
if msg[0] == "disconnect" {
s.unregister <- conn
s.broadcast <- "updatePlayers"
}
if msg[0] == "colorSelect" {
playerId, _ := strconv.Atoi(msg[1])
data, _ := strconv.Atoi(msg[2])
arr := dataReceive{msg[0], playerId, data}
s.updateData <- arr
}
if msg[0] == "ready" {
m := fmt.Sprintf("updateReadyPlayers-add-%s\n", msg[1])
s.broadcast <- m
}
if msg[0] == "updateSpeed" {
s.broadcast <- msgRemoveN
}
if msg[0] == "arrived" {
s.broadcast <- msgRemoveN
}
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment