Initial commit
This commit is contained in:
17
server/core/Client.go
Normal file
17
server/core/Client.go
Normal file
@ -0,0 +1,17 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"net"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Client represents a UDP client.
|
||||
type Client struct {
|
||||
address *net.UDPAddr
|
||||
lastPacket time.Time
|
||||
}
|
||||
|
||||
// String shows the client address.
|
||||
func (c *Client) String() string {
|
||||
return c.address.String()
|
||||
}
|
115
server/core/Server.go
Normal file
115
server/core/Server.go
Normal file
@ -0,0 +1,115 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Handler is a byte code specific packet handler.
|
||||
type Handler func([]byte, *Client)
|
||||
|
||||
// Server represents a UDP server.
|
||||
type Server struct {
|
||||
socket *net.UDPConn
|
||||
clients map[string]*Client
|
||||
handlers [256]Handler
|
||||
}
|
||||
|
||||
// New creates a new server.
|
||||
func New() *Server {
|
||||
return &Server{
|
||||
clients: make(map[string]*Client),
|
||||
}
|
||||
}
|
||||
|
||||
// AddHandler adds the handler for the given byte code.
|
||||
func (s *Server) AddHandler(code byte, handler Handler) {
|
||||
s.handlers[code] = handler
|
||||
}
|
||||
|
||||
// SendTo sends the data to a client.
|
||||
func (s *Server) SendTo(data []byte, client *Client) {
|
||||
_, err := s.socket.WriteToUDP(data, client.address)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Error sending response:", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Run starts the server on the given port.
|
||||
func (s *Server) Run(port int) {
|
||||
s.socket = listen(port)
|
||||
defer s.socket.Close()
|
||||
|
||||
s.read()
|
||||
}
|
||||
|
||||
// listen creates a socket for the server and starts listening for incoming packets.
|
||||
func listen(port int) *net.UDPConn {
|
||||
addr, err := net.ResolveUDPAddr("udp", fmt.Sprintf(":%d", port))
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
connection, err := net.ListenUDP("udp", addr)
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return connection
|
||||
}
|
||||
|
||||
// read is a blocking call which will read incoming packets and handle them.
|
||||
func (s *Server) read() {
|
||||
buffer := make([]byte, 4096)
|
||||
|
||||
for {
|
||||
n, addr, err := s.socket.ReadFromUDP(buffer)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Error reading from UDP connection:", err)
|
||||
continue
|
||||
}
|
||||
|
||||
if n == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
go s.handle(buffer[:n], addr)
|
||||
}
|
||||
}
|
||||
|
||||
// handle deals with an incoming packet.
|
||||
func (s *Server) handle(data []byte, addr *net.UDPAddr) {
|
||||
c := s.getClient(addr)
|
||||
c.lastPacket = time.Now()
|
||||
// fmt.Printf("Received %d bytes from %s: %s\n", len(data), c, string(data))
|
||||
|
||||
handler := s.handlers[data[0]]
|
||||
|
||||
if handler == nil {
|
||||
fmt.Println("Unknown packet type.")
|
||||
return
|
||||
}
|
||||
|
||||
handler(data, c)
|
||||
}
|
||||
|
||||
// getClient either returns a new or existing client for the requested address.
|
||||
func (s *Server) getClient(addr *net.UDPAddr) *Client {
|
||||
c, exists := s.clients[addr.String()]
|
||||
|
||||
if exists {
|
||||
return c
|
||||
}
|
||||
|
||||
c = &Client{
|
||||
address: addr,
|
||||
}
|
||||
|
||||
s.clients[addr.String()] = c
|
||||
return c
|
||||
}
|
3
server/go.mod
Normal file
3
server/go.mod
Normal file
@ -0,0 +1,3 @@
|
||||
module server
|
||||
|
||||
go 1.21.6
|
17
server/main.go
Normal file
17
server/main.go
Normal file
@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"server/core"
|
||||
)
|
||||
|
||||
func main() {
|
||||
server := core.New()
|
||||
|
||||
server.AddHandler(0, func(data []byte, client *core.Client) {
|
||||
// count := data[1]
|
||||
// fmt.Println(count)
|
||||
server.SendTo(data, client)
|
||||
})
|
||||
|
||||
server.Run(4242)
|
||||
}
|
Reference in New Issue
Block a user