136 lines
3.7 KiB
Go
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
|
||
|
|
}
|