mux.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package yamux
  2. import (
  3. "io"
  4. "time"
  5. )
  6. // Config is used to tune the Yamux session
  7. type Config struct {
  8. // AcceptBacklog is used to limit how many streams may be
  9. // waiting an accept.
  10. AcceptBacklog int
  11. // EnableCompression is used to control if we compress
  12. // outgoing data. We have no control over incoming data.
  13. EnableCompression bool
  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. // MaxSessionWindowSize is used to control the maximum
  20. // window size that we allow for a session.
  21. MaxSessionWindowSize uint32
  22. // MaxStreamWindowSize is used to control the maximum
  23. // window size that we allow for a stream.
  24. MaxStreamWindowSize uint32
  25. }
  26. // DefaultConfig is used to return a default configuration
  27. func DefaultConfig() *Config {
  28. return &Config{
  29. AcceptBacklog: 256,
  30. EnableCompression: true,
  31. EnableKeepAlive: true,
  32. KeepAliveInterval: 30 * time.Second,
  33. MaxSessionWindowSize: initialSessionWindow,
  34. MaxStreamWindowSize: initialStreamWindow,
  35. }
  36. }
  37. // Server is used to initialize a new server-side connection.
  38. // There must be at most one server-side connection. If a nil config is
  39. // provided, the DefaultConfiguration will be used.
  40. func Server(conn io.ReadWriteCloser, config *Config) *Session {
  41. if config == nil {
  42. config = DefaultConfig()
  43. }
  44. return newSession(config, conn, false)
  45. }
  46. // Client is used to initialize a new client-side connection.
  47. // There must be at most one client-side connection.
  48. func Client(conn io.ReadWriteCloser, config *Config) *Session {
  49. if config == nil {
  50. config = DefaultConfig()
  51. }
  52. return newSession(config, conn, true)
  53. }