Example #1
0
func (this *Conn) Start() {
	macAddr, _ := GetMac()
	b := []byte(macAddr)
	this.SendMessage(comet.MSG_REGISTER, 0, b, nil)
	go func() {
		timer := time.NewTicker(110 * time.Second)
		h := comet.Header{}
		h.Type = comet.MSG_HEARTBEAT
		heartbeat, _ := h.Serialize()
		for {
			select {
			case <-this.done:
				return
			case pack := <-this.outMsgs:
				//seqid := pack.client.nextSeq
				//pack.msg.Header.Seq = seqid
				b, _ := pack.msg.Header.Serialize()
				this.conn.Write(b)
				this.conn.Write(pack.msg.Data)
				log.Infof("send msg: (%d) (%s)", pack.msg.Header.Type, pack.msg.Data)
				//pack.client.nextSeq += 1
				time.Sleep(1 * time.Second)
			case <-timer.C:
				this.conn.Write(heartbeat)
			}
		}
	}()
}
Example #2
0
func main() {
	if len(os.Args) <= 2 {
		log.Printf("Usage: server_addr devid")
		return
	}
	svraddr := os.Args[1]
	devid := os.Args[2]

	addr, _ := net.ResolveTCPAddr("tcp4", svraddr)
	conn, err := net.DialTCP("tcp", nil, addr)
	if err != nil {
		log.Printf("connect to server failed: %v", err)
		return
	}
	conn.SetNoDelay(true)
	defer conn.Close()

	header := comet.Header{
		Type: comet.MSG_REGISTER,
		Ver:  0,
		Seq:  0,
		Len:  uint32(len(devid)),
	}

	b, _ := header.Serialize()
	n1, _ := conn.Write(b)
	n2, _ := conn.Write([]byte(devid))
	log.Printf("write out %d, %d", n1, n2)

	outMsg := make(chan *comet.Message, 10)
	go func(out chan *comet.Message) {
		timer := time.NewTicker(60 * time.Second)
		hb := comet.Header{
			Type: comet.MSG_HEARTBEAT,
			Ver:  0,
			Seq:  0,
			Len:  0,
		}
		heartbeat, _ := hb.Serialize()
		for {
			select {
			//case <- done:
			//	break
			case msg := <-out:
				b, _ := msg.Header.Serialize()
				conn.Write(b)
				conn.Write(msg.Data)
			case <-timer.C:
				conn.Write(heartbeat)
			}
		}
	}(outMsg)

	for {
		headSize := 10
		buf := make([]byte, headSize)
		if _, err := io.ReadFull(conn, buf); err != nil {
			log.Printf("read message len failed, (%v)\n", err)
			return
		}

		var header comet.Header
		if err := header.Deserialize(buf); err != nil {
			return
		}

		data := make([]byte, header.Len)
		if _, err := io.ReadFull(conn, data); err != nil {
			log.Printf("read from client failed: (%v)", err)
			return
		}

		log.Printf("recv from server (%s)", string(data))
		if header.Type == comet.MSG_REQUEST {

			var request CommandRequest
			if err := json.Unmarshal(data, &request); err != nil {
				log.Printf("invalid request, not JSON\n")
				return
			}

			fmt.Printf("UID: (%s)\n", request.Uid)

			response := CommandResponse{
				Status:   0,
				Error:    "OK",
				Response: fmt.Sprintf("Sir, %s got it!", devid),
			}

			b, _ := json.Marshal(response)
			reply_header := comet.Header{
				Type: comet.MSG_REQUEST_REPLY,
				Ver:  0,
				Seq:  header.Seq,
				Len:  uint32(len(b)),
			}
			reply_msg := &comet.Message{
				Header: reply_header,
				Data:   b,
			}
			outMsg <- reply_msg
		}
	}
}