mirror of https://github.com/synctv-org/synctv
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
168 lines
3.1 KiB
Go
168 lines
3.1 KiB
Go
2 years ago
|
package op
|
||
2 years ago
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
"sync"
|
||
|
"sync/atomic"
|
||
|
|
||
|
"github.com/gorilla/websocket"
|
||
|
log "github.com/sirupsen/logrus"
|
||
|
"github.com/synctv-org/synctv/utils"
|
||
|
"github.com/zijiren233/gencontainer/rwmap"
|
||
|
)
|
||
|
|
||
2 years ago
|
type Hub struct {
|
||
|
id uint
|
||
|
clients rwmap.RWMap[uint, *Client]
|
||
2 years ago
|
broadcast chan *broadcastMessage
|
||
|
exit chan struct{}
|
||
|
closed uint32
|
||
|
wg sync.WaitGroup
|
||
|
}
|
||
|
|
||
|
type broadcastMessage struct {
|
||
|
data Message
|
||
|
sender string
|
||
|
sendToSelf bool
|
||
2 years ago
|
ignoreId []string
|
||
2 years ago
|
}
|
||
|
|
||
|
type BroadcastConf func(*broadcastMessage)
|
||
|
|
||
|
func WithSender(sender string) BroadcastConf {
|
||
|
return func(bm *broadcastMessage) {
|
||
|
bm.sender = sender
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func WithSendToSelf() BroadcastConf {
|
||
|
return func(bm *broadcastMessage) {
|
||
|
bm.sendToSelf = true
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
func WithIgnoreId(id ...string) BroadcastConf {
|
||
2 years ago
|
return func(bm *broadcastMessage) {
|
||
2 years ago
|
bm.ignoreId = append(bm.ignoreId, id...)
|
||
2 years ago
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
func newHub(id uint) *Hub {
|
||
|
return &Hub{
|
||
2 years ago
|
id: id,
|
||
|
broadcast: make(chan *broadcastMessage, 128),
|
||
|
exit: make(chan struct{}),
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
func (h *Hub) Closed() bool {
|
||
2 years ago
|
return atomic.LoadUint32(&h.closed) == 1
|
||
|
}
|
||
|
|
||
|
var (
|
||
|
ErrAlreadyClosed = fmt.Errorf("already closed")
|
||
|
)
|
||
|
|
||
2 years ago
|
func (h *Hub) Start() {
|
||
2 years ago
|
go h.Serve()
|
||
|
}
|
||
|
|
||
2 years ago
|
func (h *Hub) Serve() error {
|
||
2 years ago
|
if h.Closed() {
|
||
|
return ErrAlreadyClosed
|
||
|
}
|
||
|
for {
|
||
|
select {
|
||
|
case message := <-h.broadcast:
|
||
|
h.devMessage(message.data)
|
||
2 years ago
|
h.clients.Range(func(_ uint, cli *Client) bool {
|
||
2 years ago
|
if !message.sendToSelf {
|
||
2 years ago
|
if cli.u.Username == message.sender {
|
||
2 years ago
|
return true
|
||
|
}
|
||
|
}
|
||
2 years ago
|
if utils.In(message.ignoreId, cli.u.Username) {
|
||
2 years ago
|
return true
|
||
|
}
|
||
|
if err := cli.Send(message.data); err != nil {
|
||
2 years ago
|
log.Debugf("hub: %d, write to client err: %s\nmessage: %+v", h.id, err, message)
|
||
2 years ago
|
cli.Close()
|
||
|
}
|
||
|
return true
|
||
|
})
|
||
|
case <-h.exit:
|
||
2 years ago
|
log.Debugf("hub: %d, closed", h.id)
|
||
2 years ago
|
return nil
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
func (h *Hub) devMessage(msg Message) {
|
||
2 years ago
|
switch msg.MessageType() {
|
||
|
case websocket.TextMessage:
|
||
2 years ago
|
log.Debugf("hub: %d, broadcast:\nmessage: %+v", h.id, msg.String())
|
||
2 years ago
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
func (h *Hub) Close() error {
|
||
2 years ago
|
if !atomic.CompareAndSwapUint32(&h.closed, 0, 1) {
|
||
|
return ErrAlreadyClosed
|
||
|
}
|
||
|
close(h.exit)
|
||
2 years ago
|
h.clients.Range(func(_ uint, client *Client) bool {
|
||
2 years ago
|
client.Close()
|
||
|
return true
|
||
|
})
|
||
|
h.wg.Wait()
|
||
|
close(h.broadcast)
|
||
|
return nil
|
||
|
}
|
||
|
|
||
2 years ago
|
func (h *Hub) Broadcast(data Message, conf ...BroadcastConf) error {
|
||
2 years ago
|
h.wg.Add(1)
|
||
|
defer h.wg.Done()
|
||
|
if h.Closed() {
|
||
|
return ErrAlreadyClosed
|
||
|
}
|
||
|
msg := &broadcastMessage{data: data}
|
||
|
for _, c := range conf {
|
||
|
c(msg)
|
||
|
}
|
||
|
select {
|
||
|
case h.broadcast <- msg:
|
||
|
return nil
|
||
|
case <-h.exit:
|
||
|
return ErrAlreadyClosed
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
func (h *Hub) RegClient(cli *Client) (*Client, error) {
|
||
2 years ago
|
if h.Closed() {
|
||
|
return nil, ErrAlreadyClosed
|
||
|
}
|
||
2 years ago
|
c, loaded := h.clients.LoadOrStore(cli.u.ID, cli)
|
||
2 years ago
|
if loaded {
|
||
|
return nil, errors.New("client already registered")
|
||
|
}
|
||
|
return c, nil
|
||
|
}
|
||
|
|
||
2 years ago
|
func (h *Hub) UnRegClient(user *User) error {
|
||
2 years ago
|
if h.Closed() {
|
||
|
return ErrAlreadyClosed
|
||
|
}
|
||
|
if user == nil {
|
||
|
return errors.New("user is nil")
|
||
|
}
|
||
2 years ago
|
_, loaded := h.clients.LoadAndDelete(user.ID)
|
||
2 years ago
|
if !loaded {
|
||
|
return errors.New("client not found")
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
2 years ago
|
func (h *Hub) ClientNum() int64 {
|
||
2 years ago
|
return h.clients.Len()
|
||
2 years ago
|
}
|