destination.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package net
  2. // Destination represents a network destination including address and protocol (tcp / udp).
  3. type Destination interface {
  4. Network() Network // Protocol of communication (tcp / udp)
  5. Address() Address // Address of destination
  6. Port() Port
  7. String() string // String representation of the destination
  8. NetAddr() string
  9. Equals(Destination) bool
  10. IsTCP() bool // True if destination is reachable via TCP
  11. IsUDP() bool // True if destination is reachable via UDP
  12. }
  13. // TCPDestination creates a TCP destination with given address
  14. func TCPDestination(address Address, port Port) Destination {
  15. return &tcpDestination{address: address, port: port}
  16. }
  17. // UDPDestination creates a UDP destination with given address
  18. func UDPDestination(address Address, port Port) Destination {
  19. return &udpDestination{address: address, port: port}
  20. }
  21. type tcpDestination struct {
  22. address Address
  23. port Port
  24. }
  25. func (dest *tcpDestination) Network() Network {
  26. return TCPNetwork
  27. }
  28. func (dest *tcpDestination) Address() Address {
  29. return dest.address
  30. }
  31. func (dest *tcpDestination) NetAddr() string {
  32. return dest.address.String() + ":" + dest.port.String()
  33. }
  34. func (dest *tcpDestination) String() string {
  35. return "tcp:" + dest.NetAddr()
  36. }
  37. func (dest *tcpDestination) IsTCP() bool {
  38. return true
  39. }
  40. func (dest *tcpDestination) IsUDP() bool {
  41. return false
  42. }
  43. func (dest *tcpDestination) Port() Port {
  44. return dest.port
  45. }
  46. func (dest *tcpDestination) Equals(another Destination) bool {
  47. if dest == nil && another == nil {
  48. return true
  49. }
  50. if dest == nil || another == nil {
  51. return false
  52. }
  53. if !another.IsTCP() {
  54. return false
  55. }
  56. return dest.Port() == another.Port() && dest.Address().Equals(another.Address())
  57. }
  58. type udpDestination struct {
  59. address Address
  60. port Port
  61. }
  62. func (dest *udpDestination) Network() Network {
  63. return UDPNetwork
  64. }
  65. func (dest *udpDestination) Address() Address {
  66. return dest.address
  67. }
  68. func (dest *udpDestination) NetAddr() string {
  69. return dest.address.String() + ":" + dest.port.String()
  70. }
  71. func (dest *udpDestination) String() string {
  72. return "udp:" + dest.NetAddr()
  73. }
  74. func (dest *udpDestination) IsTCP() bool {
  75. return false
  76. }
  77. func (dest *udpDestination) IsUDP() bool {
  78. return true
  79. }
  80. func (dest *udpDestination) Port() Port {
  81. return dest.port
  82. }
  83. func (dest *udpDestination) Equals(another Destination) bool {
  84. if dest == nil && another == nil {
  85. return true
  86. }
  87. if dest == nil || another == nil {
  88. return false
  89. }
  90. if !another.IsUDP() {
  91. return false
  92. }
  93. return dest.Port() == another.Port() && dest.Address().Equals(another.Address())
  94. }