diff options
author | xengineering <me@xengineering.eu> | 2023-06-05 18:48:54 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2023-06-05 18:48:54 +0200 |
commit | 2d9864426272055151c42cf6bc2d13fbe11ca50c (patch) | |
tree | 242749003d4b0520c6d864674d3a85bc55deaf38 | |
parent | feb6a5a38ba15d0cf4bd88283093174991e66eaa (diff) | |
download | limox-2d9864426272055151c42cf6bc2d13fbe11ca50c.tar limox-2d9864426272055151c42cf6bc2d13fbe11ca50c.tar.zst limox-2d9864426272055151c42cf6bc2d13fbe11ca50c.zip |
Re-implement XML decoding
-rw-r--r-- | xmpp/encoder_decoder.go | 33 | ||||
-rw-r--r-- | xmpp/session.go | 2 |
2 files changed, 35 insertions, 0 deletions
diff --git a/xmpp/encoder_decoder.go b/xmpp/encoder_decoder.go index 14113a6..c8ccd89 100644 --- a/xmpp/encoder_decoder.go +++ b/xmpp/encoder_decoder.go @@ -3,13 +3,18 @@ package xmpp import ( "encoding/xml" "log" + "io" "os" + "errors" ) type encoderDecoder struct{ session *session ct *xml.Encoder // client transport encoder cl *xml.Encoder // client log encoder + st *xml.Decoder // server transport decoder + sl *xml.Encoder // server log encoder + terminator chan bool } func newEncoderDecoder(s *session) encoderDecoder { @@ -23,6 +28,11 @@ func newEncoderDecoder(s *session) encoderDecoder { ed.cl = xml.NewEncoder(os.Stdout) ed.cl.Indent("C: ", " ") + ed.st = xml.NewDecoder(s.transport) + + ed.sl = xml.NewEncoder(os.Stdout) + ed.sl.Indent("S: ", " ") + return ed } @@ -42,3 +52,26 @@ func (ed *encoderDecoder) encodeToken(t xml.Token) error { return nil } + +func (ed *encoderDecoder) run() { + for { + select { + case <-ed.terminator: + return + default: + t, err := ed.st.Token() + if t != nil && err == nil { + err = ed.sl.EncodeToken(t) + if err != nil { log.Println("Could not encode to server stream log") } + err = ed.sl.Flush() + if err != nil { log.Println("Could not flush server stream log") } + _ = xml.CopyToken(t) // TODO do something meaningful here + } + if err != nil { + if errors.Is(err, io.EOF) { return } + log.Println(err) // FIXME terminate session on error + return + } + } + } +} diff --git a/xmpp/session.go b/xmpp/session.go index 234c2e6..080fb21 100644 --- a/xmpp/session.go +++ b/xmpp/session.go @@ -40,6 +40,8 @@ func (s *session) run() { defer s.transport.Close() s.ed = newEncoderDecoder(s) + go s.ed.run() + defer func() { s.ed.terminator <- true }() s.openStream() defer s.closeStreams() |