2018-12-26 09:50:01 -08:00
|
|
|
package xmpp // import "gosrc.io/xmpp"
|
2016-01-06 07:51:12 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Mediated Read / Write on socket
|
2018-09-26 07:25:04 -07:00
|
|
|
// Used if logFile from Config is not nil
|
2016-01-06 07:51:12 -08:00
|
|
|
type socketProxy struct {
|
|
|
|
socket io.ReadWriter // Actual connection
|
|
|
|
logFile *os.File
|
|
|
|
}
|
|
|
|
|
|
|
|
func newSocketProxy(conn io.ReadWriter, logFile *os.File) io.ReadWriter {
|
|
|
|
if logFile == nil {
|
|
|
|
return conn
|
|
|
|
} else {
|
|
|
|
return &socketProxy{conn, logFile}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pl *socketProxy) Read(p []byte) (n int, err error) {
|
|
|
|
n, err = pl.socket.Read(p)
|
|
|
|
if n > 0 {
|
|
|
|
pl.logFile.Write([]byte("RECV:\n")) // Prefix
|
|
|
|
if n, err := pl.logFile.Write(p[:n]); err != nil {
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
pl.logFile.Write([]byte("\n\n")) // Separator
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pl *socketProxy) Write(p []byte) (n int, err error) {
|
|
|
|
pl.logFile.Write([]byte("SEND:\n")) // Prefix
|
|
|
|
for _, w := range []io.Writer{pl.socket, pl.logFile} {
|
|
|
|
n, err = w.Write(p)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if n != len(p) {
|
|
|
|
err = io.ErrShortWrite
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pl.logFile.Write([]byte("\n\n")) // Separator
|
|
|
|
return len(p), nil
|
|
|
|
}
|