bus.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. TsLast uint64
  51. ChnList map[string]IChannel
  52. mutex *sync.Mutex
  53. }
  54. func (bus *baseBus) Init() error {
  55. if bus.ChnList == nil {
  56. bus.ChnList = make(map[string]IChannel)
  57. bus.mutex = new(sync.Mutex)
  58. }
  59. return nil
  60. }
  61. func (bus *baseBus) Uninit() {
  62. }
  63. func (bus *baseBus) stringId(chnID interface{}) string {
  64. switch reflect.TypeOf(chnID).Kind() {
  65. case reflect.String:
  66. return chnID.(string)
  67. case reflect.Int:
  68. return strconv.Itoa(chnID.(int))
  69. case reflect.Int64:
  70. return strconv.FormatInt(chnID.(int64), 10)
  71. }
  72. return ""
  73. }
  74. func (bus *baseBus) MountChannel(chnID interface{}, router []chan interface{}) IChannel {
  75. schnid := bus.stringId(chnID)
  76. if chn, has := bus.ChnList[schnid]; has {
  77. return chn
  78. }
  79. c := &BusChannel{
  80. chnID: schnid,
  81. event: nil,
  82. mountCnt: 0,
  83. bus: bus,
  84. conIdList: make([]int, 0),
  85. timeout: 0,
  86. }
  87. if len(router) > 1 {
  88. c.chout = router[1]
  89. }
  90. if len(router) > 0 {
  91. c.chin = router[0]
  92. }
  93. bus.mutex.Lock()
  94. bus.ChnList[schnid] = c
  95. bus.mutex.Unlock()
  96. return c
  97. }
  98. func (dtu *baseBus) ResetChannel(chn IChannel) {
  99. }
  100. func (bus *baseBus) FreeChannel(chn IChannel) error {
  101. id := chn.ID()
  102. bus.mutex.Lock()
  103. delete(bus.ChnList, id)
  104. bus.mutex.Unlock()
  105. basechn := chn.(*BusChannel)
  106. basechn.mountCnt = 0
  107. if basechn.event != nil {
  108. basechn.event.OnDetach(chn)
  109. }
  110. return nil
  111. }
  112. func (bus *baseBus) ScanChannel(stream []byte, connID int) IChannel {
  113. for _, ichn := range bus.ChnList {
  114. chn := ichn.(*BusChannel)
  115. if chn.event == nil || chn.mountCnt < 0 {
  116. continue
  117. }
  118. if ret := chn.event.ChannelDispatch(stream, chn.evtArg); ret != DispatchNone {
  119. chn.mountCnt += int(ret)
  120. chn.conIdList = append(chn.conIdList, connID)
  121. chn.event.OnAttach(ichn)
  122. return ichn
  123. }
  124. }
  125. return nil
  126. }
  127. func (bus *baseBus) Send(chn IChannel, buff interface{}) (int, error) {
  128. bus.TsLast = uint64(time.Now().Unix())
  129. return 0, nil
  130. }
  131. func (bus *baseBus) Recive(chn IChannel, buff interface{}) (int, error) {
  132. bus.TsLast = uint64(time.Now().Unix())
  133. return 0, nil
  134. }
  135. func (bus *baseBus) ApplyPatch(ptchName string, param ...interface{}) {
  136. }
  137. func (bus *baseBus) TimeStamp() int64 {
  138. return int64(bus.TsLast)
  139. }
  140. type funcRegBus func(param []interface{}) IBus
  141. type funcGetID func(string, []interface{}) string
  142. var BusList map[string]IBus
  143. var BusGetID map[string]funcGetID
  144. var BusReg map[string]funcRegBus
  145. func MountBus(name string, param []interface{}) IBus {
  146. busid := name
  147. if f, has := BusGetID[name]; has {
  148. busid = f(name, param)
  149. }
  150. if p, has := BusList[busid]; has && p != nil {
  151. return p
  152. }
  153. if f, has := BusReg[name]; has && f != nil {
  154. b := f(param)
  155. BusList[busid] = b
  156. return b
  157. }
  158. return nil
  159. }
  160. func init() {
  161. BusList = make(map[string]IBus)
  162. BusReg = make(map[string]funcRegBus)
  163. BusGetID = make(map[string]funcGetID)
  164. }