Exemplo n.º 1
0
func (s *Socket) Send(cmd int, data []byte, f Receiver) {
	var head protocol.PackageHead
	head.Command = uint32(cmd)
	head.PkgLen = uint16(protocol.DATA_PACKAGE_HEAD_SIZE + len(data))
	head.SequenceID = s.pkgSeq

	buf := bytes.NewBuffer(make([]byte, head.PkgLen))
	binary.Write(buf, binary.BigEndian, &head)
	binary.Write(buf, binary.BigEndian, data)
	s.sendQueue <- buf.Bytes()
	// 设置接收处理
	s.recvHandle.Set(s.pkgSeq, f)
	// 包序自增
	s.pkgSeq++
}
Exemplo n.º 2
0
func (s *Socket) heartbeat() {
	s.waitGroup.AddOne()
	defer s.waitGroup.Done()

	var head protocol.PackageHead
	head.Command = protocol.CLIENT_CMD_HEARTBEAT
	head.PkgLen = uint16(protocol.DATA_PACKAGE_HEAD_SIZE)
	tick := time.Tick(time.Minute * 2)
	exit := s.waitGroup.ExitNotify()

	for {
		select {
		case <-tick:
		case <-exit:
			return
		}
		if s.socket != nil {
			binary.Write(s.socket, binary.BigEndian, &head)
		}
	}
}