1 | package soju
|
---|
2 |
|
---|
3 | import (
|
---|
4 | "fmt"
|
---|
5 | "net"
|
---|
6 | "sync"
|
---|
7 | "time"
|
---|
8 |
|
---|
9 | "gopkg.in/irc.v3"
|
---|
10 | )
|
---|
11 |
|
---|
12 | type conn struct {
|
---|
13 | net net.Conn
|
---|
14 | irc *irc.Conn
|
---|
15 | srv *Server
|
---|
16 | logger Logger
|
---|
17 |
|
---|
18 | lock sync.Mutex
|
---|
19 | outgoing chan<- *irc.Message
|
---|
20 | closed bool
|
---|
21 | }
|
---|
22 |
|
---|
23 | func newConn(srv *Server, netConn net.Conn, logger Logger) *conn {
|
---|
24 | outgoing := make(chan *irc.Message, 64)
|
---|
25 | c := &conn{
|
---|
26 | net: netConn,
|
---|
27 | irc: irc.NewConn(netConn),
|
---|
28 | srv: srv,
|
---|
29 | outgoing: outgoing,
|
---|
30 | logger: logger,
|
---|
31 | }
|
---|
32 |
|
---|
33 | go func() {
|
---|
34 | for msg := range outgoing {
|
---|
35 | if c.srv.Debug {
|
---|
36 | c.logger.Printf("sent: %v", msg)
|
---|
37 | }
|
---|
38 | c.net.SetWriteDeadline(time.Now().Add(writeTimeout))
|
---|
39 | if err := c.irc.WriteMessage(msg); err != nil {
|
---|
40 | c.logger.Printf("failed to write message: %v", err)
|
---|
41 | break
|
---|
42 | }
|
---|
43 | }
|
---|
44 | if err := c.net.Close(); err != nil {
|
---|
45 | c.logger.Printf("failed to close connection: %v", err)
|
---|
46 | } else {
|
---|
47 | c.logger.Printf("connection closed")
|
---|
48 | }
|
---|
49 | // Drain the outgoing channel to prevent SendMessage from blocking
|
---|
50 | for range outgoing {
|
---|
51 | // This space is intentionally left blank
|
---|
52 | }
|
---|
53 | }()
|
---|
54 |
|
---|
55 | c.logger.Printf("new connection")
|
---|
56 | return c
|
---|
57 | }
|
---|
58 |
|
---|
59 | func (c *conn) isClosed() bool {
|
---|
60 | c.lock.Lock()
|
---|
61 | defer c.lock.Unlock()
|
---|
62 | return c.closed
|
---|
63 | }
|
---|
64 |
|
---|
65 | // Close closes the connection. It is safe to call from any goroutine.
|
---|
66 | func (c *conn) Close() error {
|
---|
67 | c.lock.Lock()
|
---|
68 | defer c.lock.Unlock()
|
---|
69 |
|
---|
70 | if c.closed {
|
---|
71 | return fmt.Errorf("connection already closed")
|
---|
72 | }
|
---|
73 |
|
---|
74 | err := c.net.Close()
|
---|
75 | c.closed = true
|
---|
76 | close(c.outgoing)
|
---|
77 | return err
|
---|
78 | }
|
---|
79 |
|
---|
80 | func (c *conn) ReadMessage() (*irc.Message, error) {
|
---|
81 | msg, err := c.irc.ReadMessage()
|
---|
82 | if err != nil {
|
---|
83 | return nil, err
|
---|
84 | }
|
---|
85 |
|
---|
86 | if c.srv.Debug {
|
---|
87 | c.logger.Printf("received: %v", msg)
|
---|
88 | }
|
---|
89 |
|
---|
90 | return msg, nil
|
---|
91 | }
|
---|
92 |
|
---|
93 | // SendMessage queues a new outgoing message. It is safe to call from any
|
---|
94 | // goroutine.
|
---|
95 | //
|
---|
96 | // If the connection is closed before the message is sent, SendMessage silently
|
---|
97 | // drops the message.
|
---|
98 | func (c *conn) SendMessage(msg *irc.Message) {
|
---|
99 | c.lock.Lock()
|
---|
100 | defer c.lock.Unlock()
|
---|
101 |
|
---|
102 | if c.closed {
|
---|
103 | return
|
---|
104 | }
|
---|
105 | c.outgoing <- msg
|
---|
106 | }
|
---|