43 lines
790 B
Go
43 lines
790 B
Go
package server
|
|
|
|
import (
|
|
"PD1/internal/protocol"
|
|
"PD1/internal/utils/networking"
|
|
"encoding/json"
|
|
"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")
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
func Run(port int) {
|
|
server := networking.NewServer[protocol.Packet](port)
|
|
go server.ListenLoop()
|
|
|
|
for {
|
|
//Receive connection via channel
|
|
conn := <-server.C
|
|
//Launch client handler via clientHandler
|
|
go clientHandler(conn)
|
|
}
|
|
}
|