2019-06-18 07:28:30 -07:00
|
|
|
package xmpp
|
2016-01-06 07:51:12 -08:00
|
|
|
|
|
|
|
import (
|
2019-10-29 02:49:01 -07:00
|
|
|
"context"
|
2016-01-06 07:51:12 -08:00
|
|
|
"encoding/xml"
|
|
|
|
"errors"
|
2019-09-27 07:30:12 -07:00
|
|
|
"io"
|
2016-01-06 07:51:12 -08:00
|
|
|
"net"
|
2020-03-06 07:44:01 -08:00
|
|
|
"sync"
|
2016-02-17 04:45:39 -08:00
|
|
|
"time"
|
2019-06-26 08:14:52 -07:00
|
|
|
|
|
|
|
"gosrc.io/xmpp/stanza"
|
2016-01-06 07:51:12 -08:00
|
|
|
)
|
|
|
|
|
2019-06-06 02:58:50 -07:00
|
|
|
//=============================================================================
|
2019-06-08 09:09:22 -07:00
|
|
|
// EventManager
|
2018-09-26 08:26:14 -07:00
|
|
|
|
2020-03-06 07:44:01 -08:00
|
|
|
// SyncConnState represents the current connection state.
|
|
|
|
type SyncConnState struct {
|
|
|
|
sync.RWMutex
|
|
|
|
// Current state of the client. Please use the dedicated getter and setter for this field as they are thread safe.
|
|
|
|
state ConnState
|
|
|
|
}
|
2019-06-06 02:58:50 -07:00
|
|
|
type ConnState = uint8
|
2018-09-26 08:26:14 -07:00
|
|
|
|
2020-03-06 07:44:01 -08:00
|
|
|
// getState is a thread-safe getter for the current state
|
|
|
|
func (scs *SyncConnState) getState() ConnState {
|
|
|
|
var res ConnState
|
|
|
|
scs.RLock()
|
|
|
|
res = scs.state
|
|
|
|
scs.RUnlock()
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// setState is a thread-safe setter for the current
|
|
|
|
func (scs *SyncConnState) setState(cs ConnState) {
|
|
|
|
scs.Lock()
|
|
|
|
scs.state = cs
|
|
|
|
scs.Unlock()
|
|
|
|
}
|
|
|
|
|
2019-06-06 02:58:50 -07:00
|
|
|
// This is a the list of events happening on the connection that the
|
|
|
|
// client can be notified about.
|
|
|
|
const (
|
2019-12-10 05:30:15 -08:00
|
|
|
InitialPresence = "<presence/>"
|
2019-06-06 02:58:50 -07:00
|
|
|
StateDisconnected ConnState = iota
|
2020-03-06 07:44:01 -08:00
|
|
|
StateResuming
|
2019-06-06 02:58:50 -07:00
|
|
|
StateSessionEstablished
|
2019-06-08 02:15:51 -07:00
|
|
|
StateStreamError
|
2019-11-08 03:07:55 -08:00
|
|
|
StatePermanentError
|
2019-06-06 02:58:50 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// Event is a structure use to convey event changes related to client state. This
|
|
|
|
// is for example used to notify the client when the client get disconnected.
|
|
|
|
type Event struct {
|
2020-03-06 07:44:01 -08:00
|
|
|
State SyncConnState
|
2019-06-06 02:58:50 -07:00
|
|
|
Description string
|
2019-06-08 02:15:51 -07:00
|
|
|
StreamError string
|
2019-07-31 09:47:30 -07:00
|
|
|
SMState SMState
|
|
|
|
}
|
|
|
|
|
|
|
|
// SMState holds Stream Management information regarding the session that can be
|
|
|
|
// used to resume session after disconnect
|
|
|
|
type SMState struct {
|
|
|
|
// Stream Management ID
|
|
|
|
Id string
|
|
|
|
// Inbound stanza count
|
|
|
|
Inbound uint
|
2020-03-06 07:44:01 -08:00
|
|
|
|
|
|
|
// IP affinity
|
|
|
|
preferredReconAddr string
|
|
|
|
|
|
|
|
// Error
|
|
|
|
StreamErrorGroup stanza.StanzaErrorGroup
|
|
|
|
|
|
|
|
// Track sent stanzas
|
|
|
|
*stanza.UnAckQueue
|
|
|
|
|
2019-07-31 09:47:30 -07:00
|
|
|
// TODO Store max and timestamp, to check if we should retry resumption or not
|
2018-09-26 08:26:14 -07:00
|
|
|
}
|
|
|
|
|
2019-06-06 02:58:50 -07:00
|
|
|
// EventHandler is use to pass events about state of the connection to
|
|
|
|
// client implementation.
|
2019-11-28 08:15:15 -08:00
|
|
|
type EventHandler func(Event) error
|
2019-06-06 02:58:50 -07:00
|
|
|
|
|
|
|
type EventManager struct {
|
2020-03-06 07:44:01 -08:00
|
|
|
// Store current state. Please use "getState" and "setState" to access and/or modify this.
|
|
|
|
CurrentState SyncConnState
|
2019-06-06 02:58:50 -07:00
|
|
|
|
|
|
|
// Callback used to propagate connection state changes
|
|
|
|
Handler EventHandler
|
2018-09-26 08:26:14 -07:00
|
|
|
}
|
|
|
|
|
2020-03-06 07:44:01 -08:00
|
|
|
// updateState changes the CurrentState in the event manager. The state read is threadsafe but there is no guarantee
|
|
|
|
// regarding the triggered callback function.
|
2019-12-09 03:30:37 -08:00
|
|
|
func (em *EventManager) updateState(state ConnState) {
|
2020-03-06 07:44:01 -08:00
|
|
|
em.CurrentState.setState(state)
|
2019-06-06 02:58:50 -07:00
|
|
|
if em.Handler != nil {
|
|
|
|
em.Handler(Event{State: em.CurrentState})
|
|
|
|
}
|
2018-09-26 08:26:14 -07:00
|
|
|
}
|
|
|
|
|
2020-03-06 07:44:01 -08:00
|
|
|
// disconnected changes the CurrentState in the event manager to "disconnected". The state read is threadsafe but there is no guarantee
|
|
|
|
// regarding the triggered callback function.
|
2019-12-09 03:30:37 -08:00
|
|
|
func (em *EventManager) disconnected(state SMState) {
|
2020-03-06 07:44:01 -08:00
|
|
|
em.CurrentState.setState(StateDisconnected)
|
2019-07-31 09:47:30 -07:00
|
|
|
if em.Handler != nil {
|
|
|
|
em.Handler(Event{State: em.CurrentState, SMState: state})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-06 07:44:01 -08:00
|
|
|
// streamError changes the CurrentState in the event manager to "streamError". The state read is threadsafe but there is no guarantee
|
|
|
|
// regarding the triggered callback function.
|
2019-12-09 03:30:37 -08:00
|
|
|
func (em *EventManager) streamError(error, desc string) {
|
2020-03-06 07:44:01 -08:00
|
|
|
em.CurrentState.setState(StateStreamError)
|
2019-06-08 02:15:51 -07:00
|
|
|
if em.Handler != nil {
|
|
|
|
em.Handler(Event{State: em.CurrentState, StreamError: error, Description: desc})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 08:26:14 -07:00
|
|
|
// Client
|
|
|
|
// ============================================================================
|
|
|
|
|
2019-10-29 02:49:01 -07:00
|
|
|
var ErrCanOnlySendGetOrSetIq = errors.New("SendIQ can only send get and set IQ stanzas")
|
|
|
|
|
2018-02-13 13:07:15 -08:00
|
|
|
// Client is the main structure used to connect as a client on an XMPP
|
2016-02-15 02:05:44 -08:00
|
|
|
// server.
|
2016-01-06 07:51:12 -08:00
|
|
|
type Client struct {
|
2019-06-06 02:58:50 -07:00
|
|
|
// Store user defined options and states
|
2020-03-06 07:44:01 -08:00
|
|
|
config *Config
|
2016-01-06 07:51:12 -08:00
|
|
|
// Session gather data that can be accessed by users of this library
|
2019-10-10 21:24:27 -07:00
|
|
|
Session *Session
|
2019-10-06 10:37:56 -07:00
|
|
|
transport Transport
|
2019-06-18 03:34:25 -07:00
|
|
|
// Router is used to dispatch packets
|
|
|
|
router *Router
|
2019-06-06 02:58:50 -07:00
|
|
|
// Track and broadcast connection state
|
|
|
|
EventManager
|
2019-12-05 09:12:00 -08:00
|
|
|
// Handle errors from client execution
|
|
|
|
ErrorHandler func(error)
|
2020-03-06 07:44:01 -08:00
|
|
|
|
|
|
|
// Post connection hook. This will be executed on first connection
|
2020-03-10 08:31:27 -07:00
|
|
|
PostConnectHook func() error
|
2020-03-06 07:44:01 -08:00
|
|
|
|
|
|
|
// Post resume hook. This will be executed after the client resumes a lost connection using StreamManagement (XEP-0198)
|
2020-03-10 08:31:27 -07:00
|
|
|
PostResumeHook func() error
|
2016-01-06 07:51:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Setting up the client / Checking the parameters
|
|
|
|
*/
|
|
|
|
|
2018-09-26 07:25:04 -07:00
|
|
|
// NewClient generates a new XMPP client, based on Config passed as parameters.
|
2020-03-06 07:44:01 -08:00
|
|
|
// If host is not specified, the DNS SRV should be used to find the host from the domain part of the Jid.
|
2016-02-13 08:01:06 -08:00
|
|
|
// Default the port to 5222.
|
2020-03-06 07:44:01 -08:00
|
|
|
func NewClient(config *Config, r *Router, errorHandler func(error)) (c *Client, err error) {
|
2019-12-04 13:17:58 -08:00
|
|
|
if config.KeepaliveInterval == 0 {
|
|
|
|
config.KeepaliveInterval = time.Second * 30
|
|
|
|
}
|
2020-01-09 06:33:11 -08:00
|
|
|
// Parse Jid
|
|
|
|
if config.parsedJid, err = stanza.NewJid(config.Jid); err != nil {
|
2019-06-26 00:04:03 -07:00
|
|
|
err = errors.New("missing jid")
|
2019-06-07 06:23:23 -07:00
|
|
|
return nil, NewConnError(err, true)
|
2016-01-06 07:51:12 -08:00
|
|
|
}
|
2019-06-26 03:29:39 -07:00
|
|
|
|
2019-10-01 01:59:55 -07:00
|
|
|
if config.Credential.secret == "" {
|
|
|
|
err = errors.New("missing credential")
|
2019-06-07 06:23:23 -07:00
|
|
|
return nil, NewConnError(err, true)
|
2016-01-06 07:51:12 -08:00
|
|
|
}
|
2019-06-26 03:29:39 -07:00
|
|
|
|
2019-07-27 15:19:32 -07:00
|
|
|
// Fallback to jid domain
|
2019-06-26 00:04:03 -07:00
|
|
|
if config.Address == "" {
|
|
|
|
config.Address = config.parsedJid.Domain
|
2019-07-27 09:22:04 -07:00
|
|
|
|
2019-07-27 15:19:32 -07:00
|
|
|
// Fetch SRV DNS-Entries
|
2019-07-17 16:27:11 -07:00
|
|
|
_, srvEntries, err := net.LookupSRV("xmpp-client", "tcp", config.parsedJid.Domain)
|
2019-07-27 09:22:04 -07:00
|
|
|
|
2019-07-17 16:27:11 -07:00
|
|
|
if err == nil && len(srvEntries) > 0 {
|
2019-07-27 15:19:32 -07:00
|
|
|
// If we found matching DNS records, use the entry with highest weight
|
2019-07-17 16:27:11 -07:00
|
|
|
bestSrv := srvEntries[0]
|
|
|
|
for _, srv := range srvEntries {
|
|
|
|
if srv.Priority <= bestSrv.Priority && srv.Weight >= bestSrv.Weight {
|
|
|
|
bestSrv = srv
|
2019-07-27 09:22:04 -07:00
|
|
|
config.Address = ensurePort(srv.Target, int(srv.Port))
|
2019-07-17 16:27:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-26 00:04:03 -07:00
|
|
|
}
|
2020-01-09 06:33:11 -08:00
|
|
|
if config.Domain == "" {
|
|
|
|
// Fallback to jid domain
|
|
|
|
config.Domain = config.parsedJid.Domain
|
|
|
|
}
|
|
|
|
|
2019-06-07 03:16:58 -07:00
|
|
|
c = new(Client)
|
|
|
|
c.config = config
|
2019-06-18 03:34:25 -07:00
|
|
|
c.router = r
|
2019-12-05 09:12:00 -08:00
|
|
|
c.ErrorHandler = errorHandler
|
2019-06-07 03:16:58 -07:00
|
|
|
|
2018-09-26 07:25:04 -07:00
|
|
|
if c.config.ConnectTimeout == 0 {
|
|
|
|
c.config.ConnectTimeout = 15 // 15 second as default
|
2016-02-17 04:45:39 -08:00
|
|
|
}
|
2019-06-06 02:58:50 -07:00
|
|
|
|
2019-10-18 11:29:54 -07:00
|
|
|
if config.TransportConfiguration.Domain == "" {
|
|
|
|
config.TransportConfiguration.Domain = config.parsedJid.Domain
|
|
|
|
}
|
2019-12-26 05:47:02 -08:00
|
|
|
c.config.TransportConfiguration.ConnectTimeout = c.config.ConnectTimeout
|
|
|
|
c.transport = NewClientTransport(c.config.TransportConfiguration)
|
2019-10-10 21:24:27 -07:00
|
|
|
|
2019-10-18 11:29:54 -07:00
|
|
|
if config.StreamLogger != nil {
|
|
|
|
c.transport.LogTraffic(config.StreamLogger)
|
|
|
|
}
|
|
|
|
|
2016-01-06 07:51:12 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-06 07:44:01 -08:00
|
|
|
// Connect establishes a first time connection to a XMPP server.
|
2020-03-10 08:31:27 -07:00
|
|
|
// It calls the PostConnectHook
|
2019-06-08 09:09:22 -07:00
|
|
|
func (c *Client) Connect() error {
|
2020-03-06 07:44:01 -08:00
|
|
|
err := c.connect()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// TODO: Do we always want to send initial presence automatically ?
|
|
|
|
// Do we need an option to avoid that or do we rely on client to send the presence itself ?
|
|
|
|
err = c.sendWithWriter(c.transport, []byte(InitialPresence))
|
|
|
|
// Execute the post first connection hook. Typically this holds "ask for roster" and this type of actions.
|
2020-03-10 08:31:27 -07:00
|
|
|
if c.PostConnectHook != nil {
|
|
|
|
err = c.PostConnectHook()
|
2020-03-06 07:44:01 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start the keepalive go routine
|
|
|
|
keepaliveQuit := make(chan struct{})
|
|
|
|
go keepalive(c.transport, c.config.KeepaliveInterval, keepaliveQuit)
|
|
|
|
// Start the receiver go routine
|
|
|
|
go c.recv(keepaliveQuit)
|
|
|
|
return err
|
2019-07-31 09:47:30 -07:00
|
|
|
}
|
|
|
|
|
2020-03-06 07:44:01 -08:00
|
|
|
// connect establishes an actual TCP connection, based on previously defined parameters, as well as a XMPP session
|
|
|
|
func (c *Client) connect() error {
|
|
|
|
var state SMState
|
2016-01-06 07:51:12 -08:00
|
|
|
var err error
|
2020-03-06 07:44:01 -08:00
|
|
|
// This is the TCP connection
|
2019-10-18 11:29:54 -07:00
|
|
|
streamId, err := c.transport.Connect()
|
2016-02-17 04:45:39 -08:00
|
|
|
if err != nil {
|
2019-06-08 09:09:22 -07:00
|
|
|
return err
|
2016-01-06 07:51:12 -08:00
|
|
|
}
|
|
|
|
|
2020-03-06 07:44:01 -08:00
|
|
|
// Client is ok, we now open XMPP session with TLS negotiation if possible and session resume or binding
|
|
|
|
// depending on state.
|
|
|
|
if c.Session, err = NewSession(c, state); err != nil {
|
2019-12-26 05:47:02 -08:00
|
|
|
// Try to get the stream close tag from the server.
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
val, err := stanza.NextPacket(c.transport.GetDecoder())
|
|
|
|
if err != nil {
|
|
|
|
c.ErrorHandler(err)
|
|
|
|
c.disconnected(state)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch val.(type) {
|
|
|
|
case stanza.StreamClosePacket:
|
|
|
|
// TCP messages should arrive in order, so we can expect to get nothing more after this occurs
|
|
|
|
c.transport.ReceivedStreamClose()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
c.Disconnect()
|
2019-06-08 09:09:22 -07:00
|
|
|
return err
|
2016-01-06 07:51:12 -08:00
|
|
|
}
|
2019-10-18 11:29:54 -07:00
|
|
|
c.Session.StreamId = streamId
|
2019-06-06 02:58:50 -07:00
|
|
|
c.updateState(StateSessionEstablished)
|
2016-01-06 07:51:12 -08:00
|
|
|
|
2020-03-06 07:44:01 -08:00
|
|
|
return err
|
|
|
|
}
|
2016-01-06 07:51:12 -08:00
|
|
|
|
2020-03-06 07:44:01 -08:00
|
|
|
// Resume attempts resuming a Stream Managed session, based on the provided stream management
|
|
|
|
// state. See XEP-0198
|
|
|
|
func (c *Client) Resume() error {
|
|
|
|
c.EventManager.updateState(StateResuming)
|
|
|
|
err := c.connect()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Execute post reconnect hook. This can be different from the first connection hook, and not trigger roster retrival
|
|
|
|
// for example.
|
2020-03-10 08:31:27 -07:00
|
|
|
if c.PostResumeHook != nil {
|
|
|
|
err = c.PostResumeHook()
|
2020-03-06 07:44:01 -08:00
|
|
|
}
|
2019-06-08 09:09:22 -07:00
|
|
|
return err
|
2016-01-06 07:51:12 -08:00
|
|
|
}
|
|
|
|
|
2020-03-06 07:44:01 -08:00
|
|
|
// Disconnect disconnects the client from the server, sending a stream close nonza and closing the TCP connection.
|
2019-12-26 05:47:02 -08:00
|
|
|
func (c *Client) Disconnect() error {
|
2019-10-10 21:24:47 -07:00
|
|
|
if c.transport != nil {
|
2019-12-26 05:47:02 -08:00
|
|
|
return c.transport.Close()
|
2019-09-06 01:28:49 -07:00
|
|
|
}
|
2019-12-26 05:47:02 -08:00
|
|
|
// No transport so no connection.
|
|
|
|
return nil
|
2019-06-06 02:58:50 -07:00
|
|
|
}
|
|
|
|
|
2019-06-08 09:09:22 -07:00
|
|
|
func (c *Client) SetHandler(handler EventHandler) {
|
|
|
|
c.Handler = handler
|
|
|
|
}
|
|
|
|
|
2019-06-11 06:29:08 -07:00
|
|
|
// Send marshals XMPP stanza and sends it to the server.
|
2019-06-26 08:14:52 -07:00
|
|
|
func (c *Client) Send(packet stanza.Packet) error {
|
2019-10-06 10:37:56 -07:00
|
|
|
conn := c.transport
|
2019-06-26 06:58:42 -07:00
|
|
|
if conn == nil {
|
|
|
|
return errors.New("client is not connected")
|
|
|
|
}
|
|
|
|
|
2018-01-26 00:55:39 -08:00
|
|
|
data, err := xml.Marshal(packet)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("cannot marshal packet " + err.Error())
|
|
|
|
}
|
|
|
|
|
2020-03-06 07:44:01 -08:00
|
|
|
// Store stanza as non-acked as part of stream management
|
|
|
|
// See https://xmpp.org/extensions/xep-0198.html#scenarios
|
|
|
|
if c.config.StreamManagementEnable {
|
|
|
|
if _, ok := packet.(stanza.SMRequest); !ok {
|
|
|
|
toStore := stanza.UnAckedStz{Stz: string(data)}
|
|
|
|
c.Session.SMState.UnAckQueue.Push(&toStore)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-18 11:29:54 -07:00
|
|
|
return c.sendWithWriter(c.transport, data)
|
2018-01-26 00:55:39 -08:00
|
|
|
}
|
|
|
|
|
2019-10-29 02:49:01 -07:00
|
|
|
// SendIQ sends an IQ set or get stanza to the server. If a result is received
|
|
|
|
// the provided handler function will automatically be called.
|
|
|
|
//
|
|
|
|
// The provided context should have a timeout to prevent the client from waiting
|
|
|
|
// forever for an IQ result. For example:
|
|
|
|
//
|
|
|
|
// ctx, _ := context.WithTimeout(context.Background(), 30 * time.Second)
|
2019-10-29 03:37:49 -07:00
|
|
|
// result := <- client.SendIQ(ctx, iq)
|
2019-10-29 02:49:01 -07:00
|
|
|
//
|
2020-01-31 02:48:03 -08:00
|
|
|
func (c *Client) SendIQ(ctx context.Context, iq *stanza.IQ) (chan stanza.IQ, error) {
|
2019-11-28 08:15:15 -08:00
|
|
|
if iq.Attrs.Type != stanza.IQTypeSet && iq.Attrs.Type != stanza.IQTypeGet {
|
2019-10-29 02:49:01 -07:00
|
|
|
return nil, ErrCanOnlySendGetOrSetIq
|
|
|
|
}
|
|
|
|
if err := c.Send(iq); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-29 03:37:49 -07:00
|
|
|
return c.router.NewIQResultRoute(ctx, iq.Attrs.Id), nil
|
2019-10-29 02:49:01 -07:00
|
|
|
}
|
|
|
|
|
2018-01-26 00:55:39 -08:00
|
|
|
// SendRaw sends an XMPP stanza as a string to the server.
|
|
|
|
// It can be invalid XML or XMPP content. In that case, the server will
|
|
|
|
// disconnect the client. It is up to the user of this method to
|
|
|
|
// carefully craft the XML content to produce valid XMPP.
|
|
|
|
func (c *Client) SendRaw(packet string) error {
|
2019-10-06 10:37:56 -07:00
|
|
|
conn := c.transport
|
2019-06-26 06:58:42 -07:00
|
|
|
if conn == nil {
|
|
|
|
return errors.New("client is not connected")
|
|
|
|
}
|
|
|
|
|
2020-03-06 07:44:01 -08:00
|
|
|
// Store stanza as non-acked as part of stream management
|
|
|
|
// See https://xmpp.org/extensions/xep-0198.html#scenarios
|
|
|
|
if c.config.StreamManagementEnable {
|
|
|
|
toStore := stanza.UnAckedStz{Stz: packet}
|
|
|
|
c.Session.SMState.UnAckQueue.Push(&toStore)
|
|
|
|
}
|
2019-10-18 11:29:54 -07:00
|
|
|
return c.sendWithWriter(c.transport, []byte(packet))
|
2019-06-29 01:45:25 -07:00
|
|
|
}
|
|
|
|
|
2019-09-27 07:30:12 -07:00
|
|
|
func (c *Client) sendWithWriter(writer io.Writer, packet []byte) error {
|
2019-06-06 02:58:50 -07:00
|
|
|
var err error
|
2019-09-27 07:30:12 -07:00
|
|
|
_, err = writer.Write(packet)
|
2019-06-06 02:58:50 -07:00
|
|
|
return err
|
2016-01-06 07:51:12 -08:00
|
|
|
}
|
|
|
|
|
2019-06-11 06:29:08 -07:00
|
|
|
// ============================================================================
|
|
|
|
// Go routines
|
|
|
|
|
|
|
|
// Loop: Receive data from server
|
2020-03-06 07:44:01 -08:00
|
|
|
func (c *Client) recv(keepaliveQuit chan<- struct{}) {
|
2019-06-11 06:29:08 -07:00
|
|
|
for {
|
2019-10-18 11:29:54 -07:00
|
|
|
val, err := stanza.NextPacket(c.transport.GetDecoder())
|
2019-06-11 06:29:08 -07:00
|
|
|
if err != nil {
|
2019-12-05 09:12:00 -08:00
|
|
|
c.ErrorHandler(err)
|
2019-06-11 06:29:08 -07:00
|
|
|
close(keepaliveQuit)
|
2020-03-06 07:44:01 -08:00
|
|
|
c.disconnected(c.Session.SMState)
|
2019-11-28 08:15:15 -08:00
|
|
|
return
|
2019-06-11 06:29:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle stream errors
|
|
|
|
switch packet := val.(type) {
|
2019-06-26 08:14:52 -07:00
|
|
|
case stanza.StreamError:
|
2019-06-24 02:13:25 -07:00
|
|
|
c.router.route(c, val)
|
2019-06-18 03:34:25 -07:00
|
|
|
close(keepaliveQuit)
|
2019-06-11 06:29:08 -07:00
|
|
|
c.streamError(packet.Error.Local, packet.Text)
|
2019-12-05 09:12:00 -08:00
|
|
|
c.ErrorHandler(errors.New("stream error: " + packet.Error.Local))
|
2019-12-26 05:47:02 -08:00
|
|
|
// We don't return here, because we want to wait for the stream close tag from the server, or timeout.
|
|
|
|
c.Disconnect()
|
2019-07-31 09:47:30 -07:00
|
|
|
// Process Stream management nonzas
|
|
|
|
case stanza.SMRequest:
|
|
|
|
answer := stanza.SMAnswer{XMLName: xml.Name{
|
|
|
|
Space: stanza.NSStreamManagement,
|
|
|
|
Local: "a",
|
2020-03-06 07:44:01 -08:00
|
|
|
}, H: c.Session.SMState.Inbound}
|
2019-11-28 08:15:15 -08:00
|
|
|
err = c.Send(answer)
|
|
|
|
if err != nil {
|
2019-12-05 09:12:00 -08:00
|
|
|
c.ErrorHandler(err)
|
2019-11-28 08:15:15 -08:00
|
|
|
return
|
|
|
|
}
|
2019-12-26 05:47:02 -08:00
|
|
|
case stanza.StreamClosePacket:
|
|
|
|
// TCP messages should arrive in order, so we can expect to get nothing more after this occurs
|
|
|
|
c.transport.ReceivedStreamClose()
|
|
|
|
return
|
2019-07-31 09:47:30 -07:00
|
|
|
default:
|
2020-03-06 07:44:01 -08:00
|
|
|
c.Session.SMState.Inbound++
|
2019-06-11 06:29:08 -07:00
|
|
|
}
|
2019-10-29 03:37:49 -07:00
|
|
|
// Do normal route processing in a go-routine so we can immediately
|
|
|
|
// start receiving other stanzas. This also allows route handlers to
|
|
|
|
// send and receive more stanzas.
|
|
|
|
go c.router.route(c, val)
|
2019-06-11 06:29:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loop: send whitespace keepalive to server
|
|
|
|
// This is use to keep the connection open, but also to detect connection loss
|
|
|
|
// and trigger proper client connection shutdown.
|
2019-12-04 13:17:58 -08:00
|
|
|
func keepalive(transport Transport, interval time.Duration, quit <-chan struct{}) {
|
|
|
|
ticker := time.NewTicker(interval)
|
2019-06-11 06:29:08 -07:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
2019-10-15 11:56:11 -07:00
|
|
|
if err := transport.Ping(); err != nil {
|
|
|
|
// When keepalive fails, we force close the transport. In all cases, the recv will also fail.
|
2019-06-11 06:29:08 -07:00
|
|
|
ticker.Stop()
|
2019-10-06 10:37:56 -07:00
|
|
|
_ = transport.Close()
|
2019-06-11 06:29:08 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
case <-quit:
|
|
|
|
ticker.Stop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2016-01-06 07:51:12 -08:00
|
|
|
}
|