From e97d290e2b9d34ec3db61755b233ebdc41563790 Mon Sep 17 00:00:00 2001 From: Wichert Akkerman Date: Fri, 11 Oct 2019 06:24:47 +0200 Subject: [PATCH] Use Transport in Component --- client.go | 5 ++--- component.go | 35 ++++++++++++++++------------------- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/client.go b/client.go index fd5763a..e783c0f 100644 --- a/client.go +++ b/client.go @@ -191,9 +191,8 @@ func (c *Client) Resume(state SMState) error { func (c *Client) Disconnect() { _ = c.SendRaw("") // 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() } } diff --git a/component.go b/component.go index 443962d..0a1000e 100644 --- a/component.go +++ b/component.go @@ -7,8 +7,6 @@ import ( "errors" "fmt" "io" - "net" - "time" "gosrc.io/xmpp/stanza" ) @@ -16,6 +14,8 @@ import ( const componentStreamOpen = "" 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, "%s", c.handshake(streamId)); err != nil { + if _, err := fmt.Fprintf(c.transport, "%s", 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("") // 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 }