context.go 823 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package internet
  2. import (
  3. "context"
  4. "v2ray.com/core/common/net"
  5. )
  6. type key int
  7. const (
  8. streamSettingsKey key = iota
  9. dialerSrcKey
  10. bindAddrKey
  11. )
  12. func ContextWithStreamSettings(ctx context.Context, streamSettings *MemoryStreamConfig) context.Context {
  13. return context.WithValue(ctx, streamSettingsKey, streamSettings)
  14. }
  15. func StreamSettingsFromContext(ctx context.Context) *MemoryStreamConfig {
  16. ss := ctx.Value(streamSettingsKey)
  17. if ss == nil {
  18. return nil
  19. }
  20. return ss.(*MemoryStreamConfig)
  21. }
  22. func ContextWithBindAddress(ctx context.Context, dest net.Destination) context.Context {
  23. return context.WithValue(ctx, bindAddrKey, dest)
  24. }
  25. func BindAddressFromContext(ctx context.Context) net.Destination {
  26. if addr, ok := ctx.Value(bindAddrKey).(net.Destination); ok {
  27. return addr
  28. }
  29. return net.Destination{}
  30. }