Fix crash on send when disconnected

Fixes #74
This commit is contained in:
Mickael Remond 2019-06-26 15:58:42 +02:00
parent f79a3a219b
commit 445bb8efa3
No known key found for this signature in database
GPG key ID: E6F6045D79965AA3
2 changed files with 23 additions and 3 deletions

View file

@ -154,12 +154,17 @@ func (c *Client) SetHandler(handler EventHandler) {
// Send marshals XMPP stanza and sends it to the server. // Send marshals XMPP stanza and sends it to the server.
func (c *Client) Send(packet Packet) error { func (c *Client) Send(packet Packet) error {
conn := c.conn
if conn == nil {
return errors.New("client is not connected")
}
data, err := xml.Marshal(packet) data, err := xml.Marshal(packet)
if err != nil { if err != nil {
return errors.New("cannot marshal packet " + err.Error()) return errors.New("cannot marshal packet " + err.Error())
} }
if _, err := fmt.Fprintf(c.conn, string(data)); err != nil { if _, err := fmt.Fprintf(conn, string(data)); err != nil {
return errors.New("cannot send packet " + err.Error()) return errors.New("cannot send packet " + err.Error())
} }
return nil return nil
@ -170,8 +175,13 @@ func (c *Client) Send(packet Packet) error {
// disconnect the client. It is up to the user of this method to // disconnect the client. It is up to the user of this method to
// carefully craft the XML content to produce valid XMPP. // carefully craft the XML content to produce valid XMPP.
func (c *Client) SendRaw(packet string) error { func (c *Client) SendRaw(packet string) error {
conn := c.conn
if conn == nil {
return errors.New("client is not connected")
}
var err error var err error
_, err = fmt.Fprintf(c.Session.socketProxy, packet) _, err = fmt.Fprintf(conn, packet)
return err return err
} }

View file

@ -138,12 +138,17 @@ func (c *Component) recv() (err error) {
// Send marshalls XMPP stanza and sends it to the server. // Send marshalls XMPP stanza and sends it to the server.
func (c *Component) Send(packet Packet) error { func (c *Component) Send(packet Packet) error {
conn := c.conn
if conn == nil {
return errors.New("component is not connected")
}
data, err := xml.Marshal(packet) data, err := xml.Marshal(packet)
if err != nil { if err != nil {
return errors.New("cannot marshal packet " + err.Error()) return errors.New("cannot marshal packet " + err.Error())
} }
if _, err := fmt.Fprintf(c.conn, string(data)); err != nil { if _, err := fmt.Fprintf(conn, string(data)); err != nil {
return errors.New("cannot send packet " + err.Error()) return errors.New("cannot send packet " + err.Error())
} }
return nil return nil
@ -154,6 +159,11 @@ func (c *Component) Send(packet Packet) error {
// disconnect the component. It is up to the user of this method to // disconnect the component. It is up to the user of this method to
// carefully craft the XML content to produce valid XMPP. // carefully craft the XML content to produce valid XMPP.
func (c *Component) SendRaw(packet string) error { func (c *Component) SendRaw(packet string) error {
conn := c.conn
if conn == nil {
return errors.New("component is not connected")
}
var err error var err error
_, err = fmt.Fprintf(c.conn, packet) _, err = fmt.Fprintf(c.conn, packet)
return err return err