2026-04-15 15:24:36 +08:00
|
|
|
package notify
|
|
|
|
|
|
|
|
|
|
import "context"
|
|
|
|
|
|
|
|
|
|
func (s *ServerCommon) SetRecordStreamHandler(fn func(RecordAcceptInfo) error) {
|
|
|
|
|
runtime := s.getRecordRuntime()
|
|
|
|
|
if runtime == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
runtime.setHandler(fn)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *ServerCommon) OpenRecordStreamLogical(ctx context.Context, logical *LogicalConn, opt RecordOpenOptions) (RecordStream, error) {
|
|
|
|
|
if s == nil {
|
|
|
|
|
return nil, errStreamServerNil
|
|
|
|
|
}
|
|
|
|
|
opt = normalizeRecordOpenOptions(opt)
|
|
|
|
|
stream, err := s.OpenStreamLogical(ctx, logical, opt.Stream)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
record, err := WrapStreamAsRecord(stream, opt)
|
|
|
|
|
if err != nil {
|
|
|
|
|
_ = stream.Reset(err)
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2026-04-15 19:52:45 +08:00
|
|
|
bindRecordRuntime(record, s.getRecordRuntime())
|
2026-04-15 15:24:36 +08:00
|
|
|
return record, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *ServerCommon) OpenRecordStreamTransport(ctx context.Context, transport *TransportConn, opt RecordOpenOptions) (RecordStream, error) {
|
|
|
|
|
if s == nil {
|
|
|
|
|
return nil, errStreamServerNil
|
|
|
|
|
}
|
|
|
|
|
opt = normalizeRecordOpenOptions(opt)
|
|
|
|
|
stream, err := s.OpenStreamTransport(ctx, transport, opt.Stream)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
record, err := WrapStreamAsRecord(stream, opt)
|
|
|
|
|
if err != nil {
|
|
|
|
|
_ = stream.Reset(err)
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2026-04-15 19:52:45 +08:00
|
|
|
bindRecordRuntime(record, s.getRecordRuntime())
|
2026-04-15 15:24:36 +08:00
|
|
|
return record, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *ServerCommon) claimInboundRecordStream(logical *LogicalConn, transport *TransportConn, stream *streamHandle) (bool, error) {
|
|
|
|
|
if stream == nil || stream.Channel() != StreamRecordChannel {
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
runtime := s.getRecordRuntime()
|
|
|
|
|
if runtime == nil {
|
|
|
|
|
return true, errRecordRuntimeNil
|
|
|
|
|
}
|
|
|
|
|
handler := runtime.handlerSnapshot()
|
|
|
|
|
if handler == nil {
|
|
|
|
|
return true, errRecordHandlerNotConfigured
|
|
|
|
|
}
|
|
|
|
|
record, err := WrapStreamAsRecord(stream, RecordOpenOptions{
|
|
|
|
|
Stream: StreamOpenOptions{
|
|
|
|
|
ID: stream.ID(),
|
|
|
|
|
Channel: stream.Channel(),
|
|
|
|
|
Metadata: stream.Metadata(),
|
|
|
|
|
ReadTimeout: stream.readTimeoutSnapshot(),
|
|
|
|
|
WriteTimeout: stream.writeTimeoutSnapshot(),
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return true, err
|
|
|
|
|
}
|
2026-04-15 19:52:45 +08:00
|
|
|
bindRecordRuntime(record, runtime)
|
2026-04-15 15:24:36 +08:00
|
|
|
info := RecordAcceptInfo{
|
|
|
|
|
ID: stream.ID(),
|
|
|
|
|
Metadata: stream.Metadata(),
|
|
|
|
|
LogicalConn: logical,
|
|
|
|
|
TransportConn: transport,
|
|
|
|
|
TransportGeneration: stream.TransportGeneration(),
|
|
|
|
|
RecordStream: record,
|
|
|
|
|
}
|
|
|
|
|
go func() {
|
|
|
|
|
if err := handler(info); err != nil {
|
|
|
|
|
_ = record.Reset(err)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
return true, nil
|
|
|
|
|
}
|