This commit is contained in:
Andreas Neue 2016-07-22 00:00:35 +02:00
parent 76308cbd56
commit bf9ff170b0
2 changed files with 36 additions and 24 deletions

View File

@ -63,7 +63,7 @@ func NewRemoteClient(sv *Server, conn net.Conn) *RemoteClient {
go cl.connReader() go cl.connReader()
go cl.connWriter() go cl.connWriter()
go cl.dispatch() go cl.dispatcher()
xlog.Info("RemoteClient connected") xlog.Info("RemoteClient connected")
@ -80,7 +80,7 @@ func (cl *RemoteClient) Register(success bool) {
func (cl *RemoteClient) Send(msg *irc.Message) { func (cl *RemoteClient) Send(msg *irc.Message) {
msg.Pre = cl.name msg.Pre = cl.name
cl.server.Dispatch <- msg cl.server.Dispatch(msg)
} }
func (cl *RemoteClient) Receive(msg *irc.Message) { func (cl *RemoteClient) Receive(msg *irc.Message) {
@ -114,7 +114,7 @@ func (cl *RemoteClient) Destroy() {
} }
} }
func (cl *RemoteClient) dispatch() { func (cl *RemoteClient) dispatcher() {
for { for {
time.Sleep(1 * time.Millisecond) time.Sleep(1 * time.Millisecond)
if cl.isClosed { if cl.isClosed {
@ -146,12 +146,12 @@ func (cl *RemoteClient) connReader() {
s, err := input.ReadString('\n') s, err := input.ReadString('\n')
if err == io.EOF { if err == io.EOF {
xlog.Info("connReader: Connection closed by peer") xlog.Info("connReader: Connection closed by peer")
cl.server.DelClient <- cl cl.server.DelClient(cl)
return return
} }
if err != nil { if err != nil {
xlog.Error("connReader: %s", err.Error()) xlog.Error("connReader: %s", err.Error())
cl.server.DelClient <- cl cl.server.DelClient(cl)
return return
} }
s = strings.Trim(s, "\r\n") s = strings.Trim(s, "\r\n")
@ -167,11 +167,11 @@ func (cl *RemoteClient) connWriter() {
n, err := cl.conn.Write(bytes[written:]) n, err := cl.conn.Write(bytes[written:])
if err == io.EOF { if err == io.EOF {
xlog.Info("connWriter: Connection closed by peer") xlog.Info("connWriter: Connection closed by peer")
cl.server.DelClient <- cl cl.server.DelClient(cl)
return return
} else if err != nil { } else if err != nil {
xlog.Error("connWriter: %s", err.Error()) xlog.Error("connWriter: %s", err.Error())
cl.server.DelClient <- cl cl.server.DelClient(cl)
return return
} }
written += n written += n
@ -202,7 +202,7 @@ func (cl *RemoteClient) handleCmd(s string) {
cl.password = msg.Args[0] cl.password = msg.Args[0]
case "NICK": case "NICK":
cl.name = msg.Args[0] cl.name = msg.Args[0]
cl.server.AddClient <- cl cl.server.AddClient(cl)
case "USER": case "USER":
} }

View File

@ -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" 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 { type Server struct {
Dispatch chan *irc.Message queue chan *irc.Message
AddClient chan Client addq chan Client
DelClient chan Client delq chan Client
host string host string
info string info string
@ -52,9 +52,9 @@ type Server struct {
func NewServer(configPath, software, version string) *Server { func NewServer(configPath, software, version string) *Server {
sv := &Server{software: software, version: version, created: "yes"} sv := &Server{software: software, version: version, created: "yes"}
sv.Dispatch = make(chan *irc.Message, 1024) sv.queue = make(chan *irc.Message, 1024)
sv.AddClient = make(chan Client, 1024) sv.addq = make(chan Client, 1024)
sv.DelClient = make(chan Client, 1024) sv.delq = make(chan Client, 1024)
sv.clients = make(map[string]Client) sv.clients = make(map[string]Client)
sv.chUsers = make(map[string]map[string]string) sv.chUsers = make(map[string]map[string]string)
@ -87,7 +87,19 @@ func (sv *Server) Run() {
if err == nil { if err == nil {
go sv.listenTls(laddr) 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) { 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) listenTls(laddr string) {
} }
func (sv *Server) dispatch() { func (sv *Server) dispatcher() {
for { for {
time.Sleep(1 * time.Microsecond) time.Sleep(1 * time.Microsecond)
sv.queueLen = float64(len(sv.Dispatch)) sv.queueLen = float64(len(sv.queue))
select { select {
case msg := <-sv.Dispatch: case msg := <-sv.queue:
sv.recvMsg(msg) sv.recvMsg(msg)
sv.packetsTransferred++ sv.packetsTransferred++
case cl := <-sv.AddClient: case cl := <-sv.addq:
lnick := strings.ToLower(cl.Name()) lnick := strings.ToLower(cl.Name())
if _, exists := sv.clients[lnick]; exists { if _, exists := sv.clients[lnick]; exists {
cl.Register(false) cl.Register(false)
@ -133,7 +145,7 @@ func (sv *Server) dispatch() {
xlog.Info("Server has %d client(s)", len(sv.clients)) xlog.Info("Server has %d client(s)", len(sv.clients))
xlog.Debug("Goroutines running: %d", runtime.NumGoroutine()) xlog.Debug("Goroutines running: %d", runtime.NumGoroutine())
} }
case cl := <-sv.DelClient: case cl := <-sv.delq:
nick := cl.Name() nick := cl.Name()
lnick := strings.ToLower(nick) lnick := strings.ToLower(nick)
cl.Destroy() cl.Destroy()
@ -207,16 +219,16 @@ func (sv *Server) sendMsg(msg *irc.Message) {
} }
} }
func (sv *Server) sendReply(tar, cmd, args, trail string) { func (sv *Server) sendReply(nick, cmd, args, trail string) {
lnick := strings.ToLower(tar) lnick := strings.ToLower(nick)
if _, exists := sv.clients[lnick]; !exists { if _, exists := sv.clients[lnick]; !exists {
return return
} }
cl := sv.clients[lnick] cl := sv.clients[lnick]
if args != "" { if args != "" {
args = tar + " " + args args = nick + " " + args
} else { } else {
args = tar args = nick
} }
cl.Receive(irc.M(sv.host, cmd, args, trail)) cl.Receive(irc.M(sv.host, cmd, args, trail))
} }