mux.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package yamux
  2. import (
  3. "fmt"
  4. "io"
  5. "log"
  6. "os"
  7. "time"
  8. )
  9. // Config is used to tune the Yamux session
  10. type Config struct {
  11. // AcceptBacklog is used to limit how many streams may be
  12. // waiting an accept.
  13. AcceptBacklog int
  14. // EnableKeepalive is used to do a period keep alive
  15. // messages using a ping.
  16. EnableKeepAlive bool
  17. // KeepAliveInterval is how often to perform the keep alive
  18. KeepAliveInterval time.Duration
  19. // ConnectionWriteTimeout is meant to be a "safety valve" timeout after
  20. // we which will suspect a problem with the underlying connection and
  21. // close it. This is only applied to writes, where's there's generally
  22. // an expectation that things will move along quickly.
  23. ConnectionWriteTimeout time.Duration
  24. // MaxStreamWindowSize is used to control the maximum
  25. // window size that we allow for a stream.
  26. MaxStreamWindowSize uint32
  27. // LogOutput is used to control the log destination. Either Logger or
  28. // LogOutput can be set, not both.
  29. LogOutput io.Writer
  30. // Logger is used to pass in the logger to be used. Either Logger or
  31. // LogOutput can be set, not both.
  32. Logger *log.Logger
  33. }
  34. // DefaultConfig is used to return a default configuration
  35. func DefaultConfig() *Config {
  36. return &Config{
  37. AcceptBacklog: 256,
  38. EnableKeepAlive: true,
  39. KeepAliveInterval: 30 * time.Second,
  40. ConnectionWriteTimeout: 10 * time.Second,
  41. MaxStreamWindowSize: initialStreamWindow,
  42. LogOutput: os.Stderr,
  43. }
  44. }
  45. // VerifyConfig is used to verify the sanity of configuration
  46. func VerifyConfig(config *Config) error {
  47. if config.AcceptBacklog <= 0 {
  48. return fmt.Errorf("backlog must be positive")
  49. }
  50. if config.KeepAliveInterval == 0 {
  51. return fmt.Errorf("keep-alive interval must be positive")
  52. }
  53. if config.MaxStreamWindowSize < initialStreamWindow {
  54. return fmt.Errorf("MaxStreamWindowSize must be larger than %d", initialStreamWindow)
  55. }
  56. if config.LogOutput != nil && config.Logger != nil {
  57. return fmt.Errorf("both Logger and LogOutput may not be set, select one")
  58. } else if config.LogOutput == nil && config.Logger == nil {
  59. return fmt.Errorf("one of Logger or LogOutput must be set, select one")
  60. }
  61. return nil
  62. }
  63. // Server is used to initialize a new server-side connection.
  64. // There must be at most one server-side connection. If a nil config is
  65. // provided, the DefaultConfiguration will be used.
  66. func Server(conn io.ReadWriteCloser, config *Config) (*Session, error) {
  67. if config == nil {
  68. config = DefaultConfig()
  69. }
  70. if err := VerifyConfig(config); err != nil {
  71. return nil, err
  72. }
  73. return newSession(config, conn, false), nil
  74. }
  75. // Client is used to initialize a new client-side connection.
  76. // There must be at most one client-side connection.
  77. func Client(conn io.ReadWriteCloser, config *Config) (*Session, error) {
  78. if config == nil {
  79. config = DefaultConfig()
  80. }
  81. if err := VerifyConfig(config); err != nil {
  82. return nil, err
  83. }
  84. return newSession(config, conn, true), nil
  85. }