add an Addr method to the Server

This commit is contained in:
Marten Seemann
2017-02-13 22:33:35 +07:00
parent c533a9adb8
commit ca863a86bb
2 changed files with 13 additions and 0 deletions

View File

@@ -127,6 +127,11 @@ func (s *Server) Close() error {
return conn.Close() return conn.Close()
} }
// Addr returns the server's network address
func (s *Server) Addr() net.Addr {
return s.addr
}
func (s *Server) handlePacket(conn *net.UDPConn, remoteAddr *net.UDPAddr, packet []byte) error { func (s *Server) handlePacket(conn *net.UDPConn, remoteAddr *net.UDPAddr, packet []byte) error {
if protocol.ByteCount(len(packet)) > protocol.MaxPacketSize { if protocol.ByteCount(len(packet)) > protocol.MaxPacketSize {
return qerr.PacketTooLarge return qerr.PacketTooLarge

View File

@@ -61,6 +61,14 @@ var _ = Describe("Server", func() {
firstPacket = append(append(firstPacket, b.Bytes()...), 0x01) firstPacket = append(append(firstPacket, b.Bytes()...), 0x01)
}) })
It("returns the address", func() {
server.addr = &net.UDPAddr{
IP: net.IPv4(192, 168, 13, 37),
Port: 1234,
}
Expect(server.Addr().String()).To(Equal("192.168.13.37:1234"))
})
It("composes version negotiation packets", func() { It("composes version negotiation packets", func() {
expected := append( expected := append(
[]byte{0x01 | 0x08, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, []byte{0x01 | 0x08, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},