Add session creation commands for datagrams and raw sessions, stub out a repliable datagram dialer, add a DatagramConn interface that implements both net.Conn and net.PacketConn so that Dial can return a whole DatagramConn
This commit is contained in:
@ -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
|
||||
|
19
datagram.go
Normal file
19
datagram.go
Normal file
@ -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
|
||||
}
|
14
dial.go
14
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]
|
||||
|
25
sessions.go
25
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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user