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