2020-04-03 14:34:11 +00:00
|
|
|
package soju
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2020-04-30 08:35:02 +00:00
|
|
|
"sync"
|
2020-04-03 14:34:11 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"gopkg.in/irc.v3"
|
|
|
|
)
|
|
|
|
|
2020-06-04 15:23:27 +00:00
|
|
|
// 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}
|
|
|
|
}
|
|
|
|
|
2020-04-03 14:34:11 +00:00
|
|
|
type conn struct {
|
2020-06-04 15:23:27 +00:00
|
|
|
conn ircConn
|
2020-04-30 08:35:02 +00:00
|
|
|
srv *Server
|
|
|
|
logger Logger
|
|
|
|
|
|
|
|
lock sync.Mutex
|
2020-04-03 14:34:11 +00:00
|
|
|
outgoing chan<- *irc.Message
|
2020-04-30 08:35:02 +00:00
|
|
|
closed bool
|
2020-04-03 14:34:11 +00:00
|
|
|
}
|
|
|
|
|
2020-06-04 15:23:27 +00:00
|
|
|
func newConn(srv *Server, ic ircConn, logger Logger) *conn {
|
2020-04-03 14:34:11 +00:00
|
|
|
outgoing := make(chan *irc.Message, 64)
|
|
|
|
c := &conn{
|
2020-06-04 15:23:27 +00:00
|
|
|
conn: ic,
|
2020-04-03 14:34:11 +00:00
|
|
|
srv: srv,
|
|
|
|
outgoing: outgoing,
|
|
|
|
logger: logger,
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for msg := range outgoing {
|
|
|
|
if c.srv.Debug {
|
|
|
|
c.logger.Printf("sent: %v", msg)
|
|
|
|
}
|
2020-06-04 15:23:27 +00:00
|
|
|
c.conn.SetWriteDeadline(time.Now().Add(writeTimeout))
|
|
|
|
if err := c.conn.WriteMessage(msg); err != nil {
|
2020-04-03 14:34:11 +00:00
|
|
|
c.logger.Printf("failed to write message: %v", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-06-04 15:23:27 +00:00
|
|
|
if err := c.conn.Close(); err != nil {
|
2020-04-03 14:34:11 +00:00
|
|
|
c.logger.Printf("failed to close connection: %v", err)
|
|
|
|
} else {
|
|
|
|
c.logger.Printf("connection closed")
|
|
|
|
}
|
|
|
|
// Drain the outgoing channel to prevent SendMessage from blocking
|
|
|
|
for range outgoing {
|
|
|
|
// This space is intentionally left blank
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
c.logger.Printf("new connection")
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *conn) isClosed() bool {
|
2020-04-30 08:35:02 +00:00
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
|
|
|
return c.closed
|
2020-04-03 14:34:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the connection. It is safe to call from any goroutine.
|
|
|
|
func (c *conn) Close() error {
|
2020-04-30 08:35:02 +00:00
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
|
|
|
|
|
|
|
if c.closed {
|
2020-04-03 14:34:11 +00:00
|
|
|
return fmt.Errorf("connection already closed")
|
|
|
|
}
|
2020-04-30 08:35:02 +00:00
|
|
|
|
2020-06-04 15:23:27 +00:00
|
|
|
err := c.conn.Close()
|
2020-04-30 08:35:02 +00:00
|
|
|
c.closed = true
|
2020-04-03 14:34:11 +00:00
|
|
|
close(c.outgoing)
|
2020-06-04 10:18:51 +00:00
|
|
|
return err
|
2020-04-03 14:34:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *conn) ReadMessage() (*irc.Message, error) {
|
2020-06-04 15:23:27 +00:00
|
|
|
msg, err := c.conn.ReadMessage()
|
2020-04-03 14:34:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.srv.Debug {
|
|
|
|
c.logger.Printf("received: %v", msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
return msg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendMessage queues a new outgoing message. It is safe to call from any
|
|
|
|
// goroutine.
|
2020-04-30 08:35:02 +00:00
|
|
|
//
|
|
|
|
// If the connection is closed before the message is sent, SendMessage silently
|
|
|
|
// drops the message.
|
2020-04-03 14:34:11 +00:00
|
|
|
func (c *conn) SendMessage(msg *irc.Message) {
|
2020-04-30 08:35:02 +00:00
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
|
|
|
|
|
|
|
if c.closed {
|
2020-04-03 14:34:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
c.outgoing <- msg
|
|
|
|
}
|