생성 삭제 채널을 밖으로 뺌

This commit is contained in:
2023-12-26 00:18:15 +09:00
parent e42c1969a2
commit d1e892d449

View File

@ -22,22 +22,10 @@ type WebsocketPeerHandler interface {
RegisterHandlers(serveMux *http.ServeMux, prefix string) error RegisterHandlers(serveMux *http.ServeMux, prefix string) error
} }
type connEstChannelValue struct {
accid primitive.ObjectID
conn *websocket.Conn
}
type connDisChannelValue struct {
accid primitive.ObjectID
closed bool
}
type websocketPeerHandler[T PeerInterface] struct { type websocketPeerHandler[T PeerInterface] struct {
methods map[string]peerApiFuncType[T] methods map[string]peerApiFuncType[T]
createPeer func(primitive.ObjectID) T createPeer func(primitive.ObjectID) T
sessionConsumer session.Consumer sessionConsumer session.Consumer
connEstChannel chan connEstChannelValue
connDisChannel chan connDisChannelValue
} }
type PeerInterface interface { type PeerInterface interface {
@ -156,24 +144,12 @@ func NewWebsocketPeerHandler[T PeerInterface](consumer session.Consumer, creator
sessionConsumer: consumer, sessionConsumer: consumer,
methods: methods, methods: methods,
createPeer: creator, createPeer: creator,
connEstChannel: make(chan connEstChannelValue),
connDisChannel: make(chan connDisChannelValue),
} }
consumer.RegisterOnSessionInvalidated(wsh.onSessionInvalidated)
return wsh return wsh
} }
func (ws *websocketPeerHandler[T]) onSessionInvalidated(accid primitive.ObjectID) {
ws.connDisChannel <- connDisChannelValue{
accid: accid,
closed: false,
}
}
func (ws *websocketPeerHandler[T]) RegisterHandlers(serveMux *http.ServeMux, prefix string) error { func (ws *websocketPeerHandler[T]) RegisterHandlers(serveMux *http.ServeMux, prefix string) error {
go ws.sessionMonitoring()
if *noAuthFlag { if *noAuthFlag {
serveMux.HandleFunc(prefix, ws.upgrade_noauth) serveMux.HandleFunc(prefix, ws.upgrade_noauth)
} else { } else {
@ -183,33 +159,14 @@ func (ws *websocketPeerHandler[T]) RegisterHandlers(serveMux *http.ServeMux, pre
return nil return nil
} }
func (ws *websocketPeerHandler[T]) sessionMonitoring() {
all := make(map[primitive.ObjectID]*websocket.Conn)
for {
select {
case estVal := <-ws.connEstChannel:
all[estVal.accid] = estVal.conn
case disVal := <-ws.connDisChannel:
if disVal.closed {
delete(all, disVal.accid)
} else if c := all[disVal.accid]; c != nil {
c.Close()
delete(all, disVal.accid)
}
}
}
}
func (ws *websocketPeerHandler[T]) upgrade_core(conn *websocket.Conn, accid primitive.ObjectID, nonce uint32) { func (ws *websocketPeerHandler[T]) upgrade_core(conn *websocket.Conn, accid primitive.ObjectID, nonce uint32) {
go func(c *websocket.Conn, accid primitive.ObjectID) { go func(c *websocket.Conn, accid primitive.ObjectID) {
peer := ws.createPeer(accid) peer := ws.createPeer(accid)
var closeReason string var closeReason string
peer.ClientConnected(conn) peer.ClientConnected(conn)
ws.connEstChannel <- connEstChannelValue{accid: accid, conn: conn}
defer func() { defer func() {
ws.connDisChannel <- connDisChannelValue{accid: accid, closed: true}
peer.ClientDisconnected(closeReason) peer.ClientDisconnected(closeReason)
}() }()