feat(notify): 重构通信内核并补齐 stream/bulk/record/transfer 能力
- 引入 LogicalConn/TransportConn 分层,ClientConn 保留兼容适配层 - 新增 Stream、Bulk、RecordStream 三条数据面能力及对应控制路径 - 完成 transfer/file 传输内核与状态快照、诊断能力 - 补齐 reconnect、inbound dispatcher、modern psk 等基础模块 - 增加大规模回归、并发与基准测试覆盖 - 更新依赖库
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
package notify
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
)
|
||||
|
||||
func (c *ClientCommon) SetStreamHandler(fn func(StreamAcceptInfo) error) {
|
||||
runtime := c.getStreamRuntime()
|
||||
if runtime == nil {
|
||||
return
|
||||
}
|
||||
runtime.setHandler(fn)
|
||||
}
|
||||
|
||||
func (c *ClientCommon) OpenStream(ctx context.Context, opt StreamOpenOptions) (Stream, error) {
|
||||
if c == nil {
|
||||
return nil, errStreamClientNil
|
||||
}
|
||||
runtime := c.getStreamRuntime()
|
||||
if runtime == nil {
|
||||
return nil, errStreamRuntimeNil
|
||||
}
|
||||
req := clientStreamRequest(runtime, opt)
|
||||
if req.StreamID == "" {
|
||||
return nil, errStreamIDEmpty
|
||||
}
|
||||
if _, exists := runtime.lookup(clientFileScope(), req.StreamID); exists {
|
||||
return nil, errStreamAlreadyExists
|
||||
}
|
||||
resp, err := sendStreamOpenClient(ctx, c, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if resp.DataID != 0 {
|
||||
req.DataID = resp.DataID
|
||||
}
|
||||
stream := newStreamHandle(c.clientStopContextSnapshot(), runtime, clientFileScope(), req, c.currentClientSessionEpoch(), nil, nil, resp.TransportGeneration, clientStreamCloseSender(c), clientStreamResetSender(c), clientStreamDataSender(c, c.currentClientSessionEpoch()), runtime.configSnapshot())
|
||||
stream.setClientSnapshotOwner(c)
|
||||
stream.setAddrSnapshot(c.clientStreamAddrSnapshot())
|
||||
if err := runtime.register(clientFileScope(), stream); err != nil {
|
||||
_, _ = sendStreamResetClient(context.Background(), c, StreamResetRequest{
|
||||
StreamID: req.StreamID,
|
||||
Error: err.Error(),
|
||||
})
|
||||
return nil, err
|
||||
}
|
||||
return stream, nil
|
||||
}
|
||||
|
||||
func (c *ClientCommon) clientStreamAddrSnapshot() (net.Addr, net.Addr) {
|
||||
if c == nil {
|
||||
return nil, nil
|
||||
}
|
||||
conn := c.clientTransportConnSnapshot()
|
||||
if conn == nil {
|
||||
return nil, nil
|
||||
}
|
||||
return conn.LocalAddr(), conn.RemoteAddr()
|
||||
}
|
||||
|
||||
func clientStreamRequest(runtime *streamRuntime, opt StreamOpenOptions) StreamOpenRequest {
|
||||
id := opt.ID
|
||||
if id == "" && runtime != nil {
|
||||
id = runtime.nextID()
|
||||
}
|
||||
return normalizeStreamOpenRequest(StreamOpenRequest{
|
||||
StreamID: id,
|
||||
Channel: opt.Channel,
|
||||
Metadata: cloneStreamMetadata(opt.Metadata),
|
||||
ReadTimeout: opt.ReadTimeout,
|
||||
WriteTimeout: opt.WriteTimeout,
|
||||
})
|
||||
}
|
||||
|
||||
func clientStreamCloseSender(c *ClientCommon) streamCloseSender {
|
||||
return func(ctx context.Context, stream *streamHandle, full bool) error {
|
||||
_, err := sendStreamCloseClient(ctx, c, StreamCloseRequest{
|
||||
StreamID: stream.ID(),
|
||||
Full: full,
|
||||
})
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
func clientStreamResetSender(c *ClientCommon) streamResetSender {
|
||||
return func(ctx context.Context, stream *streamHandle, message string) error {
|
||||
_, err := sendStreamResetClient(ctx, c, StreamResetRequest{
|
||||
StreamID: stream.ID(),
|
||||
Error: message,
|
||||
})
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
func clientStreamDataSender(c *ClientCommon, epoch uint64) streamDataSender {
|
||||
return func(ctx context.Context, stream *streamHandle, chunk []byte) error {
|
||||
if c == nil {
|
||||
return errStreamClientNil
|
||||
}
|
||||
if epoch != 0 && !c.isClientSessionEpochCurrent(epoch) {
|
||||
return errTransportDetached
|
||||
}
|
||||
if ctx != nil {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
default:
|
||||
}
|
||||
}
|
||||
if dataID := stream.dataIDSnapshot(); dataID != 0 {
|
||||
return c.sendFastStreamData(dataID, stream.nextOutboundDataSeq(), chunk)
|
||||
}
|
||||
return c.sendEnvelope(newStreamDataEnvelope(stream.ID(), chunk))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user