forked from v2ray/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
38 lines (30 loc) · 823 Bytes
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package internet
import (
"context"
"v2ray.com/core/common/net"
)
type key int
const (
streamSettingsKey key = iota
dialerSrcKey
bindAddrKey
)
func ContextWithStreamSettings(ctx context.Context, streamSettings *MemoryStreamConfig) context.Context {
return context.WithValue(ctx, streamSettingsKey, streamSettings)
}
func StreamSettingsFromContext(ctx context.Context) *MemoryStreamConfig {
ss := ctx.Value(streamSettingsKey)
if ss == nil {
return nil
}
return ss.(*MemoryStreamConfig)
}
func ContextWithBindAddress(ctx context.Context, dest net.Destination) context.Context {
return context.WithValue(ctx, bindAddrKey, dest)
}
func BindAddressFromContext(ctx context.Context) net.Destination {
if addr, ok := ctx.Value(bindAddrKey).(net.Destination); ok {
return addr
}
return net.Destination{}
}