mirror of
https://github.com/FluuxIO/go-xmpp.git
synced 2024-11-24 11:32:00 -08:00
Use Transport in Component
This commit is contained in:
parent
96fccbd399
commit
e97d290e2b
@ -191,9 +191,8 @@ func (c *Client) Resume(state SMState) error {
|
|||||||
func (c *Client) Disconnect() {
|
func (c *Client) Disconnect() {
|
||||||
_ = c.SendRaw("</stream:stream>")
|
_ = c.SendRaw("</stream:stream>")
|
||||||
// TODO: Add a way to wait for stream close acknowledgement from the server for clean disconnect
|
// TODO: Add a way to wait for stream close acknowledgement from the server for clean disconnect
|
||||||
conn := c.transport
|
if c.transport != nil {
|
||||||
if conn != nil {
|
_ = c.transport.Close()
|
||||||
_ = conn.Close()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
35
component.go
35
component.go
@ -7,8 +7,6 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"gosrc.io/xmpp/stanza"
|
"gosrc.io/xmpp/stanza"
|
||||||
)
|
)
|
||||||
@ -16,6 +14,8 @@ import (
|
|||||||
const componentStreamOpen = "<?xml version='1.0'?><stream:stream to='%s' xmlns='%s' xmlns:stream='%s'>"
|
const componentStreamOpen = "<?xml version='1.0'?><stream:stream to='%s' xmlns='%s' xmlns:stream='%s'>"
|
||||||
|
|
||||||
type ComponentOptions struct {
|
type ComponentOptions struct {
|
||||||
|
TransportConfiguration
|
||||||
|
|
||||||
// =================================
|
// =================================
|
||||||
// Component Connection Info
|
// Component Connection Info
|
||||||
|
|
||||||
@ -50,8 +50,7 @@ type Component struct {
|
|||||||
ComponentOptions
|
ComponentOptions
|
||||||
router *Router
|
router *Router
|
||||||
|
|
||||||
// TCP level connection
|
transport Transport
|
||||||
conn net.Conn
|
|
||||||
|
|
||||||
// read / write
|
// read / write
|
||||||
socketProxy io.ReadWriter // TODO
|
socketProxy io.ReadWriter // TODO
|
||||||
@ -70,20 +69,19 @@ func (c *Component) Connect() error {
|
|||||||
return c.Resume(state)
|
return c.Resume(state)
|
||||||
}
|
}
|
||||||
func (c *Component) Resume(sm SMState) error {
|
func (c *Component) Resume(sm SMState) error {
|
||||||
var conn net.Conn
|
|
||||||
var err error
|
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
|
return err
|
||||||
}
|
}
|
||||||
c.conn = conn
|
|
||||||
c.updateState(StateConnected)
|
c.updateState(StateConnected)
|
||||||
|
|
||||||
// 1. Send stream open tag
|
// 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)
|
c.updateState(StateStreamError)
|
||||||
return NewConnError(errors.New("cannot send stream open "+err.Error()), false)
|
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
|
// 2. Initialize xml decoder and extract streamID from reply
|
||||||
streamId, err := stanza.InitStream(c.decoder)
|
streamId, err := stanza.InitStream(c.decoder)
|
||||||
@ -93,7 +91,7 @@ func (c *Component) Resume(sm SMState) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 3. Authentication
|
// 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)
|
c.updateState(StateStreamError)
|
||||||
return NewConnError(errors.New("cannot send handshake "+err.Error()), false)
|
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() {
|
func (c *Component) Disconnect() {
|
||||||
_ = c.SendRaw("</stream:stream>")
|
_ = c.SendRaw("</stream:stream>")
|
||||||
// TODO: Add a way to wait for stream close acknowledgement from the server for clean disconnect
|
// TODO: Add a way to wait for stream close acknowledgement from the server for clean disconnect
|
||||||
conn := c.conn
|
if c.transport != nil {
|
||||||
if conn != nil {
|
_ = c.transport.Close()
|
||||||
_ = conn.Close()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -155,8 +152,8 @@ 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 stanza.Packet) error {
|
func (c *Component) Send(packet stanza.Packet) error {
|
||||||
conn := c.conn
|
transport := c.transport
|
||||||
if conn == nil {
|
if transport == nil {
|
||||||
return errors.New("component is not connected")
|
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())
|
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 errors.New("cannot send packet " + err.Error())
|
||||||
}
|
}
|
||||||
return nil
|
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
|
// 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
|
transport := c.transport
|
||||||
if conn == nil {
|
if transport == nil {
|
||||||
return errors.New("component is not connected")
|
return errors.New("component is not connected")
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
_, err = fmt.Fprintf(c.conn, packet)
|
_, err = fmt.Fprintf(transport, packet)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user