Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

85 wiersze
2.2 KiB

  1. package client
  2. import (
  3. "bufio"
  4. "crypto/tls"
  5. "net"
  6. "sync"
  7. "github.com/yosssi/gmq/mqtt/packet"
  8. )
  9. // Buffer size of the send channel
  10. const sendBufSize = 1024
  11. // connection represents a Network Connection.
  12. type connection struct {
  13. net.Conn
  14. // r is the buffered reader.
  15. r *bufio.Reader
  16. // w is the buffered writer.
  17. w *bufio.Writer
  18. // disconnected is true if the Network Connection
  19. // has been disconnected by the Client.
  20. disconnected bool
  21. // wg is the Wait Group for the goroutines
  22. // which are launched by the Connect method.
  23. wg sync.WaitGroup
  24. // connack is the channel which handles the signal
  25. // to notify the arrival of the CONNACK Packet.
  26. connack chan struct{}
  27. // send is the channel which handles the Packet.
  28. send chan packet.Packet
  29. // sendEnd is the channel which ends the goroutine
  30. // which sends a Packet to the Server.
  31. sendEnd chan struct{}
  32. // muPINGRESPs is the Mutex for pingresps.
  33. muPINGRESPs sync.RWMutex
  34. // pingresps is the slice of the channels which
  35. // handle the signal to notify the arrival of
  36. // the PINGRESP Packet.
  37. pingresps []chan struct{}
  38. // unackSubs contains the subscription information
  39. // which are not acknowledged by the Server.
  40. unackSubs map[string]MessageHandler
  41. // ackedSubs contains the subscription information
  42. // which are acknowledged by the Server.
  43. ackedSubs map[string]MessageHandler
  44. }
  45. // newConnection connects to the address on the named network,
  46. // creates a Network Connection and returns it.
  47. func newConnection(network, address string, tlsConfig *tls.Config) (*connection, error) {
  48. // Define the local variables.
  49. var conn net.Conn
  50. var err error
  51. // Connect to the address on the named network.
  52. if tlsConfig != nil {
  53. conn, err = tls.Dial(network, address, tlsConfig)
  54. } else {
  55. conn, err = net.Dial(network, address)
  56. }
  57. if err != nil {
  58. return nil, err
  59. }
  60. // Create a Network Connection.
  61. c := &connection{
  62. Conn: conn,
  63. r: bufio.NewReader(conn),
  64. w: bufio.NewWriter(conn),
  65. connack: make(chan struct{}, 1),
  66. send: make(chan packet.Packet, sendBufSize),
  67. sendEnd: make(chan struct{}, 1),
  68. unackSubs: make(map[string]MessageHandler),
  69. ackedSubs: make(map[string]MessageHandler),
  70. }
  71. // Return the Network Connection.
  72. return c, nil
  73. }