bus.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package bus
  2. import (
  3. "reflect"
  4. "strconv"
  5. "sync"
  6. "time"
  7. )
  8. const (
  9. CHAN_INPUT = 0
  10. CHAN_OUTPUT = 1
  11. )
  12. type CbBusChanDisp func(stream []byte, param interface{}) interface{}
  13. type BusChannel struct {
  14. chnID string
  15. mountCnt int
  16. event IBusEvent
  17. bus IBus
  18. timeout time.Duration
  19. conIdList []int
  20. evtArg interface{}
  21. chin chan interface{}
  22. chout chan interface{}
  23. }
  24. func (chn *BusChannel) ID() string {
  25. return chn.chnID
  26. }
  27. func (chn *BusChannel) GetChan(id int) chan interface{} {
  28. switch id {
  29. case 0:
  30. return chn.chin
  31. case 1:
  32. return chn.chout
  33. }
  34. return nil
  35. }
  36. func (chn *BusChannel) SetTimeout(timeout time.Duration) {
  37. }
  38. func (chn *BusChannel) SetEvent(evt IBusEvent, evtArgs interface{}) {
  39. chn.event = evt
  40. chn.evtArg = evtArgs
  41. }
  42. func (chn *BusChannel) GetEvent() IBusEvent {
  43. return chn.event
  44. }
  45. func (chn *BusChannel) GetBus() IBus {
  46. return chn.bus
  47. }
  48. type baseBus struct {
  49. BusId string
  50. ChnList map[string]IChannel
  51. mutex *sync.Mutex
  52. }
  53. func (bus *baseBus) Init() error {
  54. if bus.ChnList == nil {
  55. bus.ChnList = make(map[string]IChannel)
  56. bus.mutex = new(sync.Mutex)
  57. }
  58. return nil
  59. }
  60. func (bus *baseBus) Uninit() {
  61. }
  62. func (bus *baseBus) stringId(chnID interface{}) string {
  63. switch reflect.TypeOf(chnID).Kind() {
  64. case reflect.String:
  65. return chnID.(string)
  66. case reflect.Int:
  67. return strconv.Itoa(chnID.(int))
  68. case reflect.Int64:
  69. return strconv.FormatInt(chnID.(int64), 10)
  70. }
  71. return ""
  72. }
  73. func (bus *baseBus) OpenChannel(chnID interface{}, router []chan interface{}) IChannel {
  74. schnid := bus.stringId(chnID)
  75. if chn, has := bus.ChnList[schnid]; has {
  76. return chn
  77. }
  78. c := &BusChannel{
  79. chnID: schnid,
  80. event: nil,
  81. mountCnt: 0,
  82. bus: bus,
  83. conIdList: make([]int, 0),
  84. timeout: 0,
  85. }
  86. if len(router) > 1 {
  87. c.chout = router[1]
  88. }
  89. if len(router) > 0 {
  90. c.chin = router[0]
  91. }
  92. bus.mutex.Lock()
  93. bus.ChnList[schnid] = c
  94. bus.mutex.Unlock()
  95. return c
  96. }
  97. func (dtu *baseBus) ResetChannel(chn IChannel) {
  98. }
  99. func (bus *baseBus) CloseChannel(chn IChannel) error {
  100. id := chn.ID()
  101. bus.mutex.Lock()
  102. delete(bus.ChnList, id)
  103. bus.mutex.Unlock()
  104. basechn := chn.(*BusChannel)
  105. basechn.mountCnt = 0
  106. if basechn.event != nil {
  107. basechn.event.OnDetach(chn)
  108. }
  109. return nil
  110. }
  111. func (bus *baseBus) ScanChannel(stream []byte, connID int) IChannel {
  112. for _, ichn := range bus.ChnList {
  113. chn := ichn.(*BusChannel)
  114. if chn.event == nil || chn.mountCnt < 0 {
  115. continue
  116. }
  117. if ret := chn.event.ChannelDispatch(stream, chn.evtArg); ret != DispatchNone {
  118. chn.mountCnt += int(ret)
  119. chn.conIdList = append(chn.conIdList, connID)
  120. chn.event.OnAttach(ichn)
  121. return ichn
  122. }
  123. }
  124. return nil
  125. }
  126. func (bus *baseBus) Send(chn IChannel, buff interface{}) (int, error) {
  127. return 0, nil
  128. }
  129. func (bus *baseBus) Recive(chn IChannel, buff interface{}) (int, error) {
  130. return 0, nil
  131. }
  132. type funcRegBus func(param []interface{}) IBus
  133. type funcGetID func(string, []interface{}) string
  134. var BusList map[string]IBus
  135. var BusGetID map[string]funcGetID
  136. var BusReg map[string]funcRegBus
  137. func MountBus(name string, param []interface{}) IBus {
  138. busid := name
  139. if f, has := BusGetID[name]; has {
  140. busid = f(name, param)
  141. }
  142. if p, has := BusList[busid]; has && p != nil {
  143. return p
  144. }
  145. if f, has := BusReg[name]; has && f != nil {
  146. b := f(param)
  147. BusList[busid] = b
  148. return b
  149. }
  150. return nil
  151. }
  152. func init() {
  153. BusList = make(map[string]IBus)
  154. BusReg = make(map[string]funcRegBus)
  155. BusGetID = make(map[string]funcGetID)
  156. }