feat: 接线 serve 真实 auth/注册/管理/MQTT 与消息循环
This commit is contained in:
@@ -67,6 +67,17 @@ func (c *MemoryConns) Current(endpointID string) (LiveConn, bool) {
|
||||
return v, ok
|
||||
}
|
||||
|
||||
// Snapshot 返回当前连接表副本(供推送循环遍历)。
|
||||
func (c *MemoryConns) Snapshot() map[string]LiveConn {
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
out := make(map[string]LiveConn, len(c.m))
|
||||
for k, v := range c.m {
|
||||
out[k] = v
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// RecordingDownlink 记录下行发布,供测试断言。
|
||||
type RecordingDownlink struct {
|
||||
mu sync.Mutex
|
||||
|
||||
@@ -58,11 +58,16 @@ func (AllowAuthenticator) Authenticate(context.Context, string, []byte, string)
|
||||
return AuthResult{OK: true}, nil
|
||||
}
|
||||
|
||||
// PublishDroppedFunc 下行未写入发送队列时回调(对接消息线 OnPublishDropped)。
|
||||
type PublishDroppedFunc func(ctx context.Context, endpointID string, connID port.ConnID, payload []byte)
|
||||
|
||||
// Options 装配 broker。
|
||||
type Options struct {
|
||||
Authenticator Authenticator
|
||||
Uplink port.UplinkHandler
|
||||
Logger *slog.Logger
|
||||
// OnPublishDropped 可选;nil 时仅打 debug 日志。
|
||||
OnPublishDropped PublishDroppedFunc
|
||||
}
|
||||
|
||||
// Broker 内置 mochi,不自带监听端口。
|
||||
@@ -71,6 +76,7 @@ type Broker struct {
|
||||
auth Authenticator
|
||||
uplink port.UplinkHandler
|
||||
log *slog.Logger
|
||||
onDrop PublishDroppedFunc
|
||||
|
||||
hook *nixHook
|
||||
|
||||
@@ -144,6 +150,7 @@ func New(opts Options) (*Broker, error) {
|
||||
auth: auth,
|
||||
uplink: uplink,
|
||||
log: log,
|
||||
onDrop: opts.OnPublishDropped,
|
||||
current: make(map[string]*connState),
|
||||
byClient: make(map[*mqtt.Client]*connState),
|
||||
queues: make(map[string]*uplinkQueue),
|
||||
|
||||
@@ -159,6 +159,16 @@ func (h *nixHook) OnSubscribed(cl *mqtt.Client, pk packets.Packet, reasonCodes [
|
||||
|
||||
func (h *nixHook) OnPublishDropped(cl *mqtt.Client, pk packets.Packet) {
|
||||
h.b.log.Debug("publish dropped", "client", cl.ID, "topic", pk.TopicName, "size", len(pk.Payload))
|
||||
if h.b.onDrop == nil {
|
||||
return
|
||||
}
|
||||
h.b.connsMu.RLock()
|
||||
st := h.b.byClient[cl]
|
||||
h.b.connsMu.RUnlock()
|
||||
if st == nil {
|
||||
return
|
||||
}
|
||||
h.b.onDrop(context.Background(), st.endpointID, st.connID, append([]byte(nil), pk.Payload...))
|
||||
}
|
||||
|
||||
func (h *nixHook) OnSessionEstablished(cl *mqtt.Client, _ packets.Packet) {
|
||||
|
||||
@@ -101,6 +101,11 @@ func (s *Session) Attach(b *Broker) {
|
||||
s.b = b
|
||||
}
|
||||
|
||||
// SetPresence 接线时在创建最终 presence 实现后注入(可替换占位)。
|
||||
func (s *Session) SetPresence(p PresenceSink) {
|
||||
s.presence = p
|
||||
}
|
||||
|
||||
func (s *Session) OnSessionEstablished(ctx context.Context, conn port.ConnInfo) error {
|
||||
if s.b != nil {
|
||||
s.b.startHandshakeDeadline(conn.EndpointID, conn.ConnID, handshakeTimeout)
|
||||
|
||||
Reference in New Issue
Block a user