2026-04-15 15:24:36 +08:00
|
|
|
package notify
|
|
|
|
|
|
|
|
|
|
import "context"
|
|
|
|
|
|
|
|
|
|
func (c *ClientCommon) SetRecordStreamHandler(fn func(RecordAcceptInfo) error) {
|
|
|
|
|
runtime := c.getRecordRuntime()
|
|
|
|
|
if runtime == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
runtime.setHandler(fn)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *ClientCommon) OpenRecordStream(ctx context.Context, opt RecordOpenOptions) (RecordStream, error) {
|
|
|
|
|
if c == nil {
|
|
|
|
|
return nil, errStreamClientNil
|
|
|
|
|
}
|
|
|
|
|
opt = normalizeRecordOpenOptions(opt)
|
|
|
|
|
stream, err := c.OpenStream(ctx, 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, c.getRecordRuntime())
|
2026-04-15 15:24:36 +08:00
|
|
|
return record, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *ClientCommon) claimInboundRecordStream(stream *streamHandle) (bool, error) {
|
|
|
|
|
if stream == nil || stream.Channel() != StreamRecordChannel {
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
runtime := c.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(),
|
|
|
|
|
TransportGeneration: stream.TransportGeneration(),
|
|
|
|
|
RecordStream: record,
|
|
|
|
|
}
|
|
|
|
|
go func() {
|
|
|
|
|
if err := handler(info); err != nil {
|
|
|
|
|
_ = record.Reset(err)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
return true, nil
|
|
|
|
|
}
|