Exemplo n.º 1
0
// actually read incoming messages off the wire
// send Message object into ibound channel
func incoming(c *Client) {
	defer c.workers.Done()
	var err error
	var cp packets.ControlPacket

	DEBUG.Println(NET, "incoming started")

	for {
		if cp, err = packets.ReadPacket(c.conn); err != nil {
			break
		}
		DEBUG.Println(NET, "Received Message")
		c.ibound <- cp
	}
	// We received an error on read.
	// If disconnect is in progress, swallow error and return
	select {
	case <-c.stop:
		DEBUG.Println(NET, "incoming stopped")
		return
		// Not trying to disconnect, send the error to the errors channel
	default:
		ERROR.Println(NET, "incoming stopped with error")
		c.errors <- err
		return
	}
}
Exemplo n.º 2
0
func Test_DecodeMessage_pingresp(t *testing.T) {
	bs := bytes.NewBuffer([]byte{
		0xD0,
		0x00,
	})
	presp, _ := packets.ReadPacket(bs)
	if presp.(*packets.PingrespPacket).MessageType != packets.Pingresp {
		t.Errorf("DecodeMessage ping response wrong msg type: %v", presp.(*packets.PingrespPacket).MessageType)
	}
	if presp.(*packets.PingrespPacket).RemainingLength != 0 {
		t.Errorf("DecodeMessage ping response wrong rem len: %d", presp.(*packets.PingrespPacket).RemainingLength)
	}
}
Exemplo n.º 3
0
// This function is only used for receiving a connack
// when the connection is first started.
// This prevents receiving incoming data while resume
// is in progress if clean session is false.
func (c *Client) connect() byte {
	DEBUG.Println(NET, "connect started")

	ca, err := packets.ReadPacket(c.conn)
	if err != nil {
		ERROR.Println(NET, "connect got error", err)
		//c.errors <- err
		return packets.ErrNetworkError
	}
	msg := ca.(*packets.ConnackPacket)

	if msg == nil || msg.FixedHeader.MessageType != packets.Connack {
		ERROR.Println(NET, "received msg that was nil or not CONNACK")
	} else {
		DEBUG.Println(NET, "received connack")
	}
	return msg.ReturnCode
}
Exemplo n.º 4
0
// Get will retrieve a message from the store, the one associated with
// the provided key value.
func (store *FileStore) Get(key string) packets.ControlPacket {
	store.RLock()
	defer store.RUnlock()
	chkcond(store.opened)
	filepath := fullpath(store.directory, key)
	if !exists(filepath) {
		return nil
	}
	mfile, oerr := os.Open(filepath)
	chkerr(oerr)
	//all, rerr := ioutil.ReadAll(mfile)
	//chkerr(rerr)
	msg, rerr := packets.ReadPacket(mfile)
	chkerr(rerr)
	cerr := mfile.Close()
	chkerr(cerr)
	return msg
}
Exemplo n.º 5
0
func (c *client) process(in, out chan packets.ControlPacket) {
	conn := c.conn
	for {
		cp, err := packets.ReadPacket(conn)
		if err != nil {
			log.Printf("%s\n", err.Error())
			if err == io.EOF {
				return
			}
		}
		switch cp.(type) {
		case *packets.PublishPacket:
			log.Printf("%s\n", cp.String())
			p := cp.(*packets.PublishPacket)
			log.Printf("%s\n", p.TopicName)

			c.hms.Publish(p.TopicName, p.Payload)
			if p.Qos == 1 {
				pa := packets.NewControlPacket(packets.Puback).(*packets.PubackPacket)
				pa.MessageID = p.MessageID
				pa.Write(conn)
			}

		case *packets.ConnectPacket:
			log.Printf("%s\n", cp.String())
			p := cp.(*packets.ConnectPacket)
			log.Printf("%s\n", p.ProtocolName)
			c.id = p.ClientIdentifier
			ca := packets.NewControlPacket(packets.Connack).(*packets.ConnackPacket)
			ca.Write(conn)

		case *packets.SubscribePacket:
			p := cp.(*packets.SubscribePacket)
			c.hms.Subscribe(p.Topics[0], c.id)
			if p.Qos > 0 {
				pa := packets.NewControlPacket(packets.Suback).(*packets.SubackPacket)
				pa.MessageID = p.MessageID
				pa.Write(conn)
			}
		}
	}
}
Exemplo n.º 6
0
// This function is only used for receiving a connack
// when the connection is first started.
// This prevents receiving incoming data while resume
// is in progress if clean session is false.
func (c *Client) connect() byte {
	DEBUG.Println(NET, "connect started")

	ca, err := packets.ReadPacket(c.conn)
	if err != nil {
		ERROR.Println(NET, "connect got error", err)
		return packets.ErrNetworkError
	}
	if ca == nil {
		ERROR.Println(NET, "received nil packet")
		return packets.ErrNetworkError
	}

	msg, ok := ca.(*packets.ConnackPacket)
	if !ok {
		ERROR.Println(NET, "received msg that was not CONNACK")
		return packets.ErrNetworkError
	}

	DEBUG.Println(NET, "received connack")
	return msg.ReturnCode
}