Add a ExchangeConn

This commit is contained in:
Miek Gieben 2013-01-28 20:30:13 +01:00
parent c8f880217f
commit 0dc922a99b
1 changed files with 12 additions and 3 deletions

View File

@ -40,9 +40,7 @@ type Client struct {
// in, rtt, err := c.Exchange(message, "127.0.0.1:53")
//
func (c *Client) Exchange(m *Msg, a string) (r *Msg, rtt time.Duration, err error) {
w := new(reply)
w.client = c
w.addr = a
w := &reply{client: c, addr: a}
if err = w.dial(); err != nil {
return nil, 0, err
}
@ -54,6 +52,17 @@ func (c *Client) Exchange(m *Msg, a string) (r *Msg, rtt time.Duration, err erro
return r, w.rtt, err
}
// ExchangeConn performs an synchronous query. It sends the message m trough the
// connection c and waits for a reply.
func (c *Client) ExchangeConn(m *Msg, c net.Conn) (r *Msg, rtt time.Duration, err error) {
w := &reply{client: c, addr: a.RemoteAddr()}
if err = w.send(m); err != nil {
return nil, 0, err
}
r, err = w.receive()
return r, w.rtt, err
}
func (w *reply) RemoteAddr() net.Addr {
if w.conn != nil {
return w.conn.RemoteAddr()