func (this *Peer) connect() (err error) { //bind local addr since we will use this for peer addr on remote server laddr, err := net.ResolveTCPAddr("tcp", config.PushdConf.TcpListenAddr) if err != nil { panic(err) } laddr.Port = 0 dialer := &net.Dialer{LocalAddr: laddr} this.Conn, err = dialer.Dial("tcp", this.addr) if err != nil { log.Warn("s2s connect to %s error: %s", this.addr, err.Error()) } if this.Conn != nil { // just wait for s2s server close the connection go func() { for { input := make([]byte, 1460) _, err = this.Conn.Read(input) if err != nil { this.Conn.Close() return } } }() } return }
func (this Parser) match(txt, tp string) bool { re, exists := this.Regexps[tp] if !exists { log.Warn("regexp not found for %s", tp) } return re.MatchString(txt) }
func (this *Parser) parse(txt, tp string) []string { re, exists := this.Regexps[tp] if !exists { log.Warn("regexp not found for %s", tp) } match := re.FindAllStringSubmatch(txt, -1) for _, r := range match { for _, rr := range r { log.Debug(rr) } } if len(match) < 1 || len(match[0]) < 2 { return []string{} } return match[0][1:] }
func (this *Peer) writeMsg(msg string) { var err error if this.Conn != nil { _, err = this.Write([]byte(msg)) } if err != nil || this.Conn == nil { // retry for i := 0; i < RETRY_CNT; i++ { err = this.connect() if err != nil { log.Warn("write to peer %s error: %s", this.addr, err.Error()) } else { if this.Conn != nil { _, err = this.Write([]byte(msg)) if err == nil { break } } } } } }
func (this *Forwarder) reconnect() { if this.Conn != nil { this.Conn.Close() } conn, err := net.Dial("tcp", this.config.ToAddr) if err != nil { log.Error(err) } this.Conn = conn if conn != nil { go func() { for { _, err := this.Conn.Read(make([]byte, 1000)) if err != nil { log.Warn(err) } this.Conn.Close() break } }() } }