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
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")