ihub.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package nnet
  2. import (
  3. "sync"
  4. "time"
  5. )
  6. var (
  7. //
  8. DefHubConfig = HubConfig{
  9. SizeOfSendChan: 1024,
  10. SizeOfRecvChan: 1024,
  11. ReadBufSize: 2048,
  12. WriteBufSize: 2048,
  13. Timeout: 3 * time.Second,
  14. Tick: 10 * time.Second,
  15. ReadTimeout: 12 * time.Second, // should greater than Tick
  16. }
  17. )
  18. type HubConfig struct {
  19. SizeOfSendChan uint32
  20. SizeOfRecvChan uint32
  21. ReadBufSize int // used in websocket
  22. WriteBufSize int // used in websocket
  23. Timeout time.Duration // for write && listener
  24. Tick time.Duration // heartbeat callback interval
  25. ReadTimeout time.Duration // read timeout
  26. }
  27. type IHub interface {
  28. Lock() // support locker semantics
  29. Unlock()
  30. Wg() *sync.WaitGroup // for waiting goroutines quit
  31. ChQuit() <-chan struct{} // notify to quit
  32. Conf() *HubConfig //
  33. Callback() ISessionCallback // callback impl
  34. Protocol() IProtocol // protocol impl
  35. Start() error // start hub
  36. Stop() error // stop hub
  37. DoJob(int) // not used now
  38. NewConnection(string, uint64) error
  39. StartReconn(string, uint64)
  40. PutSession(uint64, ISession) error // session management base on id
  41. DelSession(uint64) error
  42. GetSession(uint64) (ISession, error)
  43. PeekSession(uint64) (ISession, error)
  44. RandSession() (ISession, error)
  45. GetSessionNum() int
  46. GetAllSessions() map[uint64]ISession
  47. }