msg_loop.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package m
  2. import (
  3. "errors"
  4. "github.com/davy66666/poker-go/src/github.com/davecgh/go-spew/spew"
  5. "github.com/davy66666/poker-go/src/github.com/dolotech/lib/route"
  6. "github.com/davy66666/poker-go/src/github.com/dolotech/lib/utils"
  7. "github.com/davy66666/poker-go/src/github.com/golang/glog"
  8. "github.com/davy66666/poker-go/src/server/protocol"
  9. )
  10. type MsgLoop struct {
  11. closedBroadcastChan chan struct{}
  12. closeChan chan IRoom
  13. msgChan chan *msgObj
  14. route.Route
  15. }
  16. func NewMsgLoop() *MsgLoop {
  17. m := &MsgLoop{
  18. closeChan: make(chan IRoom, 1),
  19. closedBroadcastChan: make(chan struct{}),
  20. msgChan: make(chan *msgObj, 128),
  21. }
  22. go m.msgLoop()
  23. return m
  24. }
  25. func (r *MsgLoop) Closed() chan struct{} {
  26. return r.closedBroadcastChan
  27. }
  28. func (r *MsgLoop) msgLoop() {
  29. defer func() {
  30. if err := utils.PrintPanicStack(); err != nil {
  31. go r.msgLoop()
  32. }
  33. }()
  34. for {
  35. select {
  36. case m := <-r.closeChan:
  37. close(r.closedBroadcastChan)
  38. DelRoom(m.(IRoom))
  39. return
  40. case m := <-r.msgChan:
  41. r.Emit(m.msg, m.o)
  42. }
  43. }
  44. }
  45. func (r *MsgLoop) Close(m IRoom) {
  46. select {
  47. case r.closeChan <- m:
  48. default:
  49. }
  50. }
  51. type msgObj struct {
  52. msg interface{}
  53. o IOccupant
  54. }
  55. func (r *MsgLoop) Send(o IOccupant, m interface{}) error {
  56. select {
  57. case r.msgChan <- &msgObj{m, o}:
  58. default:
  59. o.WriteMsg(protocol.MSG_ROOM_CLOSED)
  60. }
  61. return errors.New("room closed")
  62. }
  63. type Log struct {
  64. room IRoom
  65. }
  66. func NewLog(room IRoom) *Log {
  67. return &Log{room: room}
  68. }
  69. func (r *Log) Info(args ...interface{}) {
  70. glog.InfoDepth(1, r.parseLog(args)...)
  71. }
  72. func (r *Log) Infof(format string, args ...interface{}) {
  73. glog.InfofDepth(1, format, r.parseLog(args)...)
  74. }
  75. func (r *Log) Error(args ...interface{}) {
  76. glog.ErrorDepth(1, r.parseLog(args)...)
  77. }
  78. func (r *Log) Debug(args ...interface{}) {
  79. for k, v := range args {
  80. args[k] = spew.Sdump(v)
  81. }
  82. glog.InfoDepth(1, r.parseLog(args)...)
  83. }
  84. func (r *Log) Debugf(format string, args ...interface{}) {
  85. for k, v := range args {
  86. args[k] = spew.Sdump(v)
  87. }
  88. glog.InfofDepth(1, format, r.parseLog(args)...)
  89. }
  90. func (r *Log) Errorf(format string, args ...interface{}) {
  91. glog.ErrorfDepth(1, format, r.parseLog(args)...)
  92. }
  93. func (r *Log) parseLog(args ...interface{}) []interface{} {
  94. param := make([]interface{}, len(args)+4)
  95. param[0] = r.room.GetNumber()
  96. param[1] = r.room.Cap()
  97. param[2] = r.room.Len()
  98. param[3] = r.room.Data()
  99. copy(param[4:], args)
  100. return param
  101. }