Use Transport in Component

This commit is contained in:
Wichert Akkerman 2019-10-11 06:24:47 +02:00 committed by Mickaël Rémond
parent 96fccbd399
commit e97d290e2b
2 changed files with 18 additions and 22 deletions

View File

@ -191,9 +191,8 @@ func (c *Client) Resume(state SMState) error {
func (c *Client) Disconnect() {
_ = c.SendRaw("</stream:stream>")
// TODO: Add a way to wait for stream close acknowledgement from the server for clean disconnect
conn := c.transport
if conn != nil {
_ = conn.Close()
if c.transport != nil {
_ = c.transport.Close()
}
}

View File

@ -7,8 +7,6 @@ import (
"errors"
"fmt"
"io"
"net"
"time"
"gosrc.io/xmpp/stanza"
)
@ -16,6 +14,8 @@ import (
const componentStreamOpen = "<?xml version='1.0'?><stream:stream to='%s' xmlns='%s' xmlns:stream='%s'>"
type ComponentOptions struct {
TransportConfiguration
// =================================
// Component Connection Info
@ -50,8 +50,7 @@ type Component struct {
ComponentOptions
router *Router
// TCP level connection
conn net.Conn
transport Transport
// read / write
socketProxy io.ReadWriter // TODO
@ -70,20 +69,19 @@ func (c *Component) Connect() error {
return c.Resume(state)
}
func (c *Component) Resume(sm SMState) error {
var conn net.Conn
var err error
if conn, err = net.DialTimeout("tcp", c.Address, time.Duration(5)*time.Second); err != nil {
c.transport = &XMPPTransport{Config: c.ComponentOptions.TransportConfiguration}
if err = c.transport.Connect(c.Address); err != nil {
return err
}
c.conn = conn
c.updateState(StateConnected)
// 1. Send stream open tag
if _, err := fmt.Fprintf(conn, componentStreamOpen, c.Domain, stanza.NSComponent, stanza.NSStream); err != nil {
if _, err := fmt.Fprintf(c.transport, componentStreamOpen, c.Domain, stanza.NSComponent, stanza.NSStream); err != nil {
c.updateState(StateStreamError)
return NewConnError(errors.New("cannot send stream open "+err.Error()), false)
}
c.decoder = xml.NewDecoder(conn)
c.decoder = xml.NewDecoder(c.transport)
// 2. Initialize xml decoder and extract streamID from reply
streamId, err := stanza.InitStream(c.decoder)
@ -93,7 +91,7 @@ func (c *Component) Resume(sm SMState) error {
}
// 3. Authentication
if _, err := fmt.Fprintf(conn, "<handshake>%s</handshake>", c.handshake(streamId)); err != nil {
if _, err := fmt.Fprintf(c.transport, "<handshake>%s</handshake>", c.handshake(streamId)); err != nil {
c.updateState(StateStreamError)
return NewConnError(errors.New("cannot send handshake "+err.Error()), false)
}
@ -123,9 +121,8 @@ func (c *Component) Resume(sm SMState) error {
func (c *Component) Disconnect() {
_ = c.SendRaw("</stream:stream>")
// TODO: Add a way to wait for stream close acknowledgement from the server for clean disconnect
conn := c.conn
if conn != nil {
_ = conn.Close()
if c.transport != nil {
_ = c.transport.Close()
}
}
@ -155,8 +152,8 @@ func (c *Component) recv() (err error) {
// Send marshalls XMPP stanza and sends it to the server.
func (c *Component) Send(packet stanza.Packet) error {
conn := c.conn
if conn == nil {
transport := c.transport
if transport == nil {
return errors.New("component is not connected")
}
@ -165,7 +162,7 @@ func (c *Component) Send(packet stanza.Packet) error {
return errors.New("cannot marshal packet " + err.Error())
}
if _, err := fmt.Fprintf(conn, string(data)); err != nil {
if _, err := fmt.Fprintf(transport, string(data)); err != nil {
return errors.New("cannot send packet " + err.Error())
}
return nil
@ -176,13 +173,13 @@ func (c *Component) Send(packet stanza.Packet) error {
// disconnect the component. It is up to the user of this method to
// carefully craft the XML content to produce valid XMPP.
func (c *Component) SendRaw(packet string) error {
conn := c.conn
if conn == nil {
transport := c.transport
if transport == nil {
return errors.New("component is not connected")
}
var err error
_, err = fmt.Fprintf(c.conn, packet)
_, err = fmt.Fprintf(transport, packet)
return err
}