syncrecv.go 849 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package rocommon
  2. import (
  3. "sync"
  4. )
  5. type NetSyncRecv struct {
  6. eventCh chan ProcEvent
  7. cb func(ev ProcEvent)
  8. sess Session
  9. }
  10. func NewNetSyncRecv(node ServerNode) *NetSyncRecv {
  11. this := &NetSyncRecv{
  12. eventCh: make(chan ProcEvent),
  13. }
  14. this.cb = func(ev ProcEvent) {
  15. this.eventCh <- ev
  16. }
  17. return this
  18. }
  19. func (this *NetSyncRecv) EventCB() EventCallBack {
  20. return this.cb
  21. }
  22. func (this *NetSyncRecv) Recv(cb EventCallBack) *NetSyncRecv {
  23. cb(<-this.eventCh)
  24. return this
  25. }
  26. func (this *NetSyncRecv) WaitMsg(msg interface{}) {
  27. var wg sync.WaitGroup
  28. wg.Add(1)
  29. this.Recv(func(ev ProcEvent) {
  30. //msgType := reflect.TypeOf(msg)
  31. switch ev.Msg().(type) {
  32. case *SessionConnected:
  33. msg = ev.Msg()
  34. this.sess = ev.Session()
  35. wg.Done()
  36. }
  37. })
  38. wg.Wait()
  39. return
  40. }
  41. func (this *NetSyncRecv) Session() Session {
  42. return this.sess
  43. }