client.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package server
  2. import (
  3. "dlt645-server/protocol"
  4. "github.com/gogf/gf/net/gtcp"
  5. "github.com/gogf/gf/os/glog"
  6. "io"
  7. "net"
  8. "strings"
  9. "syscall"
  10. "time"
  11. )
  12. type Client struct {
  13. Id string
  14. Address []byte
  15. srv *Server
  16. conn *gtcp.Conn
  17. sendChan chan []byte
  18. closeChan chan struct{}
  19. closeHandler func(id string, c *Client)
  20. lastHeartBeat time.Time
  21. done chan struct{}
  22. gatewayId uint16
  23. isReg bool
  24. }
  25. func (c *Client) ReadLoop(ctx *protocol.PacketContext) {
  26. //defer c.srv.grWG.Done()
  27. for {
  28. buf, err := c.conn.Recv(-1)
  29. if err != nil {
  30. c.readError(err)
  31. return
  32. }
  33. if len(buf) > 0 {
  34. result, err := c.srv.message.Decode(ctx, buf)
  35. if err != nil {
  36. glog.Errorf("解析报文失败:%s", err.Error())
  37. }
  38. //if c.Id == "" && ctx.GetId() != "" {
  39. c.SetId("220714031227")
  40. //}
  41. if c.Id != "" {
  42. if err := c.srv.ReportStatus(c.Id, result); err != nil {
  43. c.readError(err)
  44. return
  45. }
  46. }
  47. }
  48. }
  49. }
  50. func (c *Client) SetId(id string) {
  51. c.Id = id
  52. }
  53. func (c *Client) SetAddress(address []byte) {
  54. c.Address = address
  55. }
  56. func (c *Client) readError(err error) {
  57. defer c.closeConnection()
  58. if err == io.EOF || isErrConnReset(err) {
  59. return
  60. }
  61. glog.Errorf("读取数据发生错误:%s", err.Error())
  62. }
  63. func (c *Client) closeConnection() {
  64. _ = c.conn.Close()
  65. c.conn = nil
  66. close(c.done)
  67. c.SetId("")
  68. c.isReg = false
  69. if c.closeHandler != nil {
  70. c.closeHandler(c.Id, c)
  71. }
  72. }
  73. // isErrConnReset read: connection reset by peer
  74. func isErrConnReset(err error) bool {
  75. if ne, ok := err.(*net.OpError); ok {
  76. return strings.Contains(ne.Err.Error(), syscall.ECONNRESET.Error())
  77. }
  78. return false
  79. }
  80. func (c *Client) send(buf []byte) error {
  81. if c.conn == nil {
  82. return nil
  83. }
  84. err := c.conn.Send(buf)
  85. if err != nil {
  86. glog.Error(err)
  87. c.closeConnection()
  88. return err
  89. }
  90. glog.Debugf("指令发送成功:%2X", buf)
  91. return nil
  92. }
  93. func (c *Client) Send0X3433(ctx *protocol.PacketContext) {
  94. //defer c.srv.grWG.Done()
  95. entity := protocol.Dlt_0x33333433{}
  96. for {
  97. if ctx.GetReceiveAddress() != nil {
  98. sendByte, _ := entity.Encode(ctx)
  99. err := c.send(sendByte)
  100. if err != nil {
  101. glog.Debugf("指令发送失败:%s", err.Error())
  102. }
  103. time.Sleep(10 * time.Second)
  104. }
  105. }
  106. }
  107. func (c *Client) SendGetAddress() {
  108. bytea := []byte{0x68, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0x68, 0x13, 0x00, 0xDF, 0x16}
  109. c.send(bytea)
  110. }