diff --git a/client.go b/client.go index f239257..4d07ac1 100644 --- a/client.go +++ b/client.go @@ -63,7 +63,7 @@ func NewRemoteClient(sv *Server, conn net.Conn) *RemoteClient { go cl.connReader() go cl.connWriter() - go cl.dispatch() + go cl.dispatcher() xlog.Info("RemoteClient connected") @@ -80,7 +80,7 @@ func (cl *RemoteClient) Register(success bool) { func (cl *RemoteClient) Send(msg *irc.Message) { msg.Pre = cl.name - cl.server.Dispatch <- msg + cl.server.Dispatch(msg) } func (cl *RemoteClient) Receive(msg *irc.Message) { @@ -114,7 +114,7 @@ func (cl *RemoteClient) Destroy() { } } -func (cl *RemoteClient) dispatch() { +func (cl *RemoteClient) dispatcher() { for { time.Sleep(1 * time.Millisecond) if cl.isClosed { @@ -146,12 +146,12 @@ func (cl *RemoteClient) connReader() { s, err := input.ReadString('\n') if err == io.EOF { xlog.Info("connReader: Connection closed by peer") - cl.server.DelClient <- cl + cl.server.DelClient(cl) return } if err != nil { xlog.Error("connReader: %s", err.Error()) - cl.server.DelClient <- cl + cl.server.DelClient(cl) return } s = strings.Trim(s, "\r\n") @@ -167,11 +167,11 @@ func (cl *RemoteClient) connWriter() { n, err := cl.conn.Write(bytes[written:]) if err == io.EOF { xlog.Info("connWriter: Connection closed by peer") - cl.server.DelClient <- cl + cl.server.DelClient(cl) return } else if err != nil { xlog.Error("connWriter: %s", err.Error()) - cl.server.DelClient <- cl + cl.server.DelClient(cl) return } written += n @@ -202,7 +202,7 @@ func (cl *RemoteClient) handleCmd(s string) { cl.password = msg.Args[0] case "NICK": cl.name = msg.Args[0] - cl.server.AddClient <- cl + cl.server.AddClient(cl) case "USER": } diff --git a/server.go b/server.go index cf3f524..4022f76 100644 --- a/server.go +++ b/server.go @@ -25,9 +25,9 @@ var myinfo string = "%s %s/%s * *" var isupport string = "ALIAS FRIEND UNFRIEND CASEMAPPING=rfc1459 CHANLIMIT=#:1024 CHANMODES=b,k,l,imnpst CHANNELLEN=200 CHANTYPES=# EXCEPTS=e KICKLEN MAXLIST=b:50,e:50 MODES=1 NETWORK=dnix.de NICKLEN=32 PREFIX=(aohv)&@%%+ SAFELIST STATUSMSG=&@%%+ TOPICLEN" type Server struct { - Dispatch chan *irc.Message - AddClient chan Client - DelClient chan Client + queue chan *irc.Message + addq chan Client + delq chan Client host string info string @@ -52,9 +52,9 @@ type Server struct { func NewServer(configPath, software, version string) *Server { sv := &Server{software: software, version: version, created: "yes"} - sv.Dispatch = make(chan *irc.Message, 1024) - sv.AddClient = make(chan Client, 1024) - sv.DelClient = make(chan Client, 1024) + sv.queue = make(chan *irc.Message, 1024) + sv.addq = make(chan Client, 1024) + sv.delq = make(chan Client, 1024) sv.clients = make(map[string]Client) sv.chUsers = make(map[string]map[string]string) @@ -87,7 +87,19 @@ func (sv *Server) Run() { if err == nil { go sv.listenTls(laddr) } - sv.dispatch() + sv.dispatcher() +} + +func (sv *Server) Dispatch(msg *irc.Message) { + sv.queue <- msg +} + +func (sv *Server) AddClient(cl Client) { + sv.addq <- cl +} + +func (sv *Server) DelClient(cl Client) { + sv.delq <- cl } func (sv *Server) listen(laddr string) { @@ -111,15 +123,15 @@ func (sv *Server) listen(laddr string) { func (sv *Server) listenTls(laddr string) { } -func (sv *Server) dispatch() { +func (sv *Server) dispatcher() { for { time.Sleep(1 * time.Microsecond) - sv.queueLen = float64(len(sv.Dispatch)) + sv.queueLen = float64(len(sv.queue)) select { - case msg := <-sv.Dispatch: + case msg := <-sv.queue: sv.recvMsg(msg) sv.packetsTransferred++ - case cl := <-sv.AddClient: + case cl := <-sv.addq: lnick := strings.ToLower(cl.Name()) if _, exists := sv.clients[lnick]; exists { cl.Register(false) @@ -133,7 +145,7 @@ func (sv *Server) dispatch() { xlog.Info("Server has %d client(s)", len(sv.clients)) xlog.Debug("Goroutines running: %d", runtime.NumGoroutine()) } - case cl := <-sv.DelClient: + case cl := <-sv.delq: nick := cl.Name() lnick := strings.ToLower(nick) cl.Destroy() @@ -207,16 +219,16 @@ func (sv *Server) sendMsg(msg *irc.Message) { } } -func (sv *Server) sendReply(tar, cmd, args, trail string) { - lnick := strings.ToLower(tar) +func (sv *Server) sendReply(nick, cmd, args, trail string) { + lnick := strings.ToLower(nick) if _, exists := sv.clients[lnick]; !exists { return } cl := sv.clients[lnick] if args != "" { - args = tar + " " + args + args = nick + " " + args } else { - args = tar + args = nick } cl.Receive(irc.M(sv.host, cmd, args, trail)) }