func (this *NetworkConnector) NetworkConnectTo(raddr string) bool { tcpaddr, err := net.ResolveTCPAddr("tcp", raddr) if err != nil { zwflog.LogError("resolve tcp address error! Desc:", err.Error()) return false } this.remoteAddr = raddr conn, connerr := net.DialTCP("tcp", nil, tcpaddr) if connerr != nil { this.OnConnectError() zwflog.LogError("connect to %s failed! Desc:%s", tcpaddr.String(), connerr.Error()) return false } if this.connector != nil { this.connector.Close() } this.connector = conn zwflog.LogTrace("connect to %s success.", raddr) defer this.networkConnectorClose() this.startKeepAliveTimer() this.networkConnectorStartRead() return true }
func (this *NetworkListener) NetworkListenerRun() bool { addr := fmt.Sprintf("%s:%d", this.ip, this.port) tcpaddr, err := net.ResolveTCPAddr("tcp", addr) if err != nil { zwflog.LogError("resolve tcp addr failed!") return false } lis, liserr := net.ListenTCP("tcp", tcpaddr) if liserr != nil { zwflog.LogError("listen %s:%d failed! Desc:%s", this.ip, this.port, liserr.Error()) return false } this.listener = lis defer this.NetworkListenerClose() for { conn, conerr := this.listener.AcceptTCP() if conerr != nil { zwflog.LogError("accept error! Desc:", conerr.Error()) continue } remoteAddr := conn.RemoteAddr() zwflog.LogTrace("client[%s] connected", remoteAddr.String()) connector := NewNetworkConnector(conn, false, remoteAddr.String()) if connector == nil { zwflog.LogError("new network connector failed") continue } go connector.networkConnectorStartRead() // TODO: start keep-alive timer } return true }