func (c *Client) WritePacket(pk packets.Packet) (n int, err error) {
	if atomic.LoadInt32(&c.State.Done) == 1 {
		return 0, ErrConnectionClosed
	}

	c.w.Mu.Lock()
	defer c.w.Mu.Unlock()

	buf := new(bytes.Buffer)
	switch pk.FixedHeader.Type {
	case packets.Connect:
		err = pk.ConnectEncode(buf)
	case packets.Connack:
		err = pk.ConnackEncode(buf)
	case packets.Publish:
		err = pk.PublishEncode(buf)
		if err == nil {
			atomic.AddInt64(&c.system.PublishSent, 1)
		}
	case packets.Puback:
		err = pk.PubackEncode(buf)
	case packets.Pubrec:
		err = pk.PubrecEncode(buf)
	case packets.Pubrel:
		err = pk.PubrelEncode(buf)
	case packets.Pubcomp:
		err = pk.PubcompEncode(buf)
	case packets.Subscribe:
		err = pk.SubscribeEncode(buf)
	case packets.Suback:
		err = pk.SubackEncode(buf)
	case packets.Unsubscribe:
		err = pk.UnsubscribeEncode(buf)
	case packets.Unsuback:
		err = pk.UnsubackEncode(buf)
	case packets.Pingreq:
		err = pk.PingreqEncode(buf)
	case packets.Pingresp:
		err = pk.PingrespEncode(buf)
	case packets.Disconnect:
		err = pk.DisconnectEncode(buf)
	default:
		err = fmt.Errorf("No valid packet available; %v", pk.FixedHeader.Type)
	}
	if err != nil {
		return
	}

	n, err = c.w.Write(buf.Bytes())
	if err != nil {
		return
	}
	atomic.AddInt64(&c.system.BytesSent, int64(n))
	atomic.AddInt64(&c.system.MessagesSent, 1)

	c.refreshDeadline(c.keepalive)

	return
}
// WritePacket encodes and writes a packet to the client.
WritePacket encodes and writes a packet to the client.
// WritePacket encodes and writes a packet to the client .
func (c *Client) WritePacket(pk packets.Packet) (n int, err error) {
	if atomic.LoadInt32(&c.State.Done) == 1 {
		return 0, ErrConnectionClosed
	}
	c.w.Mu.Lock()
	defer c.w.Mu.Unlock()
	buf := new(bytes.Buffer)
	switch pk.FixedHeader.Type {
	case packets.Connect:
		err = pk.ConnectEncode(buf)
	case packets.Connack:
		err = pk.ConnackEncode(buf)
	case packets.Publish:
		err = pk.PublishEncode(buf)
		if err == nil {
			atomic.AddInt64(&c.system.PublishSent, 1)
		}
	case packets.Puback:
		err = pk.PubackEncode(buf)
	case packets.Pubrec:
		err = pk.PubrecEncode(buf)
	case packets.Pubrel:
		err = pk.PubrelEncode(buf)
	case packets.Pubcomp:
		err = pk.PubcompEncode(buf)
	case packets.Subscribe:
		err = pk.SubscribeEncode(buf)
	case packets.Suback:
		err = pk.SubackEncode(buf)
	case packets.Unsubscribe:
		err = pk.UnsubscribeEncode(buf)
	case packets.Unsuback:
		err = pk.UnsubackEncode(buf)
	case packets.Pingreq:
		err = pk.PingreqEncode(buf)
	case packets.Pingresp:
		err = pk.PingrespEncode(buf)
	case packets.Disconnect:
		err = pk.DisconnectEncode(buf)
	default:
		err = fmt.Errorf("No valid packet available; %v", pk.FixedHeader.Type)
	}
	if err != nil {
		return
	}
	n, err = c.w.Write(buf.Bytes())
	if err != nil {
		return
	}
	atomic.AddInt64(&c.system.BytesSent, int64(n))
	atomic.AddInt64(&c.system.MessagesSent, 1)
	c.refreshDeadline(c.keepalive)
	return
}
func ( c * Client ) WritePacket ( pk packets . Packet ) ( n int , err error ) { if atomic . LoadInt32 ( & c . State . Done ) == 1 { return 0 , ErrConnectionClosed } c . w . Mu . Lock ( ) defer c . w . Mu . Unlock ( ) buf := new ( bytes . Buffer ) switch pk . FixedHeader . Type { case packets . Connect : err = pk . ConnectEncode ( buf ) case packets . Connack : err = pk . ConnackEncode ( buf ) case packets . Publish : err = pk . PublishEncode ( buf ) if err == nil { atomic . AddInt64 ( & c . system . PublishSent , 1 ) } case packets . Puback : err = pk . PubackEncode ( buf ) case packets . Pubrec : err = pk . PubrecEncode ( buf ) case packets . Pubrel : err = pk . PubrelEncode ( buf ) case packets . Pubcomp : err = pk . PubcompEncode ( buf ) case packets . Subscribe : err = pk . SubscribeEncode ( buf ) case packets . Suback : err = pk . SubackEncode ( buf ) case packets . Unsubscribe : err = pk . UnsubscribeEncode ( buf ) case packets . Unsuback : err = pk . UnsubackEncode ( buf ) case packets . Pingreq : err = pk . PingreqEncode ( buf ) case packets . Pingresp : err = pk . PingrespEncode ( buf ) case packets . Disconnect : err = pk . DisconnectEncode ( buf ) default : err = fmt . Errorf ( " " , pk . FixedHeader . Type ) } if err != nil { return } n , err = c . w . Write ( buf . Bytes ( ) ) if err != nil { return } atomic . AddInt64 ( & c . system . BytesSent , int64 ( n ) ) atomic . AddInt64 ( & c . system . MessagesSent , 1 ) c . refreshDeadline ( c . keepalive ) return }
WritePacket encodes and writes a packet to the client.
WritePacket encodes and writes a packet to the client .