Add downstreamConn.writeMessages

This logic will become more complicated in upcoming commits.
This commit is contained in:
Simon Ser 2020-02-17 12:41:27 +01:00
parent 09dffc0f68
commit 2a43dbd71a
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48

View File

@ -44,7 +44,7 @@ type downstreamConn struct {
irc *irc.Conn irc *irc.Conn
srv *Server srv *Server
logger Logger logger Logger
messages chan<- *irc.Message messages chan *irc.Message
registered bool registered bool
user *user user *user
@ -55,21 +55,18 @@ type downstreamConn struct {
} }
func newDownstreamConn(srv *Server, netConn net.Conn) *downstreamConn { func newDownstreamConn(srv *Server, netConn net.Conn) *downstreamConn {
msgs := make(chan *irc.Message, 64)
dc := &downstreamConn{ dc := &downstreamConn{
net: netConn, net: netConn,
irc: irc.NewConn(netConn), irc: irc.NewConn(netConn),
srv: srv, srv: srv,
logger: &prefixLogger{srv.Logger, fmt.Sprintf("downstream %q: ", netConn.RemoteAddr())}, logger: &prefixLogger{srv.Logger, fmt.Sprintf("downstream %q: ", netConn.RemoteAddr())},
messages: msgs, messages: make(chan *irc.Message, 64),
} }
go func() { go func() {
for msg := range msgs { if err := dc.writeMessages(); err != nil {
if err := dc.irc.WriteMessage(msg); err != nil {
dc.logger.Printf("failed to write message: %v", err) dc.logger.Printf("failed to write message: %v", err)
} }
}
if err := dc.net.Close(); err != nil { if err := dc.net.Close(); err != nil {
dc.logger.Printf("failed to close connection: %v", err) dc.logger.Printf("failed to close connection: %v", err)
} else { } else {
@ -115,6 +112,15 @@ func (dc *downstreamConn) readMessages() error {
return nil 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 { func (dc *downstreamConn) Close() error {
if dc.closed { if dc.closed {
return fmt.Errorf("downstream connection already closed") return fmt.Errorf("downstream connection already closed")