Introduce ircConn

This interface will allow a conn to be backed by a websocket.
This commit is contained in:
Simon Ser 2020-06-04 17:23:27 +02:00
parent 77faf72fa3
commit 283d4bf14c
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
4 changed files with 31 additions and 15 deletions

36
conn.go
View File

@ -9,9 +9,26 @@ import (
"gopkg.in/irc.v3" "gopkg.in/irc.v3"
) )
// ircConn is a generic IRC connection. It's similar to net.Conn but focuses on
// reading and writing IRC messages.
type ircConn interface {
ReadMessage() (*irc.Message, error)
WriteMessage(*irc.Message) error
Close() error
SetWriteDeadline(time.Time) error
SetReadDeadline(time.Time) error
}
func netIRCConn(c net.Conn) ircConn {
type netConn net.Conn
return struct {
*irc.Conn
netConn
}{irc.NewConn(c), c}
}
type conn struct { type conn struct {
net net.Conn conn ircConn
irc *irc.Conn
srv *Server srv *Server
logger Logger logger Logger
@ -20,11 +37,10 @@ type conn struct {
closed bool closed bool
} }
func newConn(srv *Server, netConn net.Conn, logger Logger) *conn { func newConn(srv *Server, ic ircConn, logger Logger) *conn {
outgoing := make(chan *irc.Message, 64) outgoing := make(chan *irc.Message, 64)
c := &conn{ c := &conn{
net: netConn, conn: ic,
irc: irc.NewConn(netConn),
srv: srv, srv: srv,
outgoing: outgoing, outgoing: outgoing,
logger: logger, logger: logger,
@ -35,13 +51,13 @@ func newConn(srv *Server, netConn net.Conn, logger Logger) *conn {
if c.srv.Debug { if c.srv.Debug {
c.logger.Printf("sent: %v", msg) c.logger.Printf("sent: %v", msg)
} }
c.net.SetWriteDeadline(time.Now().Add(writeTimeout)) c.conn.SetWriteDeadline(time.Now().Add(writeTimeout))
if err := c.irc.WriteMessage(msg); err != nil { if err := c.conn.WriteMessage(msg); err != nil {
c.logger.Printf("failed to write message: %v", err) c.logger.Printf("failed to write message: %v", err)
break break
} }
} }
if err := c.net.Close(); err != nil { if err := c.conn.Close(); err != nil {
c.logger.Printf("failed to close connection: %v", err) c.logger.Printf("failed to close connection: %v", err)
} else { } else {
c.logger.Printf("connection closed") c.logger.Printf("connection closed")
@ -71,14 +87,14 @@ func (c *conn) Close() error {
return fmt.Errorf("connection already closed") return fmt.Errorf("connection already closed")
} }
err := c.net.Close() err := c.conn.Close()
c.closed = true c.closed = true
close(c.outgoing) close(c.outgoing)
return err return err
} }
func (c *conn) ReadMessage() (*irc.Message, error) { func (c *conn) ReadMessage() (*irc.Message, error) {
msg, err := c.irc.ReadMessage() msg, err := c.conn.ReadMessage()
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -95,7 +95,7 @@ type downstreamConn struct {
func newDownstreamConn(srv *Server, netConn net.Conn, id uint64) *downstreamConn { func newDownstreamConn(srv *Server, netConn net.Conn, id uint64) *downstreamConn {
logger := &prefixLogger{srv.Logger, fmt.Sprintf("downstream %q: ", netConn.RemoteAddr())} logger := &prefixLogger{srv.Logger, fmt.Sprintf("downstream %q: ", netConn.RemoteAddr())}
dc := &downstreamConn{ dc := &downstreamConn{
conn: *newConn(srv, netConn, logger), conn: *newConn(srv, netIRCConn(netConn), logger),
id: id, id: id,
supportedCaps: make(map[string]string), supportedCaps: make(map[string]string),
caps: make(map[string]bool), caps: make(map[string]bool),

View File

@ -143,7 +143,7 @@ func connectToUpstream(network *network) (*upstreamConn, error) {
} }
uc := &upstreamConn{ uc := &upstreamConn{
conn: *newConn(network.user.srv, netConn, logger), conn: *newConn(network.user.srv, netIRCConn(netConn), logger),
network: network, network: network,
user: network.user, user: network.user,
channels: make(map[string]*upstreamChannel), channels: make(map[string]*upstreamChannel),