3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-12-22 18:52:41 +01:00

swap refcounting friends for iteration

This commit is contained in:
Jeremy Latt 2014-02-18 15:28:20 -08:00
parent 9e471b5b5d
commit 86d4cae141
2 changed files with 13 additions and 28 deletions

View File

@ -120,12 +120,6 @@ func (channel *Channel) Join(client *Client, key string) {
} }
client.channels.Add(channel) client.channels.Add(channel)
for member := range channel.members {
client.AddFriend(member)
member.AddFriend(client)
}
channel.Reply(RplJoin(client, channel)) channel.Reply(RplJoin(client, channel))
channel.GetTopic(client) channel.GetTopic(client)
channel.Names(client) channel.Names(client)
@ -286,11 +280,6 @@ func (channel *Channel) Notice(client *Client, message string) {
} }
func (channel *Channel) Quit(client *Client) { func (channel *Channel) Quit(client *Client) {
for member := range channel.members {
client.RemoveFriend(member)
member.RemoveFriend(client)
}
channel.members.Remove(client) channel.members.Remove(client)
client.channels.Remove(channel) client.channels.Remove(channel)
} }

View File

@ -18,7 +18,6 @@ type Client struct {
channels ChannelSet channels ChannelSet
ctime time.Time ctime time.Time
flags map[UserMode]bool flags map[UserMode]bool
friends map[*Client]uint
hasQuit bool hasQuit bool
hops uint hops uint
hostname string hostname string
@ -40,7 +39,6 @@ func NewClient(server *Server, conn net.Conn) *Client {
channels: make(ChannelSet), channels: make(ChannelSet),
ctime: now, ctime: now,
flags: make(map[UserMode]bool), flags: make(map[UserMode]bool),
friends: make(map[*Client]uint),
hostname: AddrLookupHostname(conn.RemoteAddr()), hostname: AddrLookupHostname(conn.RemoteAddr()),
phase: server.InitPhase(), phase: server.InitPhase(),
server: server, server: server,
@ -137,7 +135,6 @@ func (client *Client) Idle() {
func (client *Client) Register() { func (client *Client) Register() {
client.phase = Normal client.phase = Normal
client.loginTimer.Stop() client.loginTimer.Stop()
client.AddFriend(client)
client.Touch() client.Touch()
} }
@ -228,15 +225,15 @@ func (c *Client) String() string {
return c.Id() return c.Id()
} }
func (client *Client) AddFriend(friend *Client) { func (client *Client) Friends() ClientSet {
client.friends[friend] += 1 friends := make(ClientSet)
friends.Add(client)
for channel := range client.channels {
for member := range channel.members {
friends.Add(member)
} }
func (client *Client) RemoveFriend(friend *Client) {
client.friends[friend] -= 1
if client.friends[friend] <= 0 {
delete(client.friends, friend)
} }
return friends
} }
func (client *Client) ChangeNickname(nickname string) { func (client *Client) ChangeNickname(nickname string) {
@ -245,7 +242,7 @@ func (client *Client) ChangeNickname(nickname string) {
client.nick = nickname client.nick = nickname
for friend := range client.friends { for friend := range client.Friends() {
friend.Reply(reply) friend.Reply(reply)
} }
} }
@ -256,14 +253,13 @@ func (client *Client) Quit(message string) {
} }
client.hasQuit = true client.hasQuit = true
client.Reply(RplError(client.server, client.Nick())) client.Reply(RplError(client.server, client.Nick()))
friends := client.Friends()
friends.Remove(client)
client.Destroy() client.Destroy()
if len(client.friends) > 0 { if len(friends) > 0 {
reply := RplQuit(client, message) reply := RplQuit(client, message)
for friend := range client.friends { for friend := range friends {
if friend == client {
continue
}
friend.Reply(reply) friend.Reply(reply)
} }
} }