Introduce ircConn
This interface will allow a conn to be backed by a websocket.
This commit is contained in:
parent
77faf72fa3
commit
283d4bf14c
36
conn.go
36
conn.go
@ -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
|
||||||
}
|
}
|
||||||
|
@ -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),
|
||||||
|
@ -127,8 +127,8 @@ func init() {
|
|||||||
handle: handleServiceNetworkStatus,
|
handle: handleServiceNetworkStatus,
|
||||||
},
|
},
|
||||||
"update": {
|
"update": {
|
||||||
usage: "[-addr addr] [-name name] [-username username] [-pass pass] [-realname realname] [-nick nick] [-connect-command command]...",
|
usage: "[-addr addr] [-name name] [-username username] [-pass pass] [-realname realname] [-nick nick] [-connect-command command]...",
|
||||||
desc: "update a network",
|
desc: "update a network",
|
||||||
handle: handleServiceNetworkUpdate,
|
handle: handleServiceNetworkUpdate,
|
||||||
},
|
},
|
||||||
"delete": {
|
"delete": {
|
||||||
@ -376,7 +376,7 @@ func (f stringPtrFlag) Set(s string) error {
|
|||||||
type networkFlagSet struct {
|
type networkFlagSet struct {
|
||||||
*flag.FlagSet
|
*flag.FlagSet
|
||||||
Addr, Name, Nick, Username, Pass, Realname *string
|
Addr, Name, Nick, Username, Pass, Realname *string
|
||||||
ConnectCommands []string
|
ConnectCommands []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func newNetworkFlagSet() *networkFlagSet {
|
func newNetworkFlagSet() *networkFlagSet {
|
||||||
|
@ -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),
|
||||||
|
Loading…
Reference in New Issue
Block a user