dial.go 595 B

123456789101112131415161718192021222324252627282930313233343536
  1. package goSam
  2. import (
  3. "net"
  4. "strings"
  5. )
  6. // Dial implements the net.Dial function and can be used for http.Transport
  7. func (c *Client) Dial(network, addr string) (net.Conn, error) {
  8. portIdx := strings.Index(addr, ":")
  9. if portIdx >= 0 {
  10. addr = addr[:portIdx]
  11. }
  12. addr, err := c.Lookup(addr)
  13. if err != nil {
  14. return nil, err
  15. }
  16. id, _, err := c.CreateStreamSession("")
  17. if err != nil {
  18. return nil, err
  19. }
  20. newC, err := NewClient(c.samaddr())
  21. if err != nil {
  22. return nil, err
  23. }
  24. err = newC.StreamConnect(id, addr)
  25. if err != nil {
  26. return nil, err
  27. }
  28. return newC.SamConn, nil
  29. }