gatewayServer.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package gatewayServer
  2. import (
  3. "github.com/sirupsen/logrus"
  4. "io"
  5. "lcfns/global"
  6. "log"
  7. "net"
  8. "sync/atomic"
  9. )
  10. var ipAddress = global.Config.Foreign.GatewayServer
  11. var ConnMap = make(map[string]net.Conn, 50)
  12. var openDebug bool = false
  13. func GatewayServe() {
  14. listen, err := net.Listen("tcp", ipAddress)
  15. if err != nil {
  16. log.Fatalln("服务启动失败 ", err)
  17. }
  18. for {
  19. accept, err := listen.Accept()
  20. if err != nil {
  21. log.Fatalln("获取连接失败 ", err)
  22. }
  23. go handleConn(accept)
  24. }
  25. }
  26. var n int32
  27. func handleConn(c net.Conn) {
  28. defer func() {
  29. atomic.AddInt32(&n, -1)
  30. //gs := service.ServiceGroupApp.AppServiceGroup.GatewayService
  31. //gs.UpdateState()
  32. }()
  33. atomic.AddInt32(&n, 1)
  34. //fmt.Printf("连接数 %d\n", atomic.LoadInt32(&n))
  35. //fmt.Printf("当前连接 %s\n", c.RemoteAddr())
  36. //var filterChan = make(chan []byte, 32)
  37. var dataChan = make(chan []byte, 32)
  38. readData := func() {
  39. for {
  40. var buf = make([]byte, 1024)
  41. n, err := c.Read(buf)
  42. if err != nil {
  43. if err == io.EOF {
  44. logrus.Println("已到达文件结束")
  45. break
  46. }
  47. logrus.Println("读数据失败", err)
  48. break
  49. }
  50. if n == 0 {
  51. logrus.Println("未读取到数据")
  52. break
  53. }
  54. dataChan <- buf[:n]
  55. }
  56. }
  57. //filterData := func() {
  58. //
  59. //}
  60. handleData := func() {
  61. for {
  62. v, ok := <-dataChan
  63. if !ok {
  64. return
  65. }
  66. adu := UnPack(v)
  67. c.RemoteAddr()
  68. ParseAndResponse(adu, c)
  69. }
  70. }
  71. go readData()
  72. go handleData()
  73. }