Changeset 40 in code for trunk


Ignore:
Timestamp:
Feb 7, 2020, 10:56:36 AM (5 years ago)
Author:
contact
Message:

Add user.forEachDownstream

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/downstream.go

    r39 r40  
    116116                return fmt.Errorf("downstream connection already closed")
    117117        }
    118         if err := c.net.Close(); err != nil {
    119                 return err
    120         }
     118
     119        if u := c.user; u != nil {
     120                u.lock.Lock()
     121                for i := range u.downstreamConns {
     122                        if u.downstreamConns[i] == c {
     123                                u.downstreamConns = append(u.downstreamConns[:i], u.downstreamConns[i+1:]...)
     124                        }
     125                }
     126                u.lock.Unlock()
     127        }
     128
    121129        close(c.messages)
    122130        c.closed = true
    123         return nil
     131
     132        return c.net.Close()
    124133}
    125134
     
    182191        c.registered = true
    183192        c.user = u
     193
     194        u.lock.Lock()
     195        u.downstreamConns = append(u.downstreamConns, c)
     196        u.lock.Unlock()
    184197
    185198        c.messages <- &irc.Message{
  • trunk/server.go

    r39 r40  
    3636        srv      *Server
    3737
    38         lock          sync.Mutex
    39         upstreamConns []*upstreamConn
     38        lock            sync.Mutex
     39        upstreamConns   []*upstreamConn
     40        downstreamConns []*downstreamConn
    4041}
    4142
     
    4748                }
    4849                f(uc)
     50        }
     51        u.lock.Unlock()
     52}
     53
     54func (u *user) forEachDownstream(f func(dc *downstreamConn)) {
     55        u.lock.Lock()
     56        for _, dc := range u.downstreamConns {
     57                f(dc)
    4958        }
    5059        u.lock.Unlock()
  • trunk/upstream.go

    r37 r40  
    126126                        }
    127127
    128                         c.srv.lock.Lock()
    129                         for _, dc := range c.srv.downstreamConns {
     128                        c.user.forEachDownstream(func(dc *downstreamConn) {
    130129                                dc.messages <- msg
    131                         }
    132                         c.srv.lock.Unlock()
     130                        })
    133131                }
    134132        case "NOTICE":
     
    175173                }
    176174
    177                 c.srv.lock.Lock()
    178                 for _, dc := range c.srv.downstreamConns {
     175                c.user.forEachDownstream(func(dc *downstreamConn) {
    179176                        dc.messages <- msg
    180                 }
    181                 c.srv.lock.Unlock()
     177                })
    182178        case "PART":
    183179                if len(msg.Params) < 1 {
     
    198194                }
    199195
    200                 c.srv.lock.Lock()
    201                 for _, dc := range c.srv.downstreamConns {
     196                c.user.forEachDownstream(func(dc *downstreamConn) {
    202197                        dc.messages <- msg
    203                 }
    204                 c.srv.lock.Unlock()
     198                })
    205199        case irc.RPL_TOPIC, irc.RPL_NOTOPIC:
    206200                if len(msg.Params) < 3 {
     
    276270                ch.complete = true
    277271
    278                 c.srv.lock.Lock()
    279                 for _, dc := range c.srv.downstreamConns {
     272                c.user.forEachDownstream(func(dc *downstreamConn) {
    280273                        forwardChannel(dc, ch)
    281                 }
    282                 c.srv.lock.Unlock()
     274                })
    283275        case "PRIVMSG":
    284                 c.srv.lock.Lock()
    285                 for _, dc := range c.srv.downstreamConns {
     276                c.user.forEachDownstream(func(dc *downstreamConn) {
    286277                        dc.messages <- msg
    287                 }
    288                 c.srv.lock.Unlock()
     278                })
    289279        case irc.RPL_YOURHOST, irc.RPL_CREATED:
    290280                // Ignore
Note: See TracChangeset for help on using the changeset viewer.