You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
1.7 KiB
82 lines
1.7 KiB
package group |
|
|
|
import ( |
|
"errors" |
|
"github.com/redis/go-redis/v9" |
|
"sonet/pkg/protocol/session" |
|
"sonet/pkg/utils/logger" |
|
"sync" |
|
) |
|
|
|
var ErrNotExists = errors.New("not exists") |
|
|
|
// PostalDao persistent postal group api |
|
// 1.uid online, send online event |
|
// 2.onOnlineEvent: chat -> chat groups to postal, game -> game groups to postal |
|
// 2.postal create uid create or join memory group linkedList |
|
// 3.postalA: gid-1(uid1, uid2, uid3); postalB: gid1(uid4, uid5, uid6) |
|
// extra: room bit位判断是否存在 |
|
type PostalDao interface { |
|
LoadGroupIdsByUid(uid string) ([]string, error) |
|
GroupCreate(gid string) (string, error) |
|
GroupJoin(gid, uid string) error |
|
GroupLeave(gid, uid string) error |
|
GroupDismiss(gid string) error |
|
} |
|
|
|
type RedisPostalDao struct { |
|
rdb *redis.Client |
|
} |
|
|
|
func (d *RedisPostalDao) LoadGroupIdsByUid(uid string) (gids []string, err error) { |
|
return |
|
} |
|
|
|
// Group 群组 |
|
type Group struct { |
|
sync.RWMutex |
|
Gid string |
|
uids map[string]session.NetConn |
|
} |
|
|
|
func NewGroup(gid string) *Group { |
|
return &Group{ |
|
Gid: gid, |
|
uids: make(map[string]session.NetConn), |
|
} |
|
} |
|
|
|
func (g *Group) Write(data []byte) { |
|
g.RLock() |
|
defer g.RUnlock() |
|
for uid, conn := range g.uids { |
|
if err := conn.Write(data); err != nil { |
|
logger.Errorf("group send %s.%s error: ", g.Gid, uid, err) |
|
} |
|
} |
|
} |
|
|
|
func (g *Group) Join(uid string, conn session.NetConn) { |
|
g.Lock() |
|
defer g.Unlock() |
|
g.uids[uid] = conn |
|
} |
|
|
|
// Leave delete uid |
|
func (g *Group) Leave(uid string) { |
|
g.Lock() |
|
defer g.Unlock() |
|
delete(g.uids, uid) |
|
} |
|
|
|
// Dismiss 解散 |
|
func (g *Group) Dismiss() { |
|
|
|
} |
|
|
|
func (g *Group) Load(uid string) (conn session.NetConn, ok bool) { |
|
g.RLock() |
|
defer g.RUnlock() |
|
conn, ok = g.uids[uid] |
|
return |
|
}
|
|
|