本文整理汇总了Golang中github.com/surgemq/message.Message.Len方法的典型用法代码示例。如果您正苦于以下问题:Golang Message.Len方法的具体用法?Golang Message.Len怎么用?Golang Message.Len使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/surgemq/message.Message
的用法示例。
在下文中一共展示了Message.Len方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: writeMessage
func writeMessage(conn io.Closer, msg message.Message) error {
buf := make([]byte, msg.Len())
_, err := msg.Encode(buf)
if err != nil {
//glog.Debugf("Write error: %v", err)
return err
}
//glog.Debugf("Writing: %s", msg)
return writeMessageBuffer(conn, buf)
}
示例2: insert
func (this *Ackqueue) insert(pktid uint16, msg message.Message, onComplete interface{}) error {
if this.full() {
this.grow()
}
if _, ok := this.emap[pktid]; !ok {
// message length
ml := msg.Len()
// ackmsg
am := ackmsg{
Mtype: msg.Type(),
State: message.RESERVED,
Pktid: msg.PacketId(),
Msgbuf: make([]byte, ml),
OnComplete: onComplete,
}
if _, err := msg.Encode(am.Msgbuf); err != nil {
return err
}
this.ring[this.tail] = am
this.emap[pktid] = this.tail
this.tail = this.increment(this.tail)
this.count++
} else {
// If packet w/ pktid already exist, then this must be a PUBLISH message
// Other message types should never send with the same packet ID
pm, ok := msg.(*message.PublishMessage)
if !ok {
return fmt.Errorf("ack/insert: duplicate packet ID for %s message", msg.Name())
}
// If this is a publish message, then the DUP flag must be set. This is the
// only scenario in which we will receive duplicate messages.
if pm.Dup() {
return fmt.Errorf("ack/insert: duplicate packet ID for PUBLISH message, but DUP flag is not set")
}
// Since it's a dup, there's really nothing we need to do. Moving on...
}
return nil
}
示例3: writeMessage
// writeMessage() writes a message to the outgoing buffer
func (this *service) writeMessage(msg message.Message) (int, error) {
var (
l int = msg.Len()
n int
err error
buf []byte
)
if this.out == nil {
return 0, ErrBufferNotReady
}
// This is to serialize writes to the underlying buffer. Multiple goroutines could
// potentially get here because of calling Publish() or Subscribe() or other
// functions that will send messages. For example, if a message is received in
// another connetion, and the message needs to be published to this client, then
// the Publish() function is called, and at the same time, another client could
// do exactly the same thing.
//
// Not an ideal fix though. If possible we should remove mutex and be lockfree.
// Mainly because when there's a large number of goroutines that want to publish
// to this client, then they will all block. However, this will do for now.
//
// FIXME: Try to find a better way than a mutex...if possible.
this.wmu.Lock()
defer this.wmu.Unlock()
//buf = make([]byte, l)
buf = this.server.CreateAndGetBytes(int64(l))
n, err = msg.Encode(buf[0:l])
if err != nil {
return 0, err
}
//m, err = this.out.WriteCommit(n)
ok := this.out.WriteBuffer(&buf)
if !ok {
return 0, err
}
//}
this.outStat.increment(int64(n))
return n, nil
}
示例4: Ack
// Ack() takes the ack message supplied and updates the status of messages waiting.
func (this *Ackqueue) Ack(msg message.Message) error {
this.mu.Lock()
defer this.mu.Unlock()
switch msg.Type() {
case message.PUBACK, message.PUBREC, message.PUBREL, message.PUBCOMP, message.SUBACK, message.UNSUBACK:
// Check to see if the message w/ the same packet ID is in the queue
i, ok := this.emap[msg.PacketId()]
if ok {
// If message w/ the packet ID exists, update the message state and copy
// the ack message
this.ring[i].State = msg.Type()
ml := msg.Len()
this.ring[i].Ackbuf = make([]byte, ml)
_, err := msg.Encode(this.ring[i].Ackbuf)
if err != nil {
return err
}
//Log.Debugc(func() string{ return fmt.Sprintf("Acked: %v", msg)})
//} else {
//Log.Debugc(func() string{ return fmt.Sprintf("Cannot ack %s message with packet ID %d", msg.Type(), msg.PacketId())})
}
case message.PINGRESP:
if this.ping.Mtype == message.PINGREQ {
this.ping.State = message.PINGRESP
}
default:
return errAckMessage
}
return nil
}