CSI-ES-2324/Projs/PD1/internal/server/server.go

44 lines
790 B
Go
Raw Normal View History

2024-04-16 12:23:00 +01:00
package server
import (
"PD1/internal/protocol"
"PD1/internal/utils/networking"
"encoding/json"
2024-04-16 12:23:00 +01:00
"fmt"
)
func clientHandler(connection networking.Connection[protocol.Packet]) {
defer connection.Conn.Close()
jd := json.NewDecoder(connection.Conn)
for {
var pac protocol.Packet
jd.Decode(&pac)
switch pac.Flag {
case protocol.ReqPK:
fmt.Println("ReqPK")
case protocol.ReqAllMsg:
fmt.Println("ReqAllMsg")
case protocol.ReqMsg:
fmt.Println("ReqMsg")
case protocol.SubmitMsg:
fmt.Println("SubmitMsh")
}
}
2024-04-16 12:23:00 +01:00
}
func Run(port int) {
server := networking.NewServer[protocol.Packet](port)
go server.ListenLoop()
2024-04-16 12:23:00 +01:00
for {
//Receive connection via channel
conn := <-server.C
//Launch client handler via clientHandler
go clientHandler(conn)
2024-04-16 12:23:00 +01:00
}
}