3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-26 05:49:25 +01:00

refactor server command loop

This commit is contained in:
Jeremy Latt 2014-02-23 19:13:45 -08:00
parent 41e6baf9d4
commit 2229645a39
2 changed files with 47 additions and 41 deletions

View File

@ -17,5 +17,5 @@ func main() {
irc.DEBUG_CHANNEL = config.Debug["channel"]
irc.DEBUG_SERVER = config.Debug["server"]
irc.NewServer(config).ReceiveCommands()
irc.NewServer(config).Run()
}

View File

@ -56,7 +56,51 @@ func NewServer(config *Config) *Server {
return server
}
func (server *Server) ReceiveCommands() {
func (server *Server) ProcessCommand(cmd Command) {
client := cmd.Client()
if DEBUG_SERVER {
log.Printf("%s → %s %s", client, server, cmd)
}
switch client.phase {
case Authorization:
authCmd, ok := cmd.(AuthServerCommand)
if !ok {
client.Quit("unexpected command")
return
}
authCmd.HandleAuthServer(server)
case Registration:
regCmd, ok := cmd.(RegServerCommand)
if !ok {
client.Quit("unexpected command")
return
}
regCmd.HandleRegServer(server)
default:
srvCmd, ok := cmd.(ServerCommand)
if !ok {
client.ErrUnknownCommand(cmd.Code())
return
}
switch srvCmd.(type) {
case *PingCommand, *PongCommand:
client.Touch()
case *QuitCommand:
// no-op
default:
client.Active()
client.Touch()
}
srvCmd.HandleServer(server)
}
}
func (server *Server) Run() {
for {
select {
case conn := <-server.newConns:
@ -69,45 +113,7 @@ func (server *Server) ReceiveCommands() {
client.Quit("connection timeout")
case cmd := <-server.commands:
client := cmd.Client()
if DEBUG_SERVER {
log.Printf("%s → %s %s", client, server, cmd)
}
switch client.phase {
case Authorization:
authCmd, ok := cmd.(AuthServerCommand)
if !ok {
client.Quit("unexpected command")
continue
}
authCmd.HandleAuthServer(server)
case Registration:
regCmd, ok := cmd.(RegServerCommand)
if !ok {
client.Quit("unexpected command")
continue
}
regCmd.HandleRegServer(server)
default:
srvCmd, ok := cmd.(ServerCommand)
if !ok {
client.ErrUnknownCommand(cmd.Code())
continue
}
switch srvCmd.(type) {
case *PingCommand, *PongCommand:
client.Touch()
case *QuitCommand:
// no-op
default:
client.Active()
client.Touch()
}
srvCmd.HandleServer(server)
}
server.ProcessCommand(cmd)
}
}
}