Add downstreamConn.writeMessages
This logic will become more complicated in upcoming commits.
This commit is contained in:
parent
09dffc0f68
commit
2a43dbd71a
@ -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")
|
||||||
|
Loading…
Reference in New Issue
Block a user