123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- package gatewayServer
- import (
- "github.com/sirupsen/logrus"
- "io"
- "lcfns/global"
- "log"
- "net"
- "sync/atomic"
- )
- var ipAddress = global.Config.Foreign.GatewayServer
- var ConnMap = make(map[string]net.Conn, 50)
- var openDebug bool = false
- 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
- func handleConn(c net.Conn) {
- defer func() {
- atomic.AddInt32(&n, -1)
- //gs := service.ServiceGroupApp.AppServiceGroup.GatewayService
- //gs.UpdateState()
- }()
- 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 {
- logrus.Println("已到达文件结束")
- break
- }
- logrus.Println("读数据失败", err)
- break
- }
- if n == 0 {
- logrus.Println("未读取到数据")
- break
- }
- dataChan <- buf[:n]
- }
- }
- //filterData := func() {
- //
- //}
- handleData := func() {
- for {
- v, ok := <-dataChan
- if !ok {
- return
- }
- adu := UnPack(v)
- c.RemoteAddr()
- ParseAndResponse(adu, c)
- }
- }
- go readData()
- go handleData()
- }
|