3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-29 15:40:02 +01:00

use a select in the server commands loop

This commit is contained in:
Jeremy Latt 2014-02-16 22:38:43 -08:00
parent d5c9681a10
commit af5a05f390
2 changed files with 37 additions and 47 deletions

View File

@ -3,7 +3,6 @@ package main
import ( import (
"github.com/jlatt/ergonomadic/irc" "github.com/jlatt/ergonomadic/irc"
"log" "log"
"sync"
) )
func main() { func main() {
@ -18,10 +17,5 @@ func main() {
irc.DEBUG_CHANNEL = config.Debug["channel"] irc.DEBUG_CHANNEL = config.Debug["channel"]
irc.DEBUG_SERVER = config.Debug["server"] irc.DEBUG_SERVER = config.Debug["server"]
irc.NewServer(config) irc.NewServer(config).ReceiveCommands()
// never finishes
wg := sync.WaitGroup{}
wg.Add(1)
wg.Wait()
} }

View File

@ -16,6 +16,7 @@ type Server struct {
channels ChannelNameMap channels ChannelNameMap
clients ClientNameMap clients ClientNameMap
commands chan Command commands chan Command
conns chan net.Conn
ctime time.Time ctime time.Time
motdFile string motdFile string
name string name string
@ -28,6 +29,7 @@ func NewServer(config *Config) *Server {
channels: make(ChannelNameMap), channels: make(ChannelNameMap),
clients: make(ClientNameMap), clients: make(ClientNameMap),
commands: make(chan Command), commands: make(chan Command),
conns: make(chan net.Conn),
ctime: time.Now(), ctime: time.Now(),
motdFile: config.MOTD, motdFile: config.MOTD,
name: config.Name, name: config.Name,
@ -39,7 +41,6 @@ func NewServer(config *Config) *Server {
server.operators[opConf.Name] = opConf.Password server.operators[opConf.Name] = opConf.Password
} }
go server.receiveCommands()
for _, listenerConf := range config.Listeners { for _, listenerConf := range config.Listeners {
go server.listen(listenerConf) go server.listen(listenerConf)
} }
@ -47,38 +48,44 @@ func NewServer(config *Config) *Server {
return server return server
} }
func (server *Server) receiveCommands() { func (server *Server) ReceiveCommands() {
for command := range server.commands { for {
if DEBUG_SERVER { select {
log.Printf("%s → %s %+v", command.Client(), server, command) case conn := <-server.conns:
} NewClient(server, conn)
client := command.Client()
switch client.phase { case command := <-server.commands:
case Authorization: if DEBUG_SERVER {
authCommand, ok := command.(AuthServerCommand) log.Printf("%s → %s %+v", command.Client(), server, command)
if !ok {
client.Destroy()
return
} }
authCommand.HandleAuthServer(server) client := command.Client()
case Registration: switch client.phase {
regCommand, ok := command.(RegServerCommand) case Authorization:
if !ok { authCommand, ok := command.(AuthServerCommand)
client.Destroy() if !ok {
return client.Destroy()
} continue
regCommand.HandleRegServer(server) }
authCommand.HandleAuthServer(server)
default: case Registration:
serverCommand, ok := command.(ServerCommand) regCommand, ok := command.(RegServerCommand)
if !ok { if !ok {
client.Reply(ErrUnknownCommand(server, command.Name())) client.Destroy()
return continue
}
regCommand.HandleRegServer(server)
default:
serverCommand, ok := command.(ServerCommand)
if !ok {
client.Reply(ErrUnknownCommand(server, command.Name()))
continue
}
client.Touch()
serverCommand.HandleServer(server)
} }
client.Touch()
serverCommand.HandleServer(server)
} }
} }
} }
@ -123,9 +130,7 @@ func (s *Server) listen(config ListenerConfig) {
log.Print("Server.Accept: ", conn.RemoteAddr()) log.Print("Server.Accept: ", conn.RemoteAddr())
} }
s.commands <- &NewClientCommand{ s.conns <- conn
conn: conn,
}
} }
} }
@ -537,12 +542,3 @@ func (msg *NoticeCommand) HandleServer(server *Server) {
} }
target.Reply(RplNotice(client, target, msg.message)) target.Reply(RplNotice(client, target, msg.message))
} }
type NewClientCommand struct {
BaseCommand
conn net.Conn
}
func (msg *NewClientCommand) HandleServer(server *Server) {
NewClient(server, conn)
}