From 2a43dbd71aa920feb6a9a080a70a71f295fb8b79 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Mon, 17 Feb 2020 12:41:27 +0100 Subject: [PATCH] Add downstreamConn.writeMessages This logic will become more complicated in upcoming commits. --- downstream.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/downstream.go b/downstream.go index bd4aa54..419370d 100644 --- a/downstream.go +++ b/downstream.go @@ -44,7 +44,7 @@ type downstreamConn struct { irc *irc.Conn srv *Server logger Logger - messages chan<- *irc.Message + messages chan *irc.Message registered bool user *user @@ -55,20 +55,17 @@ type downstreamConn struct { } func newDownstreamConn(srv *Server, netConn net.Conn) *downstreamConn { - msgs := make(chan *irc.Message, 64) dc := &downstreamConn{ net: netConn, irc: irc.NewConn(netConn), srv: srv, logger: &prefixLogger{srv.Logger, fmt.Sprintf("downstream %q: ", netConn.RemoteAddr())}, - messages: msgs, + messages: make(chan *irc.Message, 64), } go func() { - for msg := range msgs { - if err := dc.irc.WriteMessage(msg); err != nil { - dc.logger.Printf("failed to write message: %v", err) - } + if err := dc.writeMessages(); err != nil { + dc.logger.Printf("failed to write message: %v", err) } if err := dc.net.Close(); err != nil { dc.logger.Printf("failed to close connection: %v", err) @@ -115,6 +112,15 @@ func (dc *downstreamConn) readMessages() error { return nil } +func (dc *downstreamConn) writeMessages() error { + for msg := range dc.messages { + if err := dc.irc.WriteMessage(msg); err != nil { + return err + } + } + return nil +} + func (dc *downstreamConn) Close() error { if dc.closed { return fmt.Errorf("downstream connection already closed")