Session_callback.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package lmaxapi
  2. import (
  3. "tickserver/api/lmaxapi/response"
  4. "net/http"
  5. )
  6. type Callback func(err error)
  7. type OrderCallback func(string, error)
  8. type UrlCallback func(resp *http.Response, err error)
  9. type SearchInstrumentCallback func([]*response.Instrument, bool, error)
  10. type HeartbeatCallback func(string, error)
  11. type CompletedOrderCallback func([]*response.CompletedOrder, string, error)
  12. type ActivityCallback func([]*response.Activity, bool, error)
  13. type AccountStatementCallback func([]*response.AccountStatement, bool, error)
  14. type OrderTransactionCallback func([]*response.OrderTransaction, bool, error)
  15. func (this *Session) asynCallback(callback Callback, f func() error) error {
  16. if callback != nil {
  17. this.asynCallWait.Add(1)
  18. go func() {
  19. callback(f())
  20. this.asynCallWait.Done()
  21. }()
  22. return nil
  23. }
  24. return f()
  25. }
  26. func (this *Session) asynUrlCallback(callback UrlCallback, f func() (*http.Response, error)) (*http.Response, error) {
  27. if callback != nil {
  28. this.asynCallWait.Add(1)
  29. go func() {
  30. callback(f())
  31. this.asynCallWait.Done()
  32. }()
  33. return nil, nil
  34. }
  35. return f()
  36. }
  37. func (this *Session) asynSearchInstrumentCallback(callback SearchInstrumentCallback, f func() ([]*response.Instrument, bool, error)) ([]*response.Instrument, bool, error) {
  38. if callback != nil {
  39. this.asynCallWait.Add(1)
  40. go func() {
  41. callback(f())
  42. this.asynCallWait.Done()
  43. }()
  44. return nil, false, nil
  45. }
  46. return f()
  47. }
  48. func (this *Session) asynGetCompletedOrder(callback CompletedOrderCallback, f func() ([]*response.CompletedOrder, string, error)) ([]*response.CompletedOrder, string, error) {
  49. if callback != nil {
  50. this.asynCallWait.Add(1)
  51. go func() {
  52. callback(f())
  53. this.asynCallWait.Done()
  54. }()
  55. return nil, "", nil
  56. }
  57. return f()
  58. }
  59. func (this *Session) asynGetAccountStatement(callback AccountStatementCallback, f func() ([]*response.AccountStatement, bool, error)) ([]*response.AccountStatement, bool, error) {
  60. if callback != nil {
  61. this.asynCallWait.Add(1)
  62. go func() {
  63. callback(f())
  64. this.asynCallWait.Done()
  65. }()
  66. return nil, false, nil
  67. }
  68. return f()
  69. }
  70. func (this *Session) asynGetOrderTransaction(callback OrderTransactionCallback, f func() ([]*response.OrderTransaction, bool, error)) ([]*response.OrderTransaction, bool, error) {
  71. if callback != nil {
  72. this.asynCallWait.Add(1)
  73. go func() {
  74. callback(f())
  75. this.asynCallWait.Done()
  76. }()
  77. return nil, false, nil
  78. }
  79. return f()
  80. }
  81. func (this *Session) asynGetActivity(callback ActivityCallback, f func() ([]*response.Activity, bool, error)) ([]*response.Activity, bool, error) {
  82. if callback != nil {
  83. this.asynCallWait.Add(1)
  84. go func() {
  85. callback(f())
  86. this.asynCallWait.Done()
  87. }()
  88. return nil, false, nil
  89. }
  90. return f()
  91. }
  92. func (this *Session) asynOrderCallback(callback OrderCallback, f func() (string, error)) (string, error) {
  93. if callback != nil {
  94. this.asynCallWait.Add(1)
  95. go func() {
  96. callback(f())
  97. this.asynCallWait.Done()
  98. }()
  99. return "", nil
  100. }
  101. return f()
  102. }
  103. func (this *Session) asynHeartbeatCallback(callback HeartbeatCallback, f func() (string, error)) (string, error) {
  104. if callback != nil {
  105. this.asynCallWait.Add(1)
  106. go func() {
  107. callback(f())
  108. this.asynCallWait.Done()
  109. }()
  110. return "", nil
  111. }
  112. return f()
  113. }
  114. func (this *Session) Wait() {
  115. this.asynCallWait.Wait()
  116. }