mux.go 2.9 KB

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