diff --git a/client.go b/client.go index a535ac7..3442110 100644 --- a/client.go +++ b/client.go @@ -22,8 +22,9 @@ type Client struct { fromport string toport string - SamConn net.Conn - rd *bufio.Reader + SamConn net.Conn + SamDGConn DatagramConn + rd *bufio.Reader sigType string destination string diff --git a/datagram.go b/datagram.go new file mode 100644 index 0000000..80b7abc --- /dev/null +++ b/datagram.go @@ -0,0 +1,19 @@ +package goSam + +import ( + "net" + "time" +) + +type DatagramConn interface { + ReadFrom(p []byte) (n int, addr net.Addr, err error) + Read(b []byte) (n int, err error) + WriteTo(p []byte, addr net.Addr) (n int, err error) + Write(b []byte) (n int, err error) + Close() error + LocalAddr() net.Addr + RemoteAddr() net.Addr + SetDeadline(t time.Time) error + SetReadDeadline(t time.Time) error + SetWriteDeadline(t time.Time) error +} diff --git a/dial.go b/dial.go index 058f71a..2f4a9de 100644 --- a/dial.go +++ b/dial.go @@ -37,6 +37,20 @@ func (c *Client) Dial(network, addr string) (net.Conn, error) { // Dial implements the net.Dial function and can be used for http.Transport func (c *Client) DialContextFree(network, addr string) (net.Conn, error) { + if network == "tcp" || network == "tcp6" || network == "tcp4" { + return c.DialStreamingContextFree(addr) + } + if network == "udp" || network == "udp6" || network == "udp4" { + return c.DialDatagramContextFree(addr) + } + return c.DialStreamingContextFree(addr) +} + +func (c *Client) DialDatagramContextFree(addr string) (DatagramConn, error) { + return c.SamDGConn, nil +} + +func (c *Client) DialStreamingContextFree(addr string) (net.Conn, error) { portIdx := strings.Index(addr, ":") if portIdx >= 0 { addr = addr[:portIdx] diff --git a/raw.go b/raw.go new file mode 100644 index 0000000..f03916b --- /dev/null +++ b/raw.go @@ -0,0 +1 @@ +package goSam diff --git a/sessions.go b/sessions.go index 8942473..0ba78a8 100644 --- a/sessions.go +++ b/sessions.go @@ -11,15 +11,16 @@ func init() { rand.Seed(time.Now().UnixNano()) } -// CreateStreamSession creates a new STREAM Session. +// CreateSession creates a new STREAM Session. // Returns the Id for the new Client. -func (c *Client) CreateStreamSession(id int32, dest string) (string, error) { +func (c *Client) CreateSession(id int32, style, dest string) (string, error) { if dest == "" { dest = "TRANSIENT" } c.id = id r, err := c.sendCmd( - "SESSION CREATE STYLE=STREAM ID=%d DESTINATION=%s %s %s %s %s \n", + "SESSION CREATE STYLE=%s ID=%d DESTINATION=%s %s %s %s %s \n", + style, c.id, dest, c.from(), @@ -43,3 +44,21 @@ func (c *Client) CreateStreamSession(id int32, dest string) (string, error) { c.destination = r.Pairs["DESTINATION"] return c.destination, nil } + +// CreateStreamSession creates a new STREAM Session. +// Returns the Id for the new Client. +func (c *Client) CreateStreamSession(id int32, dest string) (string, error) { + return c.CreateSession(id, "STREAM", dest) +} + +// CreateDatagramSession creates a new DATAGRAM Session. +// Returns the Id for the new Client. +func (c *Client) CreateDatagramSession(id int32, dest string) (string, error) { + return c.CreateSession(id, "DATAGRAM", dest) +} + +// CreateRawSession creates a new RAW Session. +// Returns the Id for the new Client. +func (c *Client) CreateRawSession(id int32, dest string) (string, error) { + return c.CreateSession(id, "RAW", dest) +}