2014-03-28 23:57:48 +00:00
|
|
|
package beam
|
|
|
|
|
|
|
|
import (
|
2014-04-02 23:38:36 +00:00
|
|
|
"fmt"
|
2014-03-28 23:57:48 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Sender interface {
|
|
|
|
Send([]byte, *os.File) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type Receiver interface {
|
|
|
|
Receive() ([]byte, *os.File, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ReceiveCloser interface {
|
|
|
|
Receiver
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type SendCloser interface {
|
|
|
|
Sender
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
|
|
|
func SendPipe(dst Sender, data []byte) (*os.File, error) {
|
|
|
|
r, w, err := os.Pipe()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := dst.Send(data, r); err != nil {
|
|
|
|
r.Close()
|
|
|
|
w.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return w, nil
|
|
|
|
}
|
|
|
|
|
2014-03-31 18:58:29 +00:00
|
|
|
func SendConn(dst Sender, data []byte) (conn *UnixConn, err error) {
|
2014-03-28 23:57:48 +00:00
|
|
|
local, remote, err := SocketPair()
|
|
|
|
if err != nil {
|
2014-03-31 18:58:29 +00:00
|
|
|
return nil, err
|
2014-03-28 23:57:48 +00:00
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
local.Close()
|
|
|
|
remote.Close()
|
|
|
|
}
|
|
|
|
}()
|
2014-03-31 18:58:29 +00:00
|
|
|
conn, err = FileConn(local)
|
2014-03-28 23:57:48 +00:00
|
|
|
if err != nil {
|
2014-03-31 18:58:29 +00:00
|
|
|
return nil, err
|
2014-03-28 23:57:48 +00:00
|
|
|
}
|
|
|
|
local.Close()
|
|
|
|
if err := dst.Send(data, remote); err != nil {
|
2014-03-31 18:58:29 +00:00
|
|
|
return nil, err
|
2014-03-28 23:57:48 +00:00
|
|
|
}
|
2014-03-31 18:58:29 +00:00
|
|
|
return conn, nil
|
2014-03-28 23:57:48 +00:00
|
|
|
}
|
|
|
|
|
2014-03-31 18:58:29 +00:00
|
|
|
func ReceiveConn(src Receiver) ([]byte, *UnixConn, error) {
|
2014-03-28 23:57:48 +00:00
|
|
|
for {
|
|
|
|
data, f, err := src.Receive()
|
|
|
|
if err != nil {
|
2014-03-31 18:58:29 +00:00
|
|
|
return nil, nil, err
|
2014-03-28 23:57:48 +00:00
|
|
|
}
|
|
|
|
if f == nil {
|
|
|
|
// Skip empty attachments
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
conn, err := FileConn(f)
|
|
|
|
if err != nil {
|
|
|
|
// Skip beam attachments which are not connections
|
|
|
|
// (for example might be a regular file, directory etc)
|
|
|
|
continue
|
|
|
|
}
|
2014-03-31 18:58:29 +00:00
|
|
|
return data, conn, nil
|
2014-03-28 23:57:48 +00:00
|
|
|
}
|
|
|
|
panic("impossibru!")
|
2014-03-31 18:58:29 +00:00
|
|
|
return nil, nil, nil
|
2014-03-28 23:57:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Copy(dst Sender, src Receiver) (int, error) {
|
|
|
|
var n int
|
|
|
|
for {
|
|
|
|
payload, attachment, err := src.Receive()
|
|
|
|
if err == io.EOF {
|
|
|
|
return n, nil
|
|
|
|
} else if err != nil {
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
if err := dst.Send(payload, attachment); err != nil {
|
|
|
|
if attachment != nil {
|
|
|
|
attachment.Close()
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
panic("impossibru!")
|
|
|
|
return n, nil
|
|
|
|
}
|
2014-04-02 23:38:36 +00:00
|
|
|
|
|
|
|
// MsgDesc returns a human readable description of a beam message, usually
|
|
|
|
// for debugging purposes.
|
|
|
|
func MsgDesc(payload []byte, attachment *os.File) string {
|
|
|
|
var filedesc string = "<nil>"
|
|
|
|
if attachment != nil {
|
|
|
|
filedesc = fmt.Sprintf("%d", attachment.Fd())
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("'%s'[%s]", payload, filedesc)
|
|
|
|
}
|