Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

MultiListen有个隐藏的bug #125

Open
Leadrive opened this issue Jan 12, 2020 · 1 comment
Open

MultiListen有个隐藏的bug #125

Leadrive opened this issue Jan 12, 2020 · 1 comment

Comments

@Leadrive
Copy link

var g_LocalConn net.Conn

func (sc *Client) MultiListen() bool {
	if g_LocalConn == nil {
		g_LocalConn, err := net.Listen("tcp", *localAddr)

g_LocalConn, err := net.Listen("tcp", *localAddr)
其实类型是 Listener,所以变成了新的变量
g_LocalConn 的赋值,应该是在下面的Accept()上。

@Leadrive
Copy link
Author

这是我修改的,不知道对不对,没测试

func (sc *Client) MultiListen() bool {
	if g_LocalConn == nil {
		// g_LocalConn, err := net.Listen("tcp", *localAddr)
		listener, err := net.Listen("tcp", *localAddr) // Rocky
		if err != nil {
			log.Println("cannot listen addr:" + err.Error())
			if remoteConn != nil {
				remoteConn.Close()
			}
			return false
		}
		go func() {
			quit := false
			ping := time.NewTicker(time.Second)
			go func() {
			out:
				for {
					select {
					case <-ping.C:
						if quit {
							break out
						}
						for _, pipe := range sc.pipes {
							common.Write(pipe, "-1", "ping", "")
						}
					}
				}
			}()
			for {
				// conn, err := g_LocalConn.Accept()
				conn, err := listener.Accept() // Rocky
				if err != nil {
					continue
				}
				sessionId := common.GetId("udp")
				pipe := sc.getOnePipe()
				if pipe == nil {
					log.Println("cannot get pipe for client")
					if remoteConn != nil {
						remoteConn.Close()
					}
					return
				}
				g_LocalConn = conn // Rocky
				sc.sessionLock.Lock()
				sc.sessions[sessionId] = &clientSession{pipe: pipe, localConn: conn}
				sc.sessionLock.Unlock()
				log.Println("client", sc.id, "create session", sessionId)
				go handleLocalServerResponse(sc, sessionId)
			}
			quit = true
			ping.Stop()
		}()
		mode := "p2p mode"
		if !sc.bUdp {
			mode = "c/s mode"
			delete(g_ClientMapKey, sc.id)
		}
		println("service start success,please connect", *localAddr, mode)
	}
	return true
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant