func sameAddress(masterAddr, slaveAddr string, req *Request) bool { if masterAddr == slaveAddr { return true } var mAddr = util.GetRealAddr(masterAddr, req.Cli.LocalAddr().String()) var sAddr = util.GetRealAddr(slaveAddr, req.Cli.LocalAddr().String()) return mAddr == sAddr || mAddr == req.Cli.LocalAddr().String() }
func (slv *slave) doConnectToMaster() { slv.mtx.Lock() var mi = slv.mi slv.mtx.Unlock() for { if slv.IsClosed() { return } c, err := net.Dial("tcp", mi.MasterAddr) if err != nil { log.Printf("Connect to master %s failed, sleep 1 second and try again.\n", mi.MasterAddr) time.Sleep(time.Second) continue } mi.MasterAddr = util.GetRealAddr(mi.MasterAddr, c.RemoteAddr().String()) mi.SlaveAddr = util.GetRealAddr(mi.SlaveAddr, c.LocalAddr().String()) if mi.MasterAddr == mi.SlaveAddr { log.Printf("Master and slave addresses are the same!\n") c.Close() slv.Close() return } cli := NewClient(c, false) slv.mtx.Lock() slv.mi = mi slv.cli = cli slv.mtx.Unlock() if slv.IsClosed() { return } cli.SetClientType(ClientTypeSlave) go cli.GoRecvRequest(slv.reqChan, slv) go cli.GoSendResponse() if len(slv.adminPwd) == 0 { err = slv.SendSlaveOfToMaster() if err != nil { log.Printf("SendSlaveOfToMaster failed(%s), close slave!", err) slv.Close() return } } else { err = slv.SendAuthToMaster() if err != nil { log.Printf("SendAuthToMaster failed(%s), close slave!", err) slv.Close() return } } for !cli.IsClosed() { time.Sleep(time.Second) } } }