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"
)
// 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 {
net net.Conn
irc *irc.Conn
conn ircConn
srv *Server
logger Logger
@ -20,11 +37,10 @@ type conn struct {
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)
c := &conn{
net: netConn,
irc: irc.NewConn(netConn),
conn: ic,
srv: srv,
outgoing: outgoing,
logger: logger,
@ -35,13 +51,13 @@ func newConn(srv *Server, netConn net.Conn, logger Logger) *conn {
if c.srv.Debug {
c.logger.Printf("sent: %v", msg)
}
c.net.SetWriteDeadline(time.Now().Add(writeTimeout))
if err := c.irc.WriteMessage(msg); err != nil {
c.conn.SetWriteDeadline(time.Now().Add(writeTimeout))
if err := c.conn.WriteMessage(msg); err != nil {
c.logger.Printf("failed to write message: %v", err)
break
}
}
if err := c.net.Close(); err != nil {
if err := c.conn.Close(); err != nil {
c.logger.Printf("failed to close connection: %v", err)
} else {
c.logger.Printf("connection closed")
@ -71,14 +87,14 @@ func (c *conn) Close() error {
return fmt.Errorf("connection already closed")
}
err := c.net.Close()
err := c.conn.Close()
c.closed = true
close(c.outgoing)
return err
}
func (c *conn) ReadMessage() (*irc.Message, error) {
msg, err := c.irc.ReadMessage()
msg, err := c.conn.ReadMessage()
if err != nil {
return nil, err
}

View File

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

View File

@ -127,8 +127,8 @@ func init() {
handle: handleServiceNetworkStatus,
},
"update": {
usage: "[-addr addr] [-name name] [-username username] [-pass pass] [-realname realname] [-nick nick] [-connect-command command]...",
desc: "update a network",
usage: "[-addr addr] [-name name] [-username username] [-pass pass] [-realname realname] [-nick nick] [-connect-command command]...",
desc: "update a network",
handle: handleServiceNetworkUpdate,
},
"delete": {
@ -376,7 +376,7 @@ func (f stringPtrFlag) Set(s string) error {
type networkFlagSet struct {
*flag.FlagSet
Addr, Name, Nick, Username, Pass, Realname *string
ConnectCommands []string
ConnectCommands []string
}
func newNetworkFlagSet() *networkFlagSet {

View File

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