diff options
author | xengineering <me@xengineering.eu> | 2023-05-18 20:51:16 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2023-05-18 20:51:16 +0200 |
commit | 05f50be52b03c7e84ba47d3b5349321920748f8b (patch) | |
tree | 9f0b2a0734849b70d53a5276cae9b2954ed7c6a7 /xmpp/encoder.go | |
parent | 8fce634716a998721cc5fb4663a7c0379c6df633 (diff) | |
download | limox-05f50be52b03c7e84ba47d3b5349321920748f8b.tar limox-05f50be52b03c7e84ba47d3b5349321920748f8b.tar.zst limox-05f50be52b03c7e84ba47d3b5349321920748f8b.zip |
Add xmpp/encoder.go
This simplifies the double-encoding to the TCP connection and the debug
console.
Diffstat (limited to 'xmpp/encoder.go')
-rw-r--r-- | xmpp/encoder.go | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/xmpp/encoder.go b/xmpp/encoder.go new file mode 100644 index 0000000..434ca68 --- /dev/null +++ b/xmpp/encoder.go @@ -0,0 +1,79 @@ +package xmpp + +import ( + "crypto/tls" + "encoding/xml" + "os" +) + +type encoder struct { + tcp *xml.Encoder + debug *xml.Encoder +} + +func newEncoder(conn *tls.Conn) encoder { + e := encoder{ + xml.NewEncoder(conn), + xml.NewEncoder(os.Stdout), + } + e.tcp.Indent("", "") + e.debug.Indent("C: ", " ") + + return e +} + +func (e *encoder) encodeNow(t xml.Token) error { + err := e.encode(t) + if err != nil { + return err + } + + err = e.flush() + if err != nil { + return err + } + + return nil +} + +func (e *encoder) encode(t xml.Token) error { + err := e.tcp.EncodeToken(t) + if err != nil { + return err + } + + err = e.debug.EncodeToken(t) + if err != nil { + return err + } + + return nil +} + +func (e *encoder) flush() error { + err := e.tcp.Flush() + if err != nil { + return err + } + + err = e.debug.Flush() + if err != nil { + return err + } + + return nil +} + +func (e *encoder) Close() error { + err := e.tcp.Close() + if err != nil { + return err + } + + err = e.debug.Close() + if err != nil { + return err + } + + return nil +} |