sum7
/
yaja
Archived
1
0
Fork 0
This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
yaja/client/start.go

98 lines
2.1 KiB
Go
Raw Normal View History

2018-02-13 20:05:18 +01:00
package client
import (
"fmt"
"dev.sum7.eu/genofire/yaja/xmpp"
2018-02-13 20:05:18 +01:00
)
var DefaultChannelSize = 30
func (client *Client) Start() error {
client.iq = make(chan *xmpp.IQClient, DefaultChannelSize)
client.presence = make(chan *xmpp.PresenceClient, DefaultChannelSize)
client.mesage = make(chan *xmpp.MessageClient, DefaultChannelSize)
client.reply = make(map[string]chan *xmpp.IQClient)
2018-02-13 20:05:18 +01:00
for {
element, err := client.Read()
if err != nil {
return err
}
errMSG := &xmpp.StreamError{}
2018-02-13 20:05:18 +01:00
err = client.Decode(errMSG, element)
if err == nil {
return fmt.Errorf("recv stream error: %s: %s -> %s", errMSG.Text, xmpp.XMLChildrenString(errMSG.StreamErrorGroup), xmpp.XMLChildrenString(errMSG.Other))
2018-02-13 20:05:18 +01:00
}
iq := &xmpp.IQClient{}
2018-02-13 20:05:18 +01:00
err = client.Decode(iq, element)
if err == nil {
if iq.Ping != nil {
client.Logging.Debug("answer ping")
iq.Type = xmpp.IQTypeResult
2018-02-13 20:05:18 +01:00
iq.To = iq.From
iq.From = client.JID
client.Send(iq)
} else {
if client.skipError && iq.Error != nil {
continue
}
if ch, ok := client.reply[iq.ID]; ok {
delete(client.reply, iq.ID)
ch <- iq
continue
}
client.iq <- iq
}
continue
}
pres := &xmpp.PresenceClient{}
2018-02-13 20:05:18 +01:00
err = client.Decode(pres, element)
if err == nil {
if client.skipError && pres.Error != nil {
continue
}
client.presence <- pres
continue
}
msg := &xmpp.MessageClient{}
2018-02-13 20:05:18 +01:00
err = client.Decode(msg, element)
if err == nil {
if client.skipError && msg.Error != nil {
continue
}
client.mesage <- msg
continue
}
client.Logging.Warnf("unsupport xml recv: %v", element)
}
}
func (client *Client) SendRecv(iq *xmpp.IQClient) *xmpp.IQClient {
2018-02-13 20:05:18 +01:00
if iq.ID == "" {
iq.ID = xmpp.CreateCookieString()
2018-02-13 20:05:18 +01:00
}
ch := make(chan *xmpp.IQClient, 1)
2018-02-13 20:05:18 +01:00
client.reply[iq.ID] = ch
client.Send(iq)
defer close(ch)
return <-ch
}
func (client *Client) RecvIQ() *xmpp.IQClient {
2018-02-13 20:05:18 +01:00
return <-client.iq
}
func (client *Client) RecvPresence() *xmpp.PresenceClient {
2018-02-13 20:05:18 +01:00
return <-client.presence
}
func (client *Client) RecvMessage() *xmpp.MessageClient {
2018-02-13 20:05:18 +01:00
return <-client.mesage
}