123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- package yamux
- import (
- "fmt"
- "io"
- "log"
- "os"
- "time"
- )
- type Config struct {
-
-
- AcceptBacklog int
-
-
- EnableKeepAlive bool
-
- KeepAliveInterval time.Duration
-
-
-
-
- ConnectionWriteTimeout time.Duration
-
-
- MaxStreamWindowSize uint32
-
-
-
-
-
- StreamOpenTimeout time.Duration
-
-
-
-
-
- StreamCloseTimeout time.Duration
-
-
- LogOutput io.Writer
-
-
- Logger *log.Logger
- }
- func DefaultConfig() *Config {
- return &Config{
- AcceptBacklog: 256,
- EnableKeepAlive: true,
- KeepAliveInterval: 30 * time.Second,
- ConnectionWriteTimeout: 10 * time.Second,
- MaxStreamWindowSize: initialStreamWindow,
- StreamCloseTimeout: 5 * time.Minute,
- StreamOpenTimeout: 75 * time.Second,
- LogOutput: os.Stderr,
- }
- }
- func VerifyConfig(config *Config) error {
- if config.AcceptBacklog <= 0 {
- return fmt.Errorf("backlog must be positive")
- }
- if config.KeepAliveInterval == 0 {
- return fmt.Errorf("keep-alive interval must be positive")
- }
- if config.MaxStreamWindowSize < initialStreamWindow {
- return fmt.Errorf("MaxStreamWindowSize must be larger than %d", initialStreamWindow)
- }
- if config.LogOutput != nil && config.Logger != nil {
- return fmt.Errorf("both Logger and LogOutput may not be set, select one")
- } else if config.LogOutput == nil && config.Logger == nil {
- return fmt.Errorf("one of Logger or LogOutput must be set, select one")
- }
- return nil
- }
- func Server(conn io.ReadWriteCloser, config *Config) (*Session, error) {
- if config == nil {
- config = DefaultConfig()
- }
- if err := VerifyConfig(config); err != nil {
- return nil, err
- }
- return newSession(config, conn, false), nil
- }
- func Client(conn io.ReadWriteCloser, config *Config) (*Session, error) {
- if config == nil {
- config = DefaultConfig()
- }
- if err := VerifyConfig(config); err != nil {
- return nil, err
- }
- return newSession(config, conn, true), nil
- }
|