notify/server_inbound_source.go
starainrt 09d972c7b7
feat(notify): 重构通信内核并补齐 stream/bulk/record/transfer 能力
- 引入 LogicalConn/TransportConn 分层,ClientConn 保留兼容适配层
  - 新增 Stream、Bulk、RecordStream 三条数据面能力及对应控制路径
  - 完成 transfer/file 传输内核与状态快照、诊断能力
  - 补齐 reconnect、inbound dispatcher、modern psk 等基础模块
  - 增加大规模回归、并发与基准测试覆盖
  - 更新依赖库
2026-04-15 15:24:36 +08:00

136 lines
3.7 KiB
Go

package notify
import (
"b612.me/stario"
"fmt"
"net"
"time"
)
type serverInboundSource struct {
Source string
Logical *LogicalConn
Conn net.Conn
RemoteAddr net.Addr
TransportGeneration uint64
HasRuntimeConn bool
}
func newServerInboundSource(logical *LogicalConn, conn net.Conn, remoteAddr net.Addr, generation uint64) serverInboundSource {
if remoteAddr == nil && conn != nil {
remoteAddr = conn.RemoteAddr()
}
source := ""
if conn != nil && conn.RemoteAddr() != nil {
source = conn.RemoteAddr().String()
}
if source == "" && logical != nil && logical.ID() != "" {
source = logical.ID()
}
if source == "" && remoteAddr != nil {
source = remoteAddr.String()
}
if source == "" && logical != nil && logical.RemoteAddr() != nil {
source = logical.RemoteAddr().String()
}
return serverInboundSource{
Source: source,
Logical: logical,
Conn: conn,
RemoteAddr: remoteAddr,
TransportGeneration: generation,
HasRuntimeConn: conn != nil,
}
}
func (s *ServerCommon) pushMessageSource(data []byte, source interface{}) {
queue := s.serverQueueSnapshot()
if queue == nil || len(data) == 0 {
return
}
if s.pushMessageSourceFast(queue, data, source) {
return
}
_ = queue.ParseMessage(data, source)
}
func (s *ServerCommon) pushMessageSourceFast(queue *stario.StarQueue, data []byte, source interface{}) bool {
dispatcher := s.serverInboundDispatcherSnapshot()
if queue == nil || dispatcher == nil || len(data) == 0 {
return false
}
if err := queue.ParseMessageOwned(data, source, func(msg stario.MsgQueue) error {
payload := msg.Msg
source := msg.Conn
s.wg.Add(1)
if !dispatcher.Dispatch(serverInboundDispatchSource(source), func() {
defer s.wg.Done()
logical, transport := s.resolveInboundSource(source)
if logical == nil {
return
}
now := time.Now()
inboundConn := serverInboundConn(source)
if err := s.dispatchInboundTransportPayload(logical, transport, inboundConn, payload, now); err != nil {
if s.showError || s.debugMode {
fmt.Println("server decode envelope error", err)
}
}
}) {
s.wg.Done()
}
return nil
}); err != nil && (s.showError || s.debugMode) {
fmt.Println("server parse inbound frame error", err)
}
return true
}
func serverInboundConn(source interface{}) net.Conn {
switch data := source.(type) {
case net.Conn:
return data
case serverInboundSource:
return data.Conn
case *serverInboundSource:
if data != nil {
return data.Conn
}
}
return nil
}
func (s *ServerCommon) resolveInboundSource(source interface{}) (*LogicalConn, *TransportConn) {
switch data := source.(type) {
case serverInboundSource:
return s.resolveInboundSourceValue(data)
case *serverInboundSource:
if data == nil {
return nil, nil
}
return s.resolveInboundSourceValue(*data)
case string:
return s.resolveLogicalBySource(data), nil
default:
return nil, nil
}
}
func (s *ServerCommon) resolveInboundSourceValue(source serverInboundSource) (*LogicalConn, *TransportConn) {
logical := source.Logical
if logical == nil {
logical = s.resolveLogicalBySource(source.Source)
} else if source.HasRuntimeConn {
transport := logical.transportConnSnapshotForInbound(source.Conn, source.RemoteAddr, source.TransportGeneration, source.HasRuntimeConn)
if transport == nil || !transport.Attached() {
if rebound := s.resolveLogicalBySource(source.Source); rebound != nil {
logical = rebound
} else if !logical.Status().Alive {
return nil, nil
}
}
}
transport := logical.transportConnSnapshotForInbound(source.Conn, source.RemoteAddr, source.TransportGeneration, source.HasRuntimeConn)
return logical, transport
}