mux.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. // StreamCloseTimeout is the maximum time that a stream will allowed to
  28. // be in a half-closed state when `Close` is called before forcibly
  29. // closing the connection. Forcibly closed connections will empty the
  30. // receive buffer, drop any future packets received for that stream,
  31. // and send a RST to the remote side.
  32. StreamCloseTimeout time.Duration
  33. // LogOutput is used to control the log destination. Either Logger or
  34. // LogOutput can be set, not both.
  35. LogOutput io.Writer
  36. // Logger is used to pass in the logger to be used. Either Logger or
  37. // LogOutput can be set, not both.
  38. Logger *log.Logger
  39. }
  40. // DefaultConfig is used to return a default configuration
  41. func DefaultConfig() *Config {
  42. return &Config{
  43. AcceptBacklog: 256,
  44. EnableKeepAlive: true,
  45. KeepAliveInterval: 30 * time.Second,
  46. ConnectionWriteTimeout: 10 * time.Second,
  47. MaxStreamWindowSize: initialStreamWindow,
  48. StreamCloseTimeout: 5 * time.Minute,
  49. LogOutput: os.Stderr,
  50. }
  51. }
  52. // VerifyConfig is used to verify the sanity of configuration
  53. func VerifyConfig(config *Config) error {
  54. if config.AcceptBacklog <= 0 {
  55. return fmt.Errorf("backlog must be positive")
  56. }
  57. if config.KeepAliveInterval == 0 {
  58. return fmt.Errorf("keep-alive interval must be positive")
  59. }
  60. if config.MaxStreamWindowSize < initialStreamWindow {
  61. return fmt.Errorf("MaxStreamWindowSize must be larger than %d", initialStreamWindow)
  62. }
  63. if config.LogOutput != nil && config.Logger != nil {
  64. return fmt.Errorf("both Logger and LogOutput may not be set, select one")
  65. } else if config.LogOutput == nil && config.Logger == nil {
  66. return fmt.Errorf("one of Logger or LogOutput must be set, select one")
  67. }
  68. return nil
  69. }
  70. // Server is used to initialize a new server-side connection.
  71. // There must be at most one server-side connection. If a nil config is
  72. // provided, the DefaultConfiguration will be used.
  73. func Server(conn io.ReadWriteCloser, config *Config) (*Session, error) {
  74. if config == nil {
  75. config = DefaultConfig()
  76. }
  77. if err := VerifyConfig(config); err != nil {
  78. return nil, err
  79. }
  80. return newSession(config, conn, false), nil
  81. }
  82. // Client is used to initialize a new client-side connection.
  83. // There must be at most one client-side connection.
  84. func Client(conn io.ReadWriteCloser, config *Config) (*Session, error) {
  85. if config == nil {
  86. config = DefaultConfig()
  87. }
  88. if err := VerifyConfig(config); err != nil {
  89. return nil, err
  90. }
  91. return newSession(config, conn, true), nil
  92. }