Server.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package TcpServer
  2. import (
  3. "Yunlot/Handle"
  4. "Yunlot/conf"
  5. "Yunlot/lib"
  6. "Yunlot/lib/TcpServerLib"
  7. "Yunlot/logs"
  8. "Yunlot/models/Device"
  9. "container/list"
  10. "encoding/json"
  11. "fmt"
  12. "net"
  13. "strconv"
  14. "strings"
  15. "time"
  16. )
  17. var SessionS list.List
  18. func TcpServer() {
  19. //SessionS = list.New()
  20. logs.Println("============TcpServer==============")
  21. logs.Println("TCPServer_Port:" + fmt.Sprintf(":%d", conf.TCPServer_Port))
  22. server := TcpServerLib.NewServer(fmt.Sprintf(":%d", conf.TCPServer_Port))
  23. server.OnNewClient(OnNewClient)
  24. server.OnClientConnectionClosed(OnClientConnectionClosed)
  25. server.OnNewMessage(OnNewMessage)
  26. go server.Listen()
  27. go Polling()
  28. }
  29. func Polling() {
  30. for true {
  31. for i := SessionS.Front(); i != nil; i = i.Next() {
  32. t := i.Value.(*TcpServerLib.Session)
  33. if len(t.Device_Sn) == 0 {
  34. t.Verify_num += 1
  35. if t.Verify_num > 10 {
  36. t.Close()
  37. SessionS.Remove(i)
  38. logs.Println("Polling 删除", t.Conn().LocalAddr().String())
  39. }
  40. }
  41. }
  42. time.Sleep(time.Second * 1)
  43. }
  44. }
  45. func OnNewClient(c *TcpServerLib.Session) {
  46. logs.Println("OnNewClient")
  47. SessionS.PushBack(c)
  48. //c.Send(string([]byte{0xff,0x00,0xff}))
  49. //c.Send("ok")
  50. }
  51. func OnClientConnectionClosed(c *TcpServerLib.Session, closeCase int) {
  52. logs.Println("OnClientConnectionClosed:", closeCase)
  53. // 离线
  54. if len(c.Device_Sn) != 0 {
  55. if _, is := lib.TcpMap[c.Device_Sn]; !is {
  56. delete(lib.TcpMap, c.Device_Sn)
  57. }
  58. r_Device := Device.Device{T_sn: c.Device_Sn}
  59. if !r_Device.Read_Tidy() {
  60. return
  61. }
  62. r_Device.T_online = 2
  63. // 同步参数
  64. r_Device.Update("T_online")
  65. var Device_online_r map[string]interface{}
  66. Device_online_r = make(map[string]interface{})
  67. Device_online_r["online"] = r_Device.T_online
  68. Device_online_r["msg"] = "设备离线[" + ToReason(closeCase) + "]"
  69. Device_online_r["time"] = time.Now().Format("2006-01-02 15:04:05")
  70. Handle.AnalysisMap(&r_Device, map[string]interface{}{"online": Device_online_r}, "")
  71. }
  72. }
  73. func ToReason(str int) (Reason string) {
  74. switch str {
  75. case 1:
  76. Reason = "客户端主动断开"
  77. break
  78. default:
  79. Reason = "客户端异常断开[" + strconv.Itoa(str) + "]"
  80. break
  81. }
  82. return Reason
  83. }
  84. func OnNewMessage(c *TcpServerLib.Session, packetData []byte) {
  85. if len(c.Device_Sn) == 0 {
  86. packetData_l := strings.Split(string(packetData), "&")
  87. if len(packetData_l) != 2 {
  88. logs.Println("登录验证 长度失败:", packetData_l, len(packetData_l))
  89. c.Send("e")
  90. return
  91. }
  92. Devicer := Device.Device{T_sn: packetData_l[0]}
  93. if !Devicer.Read_Tidy() {
  94. logs.Println("登录验证 SN失败:", packetData_l, len(packetData_l))
  95. c.Send("e")
  96. return
  97. }
  98. if Devicer.T_password != packetData_l[1] {
  99. logs.Println("登录验证 秘钥失败:", packetData_l, len(packetData_l))
  100. c.Send("e")
  101. return
  102. }
  103. c.Send("ok")
  104. c.Device_Sn = Devicer.T_sn
  105. lib.TcpMap[Devicer.T_sn] = c // 更新 MAP
  106. // 更新状态
  107. Devicer.T_online = 1
  108. // 同步参数
  109. Devicer.Update("T_online")
  110. // 获取客户端IP地址
  111. clientIP, _, _ := net.SplitHostPort(c.Conn().RemoteAddr().String())
  112. fmt.Println("Client IP:", clientIP)
  113. var Device_online_r map[string]interface{}
  114. Device_online_r = make(map[string]interface{})
  115. Device_online_r["online"] = Devicer.T_online
  116. Device_online_r["msg"] = "设备主动上线[" + clientIP + "]"
  117. Device_online_r["time"] = time.Now().Format("2006-01-02 15:04:05")
  118. Handle.AnalysisMap(&Devicer, map[string]interface{}{"online": Device_online_r}, "")
  119. return
  120. }
  121. Device_r := Device.Device{T_sn: c.Device_Sn}
  122. if !Device_r.Read_Tidy() {
  123. c.Send("e")
  124. return
  125. }
  126. logs.Println("OnNewMessage:", string(packetData))
  127. Rt_r := Handle.PullHandle(&Device_r, c.Device_Sn, packetData)
  128. // 返回
  129. data, _ := json.Marshal(Rt_r)
  130. Handle.PushHandle(&Device_r, c.Device_Sn, string(data))
  131. }