shelldoor/servers/server.go

56 lines
987 B
Go
Raw Normal View History

2024-09-25 22:56:40 +03:00
package servers
import (
"log"
"net"
)
2024-09-28 19:26:08 +03:00
// The type represents server that connects
// clients and the bots together.
2024-09-25 22:56:40 +03:00
type Server struct {
addr string
listenner net.Listener
bots []*Bot
clients []*Client
2024-09-28 19:26:08 +03:00
config ServerConfig
2024-09-25 22:56:40 +03:00
}
2024-09-28 19:26:08 +03:00
// Returns the new server with the specified configuration.
func NewServer(config ServerConfig) (*Server, error) {
2024-09-25 22:56:40 +03:00
srv := &Server{}
2024-09-28 19:26:08 +03:00
srv.config = config
2024-09-25 22:56:40 +03:00
return srv, nil
}
2024-09-28 19:26:08 +03:00
// The method handles the specified connection
// checking out if it is client or a bot.
2024-09-25 22:56:40 +03:00
func (srv *Server) HandleConn(conn net.Conn) {
defer conn.Close()
2024-09-28 19:26:08 +03:00
for {
}
2024-09-25 22:56:40 +03:00
}
2024-09-28 19:26:08 +03:00
// Start listening and serve both clients and bots.
func (srv *Server) ListenAndServe(addr string) error {
2024-09-25 22:56:40 +03:00
var err error
2024-09-28 19:26:08 +03:00
srv.listenner, err = net.Listen("tcp", addr)
2024-09-25 22:56:40 +03:00
if err != nil {
return err
}
2024-09-28 19:26:08 +03:00
srv.addr = addr
2024-09-25 22:56:40 +03:00
for {
conn, err := srv.listenner.Accept()
if err != nil {
log.Printf("listener.Accept(...): %s\n", err)
continue
}
go srv.HandleConn(conn)
}
return nil
}