package gatewayServer import ( "fmt" "io" "lc-fangdaosha/global" "log" "net" "sync/atomic" ) var ipAddress = global.Config.Foreign.GatewayServer var ConnMap = make(map[string]net.Conn, 50) var openDebug bool = false // GatewayServe 网关服务 func GatewayServe() { listen, err := net.Listen("tcp", ipAddress) if err != nil { log.Fatalln("服务启动失败 ", err) } for { accept, err := listen.Accept() if err != nil { log.Fatalln("获取连接失败 ", err) } go handleConn(accept) } } var n int32 // handleConn 处理连接 func handleConn(c net.Conn) { defer func() { atomic.AddInt32(&n, -1) }() atomic.AddInt32(&n, 1) fmt.Printf("连接数 %d\n", atomic.LoadInt32(&n)) fmt.Printf("当前连接 %s\n", c.RemoteAddr()) //var filterChan = make(chan []byte, 32) var dataChan = make(chan []byte, 32) readData := func() { for { var buf = make([]byte, 1024) n, err := c.Read(buf) if err != nil { if err == io.EOF { log.Println("已到达文件结束") break } log.Println("读数据失败", err) break } if n == 0 { log.Println("未读取到数据") break } dataChan <- buf[:n] } } handleData := func() { for { v, ok := <-dataChan if !ok { return } adu := UnPack(v) c.RemoteAddr() ParseAndResponse(adu, c) } } go readData() go handleData() }