mux.go 3.7 KB

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