diff options
author | xengineering <me@xengineering.eu> | 2023-07-04 22:10:55 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2023-07-04 22:15:17 +0200 |
commit | 48811e7d2487ebc3db49b8af7e20f57db4ac28f4 (patch) | |
tree | a317f8dc44ac9828ae5806e1fa1dee7547118619 /xmpp/session.go | |
parent | 5570ccd1d6b50042acbf2fad3793afa6dde79ca2 (diff) | |
parent | d9fe0a4360770b1e4b6b4fb3686c3275ad1b6e6e (diff) | |
download | limox-48811e7d2487ebc3db49b8af7e20f57db4ac28f4.tar limox-48811e7d2487ebc3db49b8af7e20f57db4ac28f4.tar.zst limox-48811e7d2487ebc3db49b8af7e20f57db4ac28f4.zip |
Merge branch 'element-handling'
This moves away from the concept to parse each individual XML token from
the token and group them as a []xml.Token slice for further processing.
While this is still possible, receiving aswell as sending has switched
to define structs with XML tags which can be marshalled and unmarshalled
with the xml.Encoder.EncodeElement() and xml.Decoder.DecodeElement()
functions.
Further documentation can be found at https://pkg.go.dev/encoding/xml#Unmarshal
and https://pkg.go.dev/encoding/xml#Marshal.
This merge reduces with its changes the number of code lines in the XMPP
implementation by around 41 percent!
Diffstat (limited to 'xmpp/session.go')
-rw-r--r-- | xmpp/session.go | 40 |
1 files changed, 30 insertions, 10 deletions
diff --git a/xmpp/session.go b/xmpp/session.go index a43e4f4..4dfd76f 100644 --- a/xmpp/session.go +++ b/xmpp/session.go @@ -1,9 +1,11 @@ package xmpp import ( + "context" "crypto/tls" "crypto/x509" "encoding/xml" + "io" "log" ) @@ -17,8 +19,8 @@ type session struct { in chan any out chan<- any transport *tls.Conn - ed encoderDecoder - rx chan xml.Token + tx *xml.Encoder + rx chan any resourceReq string } @@ -29,7 +31,7 @@ func StartSession(out chan<- any, jid string, pwd string) (in chan<- any) { s.pwd = pwd s.in = make(chan any) s.out = out - s.rx = make(chan xml.Token, 0) + s.rx = make(chan any, 0) go s.run() @@ -37,21 +39,39 @@ func StartSession(out chan<- any, jid string, pwd string) (in chan<- any) { } func (s *session) run() { - defer func() { s.out <- SessionDisconnect{} }() - err := s.startTransport() if err != nil { return } defer s.transport.Close() - s.ed = newEncoderDecoder(s) - go s.ed.run() - defer func() { s.ed.terminator <- true }() + ctx, cancel := context.WithCancel(context.Background()) + cpy := s.rx + go runRx(ctx, cpy, s.transport) + defer cancel() + + lw := logger{"[TX] "} + w := io.MultiWriter(s.transport, lw) + s.tx = xml.NewEncoder(w) + defer s.tx.Close() - s.out <- SessionConnect{} + openStream(s.tx, s.jid) + defer closeStream(s.tx) - runStreamPair(s) + s.out <- SessionConnect{} // TODO this should be sent after initial presence + defer func() { s.out <- SessionDisconnect{} }() + + for { + select { + case e := <-s.rx: + handle(s, e) + case signal := <-s.in: + switch signal.(type) { + case SessionShouldDisconnect: + return + } + } + } } func (s *session) startTransport() error { |