mux.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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
  27. LogOutput io.Writer
  28. }
  29. // DefaultConfig is used to return a default configuration
  30. func DefaultConfig() *Config {
  31. return &Config{
  32. AcceptBacklog: 256,
  33. EnableKeepAlive: true,
  34. KeepAliveInterval: 30 * time.Second,
  35. ConnectionWriteTimeout: 10 * time.Second,
  36. MaxStreamWindowSize: initialStreamWindow,
  37. LogOutput: os.Stderr,
  38. }
  39. }
  40. // VerifyConfig is used to verify the sanity of configuration
  41. func VerifyConfig(config *Config) error {
  42. if config.AcceptBacklog <= 0 {
  43. return fmt.Errorf("backlog must be positive")
  44. }
  45. if config.KeepAliveInterval == 0 {
  46. return fmt.Errorf("keep-alive interval must be positive")
  47. }
  48. if config.MaxStreamWindowSize < initialStreamWindow {
  49. return fmt.Errorf("MaxStreamWindowSize must be larger than %d", initialStreamWindow)
  50. }
  51. return nil
  52. }
  53. // Server is used to initialize a new server-side connection.
  54. // There must be at most one server-side connection. If a nil config is
  55. // provided, the DefaultConfiguration will be used.
  56. func Server(conn io.ReadWriteCloser, config *Config) (*Session, error) {
  57. if config == nil {
  58. config = DefaultConfig()
  59. }
  60. if err := VerifyConfig(config); err != nil {
  61. return nil, err
  62. }
  63. return newSession(config, conn, false), nil
  64. }
  65. // Client is used to initialize a new client-side connection.
  66. // There must be at most one client-side connection.
  67. func Client(conn io.ReadWriteCloser, config *Config) (*Session, error) {
  68. if config == nil {
  69. config = DefaultConfig()
  70. }
  71. if err := VerifyConfig(config); err != nil {
  72. return nil, err
  73. }
  74. return newSession(config, conn, true), nil
  75. }