Ejemplo n.º 1
0
func (rc *ReceiveChannel) receiveTopicFrom(target string) {
	// connect to a TCP server
	network := "tcp"
	raddr, err := net.ResolveTCPAddr(network, target)
	if err != nil {
		log.Printf("Fail to resolve %s:%v", target, err)
		return
	}

	// println("dial tcp", raddr.String())
	conn, err := net.DialTCP(network, nil, raddr)
	if err != nil {
		log.Printf("Fail to dial %s:%v", raddr, err)
		time.Sleep(time.Second)
		return
	}
	defer conn.Close()

	buf := make([]byte, 4)

	util.WriteBytes(conn, buf, util.NewMessage(util.Data, []byte("GET "+rc.name)))

	util.WriteUint64(conn, rc.offset)

	util.WriteBytes(conn, buf, util.NewMessage(util.Data, []byte("ok")))

	ticker := time.NewTicker(time.Millisecond * 1100)
	defer ticker.Stop()
	go func() {
		buf := make([]byte, 4)
		for range ticker.C {
			util.WriteBytes(conn, buf, util.NewMessage(util.Data, []byte("ok")))
			// print(".")
		}
	}()

	for {
		f, data, err := util.ReadBytes(conn, buf)
		if err == io.EOF {
			// print("recieve close chan2: eof")
			break
		}
		if err != nil {
			log.Printf("receive error:%v", err)
			continue
		}
		rc.offset += 4 + 1
		if f != util.Data {
			// print("recieve close chan1: ", string([]byte{byte(f)}))
			break
		}
		// println("receive raw data :", string(data.Bytes()))
		rc.offset += uint64(len(data.Data()))
		rc.Ch <- data.Data()
	}
	close(rc.Ch)
}
Ejemplo n.º 2
0
func (rc *ReceiveChannel) receiveTopicFrom(target string) {

	readWriter, err := util.Dial(rc.tlsConfig, target)
	if err != nil {
		log.Printf("Fail to dial receive %s: %v", target, err)
		return
	}
	defer readWriter.Close()

	// println("receiving", rc.name, "from", target)

	buf := make([]byte, 4)

	util.WriteBytes(readWriter, buf, util.NewMessage(util.Data, []byte("GET "+rc.name)))

	util.WriteUint64(readWriter, rc.offset)

	util.WriteBytes(readWriter, buf, util.NewMessage(util.Data, []byte("ok")))

	ticker := time.NewTicker(time.Millisecond * 1100)
	defer ticker.Stop()
	go func() {
		buf := make([]byte, 4)
		for range ticker.C {
			util.WriteBytes(readWriter, buf, util.NewMessage(util.Data, []byte("ok")))
			// print(".")
		}
	}()

	for {
		f, data, err := util.ReadBytes(readWriter, buf)
		if err == io.EOF {
			// print("recieve close chan2: eof")
			break
		}
		if err != nil {
			log.Printf("receive error:%v", err)
			continue
		}
		rc.offset += 4 + 1
		if f != util.Data {
			// print("recieve close chan1: ", string([]byte{byte(f)}))
			break
		}
		// println("receive raw data :", string(data.Bytes()))
		rc.offset += uint64(len(data.Data()))
		rc.Ch <- data.Data()
	}
	close(rc.Ch)
}