Write to downstream conn in dedicated goroutine
This commit is contained in:
parent
3f2e08b949
commit
4a816e1593
@ -43,6 +43,7 @@ type downstreamConn struct {
|
|||||||
irc *irc.Conn
|
irc *irc.Conn
|
||||||
srv *Server
|
srv *Server
|
||||||
logger Logger
|
logger Logger
|
||||||
|
messages chan<- *irc.Message
|
||||||
|
|
||||||
registered bool
|
registered bool
|
||||||
closed bool
|
closed bool
|
||||||
@ -52,12 +53,24 @@ type downstreamConn struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func newDownstreamConn(srv *Server, netConn net.Conn) *downstreamConn {
|
func newDownstreamConn(srv *Server, netConn net.Conn) *downstreamConn {
|
||||||
return &downstreamConn{
|
msgs := make(chan *irc.Message, 64)
|
||||||
|
conn := &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,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
for msg := range msgs {
|
||||||
|
if err := conn.irc.WriteMessage(msg); err != nil {
|
||||||
|
conn.logger.Printf("failed to write message: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return conn
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *downstreamConn) readMessages() error {
|
func (c *downstreamConn) readMessages() error {
|
||||||
@ -75,9 +88,7 @@ func (c *downstreamConn) readMessages() error {
|
|||||||
err = c.handleMessage(msg)
|
err = c.handleMessage(msg)
|
||||||
if ircErr, ok := err.(ircError); ok {
|
if ircErr, ok := err.(ircError); ok {
|
||||||
ircErr.Message.Prefix = c.srv.prefix()
|
ircErr.Message.Prefix = c.srv.prefix()
|
||||||
if err := c.WriteMessage(ircErr.Message); err != nil {
|
c.messages <- ircErr.Message
|
||||||
return fmt.Errorf("failed to write IRC reply: %v", err)
|
|
||||||
}
|
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return fmt.Errorf("failed to handle IRC command %q: %v", msg.Command, err)
|
return fmt.Errorf("failed to handle IRC command %q: %v", msg.Command, err)
|
||||||
}
|
}
|
||||||
@ -91,26 +102,31 @@ func (c *downstreamConn) readMessages() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *downstreamConn) Close() error {
|
func (c *downstreamConn) Close() error {
|
||||||
|
if c.closed {
|
||||||
|
return fmt.Errorf("downstream connection already closed")
|
||||||
|
}
|
||||||
if err := c.net.Close(); err != nil {
|
if err := c.net.Close(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
close(c.messages)
|
||||||
c.closed = true
|
c.closed = true
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *downstreamConn) WriteMessage(msg *irc.Message) error {
|
func (c *downstreamConn) WriteMessage(msg *irc.Message) {
|
||||||
msg.Prefix = c.srv.prefix()
|
msg.Prefix = c.srv.prefix()
|
||||||
return c.irc.WriteMessage(msg)
|
c.messages <- msg
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *downstreamConn) handleMessage(msg *irc.Message) error {
|
func (c *downstreamConn) handleMessage(msg *irc.Message) error {
|
||||||
switch msg.Command {
|
switch msg.Command {
|
||||||
case "PING":
|
case "PING":
|
||||||
// TODO: handle params
|
// TODO: handle params
|
||||||
return c.WriteMessage(&irc.Message{
|
c.WriteMessage(&irc.Message{
|
||||||
Command: "PONG",
|
Command: "PONG",
|
||||||
Params: []string{c.srv.Hostname},
|
Params: []string{c.srv.Hostname},
|
||||||
})
|
})
|
||||||
|
return nil
|
||||||
default:
|
default:
|
||||||
if c.registered {
|
if c.registered {
|
||||||
return c.handleMessageRegistered(msg)
|
return c.handleMessageRegistered(msg)
|
||||||
@ -148,45 +164,30 @@ func (c *downstreamConn) handleMessageUnregistered(msg *irc.Message) error {
|
|||||||
func (c *downstreamConn) register() error {
|
func (c *downstreamConn) register() error {
|
||||||
c.registered = true
|
c.registered = true
|
||||||
|
|
||||||
err := c.WriteMessage(&irc.Message{
|
c.WriteMessage(&irc.Message{
|
||||||
Command: irc.RPL_WELCOME,
|
Command: irc.RPL_WELCOME,
|
||||||
Params: []string{c.nick, "Welcome to jounce, " + c.nick},
|
Params: []string{c.nick, "Welcome to jounce, " + c.nick},
|
||||||
})
|
})
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = c.WriteMessage(&irc.Message{
|
c.WriteMessage(&irc.Message{
|
||||||
Command: irc.RPL_YOURHOST,
|
Command: irc.RPL_YOURHOST,
|
||||||
Params: []string{c.nick, "Your host is " + c.srv.Hostname},
|
Params: []string{c.nick, "Your host is " + c.srv.Hostname},
|
||||||
})
|
})
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = c.WriteMessage(&irc.Message{
|
c.WriteMessage(&irc.Message{
|
||||||
Command: irc.RPL_CREATED,
|
Command: irc.RPL_CREATED,
|
||||||
Params: []string{c.nick, "This server was created <datetime>"}, // TODO
|
Params: []string{c.nick, "This server was created <datetime>"}, // TODO
|
||||||
})
|
})
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = c.WriteMessage(&irc.Message{
|
c.WriteMessage(&irc.Message{
|
||||||
Command: irc.RPL_MYINFO,
|
Command: irc.RPL_MYINFO,
|
||||||
Params: []string{c.nick, c.srv.Hostname, "unknown", "aiwroO", "OovaimnqpsrtklbeI"},
|
Params: []string{c.nick, c.srv.Hostname, "unknown", "aiwroO", "OovaimnqpsrtklbeI"},
|
||||||
})
|
})
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = c.WriteMessage(&irc.Message{
|
c.WriteMessage(&irc.Message{
|
||||||
Command: irc.ERR_NOMOTD,
|
Command: irc.ERR_NOMOTD,
|
||||||
Params: []string{c.nick, "No MOTD"},
|
Params: []string{c.nick, "No MOTD"},
|
||||||
})
|
})
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user