1
Fork 0

Make g.clients be a map.

This commit is contained in:
Juliusz Chroboczek 2020-04-25 21:29:21 +02:00
parent 4ea4e7c0eb
commit df924cd468
1 changed files with 18 additions and 15 deletions

View File

@ -64,7 +64,7 @@ type group struct {
description *groupDescription description *groupDescription
mu sync.Mutex mu sync.Mutex
clients []*client clients map[string]*client
history []chatHistoryEntry history []chatHistoryEntry
} }
@ -138,6 +138,7 @@ func addGroup(name string, desc *groupDescription) (*group, error) {
g = &group{ g = &group{
name: name, name: name,
description: desc, description: desc,
clients: make(map[string]*client),
} }
groups.groups[name] = g groups.groups[name] = g
} else if desc != nil { } else if desc != nil {
@ -201,18 +202,23 @@ func addClient(name string, client *client, user, pass string) (*group, []userid
} }
client.permissions = perms client.permissions = perms
var users []userid
g.mu.Lock() g.mu.Lock()
defer g.mu.Unlock() defer g.mu.Unlock()
if !perms.Op && g.description.MaxClients > 0 { if !perms.Op && g.description.MaxClients > 0 {
if len(g.clients) >= g.description.MaxClients { if len(g.clients) >= g.description.MaxClients {
return nil, nil, userError("too many users") return nil, nil, userError("too many users")
} }
} }
if g.clients[client.id] != nil {
return nil, nil, protocolError("duplicate client id")
}
var users []userid
for _, c := range g.clients { for _, c := range g.clients {
users = append(users, userid{c.id, c.username}) users = append(users, userid{c.id, c.username})
} }
g.clients = append(g.clients, client) g.clients[client.id] = client
return g, users, nil return g, users, nil
} }
@ -220,19 +226,16 @@ func delClient(c *client) {
c.group.mu.Lock() c.group.mu.Lock()
defer c.group.mu.Unlock() defer c.group.mu.Unlock()
g := c.group g := c.group
for i, cc := range g.clients {
if cc == c { if g.clients[c.id] != c {
g.clients = log.Printf("Deleting unknown client")
append(g.clients[:i], g.clients[i+1:]...) return
c.group = nil }
delete(g.clients, c.id)
if len(g.clients) == 0 && !g.description.Public { if len(g.clients) == 0 && !g.description.Public {
delGroupUnlocked(g.name) delGroupUnlocked(g.name)
} }
return
}
}
log.Printf("Deleting unknown client")
c.group = nil
} }
func (g *group) getClients(except *client) []*client { func (g *group) getClients(except *client) []*client {