dail.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. package tinymq
  2. import (
  3. "fmt"
  4. "log"
  5. "net"
  6. "strconv"
  7. "git.me9.top/git/tinymq/config"
  8. "git.me9.top/git/tinymq/conn"
  9. "git.me9.top/git/tinymq/conn/tcp2"
  10. "git.me9.top/git/tinymq/conn/ws2"
  11. )
  12. // 建立连接,输出实际代码实现的协议
  13. // 会一直阻塞直到连接结束
  14. func Dail(host *conn.HostInfo, cf *config.Config) (rawProto string, conn conn.Connect, err error) {
  15. addr := net.JoinHostPort(host.Host, strconv.Itoa(int(host.Port)))
  16. if host.Version == ws2.VERSION && (host.Proto == ws2.PROTO || host.Proto == ws2.PROTO_STL) {
  17. rawProto = ws2.PROTO
  18. if cf.PrintMsg {
  19. log.Println("[ConnectToServer] connecting:", host.Proto, addr, host.Path, host.Hash)
  20. }
  21. conn, err = ws2.Dial(cf, host.Proto, addr, host.Path, host.Hash)
  22. } else if host.Version == tcp2.VERSION && host.Proto == tcp2.PROTO {
  23. rawProto = tcp2.PROTO
  24. if cf.PrintMsg {
  25. log.Println("[ConnectToServer] connecting:", host.Proto, addr, host.Hash)
  26. }
  27. conn, err = tcp2.Dial(cf, addr, host.Hash)
  28. } else {
  29. err = fmt.Errorf("not correct protocol and version found in: %+v", host)
  30. }
  31. return
  32. }